Skip to content

VM Power Operations and Deletion

Nick Kasprzak edited this page Sep 28, 2020 · 8 revisions

Introduction

In this example app we will 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 example/power-off-vm example or follow the steps from the Home page of the Wiki to get your project setup.

Finding a VM's uuid

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 your user's inventory and check the name of the vApps until you find the one you are looking for. This works because the 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 its name.

def print_and_get_vapp():
    vapp_name = 'Example vApp Name'
    inventory = api.get('/users/%s/inventory' % USERNAME)['inventory']
    for item in inventory:
        vapps = item['entities']['IAAS_VAPP']
        vms = item['entities']['IAAS_VM']
        for vapp in vapps:
            if vapp['name'] == vapp_name:
                vapp_uuid = vapp['uuid']
            print(vapp['name'] + ' ' + vapp['uuid'])
    return vapp_uuid

As you can see we are using the GET endpoint /users/{username}/inventory to get the user's inventory, then filter for vApps by getting the IAAS_VAPP section of the returned inventory.

We then iterate through each vApp checking to see if it's the name we are looking for.

Note We are also printing the name and UUID of every vApp. If you don't want to do this then merely take out that line and add a break after you set the vapp_uuid.

Once we have the vApp's uuid we can get its VMs by using the GET endpoint /vapps/{vappUuid}/vms.

vapp_vms = api.get('/vapps/%s/vms' % vapp_uuid)
vm_uuid = vapp_vms['data'][0]['uuid']

Looking at the documentation for the above endpoint here we can see that the response object has an array called data which is where the VMs are.

In the next line of code, I lazily get the first VM uuid that is returned.

VM Power operations

Now that we have the VM's uuid we wish to power on and off we can do that using the following code:

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'])

Here we are using the POST endpoints /vms/{vmUuid}/actions/poweron and /vms/{vmUuid}/actions/poweroff.

Both of these endpoints return tasks that we have to wait to be synced. If these tasks aren't synced then our actions might return errors from the API.

To learn about tasks and how to wait until they are synced check out the section on tasks here

Deleting a VM

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)