Table of Contents |
---|
...
This tutorial continues on from Get Started with the Abiquo API!
Modify an entity through the API
Because the VM entity is a special case, we are going to look at two types of modifications: configuring a VM and changing the state of a VM.
...
Code Block |
---|
https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/262/privatenetworks/345/ips |
You could just open this link in your browser where you are using the UI, or perform a curl request from the command line, for example.
Code Block |
---|
curl --verbose 'https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/262/privatenetworks/345/ips' \ -H "'Accept: application/vnd.abiquo.privateips+json; version=4.4"6' \ -u user:passwordadmin:xabiquo -k | jq . |
In our response body, there are 3 IP addresses: the gateway and two IP addresses we created specifically for this exercise.
Expand | ||
---|---|---|
|
In the above example, the IP that is being used on a VM has a link to that the VM.
Hint: to only retrieve IPs that are available for use, you can use the request parameter "free=true", as follows.
...
The available IP that we will use has the identifier "21717".
And the link we need from this IP object is the link with a "rel" value of "self".
Code Block |
---|
{ "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/262/privatenetworks/345/ips/21717", "type": "application/vnd.abiquo.privateip+json", "rel": "self", "title": "privateip" }, |
To add it to the VM, we just need to change "self" to "nicX", where "X" represents the number of the new NIC in the VM. So if there are no NICs on the machine, we will add "nic0" as shown here.
Code Block |
---|
{ "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/262/privatenetworks/345/ips/21717", "type": "application/vnd.abiquo.privateip+json", "rel": "nic0", "title": "privateip" }, |
Remember that when you add this NIC to the end of the links section, you must add a comma first, after the previous item. And as this will be the last link, remember to remove the comma after it.
So here is an example of the request body that would be used to update our second VM, with the nic0 link at the end of the links section.
Expand | ||
---|---|---|
|
Send a PUT request to update the whole VM, including all the links. As the VM object is quite large, instead of adding it to the -d option between single quotation marks, you can save it to a file, for example, "VMnic.json", and then use the @ notation to reference the file in the cURL.
Code Block |
---|
curl --verbose -X PUT "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/26/virtualappliances/37/virtualmachines/199" \
-H "Content-Type: application/vnd.abiquo.virtualmachine+json; version=4.4" \
-H "Accept: application/vnd.abiquo.acceptedrequest+json; version=4.4" \
-u user:password \
-d @VMnic.json |
The full text of the response object is shown in the expanding section below.
If the request is successful, the response status and message will be "204 No content".
In the UI, when you select the VM, and open the control panel in the Network tab, the NIC should display.
This expanding section contains the full request with the VM object embedded in it.
Expand | ||
---|---|---|
|
Send a PUT request to update the whole VM, including all the links. As the VM object is quite large, instead of adding it to the -d option between single quotation marks, you can save it to a file, for example, "VMnic.json", and then use the @ notation to reference the file in the cURL.
Code Block |
---|
curl --verbose -X PUT 'https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2/virtualappliances/2/virtualmachines/8' \
-H 'Content-Type: application/vnd.abiquo.virtualmachine+json; version=4.6' \
-H 'Accept: application/vnd.abiquo.acceptedrequest+json; version=4.6' \
-u admin:xabiquo \
-d @VMnic.json -k |
The full text of the response object is shown in the expanding section below.
If the request is successful, the response status and message will be "204 No content".
In the UI, when you select the VM, and open the control panel in the Network tab, the NIC should display.
This expanding section contains the full request with the VM object embedded in it.
Expand | ||
---|---|---|
|
Power actions on a VM
Before you begin this section, deploy a VM through the user interface.
Include Page | ||||
---|---|---|---|---|
|
Delete an entity through the API
To delete an entity, simply perform a DELETE request to the API link. But remember 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 using the following query, it will be removed from the platform (and if it exists on the hypervisor, it will be destroyed). The NIC we added will be detached and released back into the virtual datacenter.
Code Block |
---|
curl -X DELETE https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/262/virtualappliances/372/virtualmachines/1988 \ -H 'Accept: text/json,application/json;' \ -u user:password |
...