Last active
July 17, 2017 19:10
-
-
Save dlinsley/8623cd86ff06c8588f7a2bca16674bd2 to your computer and use it in GitHub Desktop.
Attach existing VMDK disk to vCenter VM
This file contains hidden or 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
// VMware vRealize Orchestrator (vRO) action sample | |
// | |
// Attach existing VMDK disk to vCenter VM | |
// | |
// For vRO/vRA 7.0+ | |
// | |
// Action Inputs: | |
// vcVm - VC:VirtualMachine | |
// diskPath - string | |
// | |
// Return type: void | |
System.log("Attempting to attach " + diskPath + " to " + vcVm.name); | |
// Max number of vmdks attached to a single scsi controller | |
var MAX_NUMBER_ATTACHED_VMDKS = 15; | |
var devices = vcVm.config.hardware.device; | |
var diskFilePath = diskPath; | |
var controllerKey; | |
var unitNumber; | |
var usedUnitNumbers = []; | |
for each (controller in devices) { | |
var isScsi = controller instanceof VcVirtualBusLogicController || controller instanceof VcVirtualLsiLogicController | |
|| controller instanceof VcParaVirtualSCSIController || controller instanceof VcVirtualLsiLogicSASController; | |
if (!isScsi) { | |
continue; | |
} | |
System.log("SCSI controller found: " + controller.deviceInfo.label); | |
for each (device in devices) { | |
if (device.controllerKey == controller.key) { | |
System.log("Device found: '" + device.deviceInfo.label + "' 'SCSI (" + controller.busNumber + ":" + device.unitNumber + ")'"); | |
controllerKey = controller.key; | |
usedUnitNumbers.push(device.unitNumber); | |
} | |
} | |
break; | |
} | |
if (usedUnitNumbers.length >= MAX_NUMBER_ATTACHED_VMDKS) { | |
throw "SCSI controller is full, the VMDK can not be attached!"; | |
} | |
var backing = new VcVirtualDiskFlatVer2BackingInfo(); | |
backing.fileName = diskFilePath; | |
backing.diskMode = VcVirtualDiskMode.persistent; | |
var connectable = new VcVirtualDeviceConnectInfo(); | |
connectable.startConnected = true; | |
connectable.allowGuestControl = false; | |
connectable.connected = true; | |
// Find the first available SCSI id | |
for (i = 0; i < MAX_NUMBER_ATTACHED_VMDKS; i++) { | |
if (usedUnitNumbers.indexOf(i) == -1) { | |
unitNumber = i; | |
System.log("Found available SCSI unit numebr '" + unitNumber + "'"); | |
break; | |
} | |
} | |
var device = new VcVirtualDisk(); | |
device.backing = backing; | |
device.connectable = connectable; | |
device.controllerKey = controllerKey; | |
device.unitNumber = unitNumber; | |
var deviceChange = new VcVirtualDeviceConfigSpec(); | |
deviceChange.operation = VcVirtualDeviceConfigSpecOperation.add; | |
deviceChange.device = device; | |
var deviceChangeArray = [deviceChange]; | |
var spec = new VcVirtualMachineConfigSpec(); | |
spec.deviceChange = deviceChangeArray; | |
var task = vcVm.reconfigVM_Task(spec); | |
System.log("Initiating reconfigure..."); | |
System.getModule("com.vmware.library.vc.basic").vim3WaitTaskEnd(task,true,3); | |
System.log("Reconfigure of VM '" + vcVm.name + "' successful."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment