Skip to content

Commit e7d8160

Browse files
wukathcopybara-github
authored andcommitted
fix: Add "gcloud config unset project" command to express mode flow
If the user created an express mode project but has a different gcloud project set, we should help them unset it so that they doesn't run into permissions errors / the express mode one gets used. Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 897783336
1 parent 9d4ecbe commit e7d8160

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/google/adk/cli/cli_create.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,27 @@ def _handle_login_with_google() -> (
378378
f"Express Mode project created: {project_id}",
379379
fg="green",
380380
)
381+
current_proj = _get_gcp_project_from_gcloud()
382+
if current_proj and current_proj != project_id:
383+
click.secho(
384+
"Warning: Your default gcloud project is set to"
385+
f" '{current_proj}'. This might conflict with or override your"
386+
f" Express Mode project '{project_id}'. We recommend"
387+
" unsetting it.",
388+
fg="yellow",
389+
)
390+
if click.confirm("Run 'gcloud config unset project'?", default=True):
391+
try:
392+
subprocess.run(
393+
["gcloud", "config", "unset", "project"],
394+
check=True,
395+
capture_output=True,
396+
)
397+
click.secho("Unset default gcloud project.", fg="green")
398+
except Exception:
399+
click.secho(
400+
"Failed to unset project. Please do it manually.", fg="red"
401+
)
381402
return api_key, project_id, region
382403

383404
click.secho(_NOT_ELIGIBLE_MSG, fg="red")

tests/unittests/cli/utils/test_cli_create.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,52 @@ def test_handle_login_with_google_option_2(
417417
assert region == "us-central1"
418418

419419

420+
def test_handle_login_with_google_option_2_unset_project(
421+
monkeypatch: pytest.MonkeyPatch,
422+
) -> None:
423+
"""User selects 2, goes through express sign up, and unsets existing gcloud project."""
424+
monkeypatch.setattr(gcp_utils, "check_adc", lambda: True)
425+
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
426+
monkeypatch.setattr(gcp_utils, "list_gcp_projects", lambda limit: [])
427+
monkeypatch.setattr(gcp_utils, "check_express_eligibility", lambda: True)
428+
429+
confirms = iter([True, True])
430+
monkeypatch.setattr(click, "confirm", lambda *a, **k: next(confirms))
431+
432+
prompts = iter(["2", "1"])
433+
monkeypatch.setattr(click, "prompt", lambda *a, **k: next(prompts))
434+
435+
monkeypatch.setattr(
436+
gcp_utils,
437+
"sign_up_express",
438+
lambda location="us-central1": {
439+
"api_key": "new-key",
440+
"project_id": "new-proj",
441+
"region": location,
442+
},
443+
)
444+
445+
monkeypatch.setattr(
446+
cli_create, "_get_gcp_project_from_gcloud", lambda: "old-proj"
447+
)
448+
449+
called = {}
450+
451+
def fake_run(cmd, **kwargs):
452+
if cmd == ["gcloud", "config", "unset", "project"]:
453+
called["unset"] = True
454+
return subprocess.CompletedProcess(args=cmd, returncode=0)
455+
raise ValueError(f"Unexpected command: {cmd}")
456+
457+
monkeypatch.setattr(subprocess, "run", fake_run)
458+
459+
api_key, proj, region = cli_create._handle_login_with_google()
460+
assert api_key == "new-key"
461+
assert proj == "new-proj"
462+
assert region == "us-central1"
463+
assert called.get("unset") is True
464+
465+
420466
def test_handle_login_with_google_option_3(
421467
monkeypatch: pytest.MonkeyPatch,
422468
) -> None:

0 commit comments

Comments
 (0)