From 652475df03cb0e43db17c9da7c54ad5144143919 Mon Sep 17 00:00:00 2001 From: jungle Date: Wed, 3 Jan 2024 13:30:05 +0000 Subject: [PATCH] Update examples --- README.md | 2 +- examples/create-vm.py | 20 +++++++++++++++++ examples/example.py | 29 ------------------------- examples/get-vm.py | 10 +++++++++ examples/images.py | 10 +++++++++ examples/list-vms.py | 10 +++++++++ examples/machine-types.py | 45 +++++++++++++++++++++++++++++++++++++++ examples/terminate-vm.py | 8 +++++++ 8 files changed, 104 insertions(+), 30 deletions(-) create mode 100644 examples/create-vm.py delete mode 100644 examples/example.py create mode 100644 examples/get-vm.py create mode 100644 examples/images.py create mode 100644 examples/list-vms.py create mode 100644 examples/machine-types.py create mode 100644 examples/terminate-vm.py diff --git a/README.md b/README.md index 3b3a0c1..ddf79c2 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ api = cudo_api.virtual_machines() vms = api.list_vms(cudo_api.project_id()) ``` -A more complete example of various api calls can be seen in the example in ``examples/example.py`` demonstrates how to use it. +More examples of various api calls can be found in ``examples``. diff --git a/examples/create-vm.py b/examples/create-vm.py new file mode 100644 index 0000000..f7bc006 --- /dev/null +++ b/examples/create-vm.py @@ -0,0 +1,20 @@ +from cudo_compute import cudo_api, Disk, CreateVMBody +from cudo_compute.rest import ApiException +import json + +# Hint: use examples/machine-types.py to find machine_type/data_center_id and gpu_model +# use examples/images.py to get list of OS images for boot_disk_image_id + +try: + disk = Disk(storage_class="STORAGE_CLASS_NETWORK", size_gib=100, + id="my-disk-id") + request = CreateVMBody(vm_id="my-vm-id", machine_type="epyc-rome-rtx-a4000", + data_center_id="no-luster-1", boot_disk_image_id='ubuntu-nvidia-docker', + memory_gib=16, vcpus=4, gpus=1, gpu_model="A4000", boot_disk=disk, + metadata={"group": "a"}) + api = cudo_api.virtual_machines() + vm = api.create_vm(cudo_api.project_id(), request) + + print(json.dumps(vm.to_dict(), indent=2)) +except ApiException as e: + print(e) diff --git a/examples/example.py b/examples/example.py deleted file mode 100644 index d5c58fd..0000000 --- a/examples/example.py +++ /dev/null @@ -1,29 +0,0 @@ -from cudo_compute import cudo_api - - -def machine_types(gpu_model, mem_gib, vcpu_count, gpu_count): - try: - api = cudo_api.virtual_machines() - types = api.list_vm_machine_types(mem_gib, vcpu_count, gpu=gpu_count, gpu_model=gpu_model) - types_dict = types.to_dict() - return types_dict - except Exception as e: - raise e - - -print(machine_types("", 4, 4, 0)) - -print(cudo_api.project_id()) - - -def list_instances(): - try: - api = cudo_api.virtual_machines() - vms = api.list_vms(cudo_api.project_id()) - vms_dict = vms.to_dict() - return vms_dict - except Exception as e: - raise e - - -print(list_instances()) diff --git a/examples/get-vm.py b/examples/get-vm.py new file mode 100644 index 0000000..3cf991c --- /dev/null +++ b/examples/get-vm.py @@ -0,0 +1,10 @@ +from cudo_compute import cudo_api +from cudo_compute.rest import ApiException +import json + +try: + api = cudo_api.virtual_machines() + vm = api.get_vm(cudo_api.project_id(), 'my-vm-id') + print(vm.to_dict()) +except ApiException as e: + print(e) diff --git a/examples/images.py b/examples/images.py new file mode 100644 index 0000000..a0a1e61 --- /dev/null +++ b/examples/images.py @@ -0,0 +1,10 @@ +from cudo_compute import cudo_api +from cudo_compute.rest import ApiException +import json + +try: + api = cudo_api.virtual_machines() + images = api.list_public_vm_images() + print(json.dumps(images.to_dict(), indent=2)) +except ApiException as e: + print(e) diff --git a/examples/list-vms.py b/examples/list-vms.py new file mode 100644 index 0000000..5926702 --- /dev/null +++ b/examples/list-vms.py @@ -0,0 +1,10 @@ +from cudo_compute import cudo_api +from cudo_compute.rest import ApiException +import json + +try: + api = cudo_api.virtual_machines() + vms = api.list_vms(cudo_api.project_id()) + print(json.dumps(vms.to_dict(), indent=2)) +except ApiException as e: + print(e) diff --git a/examples/machine-types.py b/examples/machine-types.py new file mode 100644 index 0000000..2e4236a --- /dev/null +++ b/examples/machine-types.py @@ -0,0 +1,45 @@ +from cudo_compute import cudo_api +from cudo_compute.rest import ApiException +import json + + +def machine_types(gpu_model, mem_gib, vcpu_count, gpu_count): + try: + api = cudo_api.virtual_machines() + types = api.list_vm_machine_types(mem_gib, vcpu_count, gpu=gpu_count, gpu_model=gpu_model) + types_dict = types.to_dict() + return types_dict['host_configs'] + except ApiException as e: + raise e + + +def gpu_types(gpu_count): + try: + api = cudo_api.virtual_machines() + types = api.list_vm_machine_types(1, 1, gpu=gpu_count, ) + gpu_list = [] + for gpu in types.to_dict()['gpu_models']: + gpu_list.append(gpu['name']) + return gpu_list + except ApiException as e: + raise e + + +print('Gpu types') +print(json.dumps(gpu_types(1), indent=2)) + +print('Machine types with GPU') +mts = machine_types("", 1, 1, 1) +mt_list = [] +for mt in mts: + mt_list.append( + {'machine_type': mt['machine_type'], 'gpu_model': mt['gpu_model'], 'data_center_id': mt['data_center_id']}) +print(json.dumps(mt_list, indent=2)) + +print('Machine types without GPU') +mts = machine_types("", 1, 1, 0) +mt_list = [] +for mt in mts: + mt_list.append( + {'machine_type': mt['machine_type'], 'data_center_id': mt['data_center_id']}) +print(json.dumps(mt_list, indent=2)) diff --git a/examples/terminate-vm.py b/examples/terminate-vm.py new file mode 100644 index 0000000..81e6232 --- /dev/null +++ b/examples/terminate-vm.py @@ -0,0 +1,8 @@ +from cudo_compute import cudo_api +from cudo_compute.rest import ApiException + +try: + api = cudo_api.virtual_machines() + api.terminate_vm(cudo_api.project_id(), 'my-vm-id') +except ApiException as e: + print(e)