Skip to content

Commit 5520397

Browse files
Fix account locator
1 parent f0ed2d4 commit 5520397

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/snowflake/cli/_plugins/connection/util.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ def is_regionless_redirect(conn: SnowflakeConnection) -> bool:
112112
def get_host_region(host: str) -> str | None:
113113
"""
114114
Looks for hosts of form
115-
<account>.[x.y.z].snowflakecomputing.com
116-
Returns the three-part [region identifier] or None.
115+
<account>.[x.y[.z]].snowflakecomputing.com
116+
Returns the two-part or three-part [region identifier] or None.
117117
"""
118118
host_parts = host.split(".")
119119
if host_parts[-1] == "local":
120120
return LOCAL_DEPLOYMENT_REGION
121121
elif len(host_parts) == 6:
122122
return ".".join(host_parts[1:4])
123+
elif len(host_parts) == 5:
124+
return ".".join(host_parts[1:3])
123125
return None
124126

125127

@@ -172,7 +174,17 @@ def get_context(conn: SnowflakeConnection) -> str:
172174
return get_region(conn)
173175

174176

175-
def get_account(conn: SnowflakeConnection) -> str:
177+
def get_account_locator(conn: SnowflakeConnection) -> str:
178+
"""
179+
Determines the account locator that this connection refers to.
180+
"""
181+
*_, cursor = conn.execute_string(
182+
f"select current_account_locator()", cursor_class=DictCursor
183+
)
184+
return cursor.fetchone()["CURRENT_ACCOUNT_LOCATOR()"].lower()
185+
186+
187+
def get_account_name(conn: SnowflakeConnection) -> str:
176188
"""
177189
Determines the account that this connection refers to.
178190
"""
@@ -214,9 +226,12 @@ def make_snowsight_url(conn: SnowflakeConnection, path: str) -> str:
214226
"""
215227
snowsight_host = get_snowsight_host(conn)
216228
deployment = get_context(conn)
217-
account = get_account(conn)
218229
path_with_slash = path if path.startswith("/") else f"/{path}"
219-
return f"{snowsight_host}/{deployment}/{account}{path_with_slash}"
230+
if len(deployment.split(".")) == 2:
231+
account_locator = get_account_locator(conn)
232+
return f"{snowsight_host}/{deployment}/{account_locator}{path_with_slash}"
233+
account_name = get_account_name(conn)
234+
return f"{snowsight_host}/{deployment}/{account_name}{path_with_slash}"
220235

221236

222237
def strip_if_value_present(value: Optional[str]) -> Optional[str]:

tests/test_utils.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_identifier_for_url(identifier, expected):
176176
assert identifier_for_url(identifier) == expected
177177

178178

179-
@patch("snowflake.cli._plugins.connection.util.get_account")
179+
@patch("snowflake.cli._plugins.connection.util.get_account_name")
180180
@patch("snowflake.cli._plugins.connection.util.get_context")
181181
@patch("snowflake.cli._plugins.connection.util.get_snowsight_host")
182182
@pytest.mark.parametrize(
@@ -218,6 +218,27 @@ def test_make_snowsight_url(
218218
assert actual == expected
219219

220220

221+
@patch(
222+
"snowflake.cli._plugins.connection.util.is_regionless_redirect", return_value=False
223+
)
224+
@patch("snowflake.cli._plugins.connection.util.get_region", return_value="x.y")
225+
@patch(
226+
"snowflake.cli._plugins.connection.util.get_account_locator",
227+
return_value="account_locator",
228+
)
229+
@patch(
230+
"snowflake.cli._plugins.connection.util.get_snowsight_host",
231+
return_value="https://test.snowsight.host",
232+
)
233+
def test_get_snowsight_host_with_account_locator(
234+
get_snowsight_host, get_locator, get_region, is_regionless
235+
):
236+
assert (
237+
make_snowsight_url(None, "an/url/path")
238+
== "https://test.snowsight.host/x.y/account_locator/an/url/path"
239+
)
240+
241+
221242
@pytest.mark.parametrize(
222243
"allowlist, expected",
223244
[
@@ -286,8 +307,8 @@ def test_get_context_local_non_regionless_gets_local_region(
286307
("org-acct.mydns.snowflakecomputing.com", None),
287308
("account.x.us-west-2.aws.snowflakecomputing.com", "x.us-west-2.aws"),
288309
("naf_test_pc.us-west-2.snowflakecomputing.com", None),
289-
("test_account.az.int.snowflakecomputing.com", None),
290-
("frozenweb.prod3.external-zone.snowflakecomputing.com", None),
310+
("test_account.az.int.snowflakecomputing.com", "az.int"),
311+
("frozenweb.prod3.external-zone.snowflakecomputing.com", "prod3.external-zone"),
291312
],
292313
)
293314
def test_get_host_region(host, expected):

0 commit comments

Comments
 (0)