Solving problems invented by others...
Snapshot usage broken down into disks

Snapshot usage broken down into disks

This week I had to work on a script which exports data out of Cloud Director in order to charge our customers for the used resources. I should add the used resources of vm snapshots.

While searching where I can find snapshot informations of vms I again had to leave PowerCLI and request the vm information from the rest api.

# get the vm object from PowerCLI
$vm = Get-CIVM -Name "mytestvm"

# define header for the rest api calls
$header = @{
	Accept='application/*+xml;version=34.0'
	"content-type"='application/xml'
	"x-vcloud-authorization"=$global:defaultciservers[0].SessionSecret
}

# execute rest api call to get the full vm data
$restapivm = (Invoke-RestMethod -Uri $vm.href -Method get -Headers $header).Vm

Inside this $restapivm xml object I found a property called SnapshotSection. If the vm has a snapshot, this property contains a sub-property called Snapshot. It looks like described in the api documentation here -> https://code.vmware.com/apis/912/vmware-cloud-director/doc/doc///types/SnapshotType.html

So far so good for me. I could find out if a vm has a snapshot and how big it is.
But since I’m working for a company that doesn’t want to go the easy way, they prepared some tripping hazards for me. 😉

Charging the customer just the size of the snapshot was not enough, because we offer different types of storage for them. We have storage with different limits of iops and backed with different types of hard disks. (hdd/ssd)
Therefore my company wanted to break down the size of the snapshot to the types of storage we offer.

My first finding was that I could have only one snapshot in cloud director. (Also described in the api documentation here -> https://code.vmware.com/apis/912/vmware-cloud-director/doc/doc/operations/POST-CreateSnapshot.html )
Second finding was that the size of a snapshot is always the size of all disks of the vms.

With this findings, I thought it was really easy because I only had to take the size of the vm disks and their storage types and export it.

Then I had a bad idea. I took a vm with one disk and create a snapshot. Then, while the snapshot exists, I added another disk of the same size to the vm but with a different storage type. Now the vm had two disks, but only one of them is contained in the snapshot. The size of the snapshot of course did also not change.
In this situation I could not decide what to export for charging the customer. I had to find a way which disk is included in the snapshot and which one not.

After playing around a bit I saw something in the UI. Disks included in a snapshot could not be increased in size. This could also be found in the $restapivm xml object in the VmSpecSection.DiskSection.DiskSettings. Every disk there has a resizable property. Disks included in the snapshot have the value false.

With this in mind, I just had to filter the disk for resizable = false and use the size and the StorageProfile.name property of the disk for my export.

This little one liner shows a list of all disks which are included in a snapshot and the name of the corresponding storage policy.

$restapivm.VmSpecSection.DiskSection.DiskSettings | 
Where-Object -FilterScript { $_.resizable -eq "false" } | 
Select-Object -Property SizeMb,@{Name="StorageProfile";Expression={$_.StorageProfile.name}}

Leave a Reply

Your email address will not be published. Required fields are marked *

one + three =