Categories
API virtualisation vmware vcd rest api

How to update, change and delete a storage Profile with VCloud Director REST API

Recently I was tasked with removing a few attached storage profiles from a number of VDC’s in vCloud Director. The task would have been a long and arduous manual task.

Fortunately vCloud Director has a REST API (A flippen good one).
The reason I say it is good is that it makes proper used of HTTP verbs to do certains tasks and returns the correct error codes and good error messages.

For example if you make a GET to a resource path that only allows PUT you will get a 415 METHOD NOT ALLOWED. So it helps to guide you – it is self documenting.

The only shit thing is that it is application/xml only right now, but xml isn’t that bad to be fair.

You when you make a bad request (400) you will get extra info in the error message – ie. it will say what fields it expected or it didn’t expect and you can correct the payload.

Remember the main page for the VCloud Director Rest documentation. I am using vCloud Director 9.5 (Cloud API 31.0 ) .

Changing a Storage Profile

The first thing I reliased is that you cannot just delete a storage profile. The profile must not be the default profile and it must be disabled first.

Unfortunately the documentation only gave examples on how to add or delete a storage profile – no way of updating it.

If you look at the service provider documentation and the Update Organization VDC Storage Profiles docs, you see there is no info about updating a profile.

If you look at the xml for a storage profile you will see it looks like this:

<AdminVdcStorageProfile href="https://{my_vcd_base_url}/api/admin/vdcStorageProfile/{uuid}...
<Link href="https://{my_vcd_base_url}/api/admin/vdcStorageProfile/{uuid}" rel="edit" type="application/vnd.vmware.admin.vdcStorageProfile+xml"/>

You will see there is an edit relation for that url.
After a bit of trial and error I saw that you could send it a PUT with the Storage profile object and it would update:

response = client.session.put(
            f'https://{my_vcd_base_url}/api/admin/vdcStorageProfile/{uuid}',
            data=f'''<?xml version="1.0" encoding="utf-8"?>
                <AdminVdcStorageProfile name="{storage_profile_name}" xmlns="http://www.vmware.com/vcloud/v1.5">
                    <Enabled>false</Enabled>
                    <Units>MB</Units>
                    <Limit>50000</Limit>
                    <Default>false</Default>
                    <ProviderVdcStorageProfile.../>
                </AdminVdcStorageProfile>
            ''',
            headers={
                'content-type': 'application/vnd.vmware.admin.vdcStorageProfile+xml'
            }
        )

that worked

So now I could set a storage profile as disabled and change it from being the default.

Deleting the storage Profile

Now deleting is as per the recent documentation on updating storage profile on the VDC. Importantly you need to add a RemoveStorageProfile element and not a DeleteStorageProfile element as per earlier docs.

Another thing, is the url you are posting to is the vdc’s storage profile endpoint and there must be at least an href attribute on the RemoveStorageProfile element – the one you want to remove.

response = client.session.post(
        f'https://{my_vcd_base_url}/api/admin/vdc/{vdc_uuid}/vdcStorageProfiles',
        data=f'''<?xml version="1.0" encoding="UTF-8"?>
            <UpdateVdcStorageProfiles
            xmlns="http://www.vmware.com/vcloud/v1.5" >
                <RemoveStorageProfile href="{storage_profile_url}">
                </RemoveStorageProfile>
            </UpdateVdcStorageProfiles>
        ''',
        headers={
            'content-type': 'application/vnd.vmware.admin.updateVdcStorageProfiles+xml'
        }
    )

The response returns a task.
Which has a URL that you can use to check if the delettion was successful.

That’s it. Hopefully this will help to automate some of the tasks you require.