Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JungleCatSW committed Jan 3, 2024
1 parent e620a46 commit 652475d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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``.



Expand Down
20 changes: 20 additions & 0 deletions examples/create-vm.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 0 additions & 29 deletions examples/example.py

This file was deleted.

10 changes: 10 additions & 0 deletions examples/get-vm.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions examples/images.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions examples/list-vms.py
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions examples/machine-types.py
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 8 additions & 0 deletions examples/terminate-vm.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 652475d

Please sign in to comment.