-
Notifications
You must be signed in to change notification settings - Fork 3
VM Power Operations and Deletion
In this example app we will show show to power on and off a VM as well as how to delete the VM.
To get started either git clone the project and cd into the power-off-vm example or follow the steps from the Home page of the Wiki to get your project setup.
Powering on and off a VM is as easy as a few lines of code.
To do this we need the VM's uuid we want to power on/off.
How do you find the uuid of a VM? Well if you know the uuid of the vApp we can easily find the UUIDs of the VMs that are within that vApp.
But how do you find a vApp's uuid? One way is to look through the your user's inventory and check the name of the vApps until you find the one you are looking for. This works because names of vApps are unique, you will find that out yourself if you try creating a vApp with the same name as an existing vApp.
The following code is you look through a user's inventory looking for a particular vApp's uuid based on it's name.
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
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'])
Deleting a VM is easy. You must have the VM's uuid and the correct permissions to delete it.
We can delete a VM with the following line of code:
def delete_vm(vm_uuid):
api.delete('/vms/%s' % vm_uuid)