Skip to content

Commit bb5dac0

Browse files
nksprzakanguenot
authored andcommitted
Add Python SDK examples (#21)
1 parent 3262c1b commit bb5dac0

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Creating a scratch vApp example
2+
In this example app, we use the Python SDK to perform various operations:
3+
* Login.
4+
* Get and print all the vDCs for the user.
5+
* Create a vApp from scratch.
6+
* Delete created vApp.
7+
8+
[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.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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()

examples/power-off-vm/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Powering on and off a VM and deleting it.
2+
In this example app, we use the Python SDK to perform various operations:
3+
* Login.
4+
* Get and print all the vApps for the user.
5+
* Power on and off a VM.
6+
* Delete the VM.
7+
8+
[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.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
vapp_uuid = print_and_get_vapp()
13+
vm_uuid = perform_vm_operations(vapp_uuid)
14+
delete_vm(vm_uuid)
15+
16+
'''
17+
This function prints all the vApps a user has in their inventory.
18+
It uses the GET endpoint /users/{username}/inventory and filters to get only vApps.
19+
It then gets the vApp uuid of the vApp of the name provided and returns the uuid.
20+
'''
21+
def print_and_get_vapp():
22+
vapp_name = 'Example vApp Name'
23+
vapp_uuid = ''
24+
inventory = api.get('/users/%s/inventory' % USERNAME)['inventory']
25+
for i in inventory:
26+
vapps = i['entities']['IAAS_VAPP']
27+
for vapp in vapps:
28+
if vapp['name'] == vapp_name:
29+
vapp_uuid = vapp['uuid']
30+
print(vapp['name'] + ' ' + vapp['uuid'])
31+
return vapp_uuid
32+
33+
'''
34+
This function performs power operations on a VM of the vApp we got in the previous function.
35+
Using the POST endpoints /vms/{vmUuid}/actions/poweron and /vms/{vmUuid}/actions/poweroff we
36+
are able to power on and off the VM. These endpoints produce a task response that we need to
37+
check until they are synced.
38+
'''
39+
def perform_vm_operations(vapp_uuid):
40+
vapp_vms = api.get('/vapps/%s/vms' % vapp_uuid)
41+
vm_uuid = vapp_vms['data'][0]['uuid']
42+
power_on_task = api.post('/vms/%s/actions/poweron' % vm_uuid)
43+
wait_for_synced_task(power_on_task['uuid'])
44+
power_off_task = api.post('/vms/%s/actions/poweroff' % vm_uuid)
45+
wait_for_synced_task(power_off_task['uuid'])
46+
return vm_uuid
47+
48+
'''
49+
This function deletes a VM with the DELETE endpoint /vms/{vmUuid}
50+
'''
51+
def delete_vm(vm_uuid):
52+
api.delete('/vms/%s' % vm_uuid)
53+
54+
'''
55+
This function gets a task with the GET endpoint /tasks/{taskUuid}
56+
'''
57+
def get_task(task_uuid):
58+
return api.get('/tasks/%s' % task_uuid)
59+
60+
'''
61+
This function waits for a task to sync. It waits 2 seconds between getting the task
62+
and checking if it is synced. We need to wait for tasks to sync when doing multiple actions
63+
on a resource so we don't try doing something that isn't possible, ie. reconfiguring properties of a VM
64+
when it is still on.
65+
'''
66+
def wait_for_synced_task(task_uuid):
67+
synced = False
68+
while not synced:
69+
# Wait two seconds before checking if task is synced
70+
time.sleep(2)
71+
# Get task
72+
task = get_task(task_uuid)
73+
synced = task['synced']
74+
75+
if __name__ == '__main__':
76+
main()

0 commit comments

Comments
 (0)