From 344574d98bf6ddd656c3fdc7e603eb884df6be3e Mon Sep 17 00:00:00 2001 From: Nick Kasprzak Date: Fri, 5 Apr 2019 21:56:01 +0000 Subject: [PATCH] Add Python SDK examples --- examples/create-scratch-vapp/README.md | 8 ++ .../create_scratch_vapp.py | 79 +++++++++++++++++++ examples/power-off-vm/README.md | 8 ++ examples/power-off-vm/power_off_vm.py | 76 ++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 examples/create-scratch-vapp/README.md create mode 100644 examples/create-scratch-vapp/create_scratch_vapp.py create mode 100644 examples/power-off-vm/README.md create mode 100644 examples/power-off-vm/power_off_vm.py diff --git a/examples/create-scratch-vapp/README.md b/examples/create-scratch-vapp/README.md new file mode 100644 index 0000000..29ebc8d --- /dev/null +++ b/examples/create-scratch-vapp/README.md @@ -0,0 +1,8 @@ +# Creating a scratch vApp example +In this example app, we use the Python SDK to perform various operations: +* Login. +* Get and print all the vDCs for the user. +* Create a vApp from scratch. +* Delete created vApp. + +[Here](https://github.com/ilanddev/python-sdk/wiki/Create-Scratch-Vapp-Example) is a guide to help you understand the code and how it is working. diff --git a/examples/create-scratch-vapp/create_scratch_vapp.py b/examples/create-scratch-vapp/create_scratch_vapp.py new file mode 100644 index 0000000..9e9240e --- /dev/null +++ b/examples/create-scratch-vapp/create_scratch_vapp.py @@ -0,0 +1,79 @@ +import iland +import time + +CLIENT_ID = '' +CLIENT_SECRET = '' +USERNAME = '' +PASSWORD = '' + +api = iland.Api(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, username=USERNAME, password=PASSWORD) + +def main(): + vdc_uuid = print_entity_inventory('IAAS_VDC') + vapp_uuid = create_scratch_vapp(vdc_uuid) + delete_vapp(vapp_uuid) + +''' +This function gets a user's inventory with the GET endpoint /users/{username}/inventory and filters to get the specified +entity type. In this case I am getting all the vDCs the user has access to so I pass IAAS_VDC. +Besides printing all the vDCS, this function lazily grabs the first vDC uuid for the scratch vApp that we create below. +''' +def print_entity_inventory(entity_type): + inventory = api.get('/users/%s/inventory' % USERNAME)['inventory'] + vdc_uuid = '' + for i in inventory: + vdcs = i['entities'][entity_type] + for vdc in vdcs: + if vdc_uuid == '': + vdc_uuid = vdc['uuid'] + print(vdc['name'] + ' ' + vdc['uuid']) + return vdc_uuid + +''' +This function creates a vApp from scratch using the endpoint POST /vdcs/{vdcUuid}/actions/build-vapp. +All of the parameters passed below are required for creating a vApp from scratch. +After creating the vApp I get all the vApps back from the vDC to get the vApp uuid by it's unique name. +''' +def create_scratch_vapp(vdc_uuid): + scratch_vapp = {'name':'Example vApp Name', 'description':'example description', + 'vms': [{'name': 'Example VM name','computer_name':'Computer-Name','ram': 2000, 'number_of_cpus': 4, + 'cpu_cores_per_socket': 2, 'hardware_version': 11, 'operating_system_version': 'ubuntu64Guest'}]} + build_vapp_task = api.post('/vdcs/%s/actions/build-vapp' % vdc_uuid, scratch_vapp) + wait_for_synced_task(build_vapp_task['uuid']) + vapps = api.get('/vdcs/%s/vapps' % vdc_uuid) + vapp_uuid = '' + for vapp in vapps['data']: + if vapp['name'] == 'Example vApp Name': + vapp_uuid = vapp['uuid'] + break + return vapp_uuid + +''' +This function deletes a vApp with the DELETE endpoint /vapps/{vappUuid} +''' +def delete_vapp(vapp_uuid): + api.delete('/vapps/%s' % vapp_uuid) + +''' +This function gets a task with the GET endpoint /tasks/{taskUuid} +''' +def get_task(task_uuid): + return api.get('/tasks/%s' % task_uuid) + +''' +This function waits for a task to sync. It waits 2 seconds between getting the task +and checking if it is synced. We need to wait for tasks to sync when doing multiple actions +on a resource so we don't try doing something that isn't possible, ie. reconfiguring properties of a VM +when it is still on. +''' +def wait_for_synced_task(task_uuid): + synced = False + while not synced: + # Wait two seconds before checking if task is synced + time.sleep(2) + # Get task + task = get_task(task_uuid) + synced = task['synced'] + +if __name__ == '__main__': + main() diff --git a/examples/power-off-vm/README.md b/examples/power-off-vm/README.md new file mode 100644 index 0000000..cc5d28d --- /dev/null +++ b/examples/power-off-vm/README.md @@ -0,0 +1,8 @@ +# Powering on and off a VM and deleting it. +In this example app, we use the Python SDK to perform various operations: +* Login. +* Get and print all the vApps for the user. +* Power on and off a VM. +* Delete the VM. + +[Here](https://github.com/ilanddev/python-sdk/wiki/VM-Power-Operations-and-Deletion) is a guide to help you understand the code and how it is working. diff --git a/examples/power-off-vm/power_off_vm.py b/examples/power-off-vm/power_off_vm.py new file mode 100644 index 0000000..561b381 --- /dev/null +++ b/examples/power-off-vm/power_off_vm.py @@ -0,0 +1,76 @@ +import iland +import time + +CLIENT_ID = '' +CLIENT_SECRET = '' +USERNAME = '' +PASSWORD = '' + +api = iland.Api(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, username=USERNAME, password=PASSWORD) + +def main(): + vapp_uuid = print_and_get_vapp() + vm_uuid = perform_vm_operations(vapp_uuid) + delete_vm(vm_uuid) + +''' +This function prints all the vApps a user has in their inventory. +It uses the GET endpoint /users/{username}/inventory and filters to get only vApps. +It then gets the vApp uuid of the vApp of the name provided and returns the uuid. +''' +def print_and_get_vapp(): + vapp_name = 'Example vApp Name' + vapp_uuid = '' + inventory = api.get('/users/%s/inventory' % USERNAME)['inventory'] + for i in inventory: + vapps = i['entities']['IAAS_VAPP'] + for vapp in vapps: + if vapp['name'] == vapp_name: + vapp_uuid = vapp['uuid'] + print(vapp['name'] + ' ' + vapp['uuid']) + return vapp_uuid + +''' +This function performs power operations on a VM of the vApp we got in the previous function. +Using the POST endpoints /vms/{vmUuid}/actions/poweron and /vms/{vmUuid}/actions/poweroff we +are able to power on and off the VM. These endpoints produce a task response that we need to +check until they are synced. +''' +def perform_vm_operations(vapp_uuid): + vapp_vms = api.get('/vapps/%s/vms' % vapp_uuid) + vm_uuid = vapp_vms['data'][0]['uuid'] + power_on_task = api.post('/vms/%s/actions/poweron' % vm_uuid) + wait_for_synced_task(power_on_task['uuid']) + power_off_task = api.post('/vms/%s/actions/poweroff' % vm_uuid) + wait_for_synced_task(power_off_task['uuid']) + return vm_uuid + +''' +This function deletes a VM with the DELETE endpoint /vms/{vmUuid} +''' +def delete_vm(vm_uuid): + api.delete('/vms/%s' % vm_uuid) + +''' +This function gets a task with the GET endpoint /tasks/{taskUuid} +''' +def get_task(task_uuid): + return api.get('/tasks/%s' % task_uuid) + +''' +This function waits for a task to sync. It waits 2 seconds between getting the task +and checking if it is synced. We need to wait for tasks to sync when doing multiple actions +on a resource so we don't try doing something that isn't possible, ie. reconfiguring properties of a VM +when it is still on. +''' +def wait_for_synced_task(task_uuid): + synced = False + while not synced: + # Wait two seconds before checking if task is synced + time.sleep(2) + # Get task + task = get_task(task_uuid) + synced = task['synced'] + +if __name__ == '__main__': + main()