Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode/
.idea/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion datacrunch/instances/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ def get_by_id(self, id: str) -> Instance:
def create(self,
instance_type: str,
image: str,
ssh_key_ids: list,
hostname: str,
description: str,
ssh_key_ids: list = [],
location: str = "FIN1",
startup_script_id: str = None,
volumes: List[Dict] = None) -> Instance:
Expand Down
14 changes: 14 additions & 0 deletions datacrunch/volumes/volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self,
target: str = None,
location: str = "FIN1",
instance_id: str = None,
ssh_key_ids: List[str] = [],
) -> None:
"""Initialize the volume object

Expand All @@ -41,6 +42,8 @@ def __init__(self,
:type location: str, optional
:param instance_id: the instance id the volume is attached to, None if detached
:type instance_id: str
:param ssh_key_ids: list of ssh keys ids
:type ssh_key_ids: List[str]
"""

self._id = id
Expand All @@ -53,6 +56,7 @@ def __init__(self,
self._target = target
self._location = location
self._instance_id = instance_id
self._ssh_key_ids = ssh_key_ids

@property
def id(self) -> str:
Expand Down Expand Up @@ -144,6 +148,14 @@ def instance_id(self) -> Optional[str]:
"""
return self._instance_id

@property
def ssh_key_ids(self) -> List[str]:
"""Get the SSH key IDs of the instance

:return: SSH key IDs
:rtype: List[str]
"""
return self._ssh_key_ids

class VolumesService:
"""A service for interacting with the volumes endpoint"""
Expand Down Expand Up @@ -172,6 +184,7 @@ def get(self, status: str = None) -> List[Volume]:
target=volume_dict['target'] if 'target' in volume_dict else None,
location=volume_dict['location'],
instance_id=volume_dict['instance_id'] if 'instance_id' in volume_dict else None,
ssh_key_ids=volume_dict['ssh_key_ids'] if 'ssh_key_ids' in volume_dict else [],
), volumes_dict))
return volumes

Expand All @@ -196,6 +209,7 @@ def get_by_id(self, id: str) -> Volume:
target=volume_dict['target'] if 'target' in volume_dict else None,
location=volume_dict['location'],
instance_id=volume_dict['instance_id'] if 'instance_id' in volume_dict else None,
ssh_key_ids=volume_dict['ssh_key_ids'] if 'ssh_key_ids' in volume_dict else [],
)
return volume

Expand Down
48 changes: 48 additions & 0 deletions tests/unit_tests/instances/test_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,54 @@ def test_create_instance_successful(self, instances_service, endpoint):
assert responses.assert_call_count(endpoint, 1) is True
assert responses.assert_call_count(url, 1) is True

def test_create_instance_attached_os_volume_successful(self, instances_service, endpoint):
# arrange - add response mock
# create instance
responses.add(
responses.POST,
endpoint,
body=INSTANCE_ID,
status=200
)
# get instance by id
url = endpoint + '/' + INSTANCE_ID
responses.add(
responses.GET,
url,
json=PAYLOAD[0],
status=200
)

# act
instance = instances_service.create(
instance_type=INSTANCE_TYPE,
image=OS_VOLUME_ID,
hostname=INSTANCE_HOSTNAME,
description=INSTANCE_DESCRIPTION,
)

# assert
assert type(instance) == Instance
assert instance.id == INSTANCE_ID
assert instance.ssh_key_ids == [SSH_KEY_ID]
assert instance.status == INSTANCE_STATUS
assert instance.image == INSTANCE_IMAGE
assert instance.instance_type == INSTANCE_TYPE
assert instance.price_per_hour == INSTANCE_PRICE_PER_HOUR
assert instance.location == INSTANCE_LOCATION
assert instance.description == INSTANCE_DESCRIPTION
assert instance.hostname == INSTANCE_HOSTNAME
assert instance.ip == INSTANCE_IP
assert instance.created_at == INSTANCE_CREATED_AT
assert instance.os_volume_id == OS_VOLUME_ID
assert type(instance.cpu) == dict
assert type(instance.gpu) == dict
assert type(instance.memory) == dict
assert type(instance.gpu_memory) == dict
assert type(instance.storage) == dict
assert responses.assert_call_count(endpoint, 1) is True
assert responses.assert_call_count(url, 1) is True

def test_create_instance_failed(self, instances_service, endpoint):
# arrange - add response mock
responses.add(
Expand Down
10 changes: 8 additions & 2 deletions tests/unit_tests/volumes/test_volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
NVME = "NVMe"
HDD = "HDD"
TARGET_VDA = "vda"
SSH_KEY_ID = '12345dc1-a5d2-4972-ae4e-d429115d055b'

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


HDD_VOL_ID = "ea4edc62-9838-4b7c-bd5b-862f2efec675"
HDD_VOL_STATUS = VolumeStatus.DETACHED
HDD_VOL_NAME = "Volume-iHdL4ysR"
Expand All @@ -37,7 +39,8 @@
"location": FIN1,
"is_os_volume": True,
"created_at": NVME_VOL_CREATED_AT,
"target": TARGET_VDA
"target": TARGET_VDA,
"ssh_key_ids": SSH_KEY_ID
}

HDD_VOLUME = {
Expand All @@ -50,7 +53,8 @@
"location": FIN1,
"is_os_volume": False,
"created_at": HDD_VOL_CREATED_AT,
"target": None
"target": None,
"ssh_key_ids": []
}

PAYLOAD = [NVME_VOLUME, HDD_VOLUME]
Expand Down Expand Up @@ -94,6 +98,7 @@ def test_get_instances(self, volumes_service, endpoint):
assert volume_nvme.is_os_volume
assert volume_nvme.created_at == NVME_VOL_CREATED_AT
assert volume_nvme.target == TARGET_VDA
assert volume_nvme.ssh_key_ids == SSH_KEY_ID

assert volume_hdd.id == HDD_VOL_ID
assert volume_hdd.status == HDD_VOL_STATUS
Expand All @@ -105,6 +110,7 @@ def test_get_instances(self, volumes_service, endpoint):
assert volume_hdd.is_os_volume is False
assert volume_hdd.created_at == HDD_VOL_CREATED_AT
assert volume_hdd.target is None
assert volume_hdd.ssh_key_ids == []

def test_get_volumes_by_status_successful(self, volumes_service, endpoint):
# arrange - add response mock
Expand Down