Manage virtual machine metadata via API
Abiquo virtual machine metadata
Abiquo stores some information in VMs as metadata and allows users to store custom metadata too. To access metadata, use the link with the "rel" value of "metadata" in the VM.
Writing metadata will overwrite ALL existing metadata, so to obtain a base object to modify, retrieve the existing metadata in the appropriate format. Abiquo recommends JSON format.
Note: Do not use a conversion tool to convert between JSON and XML format because this may produce an incompatible data transfer object.
For the metadata attribute or object, Abiquo has the reserved keys that are described in the following table.Metadata type Reserved Keys Description Chef Bootstrap script Metrics Custom
VM metadata tutorial
This tutorial will show you how to retrieve and update metadata to add a startup script to your VM via API.
To work through this tutorial, you will need:
- A VM that is not deployed (or a VM that is powered off)
- This tutorial uses an example VM that is not deployed
- User with privileges to work with VMs and Manage virtual machine backup configuration (VAPP_MANAGE_BACKUP) because backups previously used metadata
- Optional: a monitoring server and a user with the privileges to Manage virtual machine metrics (USERS_ENABLE_DISABLE_VM_METRICS)
Get the VM and the metadata link
To get started, use a query to retrieve all of your VMs from the cloud and select the desired VM. Here we are filtering the VMs using the "vmlabel" parameter to select the one that has a text string (in this case "metadata") as part of the name, which is an easy way to find a VM that you have located in the UI.
curl -X GET https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualmachines?vmlabel=metadata \ -H "Accept: application/vnd.abiquo.virtualmachines+json; version=4.7" \ -u user:password --verbose | pjson
From the VM data object, the link to the VM metadata is the one with the "rel" value of metadata.
{ "title": "metadata", "rel": "metadata", "type": "application/vnd.abiquo.metadata+json", "href": "https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/1/virtualappliances/1/virtualmachines/3/metadata" },
There is also a metadata attribute in the VM with a null value, but the correct way to access metadata is using the metadata link.
Retrieve existing VM metadata
Use the VM metadata link from the step above to retrieve any existing metadata.
curl -X GET https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/1/virtualappliances/1/virtualmachines/3/metadata \ -H 'Accept:application/vnd.abiquo.metadata+json; version=4.7' \ -u user:password --verbose
When there is no metadata, the query returns a 200 status code, and a data object with an empty links list and a null metadata attribute.
{ "links": [] }
Optionally enable monitoring, select metrics, and retrieve metadata again
To move ahead quickly, in the UI enable "Fetch metrics" and don't select any metrics to retrieve. Then retrieve metadata again using the same GET request as in the previous step.
The metadata object will now have an empty monitoring-metrics list.
{ "metadata": { "monitoring-metrics": [] }, "links": [] }
Now select one or more metrics to retrieve. In the UI we selected two of the built-in metrics from a KVM hypervisor and performed the GET request again.
{ "metadata": { "monitoring-metrics": [ { "name": "used_mem" }, { "name": "cpu_time" } ] }, "links": [] }
Prepare a metadata object with a VM startup script
The platform will store the VM startup script in the VM metadata under the "startup-script" key.
Abiquo supports cloud-config scripts with cloud-init templates in private cloud, and in public cloud.
To add the startup script to the JSON object, first escape any special characters, such as quotation marks, in the startup script.
So for example, we have the following script for use with a cloud-init template on a Linux system.
Disclaimer: this is a sample script for illustrative purposes only. Please see cloud-init documentation to find out how this script works.
#cloud-config users: - name: "myuser" # mkpasswd --method=SHA-512 --rounds=4096 mypass passwd: "$6$rounds=4096$aD0didNw/$Pau0z3YK2Ss5MWYoxScEedfMa.1N5qRqK0xYrgs79qdjHdoFUIRmVXpeEewDduSbImu7sqIjSRm40xO6PpJhk/" groups: - "sudo" ssh_authorized_keys: - "ssh-rsa USER_ID_RSA.PUB"
And after you escape the special characters it will look like this:
#cloud-config\r\nusers:\r\n - name: \"myuser\"\r\n \t# mkpasswd --method=SHA-512 --rounds=4096 mypass\r\n passwd: \"$6$rounds=4096$aD0didNw\/$Pau0z3YK2Ss5MWYoxScEedfMa.1N5qRqK0xYrgs79qdjHdoFUIRmVXpeEewDduSbImu7sqIjSRm40xO6PpJhk\/\" \r\n groups:\r\n - \"sudo\"\r\nssh_authorized_keys:\r\n - \"ssh-rsa USER_ID_RSA.PUB\"
Tip: you can use a JSON string escape/unescape tool to create this format.
Add the startup script to the metadata object with the "startup-script" key. Don't forget to add a comma after the previous object. For our example, after adding the startup script, the metadata object looked like this:
{ "metadata": { "monitoring-metrics": [ { "name": "cpu_time" }, { "name": "used_mem" } ], "startup-script": "#cloud-config\r\nusers:\r\n - name: \"myuser\"\r\n \t# mkpasswd --method=SHA-512 --rounds=4096 mypass\r\n passwd: \"$6$rounds=4096$aD0didNw\/$Pau0z3YK2Ss5MWYoxScEedfMa.1N5qRqK0xYrgs79qdjHdoFUIRmVXpeEewDduSbImu7sqIjSRm40xO6PpJhk\/\" \r\n groups:\r\n - \"sudo\"\r\nssh_authorized_keys:\r\n - \"ssh-rsa USER_ID_RSA.PUB\"" }, "links": [] }
Update VM metadata
To update the VM metadata, perform a PUT request to the metadata link.
curl -X PUT https://mjsabiquo.bcn.abiquo.com:443/api/cloud/virtualdatacenters/1/virtualappliances/1/virtualmachines/3/metadata \ -H 'Accept:application/vnd.abiquo.metadata+json; version=4.7' \ -H 'Content-Type:application/vnd.abiquo.metadata+json; version=4.7' \ -d @requestpayload.json \ -u user:password --verbose
In this case, the requestpayload.json file should contain the JSON object from the previous step.
{ "links": [], "metadata": { "monitoring-metrics": [ { "name": "cpu_time" }, { "name": "used_mem" } ], "startup-script": "#cloud-config\r\nusers:\r\n - name: \"myuser\"\r\n \t# mkpasswd --method=SHA-512 --rounds=4096 mypass\r\n passwd: \"$6$rounds=4096$aD0didNw\/$Pau0z3YK2Ss5MWYoxScEedfMa.1N5qRqK0xYrgs79qdjHdoFUIRmVXpeEewDduSbImu7sqIjSRm40xO6PpJhk\/\" \r\n groups:\r\n - \"sudo\"\r\nssh_authorized_keys:\r\n - \"ssh-rsa USER_ID_RSA.PUB\"" } }
If the request is successful, it will return a status code of 200 and the updated metadata object.
{ "metadata": { "monitoring-metrics": [ { "name": "used_mem" }, { "name": "cpu_time" } ], "startup-script": "#cloud-config\r\nusers:\r\n - name: \"myuser\"\r\n \t# mkpasswd --method=SHA-512 --rounds=4096 mypass\r\n passwd: \"$6$rounds=4096$aD0didNw/$Pau0z3YK2Ss5MWYoxScEedfMa.1N5qRqK0xYrgs79qdjHdoFUIRmVXpeEewDduSbImu7sqIjSRm40xO6PpJhk/\" \r\n groups:\r\n - \"sudo\"\r\nssh_authorized_keys:\r\n - \"ssh-rsa USER_ID_RSA.PUB\"" }, "links": [] }
And that's all!
Access metadata as part of the VM
Here we made a GET request to obtain the VM object and we removed the links to make it easier to read. Here, the metadata object is part of the VM but the links list is not shown.
{ "id": 3, "uuid": "d0876a63-4238-4182-9d10-5f4f363101b9", "description": "root : abiquo", "coresPerSocket": 1, "idState": 1, "idType": 0, "type": "MANAGED", "highDisponibility": 0, "metadata": { "monitoring-metrics": [ { "name": "used_mem" }, { "name": "cpu_time" } ], "startup-script": "#cloud-config\r\nusers:\r\n - name: \"myuser\"\r\n \t# mkpasswd --method=SHA-512 --rounds=4096 mypass\r\n passwd: \"$6$rounds=4096$aD0didNw/$Pau0z3YK2Ss5MWYoxScEedfMa.1N5qRqK0xYrgs79qdjHdoFUIRmVXpeEewDduSbImu7sqIjSRm40xO6PpJhk/\" \r\n groups:\r\n - \"sudo\"\r\nssh_authorized_keys:\r\n - \"ssh-rsa USER_ID_RSA.PUB\"" }, "monitored": true, "protected": false, "variables": {}, "backuppolicies": [], "generateGuestInitialPassword": false, "natrules": [],
Although you can read the metadata here, the most correct and convenient way to work with VM metadata is using the metadata link as described in the previous steps.
Delete metadata
If you ever need to delete the VM metadata, just send a DELETE request to the metadata link. But remember that this request will delete ALL metadata, leaving just the empty links list and the null metadata key, as shown in the first example when we retrieved the empty metadata object.
Related links
Some API references about working with VM metadata:
Copyright © 2006-2022, Abiquo Holdings SL. All rights reserved