Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...


Tip

To add a hard disk to a VM through the API, the main steps are as follows.

  1. Create a hard disk in the virtual datacenter
  2. Update the VM to add the hard disk link in the disk sequence 

This may appear to be different to how you add a hard disk through the UI, where you work directly in the VM. But behind the scenes, the UI uses the API to create the hard disk in the virtual datacenter and then adds it to the VM.

...



Summary diagram





We will walk through the following API request process with examples of the requests and data objects.

...

Numberedheadings
start-numbering-ath4

Get a VM by name 

In the UI, copy the first part of the UUID field for the VM. Send a GET request to list all VMs with the "has" parameter to filter and only retrieve the VM with a name that matches the UUID.  




 

Use the VDC link from the VM to get the VDC

Make a GET request to the virtualdatacenter link to obtain the virtual datacenter object. From the virtual datacenter object use the "disks" link. This is where you will create the disks.

This is the disks link.

This is the full response

 

By default your new disk will use the same datasatore tier as the existing disk.

Create a data object for the hard disk

To determine what the hard disk object should look like, create a hard disk using the UI and check the post request to the virtualdatacenter's disks link.

Copy the request body data object and modify the data object for a hard disk that you will create with the API, e.g. change the label. For example:

For more information see

Create a hard disk entity.

Create the hard disk in the VDC

To create the hard disk in the VDC, send a POST request to the virtual datacenter disks link with the disk data object.

In the response object, look for the edit link to the hard disk. You will need to modify this link and add it to the VM entity.



 

If necessary, power off the VM

If the VM is deployed and powered on, perform a PUT request to the VM state link, sending a VM state object to power it off. If your test platform supports "gracefulShutdown", you can use it as shown here. A status code of 202 means that the request was accepted. However, you should always check that the VM is really powered off.

 

Get the VM using the URL of the VM edit link

It's always best to get the VM again, just to check, and using the edit link from the VM object itself, instead of retrieving a list of one VM from the all VMs resource. This makes it easier to edit the VM object.




 

Modify the link to the hard disk to add it in the boot sequence of the VM

To add the hard disk to the VM, we will modify the VM entity and add a link to the hard disk. So the first step is to create a link to the hard disk.

First check if there are already any existing disks in the VM to determine the boot sequence. The disk "rel" (relation) link values are in the format "diskX", where "X" is the sequence number starting with 0 for the boot disk.

This is the boot disk of the VM taken from the VM object.

And this is the edit link of our hard disk.

So we should modify the edit link from the hard disk and change the "rel" link to specify that it should be included in the VM boot sequence as "disk3". So the link to send in the VM put request would look like this:

Next we will add this link to the VM.

Edit the VM data object to add the link to the hard disk

Add the link at the end of the links section. Don't forget to add a comma before the new link!



 

Modify the VM to add the hard disk

Now we need to send a PUT request to the VM to modify it and add the hard disk. The VM data object is enormous, so we saved it to a file and used the @ notation to specify the file. If the request is successful, for a VM that is not deployed, the status code returned will be a 204, with no content. If the VM is deployed, a successful request will return a status of 202 Accepted and an accepted request object, with a link to the reconfigure task.

 

Check the result in your VM

Get the VM to check that the disk has been added.

The disk was added successfully because this disk3 link can be found in the following VM object.





...