|
| 1 | +import iland |
| 2 | +import time |
| 3 | + |
| 4 | +CLIENT_ID = '' |
| 5 | +CLIENT_SECRET = '' |
| 6 | +USERNAME = '' |
| 7 | +PASSWORD = '' |
| 8 | + |
| 9 | +api = iland.Api(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, username=USERNAME, password=PASSWORD) |
| 10 | + |
| 11 | +def main(): |
| 12 | + vdc_uuid = print_entity_inventory('IAAS_VDC') |
| 13 | + vapp_uuid = create_scratch_vapp(vdc_uuid) |
| 14 | + delete_vapp(vapp_uuid) |
| 15 | + |
| 16 | +''' |
| 17 | +This function gets a user's inventory with the GET endpoint /users/{username}/inventory and filters to get the specified |
| 18 | +entity type. In this case I am getting all the vDCs the user has access to so I pass IAAS_VDC. |
| 19 | +Besides printing all the vDCS, this function lazily grabs the first vDC uuid for the scratch vApp that we create below. |
| 20 | +''' |
| 21 | +def print_entity_inventory(entity_type): |
| 22 | + inventory = api.get('/users/%s/inventory' % USERNAME)['inventory'] |
| 23 | + vdc_uuid = '' |
| 24 | + for i in inventory: |
| 25 | + vdcs = i['entities'][entity_type] |
| 26 | + for vdc in vdcs: |
| 27 | + if vdc_uuid == '': |
| 28 | + vdc_uuid = vdc['uuid'] |
| 29 | + print(vdc['name'] + ' ' + vdc['uuid']) |
| 30 | + return vdc_uuid |
| 31 | + |
| 32 | +''' |
| 33 | +This function creates a vApp from scratch using the endpoint POST /vdcs/{vdcUuid}/actions/build-vapp. |
| 34 | +All of the parameters passed below are required for creating a vApp from scratch. |
| 35 | +After creating the vApp I get all the vApps back from the vDC to get the vApp uuid by it's unique name. |
| 36 | +''' |
| 37 | +def create_scratch_vapp(vdc_uuid): |
| 38 | + scratch_vapp = {'name':'Example vApp Name', 'description':'example description', |
| 39 | + 'vms': [{'name': 'Example VM name','computer_name':'Computer-Name','ram': 2000, 'number_of_cpus': 4, |
| 40 | + 'cpu_cores_per_socket': 2, 'hardware_version': 11, 'operating_system_version': 'ubuntu64Guest'}]} |
| 41 | + build_vapp_task = api.post('/vdcs/%s/actions/build-vapp' % vdc_uuid, scratch_vapp) |
| 42 | + wait_for_synced_task(build_vapp_task['uuid']) |
| 43 | + vapps = api.get('/vdcs/%s/vapps' % vdc_uuid) |
| 44 | + vapp_uuid = '' |
| 45 | + for vapp in vapps['data']: |
| 46 | + if vapp['name'] == 'Example vApp Name': |
| 47 | + vapp_uuid = vapp['uuid'] |
| 48 | + break |
| 49 | + return vapp_uuid |
| 50 | + |
| 51 | +''' |
| 52 | +This function deletes a vApp with the DELETE endpoint /vapps/{vappUuid} |
| 53 | +''' |
| 54 | +def delete_vapp(vapp_uuid): |
| 55 | + api.delete('/vapps/%s' % vapp_uuid) |
| 56 | + |
| 57 | +''' |
| 58 | +This function gets a task with the GET endpoint /tasks/{taskUuid} |
| 59 | +''' |
| 60 | +def get_task(task_uuid): |
| 61 | + return api.get('/tasks/%s' % task_uuid) |
| 62 | + |
| 63 | +''' |
| 64 | +This function waits for a task to sync. It waits 2 seconds between getting the task |
| 65 | +and checking if it is synced. We need to wait for tasks to sync when doing multiple actions |
| 66 | +on a resource so we don't try doing something that isn't possible, ie. reconfiguring properties of a VM |
| 67 | +when it is still on. |
| 68 | +''' |
| 69 | +def wait_for_synced_task(task_uuid): |
| 70 | + synced = False |
| 71 | + while not synced: |
| 72 | + # Wait two seconds before checking if task is synced |
| 73 | + time.sleep(2) |
| 74 | + # Get task |
| 75 | + task = get_task(task_uuid) |
| 76 | + synced = task['synced'] |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + main() |
0 commit comments