Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow prefect cloud login to override current workspace #12867

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/prefect/cli/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ async def login(
profiles = load_profiles()
current_profile = get_settings_context().profile
env_var_api_key = PREFECT_API_KEY.value()
selected_workspace = None

if env_var_api_key and key and env_var_api_key != key:
exit_with_error(
Expand Down Expand Up @@ -483,6 +484,7 @@ async def login(
# Search for the given workspace
for workspace in workspaces:
if workspace.handle == workspace_handle:
selected_workspace = workspace
break
else:
if workspaces:
Expand Down Expand Up @@ -516,7 +518,8 @@ async def login(
)
if selected_workspace is None:
exit_with_error("No workspace selected.")
else:

elif not selected_workspace and not workspace_handle:
if current_workspace:
selected_workspace = current_workspace
elif len(workspaces) > 0:
Expand Down
33 changes: 33 additions & 0 deletions tests/cli/test_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,39 @@ def test_login_with_non_interactive_missing_args(args):
)


def test_login_with_key_and_workspace_overrides_current_workspace(respx_mock):
foo_workspace = gen_test_workspace(account_handle="test", workspace_handle="foo")
bar_workspace = gen_test_workspace(account_handle="test", workspace_handle="bar")

respx_mock.get(PREFECT_CLOUD_API_URL.value() + "/me/workspaces").mock(
return_value=httpx.Response(
status.HTTP_200_OK,
json=[
foo_workspace.dict(json_compatible=True),
bar_workspace.dict(json_compatible=True),
],
)
)

# Set up a current profile with a different workspace
profiles = load_profiles()
profiles.set_active("default")
assert profiles.active_profile is not None
# set a different current workspace
zzstoatzz marked this conversation as resolved.
Show resolved Hide resolved
profiles.active_profile.settings[PREFECT_API_URL] = foo_workspace.api_url()
assert profiles.active_profile.settings[PREFECT_API_URL] == foo_workspace.api_url()

invoke_and_assert(
["cloud", "login", "--key", "new_key", "--workspace", "test/bar"],
expected_code=0,
expected_output="Authenticated with Prefect Cloud! Using workspace 'test/bar'.",
)

settings = load_current_profile().settings
assert settings[PREFECT_API_KEY] == "new_key"
assert settings[PREFECT_API_URL] == bar_workspace.api_url()


@pytest.mark.usefixtures("interactive_console")
def test_login_with_key_and_no_workspaces(respx_mock):
respx_mock.get(PREFECT_CLOUD_API_URL.value() + "/me/workspaces").mock(
Expand Down