|
30 | 30 |
|
31 | 31 | from stardag._cli._helpers import get_authenticated_client |
32 | 32 | from stardag._cli.credentials import ( |
33 | | - get_active_profile, |
34 | | - list_profiles, |
| 33 | + list_registries, |
35 | 34 | resolve_environment_slug_to_id, |
36 | 35 | resolve_workspace_slug_to_id, |
37 | 36 | ) |
| 37 | +from stardag.config.cache import get_cached_slugs |
38 | 38 | from stardag.config.loader import clear_config_cache, get_config |
39 | 39 | from stardag.integration.modal import StardagApp |
40 | 40 |
|
|
61 | 61 | app.add_typer(stardag_api_key_app, name="stardag-api-key") |
62 | 62 |
|
63 | 63 |
|
64 | | -def _get_profile_slugs( |
65 | | - profile_name: str | None, |
66 | | - workspace_id: str | None = None, |
67 | | - environment_id: str | None = None, |
68 | | -) -> tuple[str | None, str | None]: |
69 | | - """Get workspace and environment slugs from a profile. |
| 64 | +def _registry_name_for_url(api_url: str | None) -> str | None: |
| 65 | + """Look up the configured registry name for a given URL. |
70 | 66 |
|
71 | | - Returns (workspace_slug, environment_slug) where slug is None if |
72 | | - the profile value matches the resolved ID (i.e. it's already a UUID). |
| 67 | + The id-cache is keyed by registry *name* (the TOML key under ``[registry.<name>]``) |
| 68 | + rather than URL, so we reverse-lookup the name from the URL the resolved IDs were |
| 69 | + fetched against. Returns ``None`` if no registry with a matching URL is configured |
| 70 | + (e.g. STARDAG_API_URL is set without a corresponding TOML entry). |
73 | 71 | """ |
74 | | - if not profile_name: |
75 | | - profile_name_resolved, _ = get_active_profile() |
76 | | - profile_name = profile_name_resolved |
77 | | - |
78 | | - if not profile_name: |
79 | | - return None, None |
80 | | - |
81 | | - profiles = list_profiles() |
82 | | - prof = profiles.get(profile_name) |
83 | | - if not prof: |
| 72 | + if not api_url: |
| 73 | + return None |
| 74 | + target = api_url.rstrip("/") |
| 75 | + for name, url in list_registries().items(): |
| 76 | + if url.rstrip("/") == target: |
| 77 | + return name |
| 78 | + return None |
| 79 | + |
| 80 | + |
| 81 | +def _resolve_display_slugs( |
| 82 | + api_url: str | None, |
| 83 | + workspace_id: str | None, |
| 84 | + environment_id: str | None, |
| 85 | +) -> tuple[str | None, str | None]: |
| 86 | + """Reverse-lookup workspace/environment slugs for the resolved UUIDs. |
| 87 | +
|
| 88 | + Why: the resolved UUIDs may come from env vars or a custom config_provider |
| 89 | + override and have no relation to the active profile's TOML slugs. Reading |
| 90 | + `prof["environment"]` could display a slug for a totally different env |
| 91 | + than the resolved UUID. We also can't trust ``get_config().context.registry_name`` |
| 92 | + because callers like ``deploy(--profile foo)`` and ``stardag_api_key_create`` |
| 93 | + resolve IDs under a temporarily-overridden profile, then restore the env |
| 94 | + before this function runs — leaving ``get_config()`` pointing at the |
| 95 | + *active* profile, which may use a different registry. Pin the registry to |
| 96 | + the URL the IDs were actually resolved against. |
| 97 | + """ |
| 98 | + registry_name = _registry_name_for_url(api_url) |
| 99 | + if not registry_name: |
84 | 100 | return None, None |
85 | | - |
86 | | - ws_slug = prof["workspace"] if prof["workspace"] != workspace_id else None |
87 | | - env_slug = prof["environment"] if prof["environment"] != environment_id else None |
88 | | - return ws_slug, env_slug |
| 101 | + return get_cached_slugs(registry_name, workspace_id, environment_id) |
89 | 102 |
|
90 | 103 |
|
91 | | -def _print_stardag_context( |
92 | | - env_vars: dict[str, str], |
93 | | - profile_name: str | None = None, |
94 | | -) -> None: |
| 104 | +def _print_stardag_context(env_vars: dict[str, str]) -> None: |
95 | 105 | """Print stardag context info (registry, workspace, environment, target roots).""" |
96 | 106 | import json |
97 | 107 |
|
98 | 108 | registry = env_vars.get("STARDAG_API_URL", "none (local mode)") |
99 | 109 | ws_id = env_vars.get("STARDAG_WORKSPACE_ID", "N/A") |
100 | 110 | env_id = env_vars.get("STARDAG_ENVIRONMENT_ID", "N/A") |
101 | 111 |
|
102 | | - ws_slug, env_slug = _get_profile_slugs(profile_name, ws_id, env_id) |
| 112 | + ws_slug, env_slug = _resolve_display_slugs( |
| 113 | + env_vars.get("STARDAG_API_URL"), ws_id, env_id |
| 114 | + ) |
103 | 115 |
|
104 | 116 | console.print(f"[dim] Registry: {registry}[/dim]") |
105 | 117 |
|
@@ -297,7 +309,7 @@ def stardag_api_key_create( |
297 | 309 | if api_key_name is None: |
298 | 310 | api_key_name = f"modal-{modal_env or 'default'}" |
299 | 311 |
|
300 | | - ws_slug, env_slug = _get_profile_slugs(stardag_profile, ws_id, env_id) |
| 312 | + ws_slug, env_slug = _resolve_display_slugs(api_url, ws_id, env_id) |
301 | 313 | ws_display = f"{ws_id} ({ws_slug})" if ws_slug else ws_id |
302 | 314 | env_display = f"{env_id} ({env_slug})" if env_slug else env_id |
303 | 315 |
|
@@ -561,7 +573,7 @@ def deploy( |
561 | 573 | console.print(f"[cyan]Using stardag profile: {profile}[/cyan]") |
562 | 574 | env_vars = get_profile_env_vars(profile) |
563 | 575 | if env_vars: |
564 | | - _print_stardag_context(env_vars, profile) |
| 576 | + _print_stardag_context(env_vars) |
565 | 577 | extra_secrets.append( |
566 | 578 | modal.Secret.from_dict(dict(env_vars)) # type: ignore[arg-type] |
567 | 579 | ) |
|
0 commit comments