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:

  1. Configure a VM by adding an IP address (NIC)
  2. Change the state of a VM




Requirements

This tutorial requires two VMs that are:

  1. not deployed
  2. do not have any IP addresses.

To use a VM from the last 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 API:

  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, e.g. delete a disk from your VM!





Configure a VM to add a NIC using the UI

First add a NIC using the UI, in order to view the request made in the API.

We will add a network interface card (NIC) with an IP address in a private network to the first VM we created. In this case, we will assume that the VM is not deployed.

  1. Open the browser console to the Network tab and record actions
  2. Edit the VM and go to Network → Private → select network → drag an IP address into the NICs panel, and Save 
  3. Find the PUT request and copy it, or get the NIC link and VM data object

     Click here to show/hide the screenshot

Obtain the link to the NIC

In the VM object, you will probably find the link to the NIC near the end of the VM links section.

      {
         "title":"privateip",
         "rel":"nic0",
         "type":"application/vnd.abiquo.privateip+json",
         "href":"https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/326"
      }, 




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://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips
  2. Use a GET request to obtain the first 3 IP addresses in this network, with the parameter "limit=3".  

    curl --verbose 'href":"https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips?limit=3' \
         -H 'Accept: application/vnd.abiquo.privateips+json; version=5.2' \
    	 -u user:password -k | jq . 

    To only retrieve IPs that are available for use, you can use the query parameter "free=true".

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

     Click here to expand...
    √ Abiquo % curl --verbose 'https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips?limit=3' \  
         -H 'Accept: application/vnd.abiquo.privateips+json; version=5.2' \
             -u user:password  -k | jq .
     
    > GET /api/cloud/virtualdatacenters/42/privatenetworks/53/ips?limit=3 HTTP/1.1
    > Host: linatest.bcn.abiquo.com
    > Authorization: Basic XXXXXXX
    > User-Agent: curl/7.64.1
    > Accept: application/vnd.abiquo.privateips+json; version=5.2
    > 
    < HTTP/1.1 200 200
    < Date: Fri, 16 Apr 2021 19:00:47 GMT
    < Server: Apache
    < Set-Cookie: ABQSESSIONID=3701578778846185265; Max-Age=1800; Expires=Fri, 16-Apr-2021 19:30:48 GMT; Path=/; Secure; HttpOnly; SameSite=strict
    < X-Abiquo-TracerContext: aff6905e-3a00-47e5-a640-e70556ab7ffc
    < X-Abiquo-Token: a2e19816735381c1d074441cbd002aa0b2dfac898841de657740584d88f344f6868d440ef8c65b7687ac23cb388ca40e78f93c9d3156bff79e474f6222358857
    < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    < Pragma: no-cache
    < Expires: 0
    < Strict-Transport-Security: max-age=31536000 ; includeSubDomains
    < X-XSS-Protection: 1; mode=block
    < X-Frame-Options: DENY
    < X-Content-Type-Options: nosniff
    < Content-Type: application/vnd.abiquo.privateips+json; version=5.2
    < Transfer-Encoding: chunked
    < 
    
    {
      "totalSize": 10,
      "links": [
        {
          "rel": "first",
          "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips?limit=3&by=ip&asc=true"
        },
        {
          "rel": "next",
          "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips?startwith=3&limit=3&by=ip&asc=true"
        },
        {
          "rel": "last",
          "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips?startwith=7&limit=3&by=ip&asc=true"
        }
      ],
      "collection": [
        {
          "id": 324,
          "ip": "192.168.0.1",
          "mac": "00:50:56:2B:4D:8D",
          "name": "0050562B4D8D_host",
          "networkName": "default_private_network",
          "ipv6": false,
          "quarantine": false,
          "available": true,
          "links": [
            {
              "title": "default_private_network",
              "rel": "privatenetwork",
              "type": "application/vnd.abiquo.vlan+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53"
            },
            {
              "title": "privateip",
              "rel": "self",
              "type": "application/vnd.abiquo.privateip+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/324"
            },
            {
              "title": "vdc_ESXI_vCenter",
              "rel": "virtualdatacenter",
              "type": "application/vnd.abiquo.virtualdatacenter+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42"
            }
          ]
        },
        {
          "id": 326,
          "ip": "192.168.0.2",
          "mac": "00:50:56:05:FE:8F",
          "name": "00505605FE8F_host",
          "networkName": "default_private_network",
          "ipv6": false,
          "usedBy": "ABQ_039b03e9-1639-4c46-a915-b53af7ea3445",
          "quarantine": false,
          "available": true,
          "links": [
            {
              "title": "default_private_network",
              "rel": "privatenetwork",
              "type": "application/vnd.abiquo.vlan+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53"
            },
            {
              "title": "privateip",
              "rel": "self",
              "type": "application/vnd.abiquo.privateip+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/326"
            },
            {
              "title": "vdc_ESXI_vCenter",
              "rel": "virtualdatacenter",
              "type": "application/vnd.abiquo.virtualdatacenter+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42"
            },
            {
              "title": "vapp_web",
              "rel": "virtualappliance",
              "type": "application/vnd.abiquo.virtualappliance+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110"
            },
            {
              "title": "ABQ_039b03e9-1639-4c46-a915-b53af7ea3445",
              "rel": "virtualmachine",
              "type": "application/vnd.abiquo.virtualmachine+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1918"
            }
          ]
        },
        {
          "id": 335,
          "ip": "192.168.0.3",
          "mac": "00:50:56:0D:BE:B9",
          "name": "0050560DBEB9_host",
          "networkName": "default_private_network",
          "ipv6": false,
          "quarantine": false,
          "available": true,
          "links": [
            {
              "title": "default_private_network",
              "rel": "privatenetwork",
              "type": "application/vnd.abiquo.vlan+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53"
            },
            {
              "title": "privateip",
              "rel": "self",
              "type": "application/vnd.abiquo.privateip+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/335"
            },
            {
              "title": "vdc_ESXI_vCenter",
              "rel": "virtualdatacenter",
              "type": "application/vnd.abiquo.virtualdatacenter+json",
              "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42"
            }
          ]
        }
      ]
    }
    
    
    

