Get Started with the Abiquo API part 2
- 1 Introduction
- 2 Requirements
- 3 Get and modify an entity using the API
- 4 Configure a VM to add a NIC using the UI
- 5 Obtain the link to the NIC
- 6 Get an IP address to add to the VM
- 7 Create a NIC link
- 8 Add the NIC link to the VM object
- 9 Update the VM to add the NIC
- 10 Check your VM in the UI
- 11 Change the power states of a VM
- 12 Delete a VM using the API
- 13 Conclusion
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.
Configure a VM by adding an IP address (NIC)
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:
Not deployed
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.
Use a
GET
request to retrieve the entity's data objectModify the data object
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.
Open the browser console to the Network tab and record actions
Edit the VM and go to Network → Private
Select the network and drag an IP address into the NICs panel, and Save the VM
Find the
PUT
request and copy it, or get thenic
link and VM data object
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:
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
Â
Use a GET request to obtain the first 3 IP addresses in this network, and add the query parameter
limit
with a value of3
 Â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 oftrue
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.
Â
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