Hi,
I have the following output from Onyx that I captured while adding an additional NIC to one of my Guests.
I found how to change the script to make it run in vCO, but I need help to understand how to set or find the following two parameters:
spec.deviceChange[0].device.backing.port.switchUuid = "27 21 1c 50 5d 02 8e 0c-08 a7 b4 37 f0 58 49 c0";
spec.deviceChange[0].device.backing.port.portgroupKey = "dvportgroup-8392";
How can I get vCO to ask me these two parameters from input?
I know that the portgroupKey corresponds to the Network I want to connect the Guest to. But I have no idea how to input my APP1-HSV Network and make vCO translate it into the portgroupKey.
The same is true for the switchUuid parameter.
Please somebody help!!
Here the complete script from Onyx which is already adapted to run in vCO but with switchUuid and portgroupKey not asked from input:
var managedObject = vm;
// ------- ReconfigVM_Task -------
var spec = new VcVirtualMachineConfigSpec();
//spec.changeVersion = "2012-09-14T13:58:04.147363Z";
spec.deviceChange = System.getModule("com.vmware.onyx").array(VcVirtualDeviceConfigSpec, 1);
spec.deviceChange[0] = new VcVirtualDeviceConfigSpec();
spec.deviceChange[0].operation = VcVirtualDeviceConfigSpecOperation.add;
spec.deviceChange[0].device = new VcVirtualVmxnet3();
spec.deviceChange[0].device.key = -100;
spec.deviceChange[0].device.backing = new VcVirtualEthernetCardDistributedVirtualPortBackingInfo();
spec.deviceChange[0].device.backing.port = new VcDistributedVirtualSwitchPortConnection();
spec.deviceChange[0].device.backing.port.switchUuid = "27 21 1c 50 5d 02 8e 0c-08 a7 b4 37 f0 58 49 c0";
spec.deviceChange[0].device.backing.port.portgroupKey = "dvportgroup-8392";
spec.deviceChange[0].device.connectable = new VcVirtualDeviceConnectInfo();
spec.deviceChange[0].device.connectable.startConnected = true;
spec.deviceChange[0].device.connectable.allowGuestControl = true;
spec.deviceChange[0].device.connectable.connected = true;
spec.deviceChange[0].device.controllerKey = 100;
spec.deviceChange[0].device.addressType = "generated";
spec.deviceChange[0].device.wakeOnLanEnabled = true;
task = managedObject.reconfigVM_Task(spec); // VirtualMachine
Thanks in advance
Axel
Hi,
hm, try it this way:
var managedObject = vm;
// ------- ReconfigVM_Task -------
var spec = new VcVirtualMachineConfigSpec();
//spec.changeVersion = "2012-09-14T13:58:04.147363Z";
spec.deviceChange = System.getModule("com.vmware.onyx").array(VcVirtualDeviceConfigSpec, 1);
spec.deviceChange[0] = new VcVirtualDeviceConfigSpec();
spec.deviceChange[0].operation = VcVirtualDeviceConfigSpecOperation.add;
spec.deviceChange[0].device = new VcVirtualVmxnet3();
spec.deviceChange[0].device.key = -100;
spec.deviceChange[0].device.backing = new VcVirtualEthernetCardDistributedVirtualPortBackingInfo();
//spec.deviceChange[0].device.backing.port = new VcDistributedVirtualSwitchPortConnection();
//spec.deviceChange[0].device.backing.port.switchUuid = "27 21 1c 50 5d 02 8e 0c-08 a7 b4 37 f0 58 49 c0";
//spec.deviceChange[0].device.backing.port.portgroupKey = "dvportgroup-8392";
spec.deviceChange[0].device.backing.deviceName = <name of the network>
spec.deviceChange[0].device.connectable = new VcVirtualDeviceConnectInfo();
spec.deviceChange[0].device.connectable.startConnected = true;
spec.deviceChange[0].device.connectable.allowGuestControl = true;
spec.deviceChange[0].device.connectable.connected = true;
spec.deviceChange[0].device.controllerKey = 100;
spec.deviceChange[0].device.addressType = "generated";
spec.deviceChange[0].device.wakeOnLanEnabled = true;
task = managedObject.reconfigVM_Task(spec); // VirtualMachine
Regards
Thomas
Hi Thomas,
thank you for the answer.
If I don't use these 3 lines I get the following error:
[2012-09-24 14:50:31.591] [I] ; nested exception is:
java.io.IOException: java.io.IOException: java.io.IOException: java.io.IOException: java.io.IOException: Non nillable element 'port' is null. (Workflow:H Add NIC / Change VM Config (item0)#22)
And in real life I somehow have to tell the script to connect to a given Network-label. So I need to use two input variables. The first is the Guest that is already done and the second would be the network-label where I don't know how to translate that in something the operator can understand. He definitivly can't understand:
spec.deviceChange[0].device.backing.port.switchUuid = "27 21 1c 50 5d 02 8e 0c-08 a7 b4 37 f0 58 49 c0";
spec.deviceChange[0].device.backing.port.portgroupKey = "dvportgroup-8392";
Axel
Hi,
hmm strange, here is the script i'm using:
//
// ToDo
var spec = new VcVirtualMachineConfigSpec();
//
// workaround, as javascript can not work with dynamic arrays in this case ...
//
var deviceSpec = [];
deviceSpec[0] = new VcVirtualDeviceConfigSpec();
deviceSpec[0].operation = VcVirtualDeviceConfigSpecOperation.add;
deviceSpec[0].device = new VcVirtualVmxnet3();
deviceSpec[0].device.key = -100;
deviceSpec[0].device.backing = new VcVirtualEthernetCardNetworkBackingInfo();
deviceSpec[0].device.backing.deviceName =VMNetwork.name;
deviceSpec[0].device.connectable = new VcVirtualDeviceConnectInfo();
deviceSpec[0].device.connectable.startConnected = true;
deviceSpec[0].device.connectable.allowGuestControl = true;
deviceSpec[0].device.connectable.connected = true;
deviceSpec[0].device.addressType = "generated";
deviceSpec[0].device.wakeOnLanEnabled = false;
spec.deviceChange = deviceSpec;
System.log(spec.deviceChange[0]);
var ConfigTask = VM.reconfigVM_Task(spec); // VirtualMachine
while ( ConfigTask.Info.State == VcTaskInfoState.running ) {
// still running wait 1 second
System.sleep(1000);
}
Regards
Thomas
Try something like this, where network is the portgroup object you want the vm connected to:
I inserted my code in to yours so i haven't tested it but it should work.
var managedObject = vm;
// ------- ReconfigVM_Task -------
var spec = new VcVirtualMachineConfigSpec();
//spec.changeVersion = "2012-09-14T13:58:04.147363Z";
spec.deviceChange = System.getModule("com.vmware.onyx").array(VcVirtualDeviceConfigSpec, 1);
spec.deviceChange[0] = new VcVirtualDeviceConfigSpec();
spec.deviceChange[0].operation = VcVirtualDeviceConfigSpecOperation.add;
spec.deviceChange[0].device = new VcVirtualVmxnet3();
spec.deviceChange[0].device.key = -100;
var backingInfo = new VcVirtualEthernetCardDistributedVirtualPortBackingInfo();
var connection = new VcDistributedVirtualSwitchPortConnection();
connection.PortgroupKey = network.id;
connection.SwitchUuid = network.config.distributedVirtualSwitch.uuid;
backingInfo.port = connection;
spec.deviceChange[0].device.backing = backingInfo;
spec.deviceChange[0].device.connectable = new VcVirtualDeviceConnectInfo();
spec.deviceChange[0].device.connectable.startConnected = true;
spec.deviceChange[0].device.connectable.allowGuestControl = true;
spec.deviceChange[0].device.connectable.connected = true;
spec.deviceChange[0].device.controllerKey = 100;
spec.deviceChange[0].device.addressType = "generated";
spec.deviceChange[0].device.wakeOnLanEnabled = true;
task = managedObject.reconfigVM_Task(spec); // VirtualMachine
oops.. posted it with the wrong account first time around..
Hi,
forget my script, you are working on a distributed switch, the script is for a 'normal' vSwitch.
Regards
Thomas
Also, you can take a look at the createVirtualEthernetCardDvNetworkConfigSpec action, it does it slightly differently so some of your settings may not be identically, plus it does not create Vmxnet3 nics, but it could be modified to do that.
var managedObject = vm;
// ------- ReconfigVM_Task -------
var spec = new VcVirtualMachineConfigSpec();
spec.deviceChange = new Array();
spec.deviceChange[0] = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardDvNetworkConfigSpec( network );
task = managedObject.reconfigVM_Task(spec); // VirtualMachine
Axel,
I use the following code when creating the NIC. I check a couple things first. Since I use both standard and distributed vSwitches, I check to see which type of network I'm selecting, I also check to see if I am assigning a static MAC or using autogenerated.
network1 is distributed port group of type VC:DistributedVirtualPortgroup
deviceConfigSpec = new VcVirtualDeviceConfigSpec(); System.log("Creating virtual NIC") deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.add; deviceConfigSpec.device = new VcVirtualVmxnet2(); deviceConfigSpec.device.key = -48; if(legacyNetworkType==true) { deviceConfigSpec.device.backing = new VcVirtualEthernetCardNetworkBackingInfo(); deviceConfigSpec.device.backing.deviceName = legacyNetwork.name; System.log("Virtual Machine Network: " + legacyNetwork.name); } else { deviceConfigSpec.device.backing = new VcVirtualEthernetCardDistributedVirtualPortBackingInfo(); deviceConfigSpec.device.backing.port = new VcDistributedVirtualSwitchPortConnection(); deviceConfigSpec.device.backing.port.switchUuid = network1.config.distributedVirtualSwitch.uuid; deviceConfigSpec.device.backing.port.portgroupKey = network1.key; System.log("dvSwitch Port Group: " + network1.name); } deviceConfigSpec.device.connectable = new VcVirtualDeviceConnectInfo(); deviceConfigSpec.device.connectable.startConnected = true; deviceConfigSpec.device.connectable.allowGuestControl = true; deviceConfigSpec.device.connectable.connected = true; if ( generateMacAddress1 == true ) { System.log("MAC Address: autogenerated") deviceConfigSpec.device.addressType = "generated"; } else { System.log("MAC Address: " + macAddress1) deviceConfigSpec.device.addressType = "assigned"; deviceConfigSpec.device.macAddress = macAddress1; } deviceConfigSpec.device.wakeOnLanEnabled = true; deviceConfigSpecs[ii++] = deviceConfigSpec;
This is what i ended up with. I wanted to change the network of my VMs with a single NIC at provisioning on my distributed virtual switch. This has vm as VC:VirtualMachine and network as string as inputs.
var spec = new VcVirtualMachineConfigSpec(); // Initialize a Virtual Machine Config Spec first
var myDeviceChange = new Array(); // Create an array to hold all of your changes
var devices = vm.config.hardware.device;
var pgs = VcPlugin.getAllDistributedVirtualPortgroups();
for each (pg in pgs) {
if (pg.name.indexOf(network) > -1) {
portGroup = pg;
System.log("Found Portgroup: " + portGroup.name);
}
}
//Find devices that are VMXNET3 or E1000
for (var i in devices)
{
if (
(devices[i] instanceof VcVirtualVmxnet3) ||
(devices[i] instanceof VcVirtualE1000)
)
{
System.log("The device we are going to modify is: " + devices[i]);
var nicSpec = new VcVirtualDeviceConfigSpec();
nicSpec.operation = VcVirtualDeviceConfigSpecOperation.edit;
nicSpec.device = new VcVirtualVmxnet3();
nicSpec.device.key = devices[i].key;
nicSpec.device.deviceInfo = new VcDescription();
nicSpec.device.deviceInfo.label = devices[i].deviceInfo.label;
nicSpec.device.deviceInfo.summary = devices[i].deviceInfo.summary;
nicSpec.device.backing = new VcVirtualEthernetCardDistributedVirtualPortBackingInfo();
nicSpec.device.backing.port = new VcDistributedVirtualSwitchPortConnection();
nicSpec.device.backing.port.switchUuid = portGroup.config.distributedVirtualSwitch.uuid;
nicSpec.device.backing.port.portgroupKey = portGroup.key;
nicSpec.device.connectable = new VcVirtualDeviceConnectInfo();
nicSpec.device.connectable.startConnected = devices[i].connectable.startConnected;
nicSpec.device.connectable.allowGuestControl = devices[i].connectable.allowGuestControl;
nicSpec.device.connectable.connected = devices[i].connectable.connected;
nicSpec.device.connectable.status = devices[i].connectable.status;
nicSpec.device.controllerKey = devices[i].controllerKey;
nicSpec.device.unitNumber = devices[i].unitNumber;
nicSpec.device.addressType = devices[i].addressType;
nicSpec.device.macAddress = devices[i].macAddress;
nicSpec.device.wakeOnLanEnabled = devices[i].wakeOnLanEnabled;
//Push change spec to device change variable
myDeviceChange.push(nicSpec);
}
}
spec.deviceChange = myDeviceChange
System.log("DeviceChange Spec is: " + spec.deviceChange);
return vm.reconfigVM_Task(spec);
Get inputs / attributes:
dvSwitch, type VC:DistributedVirtualSwitch
dvPortGroup, type VC:DistributedVirtualPortGroup
Set those values to be the option you want from your vSphere vCenter.
Pass the attributes in to your script object
Replace your 2 lines of code with this:
spec.deviceChange[0].device.backing.port.switchUuid = dvSwitch.uuid;
spec.deviceChange[0].device.backing.port.portgroupKey = dvPortGroup.key
And you *should* be good to go!