Detaching and Deleting Independent Disks in vCloud Director via REST API

Yesterday while working on one of the production issue where we had to deprovision a tenant environment in vCloud Air, I noticed that independent disks were preventing automated deprovision of the environment and the error messages were loud and clear in the log files.

It was a new issue for me so I started reading about independent disks in vCloud Director and want to share few things about this.

First of all independent Disk feature in vCD is completely different from an Independent Disk in vSphere. Independent disks can be shared across multiple vApps/VM’s in vCloud Director. This feature was first introduced in vCD v5.1.

Following quote from vCloud Architecture Toolkit document rightly explains about independent disks

The use of independent disks with vCloud Director allows updates of virtual machines without impacting the underlying data.

The feature is designed to enable users to create virtual disks which can be attached to and detached from virtual machines. There is no functionality to control this feature from the vCD UI and this can be controlled via API’s only. 

When you create an independent disk, it is associated with an organization vDC but not with a virtual machine. After the disk has been created, the disk owner or an administrator can attach it to any virtual machine deployed in that vDC, detach it from a virtual machine, and remove it from the vDC.

Presence of Independent disks in vCD can be seen on navigating to Org > Administration > Org vDC > Independent Disks tab. If you right click on any of the disk you will not see any action window opening. 

0.PNG

In this post I am going to demonstrate how we can detach/delete independent disks from VM via API calls. Lets get started.

For sake of this demonstration, I have used some hypothetical names for Org and Org vDC.

Step 1: Obtain vCD Auth token code

# curl -sik -H “Accept:application/*+xml;version=5.6” -u “admin@system” -X POST https://vCD-FQDN/api/sessions | grep auth

Enter host password for user ‘admin@system’:

x-vcloud-authorization: Auth

Step 2: Locate your Org 

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “x-vcloud-authorization:Auth-X GET https://vCD-FQDN/api/org/

On using the above API call, you will see a href to all your Org that are present in vCD. For your next query chose the href of the org where independent disks are lying.  

Step 3: Locate your Org vDC

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “x-vcloud-authorization: Auth” -X GET https://vCD-FQDN:443/api/org/fc432145-f1f3-42f6-a26f-eeb3d306a405 | grep vdc

Use the above href of your org vDC in next query.

Step 4: Find the href of all independent disks

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “x-vcloud-authorization:Auth” -X GET https://vCD-FQDN:443/api/vdc/adf0929b-a107-4671-9f85-b629b744c2b7 | grep disk

Step 5: Find the name of associated VM with the independent disk

First we have to get href of the VM and then the actual VM name. This can be achieved via below queries:

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “x-vcloud-authorization:Auth” -X GET https://vCD-FQDN:443/api/disk/f1d99e1c-b175-4715-a7bc-322be23a07a3 | grep attachedVms

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “x-vcloud-authorization:Auth” -X GET https://vCD-FQDN:443/api/disk/f1d99e1c-b175-4715-a7bc-322be23a07a3/attachedVms | grep VmReference

Step 6: Detach Independent Disk from VM

For detaching the independent disk, we have to take care of two things. First we have to pass “application/vnd.vmware.vcloud.diskAttachOrDetachParams+xml” as Content-Type in header. This is explained in vCD 8.x documentation

Also we have to create a XML file which have href of the independent disk which we obtained in Step 4

I have named my XML file as detach.xml with below content

Also make sure to format your xml file in correct format. It can be done online by visiting xml-formatter website. 

Now make following API call for detaching the disk

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “Content-Type:application/vnd.vmware.vcloud.diskAttachOrDetachParams+xml” -H “x-vcloud-authorization:Auth” -d @detach.xml -X POST https://vCD-FQDN:443/api/vApp/vm-8998e541-a54c-432b-94ba-bafe0b7edd2f/disk/action/detach

If you are not making any mistake then you should see a HTTP 202 Accepted header alongwith an xml file which list the details of the detach task triggered

HTTP/1.1 202 Accepted
Date: Tue, 20 Jun 2017 07:01:11 GMT

the xml output looks like below

xml.PNG

Step 7: Delete independent disk

Supply the href of independent disk obtained in step 4 in below query

# curl -sik -H “Accept:application/*+xml;version=5.6” -H “x-vcloud-authorization:Auth” -X DELETE https://vCD-FQDN:443/api/disk/f1d99e1c-b175-4715-a7bc-322be23a07a3

Again you should see 2 202 Accepted alongwith an xml file as output

HTTP/1.1 202 Accepted
Date: Tue, 20 Jun 2017 07:02:53 GMT

xml-2

I hope you enjoyed reading this post. Feel free to share this on social media if it is worth sharing. Be sociable :)

Leave a Reply