In the above example, the IP that is being used on a VM has a link to the VM. 

The available IP that we will use has the identifier "335".

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

        {
          "title": "privateip",
          "rel": "self",
          "type": "application/vnd.abiquo.privateip+json",
          "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/335"
        },

To prepare it for the VM, change "self" to "nicX", where "X" represents the number of the new NIC in the VM. So if there are no NICs, add "nic0" as shown here.

        {
          "title": "privateip",
          "rel": "nic0",
          "type": "application/vnd.abiquo.privateip+json",
          "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/335"
        },


Add the NIC link to the VM object

Go the end of the links section and add the NIC from the previous step.

(warning) If this is the last link, add a comma before it, and remove any comma after it.

This is an example of the modified VM object, with the nic0 link at the end of the links section. 


 Click here to expand...
 {
  "id": 1920,
  "uuid": "6b2d0d93-7f23-4a78-af5f-a9398be58773",
  "fqdn": "box.localdomain",
  "description": "A virtual machine",
  "coresPerSocket": 1,
  "idState": 1,
  "idType": 0,
  "type": "MANAGED",
  "highDisponibility": 0,
  "monitored": true,
  "monitoringLevel": "DEFAULT",
  "protected": false,
  "variables": {},
  "backuppolicies": [],
  "lastSynchronize": 1618592955000,
  "generateGuestInitialPassword": false,
  "natrules": [],
  "vdrpEnabled": true,
  "vdrpPort": 0,
  "password": "xPUOwNxb",
  "deallocated": false,
  "name": "ABQ_6b2d0d93-7f23-4a78-af5f-a9398be58773",
  "label": "yVM_mj_2",
  "ram": 48,
  "cpu": 1,
  "state": "NOT_ALLOCATED",
  "creationTimestamp": 1618588107000,
  "links": [
    {
      "title": "Abiquo",
      "rel": "enterprise",
      "type": "application/vnd.abiquo.enterprise+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/admin/enterprises/1"
    },
    {
      "title": "ABQ_6b2d0d93-7f23-4a78-af5f-a9398be58773",
      "rel": "edit",
      "type": "application/vnd.abiquo.virtualmachine+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920"
    },
    {
      "rel": "asynctasks",
      "type": "application/vnd.abiquo.asynctasks+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/asynctasks"
    },
    {
      "title": "send mail",
      "rel": "sendmail",
      "type": "application/vnd.abiquo.mail+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/admin/publiccloudregions/7/enterprises/1/virtualmachines/1920/action/sendmail"
    },
    {
      "title": "cloudadmin",
      "rel": "user",
      "type": "application/vnd.abiquo.user+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/admin/enterprises/1/users/10"
    },
    {
      "title": "vdc_ESXI_vCenter",
      "rel": "virtualdatacenter",
      "type": "application/vnd.abiquo.virtualdatacenter+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42"
    },
    {
      "title": "vapp_web",
      "rel": "virtualappliance",
      "type": "application/vnd.abiquo.virtualappliance+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110"
    },
    {
      "title": "metadata",
      "rel": "metadata",
      "type": "application/vnd.abiquo.metadata+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/metadata"
    },
    {
      "title": "vlan network configurations",
      "rel": "configurations",
      "type": "application/vnd.abiquo.virtualmachinenetworkconfigurations+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/network/configurations"
    },
    {
      "title": "nics",
      "rel": "nics",
      "type": "application/vnd.abiquo.nics+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/network/nics"
    },
    {
      "title": "disks",
      "rel": "harddisks",
      "type": "application/vnd.abiquo.harddisks+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/storage/disks"
    },
    {
      "title": "NOT_ALLOCATED",
      "rel": "state",
      "type": "application/vnd.abiquo.virtualmachinestate+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/state"
    },
    {
      "title": "virtual machine undeploy",
      "rel": "undeploy",
      "type": "application/vnd.abiquo.acceptedrequest+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/undeploy"
    },
    {
      "title": "virtual machine deploy",
      "rel": "deploy",
      "type": "application/vnd.abiquo.acceptedrequest+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/deploy"
    },
    {
      "title": "virtual machine reset",
      "rel": "reset",
      "type": "application/vnd.abiquo.acceptedrequest+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/reset"
    },
    {
      "title": "virtual machine snapshot",
      "rel": "instance",
      "type": "application/vnd.abiquo.acceptedrequest+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/instance"
    },
    {
      "title": "remote access",
      "rel": "rdpaccess",
      "type": "application/vnd.abiquo.virtualmachineconsole+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/config/rdpaccess"
    },
    {
      "title": "tasks",
      "rel": "tasks",
      "type": "application/vnd.abiquo.tasks+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tasks"
    },
    {
      "title": "firewalls",
      "rel": "firewalls",
      "type": "application/vnd.abiquo.links+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/firewalls"
    },
    {
      "title": "load balancers",
      "rel": "loadbalancers",
      "type": "application/vnd.abiquo.loadbalancers+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/loadbalancers"
    },
    {
      "title": "request on demand backup",
      "rel": "requestbackup",
      "type": "application/vnd.abiquo.ondemandbackup+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/backup/action/request"
    },
    {
      "title": "request a restore of a backup",
      "rel": "requestrestore",
      "type": "application/vnd.abiquo.restore+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/backup/action/restore"
    },
    {
      "title": "move VM to a virtual appliance",
      "rel": "vappmove",
      "type": "application/vnd.abiquo.links+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/vappmove"
    },
    {
      "title": "move VM to another virtual datacenter",
      "rel": "move",
      "type": "application/vnd.abiquo.movevm+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/move"
    },
    {
      "title": "volumes",
      "rel": "volumes",
      "type": "application/vnd.abiquo.volumes+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/storage/volumes"
    },
    {
      "diskAllocation": "THIN",
      "diskControllerType": "IDE",
      "diskLabel": "Hard disk 1",
      "length": "64",
      "title": "4166504b-d350-439e-adcb-1bc2b57814e0",
      "rel": "disk0",
      "type": "application/vnd.abiquo.harddisk+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/disks/1896"
    },
    {
      "title": "dstier_shared",
      "rel": "datastoretier0",
      "type": "application/vnd.abiquo.datastoretier+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/locations/7/datastoretiers/9"
    },
    {
      "title": "protect",
      "rel": "protect",
      "type": "text/plain",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/protect"
    },
    {
      "title": "unprotect",
      "rel": "unprotect",
      "type": "text/plain",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/unprotect"
    },
    {
      "title": "metricsmetadata",
      "rel": "metricsmetadata",
      "type": "application/vnd.abiquo.metricsmetadata+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/metrics"
    },
    {
      "title": "disablemonitoring",
      "rel": "disablemonitoring",
      "type": "",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/disablemonitoring"
    },
    {
      "title": "collectd",
      "rel": "collectd",
      "type": "application/json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/metrics/collectd"
    },
    {
      "title": "alarmssearch",
      "rel": "alarmssearch",
      "type": "application/vnd.abiquo.alarms+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/alarms"
    },
    {
      "title": "clone",
      "rel": "clone",
      "type": "application/vnd.abiquo.virtualmachinecloneoptions+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/action/clone"
    },
    {
      "rel": "tags",
      "type": "application/vnd.abiquo.resourcetags+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags"
    },
    {
      "rel": "tags-update",
      "type": "application/vnd.abiquo.tags+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags"
    },
    {
      "rel": "tags-format",
      "type": "application/vnd.abiquo.tagformatrestrictions+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags"
    },
    {
      "rel": "tags-create",
      "type": "application/vnd.abiquo.tags+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags/action/create"
    },
    {
      "rel": "tags-delete",
      "type": "application/vnd.abiquo.tagkeys+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags/action/delete"
    },
    {
      "rel": "tags-edit",
      "type": "application/vnd.abiquo.tags+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags/action/edit"
    },
    {
      "rel": "tags-sync",
      "type": "",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920/tags/action/sync"
    },
    {
      "title": "VMware vCenter",
      "rel": "hypervisortype",
      "type": "application/vnd.abiquo.hypervisortype+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/config/hypervisortypes/VMX_04"
    },
    {
      "title": "yVM_mj",
      "rel": "virtualmachinetemplate",
      "type": "application/vnd.abiquo.virtualmachinetemplate+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/admin/enterprises/1/datacenterrepositories/7/virtualmachinetemplates/520"
    },
    {
      "title": "Others",
      "rel": "category",
      "type": "application/vnd.abiquo.category+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/config/categories/1"
    },
    {
      "title": "privateip",
      "rel": "nic0",
      "type": "application/vnd.abiquo.privateip+json",
      "href": "https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/privatenetworks/53/ips/335"
    }
  ],
  "usageStatistics": []
}


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, we saved it to a file called  "VMnic.json", and then used the @ notation to reference the file.

