Hi all, having some issues with the cloning of a template and modifying the existing virtual network device on the way through. I am getting the error "The operation is not allowed in the current state"
I am using ApplyStorageDrsRecommendation_Task due to us using SDRS clusters - wondering if there is something specific here i need to do differently when not using the CloneVM task?
Basic code for changing the backing below;
The configSpec object is a VirtualMachineConfigSpec which is attached to the VirtualMachineCloneSpec.config
I am finding the "networkDevice" object by iterating the VirtualMachineConfigInfo.hardware.device - FYI getting this via (VirtualMachineConfigInfo)cb.getServiceUtil().GetDynamicProperty(vmRef, "config");, vmRef is found via _service.FindByInventoryPath which is the template path.
VirtualDeviceConfigSpec[] devSpec = new VirtualDeviceConfigSpec[0];
configSpec.deviceChange = new VirtualDeviceConfigSpec[1];
configSpec.deviceChange[0] = new VirtualDeviceConfigSpec();
configSpec.deviceChange[0].operation = VirtualDeviceConfigSpecOperation.edit;
configSpec.deviceChange[0].device = networkDevice;
VirtualEthernetCardDistributedVirtualPortBackingInfo nicBack = new VirtualEthernetCardDistributedVirtualPortBackingInfo();
nicBack.port = new DistributedVirtualSwitchPortConnection();
nicBack.port.switchUuid = dvsUUID;
nicBack.port.portgroupKey= pgKey;
configSpec.deviceChange[0].device.backing = nicBack;
configSpec.deviceChange[0].device.connectable = new VirtualDeviceConnectInfo();
configSpec.deviceChange[0].device.connectable.startConnected = true;
configSpec.deviceChange[0].device.connectable.allowGuestControl = true;
configSpec.deviceChange[0].device.connectable.connected = true;
Am i meant to be adding the device changes to something else such as the StoragePlacementResult?
I have also tried a simple remove of the NIC also which gives the same not allowed in current state error.
Any help/direction/things to trial would be much appreciated!
Ben,
You do not have to add the device change to the storageplacementspec, where you have it in the configspec is correct.
I have a program that does almost exactly what you are doing, it clones a vm and and edit's some nic properties before cloning and it works well..
One thing I I'm doing different is instantiating a new network adapter before I change the properties.
Something like this:
configSpec.deviceChange[0].operation = VirtualDeviceConfigSpecOperation.edit;
configSpec.deviceChange[0].device = new VirtualVmxnet3();
configSpec.deviceChange[0].device = networkdevice ***** this assumes your device type is vmxnet3
VirtualEthernetCardDistributedVirtualPortBackingInfo nicBack = new VirtualEthernetCardDistributedVirtualPortBackingInfo();
nicBack.port = new DistributedVirtualSwitchPortConnection();
nicBack.port.switchUuid = dvsUUID;
nicBack.port.portgroupKey= pgKey;
configSpec.deviceChange[0].device.backing = nicBack;
Let me know if that helps at all.
Thanks for the reply - odd still getting the same error after initializing a new VMXNet3 which i am using btw - i wonder if it is something to do with the template setup/state as opposed to incorrect code as such - ideas?
Ok so the next thing I was going to comment on was the template state.
The only time i have seen the error you reference "The operation is not allowed in the current state" was when i was deploying from a template but someone was doing maintanence on it, so it was turned on and therefore not a template. I would double check the state of the template becuase this error could be referencing that the operation (cloning) is not allowed in the current state (not a template).
That is what i thought too but template seems fine and the GUI can deploy (and make "experimental" hardware changes) ok. I have parked this for the moment to give me back some sanity! Just doing a post deployment reconfiguration - works perfectly initially is not connected then the reconfig enables it