Get Started with the Abiquo API part 2

Introduction

This tutorial continues on from Get started with the Abiquo API.

In this tutorial, you will use the Abiquo API to do the following.

  1. Configure a VM by adding an IP address (NIC)

  2. Delete a VM

There is also a link to another section that describes how to power cycle a VM.




Requirements

This tutorial requires two VMs that are:

  1. Not deployed

  2. Have no IP addresses (NICs)

You will also need 2 free IP addresses in private networks in the virtual datacenter with the VMs.

To use VMs from the previous tutorial, in the UI undeploy it and delete its IP address. 

 

Working with Deployed VMs

A deployed VM must always have a NIC. If you want to add another NIC, you may need to create another network to avoid trying to add two IP addresses in the same network.

To change the configuration of a deployed VM, if the VM doesn't have network hot-reconfigure, then you will need to shut it down first.






Get and modify an entity using the API

In general, to modify an entity using the Abiquo API, do these steps.

  1. Use a GET request to retrieve the entity's data object

  2. Modify the data object

  3. Use a PUT request to update the entity

Within a data object, links can define an entity's configuration.

When you edit the VM data object, be careful not to accidentally delete links because you could change the configuration, such as by deleting a disk from your VM!






Configure a VM to add a NIC using the UI

First perform the action using the Abiquo UI, to view the API request that the UI makes.

To the first VM, which is not deployed, add a network interface card (NIC) with an IP address in a private network.

  1. Open the browser console to the Network tab and record actions

  2. Edit the VM and go to Network → Private

  3. Select the network and drag an IP address into the NICs panel, and Save the VM

  4. Find the PUT request and copy it, or get the nic link and VM data object

The PUT request to add a NIC to a VM in the browser console



Obtain the link to the NIC

In the VM object, find the link to the NIC (with a rel attribute with a value of nic1 near the end of the VM links section.

{ "title":"privateip", "rel":"nic1", "type":"application/vnd.abiquo.privateip+json", "href":"https://abiquo.lab.example.com:443/api/cloud/virtualdatacenters/12/privatenetworks/9/ips/11" },






Get an IP address to add to the VM

To find a free IP address:

  1. From the link to the NIC in the previous step, get only the networks IPs link

    https://abiquo.lab.example.com:443/api/cloud/virtualdatacenters/12/privatenetworks/9/ips

     

  2. Use a GET request to obtain the first 3 IP addresses in this network, and add the query parameter limit with a value of 3  

    curl --verbose 'https://abiquo.lab.example.com:443/api/cloud/virtualdatacenters/12/privatenetworks/9/ips?limit=3' \ -H 'Accept: application/vnd.abiquo.privateips+json; version=6.1' \ -u user:password -k | jq .

     

     To retrieve IPs that are available for use only, use the query parameter free with a value of true

  3. The response has 3 IP addresses: the gateway and the IP addresses we created specifically for this exercise.



In the above example, the NIC for the IP address of 192.168.0.2 is the one that we just added to the VM called vmapihowto and it has a link to the VM.

The available IP address of 192.168.0.3 is the one we will use, and it has an ID of 12.

Create a NIC link

From the IP address object in the above step, get the link with a rel attribute that has a value of self.

To prepare it for the VM, change the rel attribute from a value of self to nicX, where X represents the number of the new NIC in the VM. So if there are no NICs, add nic1 as shown here.


Add the NIC link to the VM object

Get the VM object that you created using the API in the first part of this tutorial.

To get the VM entity again, you can send a GET request to the cloud/virtualmachines link.
In this case, we are using a vmname query parameter to filter on the number 2 in the VM name to only retrieve the VM from the first part of the tutorial.

This request returns a collection of VMs, so you will need to select the individual VM from the collection.

In the VM object, go the end of the links section and add the NIC link from the previous step.

 If this is the last link, remember to add a comma before it, and remove any comma after it.

This is an example of a modified VM object, with the nic1 link at the end of a shortened links section. We shortened the links section to make the example more readable 




Update the VM to add the NIC

Create a PUT request to update the whole VM, including all the links.

To easily manage the large data object, you can save it to a JSON file, and use the @ notation to reference the file in the PUT request.

In our example, we called the file  vmapihowto2.json.

Send the PUT request.

If the platform updates the VM successfully, the response status and message will be 204  No content for an undeployed VM.

An example of the request is given here.


Check your VM in the UI

In the UI, when you select the VM, and open the VM details panel to the NICs tab, the NIC should display.

The NIC will display on the VM details panel on the NICs tab

 

Change the power states of a VM

To prepare a VM to change power states, deploy a VM using the UI or the API.

To deploy the VM you were using in the previous step, send a POST request to the deploy link.

Then see Administer and troubleshoot VMs in the "Manage VM power states using the API" section

 


 

Delete a VM using the API

To delete a VM (or another entity), simply perform a DELETE request to the API link.

Note that there may be restrictions on what you can delete. For example, you cannot delete a virtual datacenter that contains virtual appliances.

If we delete the VM as shown here, it will be removed from the platform (and if it exists on the hypervisor, it will be destroyed). And the IP address will return to the virtual datacenter.




Conclusion

Congratulations, you have now completed the Abiquo API tutorials. You can now continue to experiment using the API together with the UI. And you can find more examples in the API Howtos section. 

And of course don't forget to check out the Java and Python libraries. Enjoy!



Related links

Copyright © 2006-2024, Abiquo Holdings SL. All rights reserved