Skip to content

Commit b3f55c4

Browse files
author
wangjiaju.716
committed
Add ve_cr create_instance, create_namespace, create_repo
1 parent 7f19c35 commit b3f55c4

File tree

1 file changed

+149
-23
lines changed

1 file changed

+149
-23
lines changed

veadk/integrations/ve_cr/ve_cr.py

Lines changed: 149 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from veadk.utils.volcengine_sign import ve_request
216
from veadk.utils.logger import get_logger
317
from veadk.consts import (
@@ -18,7 +32,20 @@ def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing")
1832
assert region in ["cn-beijing", "cn-guangzhou", "cn-shanghai"]
1933
self.version = "2022-05-12"
2034

21-
def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME):
35+
def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME) -> str:
36+
"""
37+
create cr instance
38+
39+
Args:
40+
instance_name: cr instance name
41+
42+
Returns:
43+
cr instance name
44+
"""
45+
status = self._check_instance(instance_name)
46+
if status != "NONEXIST":
47+
logger.debug(f"cr instance {instance_name} already running")
48+
return instance_name
2249
response = ve_request(
2350
request_body={
2451
"Name": instance_name,
@@ -34,10 +61,30 @@ def _create_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME):
3461
region=self.region,
3562
host=f"cr.{self.region}.volcengineapi.com",
3663
)
37-
logger.info(f"create cr instance {instance_name}: {response}")
38-
return response
64+
logger.debug(f"create cr instance {instance_name}: {response}")
65+
66+
while True:
67+
status = self._check_instance(instance_name)
68+
if status == "Running":
69+
break
70+
elif status == "Failed":
71+
raise ValueError(f"cr instance {instance_name} create failed")
72+
else:
73+
logger.debug(f"cr instance status: {status}")
74+
time.sleep(5)
75+
76+
return instance_name
3977

40-
def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME):
78+
def _check_instance(self, instance_name: str) -> str:
79+
"""
80+
check cr instance status
81+
82+
Args:
83+
instance_name: cr instance name
84+
85+
Returns:
86+
cr instance status
87+
"""
4188
response = ve_request(
4289
request_body={
4390
"Filter": {
@@ -47,33 +94,112 @@ def _check_instance(self, instance_name: str = DEFAULT_CR_INSTANCE_NAME):
4794
action="ListRegistries",
4895
ak=self.ak,
4996
sk=self.sk,
50-
service="vecr",
97+
service="cr",
5198
version=self.version,
5299
region=self.region,
53100
host=f"cr.{self.region}.volcengineapi.com",
54101
)
102+
logger.debug(f"check cr instance {instance_name}: {response}")
55103

56104
try:
57-
return response["Result"]["Items"][0]["Status"]
58-
except Exception as e:
59-
raise ValueError(f"cr instance {instance_name} not found: {e}")
105+
if response["Result"]["TotalCount"] == 0:
106+
return "NONEXIST"
107+
return response["Result"]["Items"][0]["Status"]["Phase"]
108+
except Exception as _:
109+
raise ValueError(f"Error check cr instance {instance_name}: {response}")
60110

61-
def _create_namespace(self, namespace_name: str = DEFAULT_CR_NAMESPACE_NAME):
62-
pass
111+
def _create_namespace(
112+
self,
113+
instance_name: str = DEFAULT_CR_INSTANCE_NAME,
114+
namespace_name: str = DEFAULT_CR_NAMESPACE_NAME,
115+
) -> str:
116+
"""
117+
create cr namespace
63118
64-
def _create_repo(self, repo_name: str = DEFAULT_CR_REPO_NAME):
65-
pass
119+
Args:
120+
instance_name: cr instance name
121+
namespace_name: cr namespace name
66122
123+
Returns:
124+
cr namespace name
125+
"""
126+
response = ve_request(
127+
request_body={
128+
"Name": namespace_name,
129+
"Registry": instance_name,
130+
},
131+
action="CreateNamespace",
132+
ak=self.ak,
133+
sk=self.sk,
134+
service="cr",
135+
version=self.version,
136+
region=self.region,
137+
host=f"cr.{self.region}.volcengineapi.com",
138+
)
139+
logger.debug(f"create cr namespace {namespace_name}: {response}")
140+
141+
if "Error" in response["ResponseMetadata"]:
142+
error_code = response["ResponseMetadata"]["Error"]["Code"]
143+
error_message = response["ResponseMetadata"]["Error"]["Message"]
144+
if error_code == "AlreadyExists.Namespace":
145+
logger.debug(f"cr namespace {namespace_name} already exists")
146+
return namespace_name
147+
else:
148+
logger.error(
149+
f"Error create cr namespace {namespace_name}: {error_code} {error_message}"
150+
)
151+
raise ValueError(
152+
f"Error create cr namespace {namespace_name}: {error_code} {error_message}"
153+
)
154+
155+
return namespace_name
156+
157+
def _create_repo(
158+
self,
159+
instance_name: str = DEFAULT_CR_INSTANCE_NAME,
160+
namespace_name: str = DEFAULT_CR_NAMESPACE_NAME,
161+
repo_name: str = DEFAULT_CR_REPO_NAME,
162+
) -> str:
163+
"""
164+
create cr repo
165+
166+
Args:
167+
instance_name: cr instance name
168+
namespace_name: cr namespace name
169+
repo_name: cr repo name
170+
171+
Returns:
172+
cr repo name
173+
"""
174+
response = ve_request(
175+
request_body={
176+
"Name": repo_name,
177+
"Registry": instance_name,
178+
"Namespace": namespace_name,
179+
"Description": "veadk cr repo",
180+
},
181+
action="CreateRepository",
182+
ak=self.ak,
183+
sk=self.sk,
184+
service="cr",
185+
version=self.version,
186+
region=self.region,
187+
host=f"cr.{self.region}.volcengineapi.com",
188+
)
189+
logger.debug(f"create cr repo {repo_name}: {response}")
67190

68-
if __name__ == "__main__":
69-
cr = VeCR("", "")
70-
cr._create_instance()
191+
if "Error" in response["ResponseMetadata"]:
192+
error_code = response["ResponseMetadata"]["Error"]["Code"]
193+
error_message = response["ResponseMetadata"]["Error"]["Message"]
194+
if error_code == "AlreadyExists.Repository":
195+
logger.debug(f"cr repo {repo_name} already exists")
196+
return repo_name
197+
else:
198+
logger.error(
199+
f"Error create cr repo {repo_name}: {error_code} {error_message}"
200+
)
201+
raise ValueError(
202+
f"Error create cr repo {repo_name}: {error_code} {error_message}"
203+
)
71204

72-
while True:
73-
status = cr._check_instance()
74-
if status["Phase"] == "Running":
75-
print("cr instance running")
76-
break
77-
else:
78-
print("cr instance not running")
79-
time.sleep(30)
205+
return repo_name

0 commit comments

Comments
 (0)