Skip to content

Commit 197ca81

Browse files
committed
refactor: adjust based on feedback
Signed-off-by: Jack Yu <[email protected]>
1 parent fad604d commit 197ca81

File tree

6 files changed

+118
-110
lines changed

6 files changed

+118
-110
lines changed

apiclient/harvester_api/managers/images.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,13 @@ def delete(self, name, namespace=DEFAULT_NAMESPACE, *, raw=False):
7878
def download(self, name, namespace=DEFAULT_NAMESPACE):
7979
return self._get(self.DOWNLOAD_fmt.format(uid=name, ns=namespace), raw=True)
8080

81-
def create_crypto_image(
81+
def create_by_encrypt(
8282
self,
8383
source_image_name,
8484
new_image_name,
8585
storage_class_name,
86-
crypto_operation,
87-
namespace="default",
86+
namespace=DEFAULT_NAMESPACE,
8887
):
89-
"""
90-
Create encrypted/decrypted image, crypto_operation can be "encrypt" or "decrypt"
91-
"""
9288
data = self.create_data(
9389
name=new_image_name,
9490
url=None,
@@ -100,12 +96,36 @@ def create_crypto_image(
10096
storageclass=storage_class_name
10197
)
10298

103-
# Remove unnecessary fields
104-
data["spec"].pop("checksum", None)
105-
data["spec"].pop("url", None)
10699
# Add securityParameters
107100
data["spec"]["securityParameters"] = {
108-
"cryptoOperation": crypto_operation,
101+
"cryptoOperation": "encrypt",
102+
"sourceImageName": source_image_name,
103+
"sourceImageNamespace": namespace,
104+
}
105+
106+
return self.create("", namespace=namespace, json=data)
107+
108+
def create_by_decrypt(
109+
self,
110+
source_image_name,
111+
new_image_name,
112+
storage_class_name,
113+
namespace=DEFAULT_NAMESPACE,
114+
):
115+
data = self.create_data(
116+
name=new_image_name,
117+
url=None,
118+
desc="",
119+
stype="clone",
120+
namespace=namespace,
121+
image_checksum=None,
122+
display_name=new_image_name,
123+
storageclass=storage_class_name
124+
)
125+
126+
# Add securityParameters
127+
data["spec"]["securityParameters"] = {
128+
"cryptoOperation": "decrypt",
109129
"sourceImageName": source_image_name,
110130
"sourceImageNamespace": namespace,
111131
}

apiclient/harvester_api/managers/secrets.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ def create_data(self, name, namespace, data, annotations=None):
2525

2626
def create(self, name, data, namespace=DEFAULT_NAMESPACE, annotations=None, **kwargs):
2727
secret_data = self.create_data(name, namespace, data, annotations=annotations)
28-
return self._create(
29-
self.CREATE_fmt,
30-
json=secret_data, **kwargs)
28+
return self._create(self.CREATE_fmt, json=secret_data, **kwargs)
3129

3230
def get(self, name, namespace=DEFAULT_NAMESPACE, *, raw=False):
33-
return self._get(
34-
self.PATH_fmt.format(ns=namespace, name=name), raw=raw)
31+
return self._get(self.PATH_fmt.format(ns=namespace, name=name), raw=raw)
3532

3633
def delete(self, name, namespace=DEFAULT_NAMESPACE, *, raw=False):
37-
return self._delete(
38-
self.PATH_fmt.format(ns=namespace, name=name), raw=raw)
34+
return self._delete(self.PATH_fmt.format(ns=namespace, name=name), raw=raw)

apiclient/harvester_api/managers/storageclasses.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_default(self):
2323
else:
2424
return code, data
2525

26-
def create_data(self, name, replicas):
26+
def create_data(self, name, replicas, **options):
2727
data = {
2828
"type": f"{self.API_VERSION}",
2929
"metadata": {
@@ -39,18 +39,11 @@ def create_data(self, name, replicas):
3939
"reclaimPolicy": "Delete",
4040
"volumeBindingMode": "Immediate"
4141
}
42+
return merge_dict(options, data)
4243

43-
return data
44-
45-
def create(self, name, replicas=3, *, raw=False):
46-
path = self.CREATE_PATH_fmt.format(SC_API=self.API_VERSION)
47-
data = self.create_data(name, replicas)
48-
return self._create(path, json=data, raw=raw)
49-
50-
def create_by_parameters(self, name, parameters, replicas=3, *, raw=False):
44+
def create(self, name, replicas=3, *, raw=False, **options):
5145
path = self.CREATE_PATH_fmt.format(SC_API=self.API_VERSION)
52-
data = self.create_data(name, replicas)
53-
merge_dict(parameters, data["parameters"])
46+
data = self.create_data(name, replicas, **options)
5447
return self._create(path, json=data, raw=raw)
5548

5649
def set_default(self, name, *, raw=False):
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from .base import wait_until
3+
4+
5+
@pytest.fixture(scope="session")
6+
def resource_checker(api_client, wait_timeout, sleep_timeout):
7+
class ResourceChecker:
8+
def __init__(self):
9+
pass
10+
11+
@wait_until(wait_timeout, sleep_timeout)
12+
def wait_deleted(self, func_get, name, namespace=None):
13+
if namespace is not None:
14+
code, data = func_get(name, namespace=namespace)
15+
else:
16+
code, data = func_get(name)
17+
if code == 404:
18+
return True, (code, data)
19+
return False, (code, data)
20+
21+
return ResourceChecker()

harvester_e2e_tests/integrations/test_1_images.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ def fake_invalid_image_file():
3737
yield Path(f.name)
3838

3939

40-
def wait_resource_deleted(get_func, name, wait_timeout, namespace=None, sleep_time=3):
41-
"""
42-
Long polling until resource is deleted (get_func returns 404) or timeout.
43-
get_func: function to get the resource, should return (code, data)
44-
name: resource name
45-
wait_timeout: timeout in seconds
46-
namespace: optional, if needed by get_func
47-
sleep_time: polling interval in seconds
48-
"""
49-
endtime = datetime.now() + timedelta(seconds=wait_timeout)
50-
while endtime > datetime.now():
51-
if namespace is not None:
52-
code, _ = get_func(name, namespace=namespace)
53-
else:
54-
code, _ = get_func(name)
55-
if code == 404:
56-
return
57-
sleep(sleep_time)
58-
raise AssertionError(f"Failed to delete resource {name} in {wait_timeout} seconds")
59-
60-
6140
def wait_image_progress(api_client, image_name, wait_timeout):
6241
"""
6342
Long polling image status until progress == 100 or timeout.
@@ -162,7 +141,7 @@ def cluster_network(api_client, vlan_nic):
162141

163142

164143
@pytest.fixture(scope="module")
165-
def shared_image(api_client, image_ubuntu, unique_name, wait_timeout):
144+
def shared_image(api_client, image_ubuntu, unique_name, wait_timeout, image_checker):
166145
source_image_name = f"shared-ubuntu-{unique_name}"
167146
image_info = image_ubuntu
168147
create_image_url(
@@ -178,7 +157,7 @@ def shared_image(api_client, image_ubuntu, unique_name, wait_timeout):
178157
# Cleanup: delete the image after tests
179158
code, _ = api_client.images.delete(source_image_name)
180159
if code == 200:
181-
wait_resource_deleted(api_client.images.get, source_image_name, wait_timeout)
160+
image_checker.wait_deleted(source_image_name)
182161

183162

184163
@pytest.fixture(scope="class")

0 commit comments

Comments
 (0)