Skip to content

Commit

Permalink
SNOW-1011769: Use urlparse to get registry url from repo url. Improvi…
Browse files Browse the repository at this point in the history
…ng error name
  • Loading branch information
sfc-gh-davwang committed Feb 6, 2024
1 parent e379138 commit 57261f9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/snowflake/cli/plugins/spcs/image_registry/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from snowflake.cli.api.commands.flags import DEFAULT_CONTEXT_SETTINGS
from snowflake.cli.api.output.types import MessageResult, ObjectResult
from snowflake.cli.plugins.spcs.image_registry.manager import (
NoRepositoriesViewableError,
NoImageRepositoriesFoundError,
RegistryManager,
)

Expand Down Expand Up @@ -35,7 +35,7 @@ def url(**options) -> MessageResult:
"""Gets the image registry URL for the current account. Must be called from a role that can view at least one image repository in the image registry."""
try:
return MessageResult(RegistryManager().get_registry_url())
except NoRepositoriesViewableError:
except NoImageRepositoriesFoundError:
raise ClickException(
"No image repository found. To get the registry url, please switch to a role with read access to at least one image repository or create a new image repository first."
)
13 changes: 9 additions & 4 deletions src/snowflake/cli/plugins/spcs/image_registry/manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import json
import re
from urllib.parse import urlparse

import requests
Expand All @@ -8,7 +9,7 @@
from snowflake.connector.cursor import DictCursor


class NoRepositoriesViewableError(ClickException):
class NoImageRepositoriesFoundError(ClickException):
def __init__(self, msg: str = "No image repository found."):
super().__init__(msg)

Expand Down Expand Up @@ -52,12 +53,16 @@ def login_to_registry(self, repo_url):
raise ClickException(f"Failed to login to the repository {resp.text}")
return json.loads(resp.text)["token"]

def _has_url_scheme(self, url: str):
return re.fullmatch(r"^.*//.+", url) is not None

def get_registry_url(self):
repositories_query = "show image repositories in account"
result_set = self._execute_query(repositories_query, cursor_class=DictCursor)
results = result_set.fetchall()
if len(results) == 0:
raise NoRepositoriesViewableError()
raise NoImageRepositoriesFoundError()
sample_repository_url = results[0]["repository_url"]

return "/".join(sample_repository_url.split("/")[:-3])
if not self._has_url_scheme(sample_repository_url):
sample_repository_url = f"//{sample_repository_url}"
return urlparse(sample_repository_url).netloc
25 changes: 21 additions & 4 deletions tests/spcs/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from tests.testing_utils.fixtures import *
from snowflake.cli.plugins.spcs.image_registry.manager import (
RegistryManager,
NoRepositoriesViewableError,
NoImageRepositoriesFoundError,
)
from snowflake.connector.cursor import DictCursor

Expand All @@ -11,7 +11,7 @@
@mock.patch(
"snowflake.cli.plugins.spcs.image_registry.manager.RegistryManager._execute_query"
)
def test_registry_get_token_2(mock_execute, mock_conn, mock_cursor, runner):
def test_registry_get_token(mock_execute, mock_conn, mock_cursor, runner):
mock_execute.return_value = mock_cursor(
["row"], ["Statement executed successfully"]
)
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_get_registry_url_no_repositories(mock_execute, mock_conn, mock_cursor):
rows=[],
columns=MOCK_REPO_COLUMNS,
)
with pytest.raises(NoRepositoriesViewableError):
with pytest.raises(NoImageRepositoriesFoundError):
RegistryManager().get_registry_url()

expected_query = "show image repositories in account"
Expand All @@ -84,7 +84,24 @@ def test_get_registry_url_no_repositories(mock_execute, mock_conn, mock_cursor):
"snowflake.cli.plugins.spcs.image_registry.manager.RegistryManager.get_registry_url"
)
def test_get_registry_url_no_repositories_cli(mock_get_registry_url, runner, snapshot):
mock_get_registry_url.side_effect = NoRepositoriesViewableError()
mock_get_registry_url.side_effect = NoImageRepositoriesFoundError()
result = runner.invoke(["spcs", "image-registry", "url"])
assert result.exit_code == 1, result.output
assert result.output == snapshot


@pytest.mark.parametrize(
"url, expected",
[
("www.google.com", False),
("https://www.google.com", True),
("//www.google.com", True),
("snowservices.registry.snowflakecomputing.com/db/schema/tutorial_repo", False),
(
"http://snowservices.registry.snowflakecomputing.com/db/schema/tutorial_repo",
True,
),
],
)
def test_has_url_scheme(url: str, expected: bool):
assert RegistryManager()._has_url_scheme(url) == expected
11 changes: 0 additions & 11 deletions tests_integration/spcs/__snapshots__/test_registry.ambr

This file was deleted.

0 comments on commit 57261f9

Please sign in to comment.