curl --verbose -X PUT 'https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920' \
   -H 'Content-Type: application/vnd.abiquo.virtualmachine+json; version=5.2' \
   -H 'Accept: application/vnd.abiquo.acceptedrequest+json; version=5.2' \
   -u user:password \
   -d @VMwithNIC.json -k 

Send the PUT request.

If it is successful, the response status and message will be "204  No content" for an undeployed VM.

The example request is given here.

 Click here to expand...
 √ Abiquo % curl --verbose -X PUT 'https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920' \
   -H 'Content-Type: application/vnd.abiquo.virtualmachine+json; version=5.2' \
   -H 'Accept: application/vnd.abiquo.acceptedrequest+json; version=5.2' \
   -u user:password \
   -d @VMwithNIC.json -k

> PUT /api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920 HTTP/1.1
> Host: linatest.bcn.abiquo.com
> Authorization: Basic XXXXXX
> User-Agent: curl/7.64.1
> Content-Type: application/vnd.abiquo.virtualmachine+json; version=5.2
> Accept: application/vnd.abiquo.acceptedrequest+json; version=5.2
> Content-Length: 11899
> Expect: 100-continue
> 
< HTTP/1.1 100 Continue

< HTTP/1.1 204 204
< Date: Fri, 16 Apr 2021 19:19:17 GMT
< Server: Apache
< Set-Cookie: ABQSESSIONID=8608303820936303687; Max-Age=1800; Expires=Fri, 16-Apr-2021 19:49:17 GMT; Path=/; Secure; HttpOnly; SameSite=strict
< X-Abiquo-TracerContext: 93272692-df60-4efa-a478-a4fcfd3a859b
< X-Abiquo-Token: a2e19816735381c1d074441cbd002aa06429084b01dc4d254adc14535eae44c5c3f4ad90e8e51aabe5134b3d3b841586306916bfe4fdcb486f331e2501f490c0
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< Strict-Transport-Security: max-age=31536000 ; includeSubDomains
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: DENY
< X-Content-Type-Options: nosniff
< 



