-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathmanager.py
87 lines (74 loc) · 3.09 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright (c) 2024 Snowflake Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from urllib.parse import urlparse
from snowflake.cli._plugins.spcs.common import handle_object_already_exists
from snowflake.cli.api.constants import ObjectType
from snowflake.cli.api.identifiers import FQN
from snowflake.cli.api.sql_execution import SqlExecutionMixin
from snowflake.connector.cursor import SnowflakeCursor
from snowflake.connector.errors import ProgrammingError
class ImageRepositoryManager(SqlExecutionMixin):
def get_database(self):
return self._conn.database
def get_schema(self):
return self._conn.schema
def get_role(self):
return self._conn.role
def get_repository_url(self, repo_name: str, with_scheme: bool = True):
repo_row = self.show_specific_object(
"image repositories", repo_name, check_schema=True
)
if repo_row is None:
fqn = FQN.from_string(repo_name).using_connection(self._conn)
raise ProgrammingError(
f"Image repository '{fqn.identifier}' does not exist or not authorized."
)
if with_scheme:
return f"https://{repo_row['repository_url']}"
else:
return repo_row["repository_url"]
def get_repository_api_url(self, repo_url):
"""
Converts a repo URL to a registry OCI API URL.
https://reg.com/db/schema/repo becomes https://reg.com/v2/db/schema/repo
"""
parsed_url = urlparse(repo_url)
scheme = parsed_url.scheme
host = parsed_url.netloc
path = parsed_url.path
return f"{scheme}://{host}/v2{path}"
def create(
self,
name: str,
if_not_exists: bool,
replace: bool,
) -> SnowflakeCursor:
if if_not_exists and replace:
raise ValueError(
"'replace' and 'if_not_exists' options are mutually exclusive for ImageRepositoryManager.create"
)
elif replace:
create_statement = "create or replace image repository"
elif if_not_exists:
create_statement = "create image repository if not exists"
else:
create_statement = "create image repository"
try:
return self._execute_schema_query(f"{create_statement} {name}", name=name)
except ProgrammingError as e:
handle_object_already_exists(
e, ObjectType.IMAGE_REPOSITORY, name, replace_available=True
)
def list_images(self, repo_name: str) -> SnowflakeCursor:
return self.execute_query(f"show images in image repository {repo_name}")