GeekAfterFive

Infrastructure as Code

home

vCloud API/GUI Throwdown: Part 2

24 Feb 2012

In the first part of vCloud API/GUI Throwdown, we watched in awe as the API completely pummeled the GUI. I gave you some hints in part 1 to do this on your own with the API, but this time around we're going to use PowerCLI 5.0.1 and the new vCloud Snap-in!

Requirements:
  • vCloud Director 1.5 (Hit me up in the blog comments if you need Powershell code for 1.0.x)
  • PowerCLI 5.0.1

Hot Cloning vApps

This will make an EXACT copy of the vApp, MAC addresses and all. If you don't want that, you'll need to make some modifications. Hit the comments for specific requests.

Add-PSSnapin vmware.vimautomation.cloud
Connect-CIServer vcloud.example.com -org MyOrg -User MyUser -Password MyPassword

$sourceName = "SourceVApp"
$cloneName = "NewVAppName"
$vdcName = "MyOrgVdcName"

$sourceVapp = Get-CIVApp $sourceName
$vdc = Get-OrgVdc $vdcName
$cloneParams = new-object VMware.VimAutomation.Cloud.Views.CloneVAppParams
$instParams = new-object VMware.VimAutomation.Cloud.Views.InstantiationParams

$instParams.section = $sourceVapp.ExtensionData.Section[0,1,3]
$cloneParams.InstantiationParams = $instParams
$cloneParams.Source = $sourceVapp.Href
$cloneParams.Name = $cloneName

$vdc.ExtensionData.CloneVApp($cloneParams)

Lines 1-2: Add Snap-in and connect. Lines 4-6: Source, Destination, and vDC names. Lines 8-9: Get vApp and vDC objects Line 10: Create a CloneVAppParams Object. This is all the info vCloud needs to clone the vApp. Line 11: Create a InstantiationParams Object. This is a container object for the OVF data. Line 13: Add OVF Data to InstantiationParams Object. Notice I picked specific sections of the OVF [0,1,3]. Section 2 is NetworkConnectionSection. We just need Section 3: NetworkConfigSection. Attempting to clone Section 2 will cause it to fail. I'll deep dive later. :D EDIT: If you are not utilizing vApp Networks, you should only copy sections [0,1]. I will follow up with another blog post dedicated to cloning and customizing vApps. Line 14-16: Put our InstantiationParams and other info into our CloneVAppParams object. Line 18: Clone!

The below image was my source vApp....

[caption id="attachment_405" align="alignnone" width="490" caption="My source vApp."][/caption] [caption id="attachment_406" align="alignnone" width="490" caption="cloneVApp method return."][/caption] [caption id="attachment_407" align="alignnone" width="490" caption="Dolly, My cloned Sheep! Er, um... vApp!"][/caption]

Hot Modify connected VM network

This will change the network, and assigned IP in vCloud Director if you are using a Static IP Pool address. This does NOT change the guest address. You'll have to change it manually in the OS or force re-customization.

$newNetworkName = "MyNewNetwork"

$vm = Get-CIVM MyCIVM

($vm.ExtensionData.Section | where {$_ -is [VMware.VimAutomation.Cloud.Views.NetworkConnectionSection]}).NetworkConnection[0].network = $newNetworkName
($vm.ExtensionData.Section | where {$_ -is [VMware.VimAutomation.Cloud.Views.NetworkConnectionSection]}).updateServerData()

Line 1: The name of the network you want your VM connected to. Line 3: Get the VM. Line 5: This is a long line. It basically says Get the Network Connection OVF section and assign the first NIC to the new network. Line 7: UpdateServerData() This sends the updated changes to vCloud Director. Cool, eh?

(Jake tip: You can use UpdateViewData() to refresh the VM with any data that may have changed. No need to re-'Get' the VM!)

Hot Modify Disk Size

This one is also a bit ugly. I'll try to clean it up later, but it works. ;)

Also, I shouldn't need to tell you that THIS IS DANGEROUS. For giggles, I tried to set it to a lower size. The command threw an exception, but vCloud still displays it at the lower size. I'll update after I check if vSphere actually allowed this (I'd be surprised). If not, it'll be a bug report to VMware. Either way, make sure you are GROWING the disk. Mind the zeros.

$vm = Get-CIVM MyCIVM
$newsize = 10240

(($vm.ExtensionData.Section | where {$_ -is [VMware.VimAutomation.Cloud.Views.OvfVirtualHardwareSection]}).item | where {$_.ResourceType.value -eq 17})[0].hostresource[0].AnyAttr[0]."#text" = $newsize
($vm.ExtensionData.Section | where {$_ -is [VMware.VimAutomation.Cloud.Views.OvfVirtualHardwareSection]}).updateserverdata()

Line 1: Get the VM Line 2: New size in megabytes. Line 4: In plain English...Get the disks from the OVF hardware section, set the first disk's new size to the new value. Line 5: Update server data.

Bonus! Hot Modify Org Name!

The fine chaps over at vcoteam.info mentioned adding the ability to hot change an Org name to the list, which you can't do in the GUI. Watch this. Three lines of code...Ready? You'll need to be a system admin for this one.

$org = Get-Org OrgName
$org.ExtensionData.name = "NewName"
$org.ExtensionData.UpdateServerData()

Line 1: That Line 2: was Line 3: easy.

Of course, if you want the display name changed, you'll have to modify $org.ExtensionData.FullName as well ;)

Have a specific vCloud API request? Let's hear it in the comments!

Cheers! -Jake
comments powered by Disqus