Check your VM in the UI

In the UI, when you select the VM, and open the control panel in the Network tab, the NIC should display.

Power actions on a VM

Before you begin this section, deploy a VM using the UI or the API.

To perform a power action (except for reset) on a VM using the API:

  1. Perform a GET request to obtain the VM object and find the VM state link
  2. Create a virtualmachinestate object
  3. Send a PUT request to the VM state link

The VM state link is a link in the VM object with the "rel" attribute set to state. The "title" attribute contains the current state. You can send a PUT request of a virtualmachine state object to the link (in the "href" attribute) to change the power state of the VM.

    {
      "title": "ON",
      "rel": "state",
      "type": "application/vnd.abiquo.virtualmachinestate+json",
      "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454/state"
    }, 


Here are some examples of virtualmachinestate objects and notes about changing VM states

  • Hard power off

    {"state": "OFF"}

    When you perform a power off via API, the response will include a link where you can monitor the progress of this operation. For an example of a hard power off, see https://wiki.abiquo.com/api/latest/VirtualMachinesResource.html#change-the-state-of-a-virtual-machine

  • Graceful shutdown

    {"state": "OFF", "gracefulShutdown": true}

    To perform a graceful shutdown, your VM will need to have guest extensions installed on it. After an operation completes, you can view the status of the task by going to the link in the accepted request link of the response. In this case, the graceful shutdown was successful.

     Click here to show or hide the request example
    curl -X PUT 'https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454/state' \
      -k --verbose \
      -H 'Accept: application/vnd.abiquo.acceptedrequest+json; version=5.0' \
      -H 'Content-Type: application/vnd.abiquo.virtualmachinestate+json; version=5.0' \
      -d '{"state": "OFF", "gracefulShutdown": true}' \
      -u user:password | jq .
    {
      "taskId": "d62be542-f34c-4fed-b9f8-6443f2db4cc1",
      "userId": "10",
      "type": "SHUTDOWN",
      "ownerId": "19454",
      "state": "FINISHED_SUCCESSFULLY",
      "creationTimestamp": 1596040226,
      "timestamp": 1596040226,
      "jobs": {
        "links": [],
        "collection": [
          {
            "id": "d62be542-f34c-4fed-b9f8-6443f2db4cc1.2cd94a0c-179d-4506-a71d-fabaf29a4d43",
            "parentTaskId": "d62be542-f34c-4fed-b9f8-6443f2db4cc1",
            "type": "SHUTDOWN",
            "description": "Shutdown task's shutdown on virtual machine with id 19454",
            "state": "DONE",
            "rollbackState": "UNKNOWN",
            "creationTimestamp": 1596040226,
            "timestamp": 1596040226,
            "links": []
          }
        ]
      },
      "links": [
        {
          "rel": "self",
          "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454/tasks/d62be542-f34c-4fed-b9f8-6443f2db4cc1"
        },
        {
          "rel": "parent",
          "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454/tasks"
        },
        {
          "rel": "result",
          "type": "application/vnd.abiquo.virtualmachine+json",
          "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454"
        },
        {
          "title": "user",
          "rel": "user",
          "type": "application/vnd.abiquo.user+json",
          "href": "https://nardo40.bcn.abiquo.com:443/api/admin/enterprises/336/users/10"
        },
        {
          "title": "ABQ_2fb11009-8157-4d61-915d-40fa45f440ac",
          "rel": "virtualmachine",
          "type": "application/vnd.abiquo.virtualmachine+json",
          "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454"
        }
      ]
    } 
  • Power on

    {"state": "ON"}
  • Pause

    {"state": "PAUSED"}
  • Azure power off and deallocate

    Azure has two power off states - powered off and deallocated.

    • To power off a VM in Azure via the Abiquo API, use the graceful shutdown

    • To deallocate a VM in Azure via the Abiquo API, use the hard power off

      • The deallocated VM will have a "deallocated" attribute that is set to "true".


To reset a VM using the API, use a POST request to the reset action link. If you are using a test environment, you may wish to add the --insecure option.

 Click here to expand...

cURL:

curl -X POST 'https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454/action/reset' \ 
-k --verbose \
-H 'Accept: application/vnd.abiquo.acceptedrequest+json; version=5.0' \
-u user:password | jq . 

Success status code: 202

Request payload:

--none--

Response payload:

{
  "message": "You can keep track of the progress in the link",
  "links": [
    {
      "title": "status",
      "rel": "status",
      "type": "application/vnd.abiquo.task+json",
      "href": "https://nardo40.bcn.abiquo.com:443/api/cloud/virtualdatacenters/2486/virtualappliances/2990/virtualmachines/19454/tasks/edec6cf1-8874-451d-a4c0-57f4c24da371"
    }
  ]
} 




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.

curl -X DELETE https://linatest.bcn.abiquo.com:443/api/cloud/virtualdatacenters/42/virtualappliances/110/virtualmachines/1920 \
	-H 'Accept: text/json,application/json;' \
	-u user:password -k

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-2022, Abiquo Holdings SL. All rights reserved