diff --git a/.gitignore b/.gitignore index 6168b23..60489fa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode/ +.idea/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/datacrunch/instances/instances.py b/datacrunch/instances/instances.py index e2d20b9..2b089cd 100644 --- a/datacrunch/instances/instances.py +++ b/datacrunch/instances/instances.py @@ -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: diff --git a/datacrunch/volumes/volumes.py b/datacrunch/volumes/volumes.py index f3b01da..0ff768c 100644 --- a/datacrunch/volumes/volumes.py +++ b/datacrunch/volumes/volumes.py @@ -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 @@ -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 @@ -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: @@ -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""" @@ -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 @@ -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 diff --git a/tests/unit_tests/instances/test_instances.py b/tests/unit_tests/instances/test_instances.py index 8247fc4..367b20d 100644 --- a/tests/unit_tests/instances/test_instances.py +++ b/tests/unit_tests/instances/test_instances.py @@ -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( diff --git a/tests/unit_tests/volumes/test_volumes.py b/tests/unit_tests/volumes/test_volumes.py index a81405b..0e0d5af 100644 --- a/tests/unit_tests/volumes/test_volumes.py +++ b/tests/unit_tests/volumes/test_volumes.py @@ -14,6 +14,7 @@ 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 @@ -21,6 +22,7 @@ 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" @@ -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 = { @@ -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] @@ -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 @@ -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