Created
July 31, 2019 12:27
-
-
Save skunkie/c9504e7e3b0e43b1edcd37c70a1dba75 to your computer and use it in GitHub Desktop.
VMware vRealize Orchestrator Workflow: Extend Disks of Virtual Machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* For vRO/VRA 7.0+ | |
* | |
* Workflow Inputs: | |
* payload - Properties - vRA Payload | |
* | |
* Workflow Outputs: | |
* virtualMachineAddOrUpdateProperties - Properties | |
*/ | |
var machine = payload.get("machine"); | |
var componentId = payload.get("componentId"); | |
var vCACVmProperties = machine.get("properties"); | |
var hosts = Server.findAllForType("vCACCAFE:VCACHost"); | |
var requestId = vCACVmProperties.get("__Cafe.Root.Request.Id"); | |
var uuid = vCACVmProperties.get("VirtualMachine.Admin.UUID"); | |
// use it to pass updated properties to the payload | |
var virtualMachineAddOrUpdateProperties = payload.virtualMachineAddOrUpdateProperties || new Properties(); | |
// find catalog item request | |
var request; | |
for (var i = 0; i < hosts.length; i++) { | |
request = vCACCAFEEntitiesFinder.getCatalogItemRequest(hosts[i], requestId); | |
if (request !== null) { | |
break; | |
} | |
} | |
// find VC virtual machine | |
var vcVm; | |
var allSdkConnections = VcPlugin.allSdkConnections; | |
for (var i = 0; i < allSdkConnections.length; i++) { | |
var sdkConnection = allSdkConnections[i]; | |
try { | |
vcVm = sdkConnection.searchIndex.findByUuid(null, uuid, true, true); | |
if (vcVm !== null) { | |
break; | |
} | |
} catch(e) { | |
System.debug( | |
"Unable to find VM by BIOS UUID :" + uuid + ", for SDK connection " + sdkConnection.name + ", reason: " + e); | |
continue; | |
} | |
} | |
if (vcVm === null) { | |
throw "Unable to find VM " + machine.get("name") + " in all SDK connections"; | |
} | |
// get all virtual disks of the virtual machine | |
var devices = vcVm.config.hardware.device; | |
var disks = new Array(); | |
for (device in devices) { | |
if (devices[device] instanceof VcVirtualDisk) { | |
disks.push(devices[device]); | |
} | |
} | |
var requestedComponent = request.requestData.get("provider-" + componentId).getValue(); | |
var requestedDisks = new Array(); | |
if (requestedComponent.containsKey("disks")) requestedDisks = requestedComponent.get("disks").getValue(); | |
for (var i = 0; i < disks.length; i++) { | |
var disk = disks[i]; | |
var targetSizeGB = 0; | |
var vCACPropertySizeGB = 0; // size from vCAC Properties | |
var requestedDiskSizeGB = 0; // requested disk size in "disks" | |
var requestedDiskSizeGBFromProperties = 0; // requested disk size in Property "VirtualMachine.DiskN.Size" | |
var requestedDisk = requestedDisks.filter(function(requestedDisk) { | |
if (requestedDisk.getValue().get("userCreated").getValue() === false && | |
requestedDisk.getValue().get("label").getValue() === disk.deviceInfo.label) return true; | |
})[0]; | |
if (requestedDisk !== undefined) { | |
requestedDiskSizeGB = requestedDisk.getValue().get("capacity").getValue(); | |
System.debug(disk.deviceInfo.label + ": found requested disk size " + requestedDiskSizeGB); | |
} | |
// get vCAC property VirtualMachine.DiskN.ExternalID | |
var propertyExternalID = vCACVmProperties.keys.filter(function(key) { | |
return vCACVmProperties.get(key) === disk.backing.uuid; | |
})[0]; | |
// get vCAC property VirtualMachine.DiskN.Size, it is in GB | |
var propertySize = propertyExternalID.replace('ExternalID', 'Size'); | |
if (requestedComponent.containsKey(propertySize)) { | |
requestedDiskSizeGBFromProperties = requestedComponent.get(propertySize).getValue(); | |
System.debug(disk.deviceInfo.label + ": found requested disk size from Properties " + requestedDiskSizeGBFromProperties); | |
} | |
vCACPropertySizeGB = vCACVmProperties.get(propertySize); | |
System.debug(disk.deviceInfo.label + ": found vCAC Property for disk size " + vCACPropertySizeGB); | |
// get the biggest value for targetSizeGB | |
targetSizeGB = [ | |
vCACPropertySizeGB, | |
requestedDiskSizeGBFromProperties, | |
requestedDiskSizeGB].sort(function(a,b) {return b-a})[0]; | |
System.debug(disk.deviceInfo.label + ": target size " + targetSizeGB); | |
var targetSizeKB = targetSizeGB * 1024 * 1024; | |
if (disk.capacityInKB >= targetSizeKB) { | |
System.debug(disk.deviceInfo.label + ": size is greater than or equal to the target size"); | |
continue; | |
} | |
// Set new disk size | |
disk.capacityInKB = targetSizeKB; | |
//Add disk to devicespec and devicespec to configspec | |
var deviceSpec = new VcVirtualDeviceConfigSpec(); | |
deviceSpec.operation = VcVirtualDeviceConfigSpecOperation.edit; | |
deviceSpec.device = disk; | |
var deviceConfigSpecs = new Array(); | |
deviceConfigSpecs.push(deviceSpec); | |
var configSpec = new VcVirtualMachineConfigSpec(); | |
configSpec.deviceChange = deviceConfigSpecs; | |
System.log(disk.deviceInfo.label + ": setting new disk size: " + targetSizeGB); | |
reconfigTask = vcVm.reconfigVM_Task(configSpec); | |
System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(reconfigTask, false, 5); | |
virtualMachineAddOrUpdateProperties.put(propertySize, targetSizeGB); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I have Django based program to talk to IPAM to get IP,Hostname, MAC, VLAN etc. This Django also talk to AD, internal costing tool as well. I am looking for an option to update MAC, Hostname, IP , Mac address type in new version of vRA7vRO. Found your website so promising and thought of asking here.
As of now I am using vRO(7.5) workflow to update properties of VM using payload. Code shared below.
virtualMachineAddOrUpdateProperties = new Properties ();
virtualMachineAddOrUpdateProperties.put('Name', hostname);
virtualMachineAddOrUpdateProperties.put('VirtualMachine.Network0.MacAddressType', 'static');
virtualMachineAddOrUpdateProperties.put('VirtualMachine.Network0.MacAddress', mac);
virtualMachineAddOrUpdateProperties.put('VirtualMachine.Network0.Name', "VLAN"+vlan);
And I have couple of API calls to Django program to get above values. It would be great if you can give me an hint "How do I implement same in vRA7vRO 8.6.2"