Skip to content

Commit 7392161

Browse files
hi-leimenglei
andauthored
add ssh_key_ids property for Volume entity (#5)
* add ssh_key_ids property for Volume entity deploy a new instance with os_volume attached * add test coverage for ssh_key_ids Co-authored-by: menglei <meng@datacrunch.io>
1 parent 66b82ca commit 7392161

5 files changed

Lines changed: 72 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode/
2+
.idea/
23

34
# Byte-compiled / optimized / DLL files
45
__pycache__/

datacrunch/instances/instances.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ def get_by_id(self, id: str) -> Instance:
321321
def create(self,
322322
instance_type: str,
323323
image: str,
324-
ssh_key_ids: list,
325324
hostname: str,
326325
description: str,
326+
ssh_key_ids: list = [],
327327
location: str = "FIN1",
328328
startup_script_id: str = None,
329329
volumes: List[Dict] = None,

datacrunch/volumes/volumes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self,
1818
target: str = None,
1919
location: str = "FIN1",
2020
instance_id: str = None,
21+
ssh_key_ids: List[str] = [],
2122
) -> None:
2223
"""Initialize the volume object
2324
@@ -41,6 +42,8 @@ def __init__(self,
4142
:type location: str, optional
4243
:param instance_id: the instance id the volume is attached to, None if detached
4344
:type instance_id: str
45+
:param ssh_key_ids: list of ssh keys ids
46+
:type ssh_key_ids: List[str]
4447
"""
4548

4649
self._id = id
@@ -53,6 +56,7 @@ def __init__(self,
5356
self._target = target
5457
self._location = location
5558
self._instance_id = instance_id
59+
self._ssh_key_ids = ssh_key_ids
5660

5761
@property
5862
def id(self) -> str:
@@ -144,6 +148,14 @@ def instance_id(self) -> Optional[str]:
144148
"""
145149
return self._instance_id
146150

151+
@property
152+
def ssh_key_ids(self) -> List[str]:
153+
"""Get the SSH key IDs of the instance
154+
155+
:return: SSH key IDs
156+
:rtype: List[str]
157+
"""
158+
return self._ssh_key_ids
147159

148160
class VolumesService:
149161
"""A service for interacting with the volumes endpoint"""
@@ -172,6 +184,7 @@ def get(self, status: str = None) -> List[Volume]:
172184
target=volume_dict['target'] if 'target' in volume_dict else None,
173185
location=volume_dict['location'],
174186
instance_id=volume_dict['instance_id'] if 'instance_id' in volume_dict else None,
187+
ssh_key_ids=volume_dict['ssh_key_ids'] if 'ssh_key_ids' in volume_dict else [],
175188
), volumes_dict))
176189
return volumes
177190

@@ -196,6 +209,7 @@ def get_by_id(self, id: str) -> Volume:
196209
target=volume_dict['target'] if 'target' in volume_dict else None,
197210
location=volume_dict['location'],
198211
instance_id=volume_dict['instance_id'] if 'instance_id' in volume_dict else None,
212+
ssh_key_ids=volume_dict['ssh_key_ids'] if 'ssh_key_ids' in volume_dict else [],
199213
)
200214
return volume
201215

tests/unit_tests/instances/test_instances.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,54 @@ def test_create_instance_successful(self, instances_service, endpoint):
261261
assert responses.assert_call_count(endpoint, 1) is True
262262
assert responses.assert_call_count(url, 1) is True
263263

264+
def test_create_instance_attached_os_volume_successful(self, instances_service, endpoint):
265+
# arrange - add response mock
266+
# create instance
267+
responses.add(
268+
responses.POST,
269+
endpoint,
270+
body=INSTANCE_ID,
271+
status=200
272+
)
273+
# get instance by id
274+
url = endpoint + '/' + INSTANCE_ID
275+
responses.add(
276+
responses.GET,
277+
url,
278+
json=PAYLOAD[0],
279+
status=200
280+
)
281+
282+
# act
283+
instance = instances_service.create(
284+
instance_type=INSTANCE_TYPE,
285+
image=OS_VOLUME_ID,
286+
hostname=INSTANCE_HOSTNAME,
287+
description=INSTANCE_DESCRIPTION,
288+
)
289+
290+
# assert
291+
assert type(instance) == Instance
292+
assert instance.id == INSTANCE_ID
293+
assert instance.ssh_key_ids == [SSH_KEY_ID]
294+
assert instance.status == INSTANCE_STATUS
295+
assert instance.image == INSTANCE_IMAGE
296+
assert instance.instance_type == INSTANCE_TYPE
297+
assert instance.price_per_hour == INSTANCE_PRICE_PER_HOUR
298+
assert instance.location == INSTANCE_LOCATION
299+
assert instance.description == INSTANCE_DESCRIPTION
300+
assert instance.hostname == INSTANCE_HOSTNAME
301+
assert instance.ip == INSTANCE_IP
302+
assert instance.created_at == INSTANCE_CREATED_AT
303+
assert instance.os_volume_id == OS_VOLUME_ID
304+
assert type(instance.cpu) == dict
305+
assert type(instance.gpu) == dict
306+
assert type(instance.memory) == dict
307+
assert type(instance.gpu_memory) == dict
308+
assert type(instance.storage) == dict
309+
assert responses.assert_call_count(endpoint, 1) is True
310+
assert responses.assert_call_count(url, 1) is True
311+
264312
def test_create_instance_failed(self, instances_service, endpoint):
265313
# arrange - add response mock
266314
responses.add(

tests/unit_tests/volumes/test_volumes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
NVME = "NVMe"
1515
HDD = "HDD"
1616
TARGET_VDA = "vda"
17+
SSH_KEY_ID = '12345dc1-a5d2-4972-ae4e-d429115d055b'
1718

1819
NVME_VOL_ID = "cf995e26-ce69-4149-84a3-cdd1e100670f"
1920
NVME_VOL_STATUS = VolumeStatus.ATTACHED
2021
NVME_VOL_NAME = "Volume-nxC2tf9F"
2122
NVME_VOL_SIZE = 50
2223
NVME_VOL_CREATED_AT = "2021-06-02T12:56:49.582Z"
2324

25+
2426
HDD_VOL_ID = "ea4edc62-9838-4b7c-bd5b-862f2efec675"
2527
HDD_VOL_STATUS = VolumeStatus.DETACHED
2628
HDD_VOL_NAME = "Volume-iHdL4ysR"
@@ -37,7 +39,8 @@
3739
"location": FIN1,
3840
"is_os_volume": True,
3941
"created_at": NVME_VOL_CREATED_AT,
40-
"target": TARGET_VDA
42+
"target": TARGET_VDA,
43+
"ssh_key_ids": SSH_KEY_ID
4144
}
4245

4346
HDD_VOLUME = {
@@ -50,7 +53,8 @@
5053
"location": FIN1,
5154
"is_os_volume": False,
5255
"created_at": HDD_VOL_CREATED_AT,
53-
"target": None
56+
"target": None,
57+
"ssh_key_ids": []
5458
}
5559

5660
PAYLOAD = [NVME_VOLUME, HDD_VOLUME]
@@ -94,6 +98,7 @@ def test_get_instances(self, volumes_service, endpoint):
9498
assert volume_nvme.is_os_volume
9599
assert volume_nvme.created_at == NVME_VOL_CREATED_AT
96100
assert volume_nvme.target == TARGET_VDA
101+
assert volume_nvme.ssh_key_ids == SSH_KEY_ID
97102

98103
assert volume_hdd.id == HDD_VOL_ID
99104
assert volume_hdd.status == HDD_VOL_STATUS
@@ -105,6 +110,7 @@ def test_get_instances(self, volumes_service, endpoint):
105110
assert volume_hdd.is_os_volume is False
106111
assert volume_hdd.created_at == HDD_VOL_CREATED_AT
107112
assert volume_hdd.target is None
113+
assert volume_hdd.ssh_key_ids == []
108114

109115
def test_get_volumes_by_status_successful(self, volumes_service, endpoint):
110116
# arrange - add response mock

0 commit comments

Comments
 (0)