Skip to content

Create Scratch Vapp Example

Nick Kasprzak edited this page Apr 10, 2019 · 8 revisions

Introduction

This is a guide to take you through building you a vApp from scratch using iland's Python SDK.

Follow the guide here to get started using iland's Python SDK and setting up your project.

To follow along with the examples I will be talking about use the following command to get iland's Python SDK

git clone https://github.com/ilanddev/python-sdk.git

Then you can cd into the examples/create-scratch-vapp

Getting a user's inventory

To create a vApp we must pass two things, first the UUID of the vDC where we want to create the vApp and secondly the build vApp request object.

How do you determine what are the required parameters for an endpoint?

Looking at the API documentation for /vdcs/{vdcUuid}/actions/build-vapp here we can see the two required fields are the vdcUuid and the body.

We will get to the body part of build vApp request but first we need to get a vDC uuid.

Let's get a user's inventory with the GET endpoint /users/{username}/inventory

inventory = api.get('/users/%s/inventory' % USERNAME)['inventory']

Looking at the documentation for the inventory endpoint here we can see that the endpoint returns an array called inventory. This shows a user every resource they have access to.

Now that we have the user's inventory we can iterate through and find vDCs. Let's do that with the following code:

for item in inventory:
    vdcs = item['entities']['IAAS_VDC']
    vapps = item['entities']['IAAS_VAPP']
    vms = item['entities']['IAAS_VM']
    for vdc in vdcs:
         vdc_uuid = vdc['uuid']
         print(vdc['name'] + ' ' + vdc['uuid'])

In the above code we iterate through each companies' inventory we have access to in the first for loop and then get the vDCs for each company by using the entity type IAAS_VDC to get just the vDC entities.

You can see as well I show how to get the vApps and VMs from the user's inventory.

Next we iterate through each vDC printing its name and uuid. I lazily grab a first vDC uuid for vApp creation.

Creating a Scratch vApp

Now since we have a vDC uuid we can start composing our scratch vApp.

Looking at the documentation we can see the build-vapp endpoint takes a request body that contains such things like name, description and vms.

We are interested in the vms section of the request because that is where we can specify exactly what we want in
VMs that are created within our vApp.

Let's look at an example of how to build a scratch vApp.

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)

Here we are creating a vApp with a Ubuntu VM within it. As you can tell I specify things such as RAM size, number of CPUs and CPUs per socket, etc.

Note All the fields that I have provided in the above example are required when creating a VM from scratch. If you don't provide any of the above fields when making a VM from scratch then you will get an error back from the iland API.

A useful endpoint to see what operating systems you can create is the GET /constants/operating-systems

Getting and waiting for synced tasks

The response you get from build-vapp endpoint is a task which enables you to track the progress of build-vapp request.

To get a task you use the GET endpoint /tasks/{taskUuid} documented here

Here's what that looks like using the SDK:

def get_task(task_uuid):
    return api.get('/tasks/%s' % task_uuid)

Note that there is a field in the task response called synced. This tells us whether or not the task has been synced yet.

Before doing anything with our newly created vApp and VM we must ensure that the build-vapp request has synced. We can do that with the following code:

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

In the above function we continuously check if the task is synced in a while loop waiting 2 seconds between getting the task again.

We must wait for the task to be synced to ensure that our vApp has been created.

Deleting a vApp

If you have an vApp's uuid then deleting a vApp is easy as the following code.

api.delete('/vapps/%s' % vapp_uuid)