Skip to content

Commit 0fedb3b

Browse files
wukathcopybara-github
authored andcommitted
fix: Change express mode user flow so it's more clear that an express mode project is being created
Also adds link to express mode so the user knows that it is Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 896778982
1 parent d62558c commit 0fedb3b

File tree

2 files changed

+100
-81
lines changed

2 files changed

+100
-81
lines changed

src/google/adk/cli/cli_create.py

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -327,48 +327,61 @@ def _handle_login_with_google() -> (
327327
selected_project_id = projects[project_index - 1][0]
328328
region = _prompt_for_google_cloud_region(None)
329329
return None, selected_project_id, region
330-
else:
331-
if click.confirm(
332-
"No projects found automatically. Would you like to enter one"
333-
" manually?",
334-
default=False,
335-
):
336-
selected_project_id = _prompt_for_google_cloud(None)
337-
region = _prompt_for_google_cloud_region(None)
338-
return None, selected_project_id, region
339-
340-
# Check Express eligibility
341-
if gcp_utils.check_express_eligibility():
342-
click.secho(_EXPRESS_TOS_MSG, fg="yellow")
343-
if click.confirm("Do you accept the Terms of Service?", default=False):
344-
selected_region = click.prompt(
345-
"""\
330+
331+
click.secho(
332+
"A Google Cloud project is required to continue. You can enter an"
333+
" existing project ID or create an Express Mode project. Learn more:"
334+
" https://cloud.google.com/resources/cloud-express-faqs",
335+
fg="green",
336+
)
337+
action = click.prompt(
338+
"1. Enter an existing Google Cloud project ID\n"
339+
"2. Create a new project (Express Mode)\n"
340+
"3. Abandon\n"
341+
"Choose an action",
342+
type=click.Choice(["1", "2", "3"]),
343+
)
344+
345+
if action == "3":
346+
raise click.Abort()
347+
348+
if action == "1":
349+
google_cloud_project = _prompt_for_google_cloud(None)
350+
google_cloud_region = _prompt_for_google_cloud_region(None)
351+
return None, google_cloud_project, google_cloud_region
352+
353+
elif action == "2":
354+
if gcp_utils.check_express_eligibility():
355+
click.secho(_EXPRESS_TOS_MSG, fg="yellow")
356+
if click.confirm("Do you accept the Terms of Service?", default=False):
357+
selected_region = click.prompt(
358+
"""\
346359
Choose a region for Express Mode:
347360
1. us-central1
348361
2. europe-west1
349362
3. asia-southeast1
350363
Choose region""",
351-
type=click.Choice(["1", "2", "3"]),
352-
default="1",
353-
)
354-
region_map = {
355-
"1": "us-central1",
356-
"2": "europe-west1",
357-
"3": "asia-southeast1",
358-
}
359-
region = region_map[selected_region]
360-
express_info = gcp_utils.sign_up_express(location=region)
361-
api_key = express_info.get("api_key")
362-
project_id = express_info.get("project_id")
363-
region = express_info.get("region", region)
364-
click.secho(
365-
f"Express Mode project created: {project_id}",
366-
fg="green",
367-
)
368-
return api_key, project_id, region
369-
370-
click.secho(_NOT_ELIGIBLE_MSG, fg="red")
371-
raise click.Abort()
364+
type=click.Choice(["1", "2", "3"]),
365+
default="1",
366+
)
367+
region_map = {
368+
"1": "us-central1",
369+
"2": "europe-west1",
370+
"3": "asia-southeast1",
371+
}
372+
region = region_map[selected_region]
373+
express_info = gcp_utils.sign_up_express(location=region)
374+
api_key = express_info.get("api_key")
375+
project_id = express_info.get("project_id")
376+
region = express_info.get("region", region)
377+
click.secho(
378+
f"Express Mode project created: {project_id}",
379+
fg="green",
380+
)
381+
return api_key, project_id, region
382+
383+
click.secho(_NOT_ELIGIBLE_MSG, fg="red")
384+
raise click.Abort()
372385

373386

374387
def _prompt_to_choose_type() -> str:

tests/unittests/cli/utils/test_cli_create.py

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -356,17 +356,51 @@ def test_handle_login_with_google_select_gcp_project(
356356
assert region == "us-east1"
357357

358358

359-
def test_handle_login_with_google_express_signup(
359+
def test_handle_login_with_google_manual_project(
360360
monkeypatch: pytest.MonkeyPatch,
361361
) -> None:
362-
"""Handler should sign up for Express if eligible and user accepts TOS."""
362+
"""Handler should allow manual project ID entry when '0' is selected."""
363+
monkeypatch.setattr(gcp_utils, "check_adc", lambda: True)
364+
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
365+
monkeypatch.setattr(
366+
gcp_utils, "list_gcp_projects", lambda limit: [("p1", "Project 1")]
367+
)
368+
prompts = iter([0, "manual-proj", "us-east1"])
369+
monkeypatch.setattr(click, "prompt", lambda *a, **k: next(prompts))
370+
371+
api_key, proj, region = cli_create._handle_login_with_google()
372+
assert api_key is None
373+
assert proj == "manual-proj"
374+
assert region == "us-east1"
375+
376+
377+
def test_handle_login_with_google_option_1(
378+
monkeypatch: pytest.MonkeyPatch,
379+
) -> None:
380+
"""User selects 1, enters project ID and region."""
381+
monkeypatch.setattr(gcp_utils, "check_adc", lambda: True)
382+
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
383+
monkeypatch.setattr(gcp_utils, "list_gcp_projects", lambda limit: [])
384+
prompts = iter(["1", "test-proj", "us-east1"])
385+
monkeypatch.setattr(click, "prompt", lambda *a, **k: next(prompts))
386+
387+
api_key, proj, region = cli_create._handle_login_with_google()
388+
assert api_key is None
389+
assert proj == "test-proj"
390+
assert region == "us-east1"
391+
392+
393+
def test_handle_login_with_google_option_2(
394+
monkeypatch: pytest.MonkeyPatch,
395+
) -> None:
396+
"""User selects 2, goes through express sign up."""
363397
monkeypatch.setattr(gcp_utils, "check_adc", lambda: True)
364398
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
365399
monkeypatch.setattr(gcp_utils, "list_gcp_projects", lambda limit: [])
366400
monkeypatch.setattr(gcp_utils, "check_express_eligibility", lambda: True)
367-
confirms = iter([False, True])
368-
monkeypatch.setattr(click, "confirm", lambda *a, **k: next(confirms))
369-
monkeypatch.setattr(click, "prompt", lambda *a, **k: "1")
401+
monkeypatch.setattr(click, "confirm", lambda *a, **k: True)
402+
prompts = iter(["2", "1"])
403+
monkeypatch.setattr(click, "prompt", lambda *a, **k: next(prompts))
370404
monkeypatch.setattr(
371405
gcp_utils,
372406
"sign_up_express",
@@ -383,6 +417,17 @@ def test_handle_login_with_google_express_signup(
383417
assert region == "us-central1"
384418

385419

420+
def test_handle_login_with_google_option_3(
421+
monkeypatch: pytest.MonkeyPatch,
422+
) -> None:
423+
"""User selects 3, aborts."""
424+
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
425+
monkeypatch.setattr(gcp_utils, "list_gcp_projects", lambda limit: [])
426+
monkeypatch.setattr(click, "prompt", lambda *a, **k: "3")
427+
with pytest.raises(click.Abort):
428+
cli_create._handle_login_with_google()
429+
430+
386431
# prompt_str
387432
def test_prompt_str_non_empty(monkeypatch: pytest.MonkeyPatch) -> None:
388433
"""_prompt_str should retry until a non-blank string is provided."""
@@ -416,42 +461,3 @@ def test_get_gcp_region_from_gcloud_fail(
416461
),
417462
)
418463
assert cli_create._get_gcp_region_from_gcloud() == ""
419-
420-
421-
def test_handle_login_with_google_manual_project(
422-
monkeypatch: pytest.MonkeyPatch,
423-
) -> None:
424-
"""Handler should allow manual project ID entry when '0' is selected."""
425-
monkeypatch.setattr(gcp_utils, "check_adc", lambda: True)
426-
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
427-
monkeypatch.setattr(
428-
gcp_utils, "list_gcp_projects", lambda limit: [("p1", "Project 1")]
429-
)
430-
# First prompt is for project selection (0), second is for manual ID entry,
431-
# third is for region selection.
432-
prompts = iter([0, "manual-proj", "us-east1"])
433-
monkeypatch.setattr(click, "prompt", lambda *a, **k: next(prompts))
434-
435-
api_key, proj, region = cli_create._handle_login_with_google()
436-
assert api_key is None
437-
assert proj == "manual-proj"
438-
assert region == "us-east1"
439-
440-
441-
def test_handle_login_with_google_empty_projects_manual_entry(
442-
monkeypatch: pytest.MonkeyPatch,
443-
) -> None:
444-
"""Handler should allow manual entry if no projects are found and user accepts."""
445-
monkeypatch.setattr(gcp_utils, "check_adc", lambda: True)
446-
monkeypatch.setattr(gcp_utils, "retrieve_express_project", lambda: None)
447-
monkeypatch.setattr(gcp_utils, "list_gcp_projects", lambda limit: [])
448-
449-
# User says Yes to "enter manually", then provides project ID and region
450-
prompts = iter(["manual-proj", "us-east1"])
451-
monkeypatch.setattr(click, "confirm", lambda *a, **k: True)
452-
monkeypatch.setattr(click, "prompt", lambda *a, **k: next(prompts))
453-
454-
api_key, proj, region = cli_create._handle_login_with_google()
455-
assert api_key is None
456-
assert proj == "manual-proj"
457-
assert region == "us-east1"

0 commit comments

Comments
 (0)