Skip to content

Instantly share code, notes, and snippets.

@grier
Created February 13, 2012 07:07
Show Gist options
  • Save grier/1814534 to your computer and use it in GitHub Desktop.
Save grier/1814534 to your computer and use it in GitHub Desktop.
vSphere API - Edit VM network interface
public static void doChangeNic(String url, String username, String password, String hostname, String vmname, String portGroup)
{
ServiceInstance si = new ServiceInstance(new URL(url), username, password, true);
Folder rootFolder = si.getRootFolder();
VirtualMachine vm = null;
HostSystem host = null;
vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);
host = (HostSystem) new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem", hostname);
if(vm==null || host==null)
{
System.out.println("Host not found");
return;
}
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] nicSpec = getNICDeviceConfigSpec(vm, portGroup);
String result = null;
if(nicSpec!=null)
{
vmConfigSpec.setDeviceChange(nicSpec);
Task task = vm.reconfigVM_Task(vmConfigSpec);
result = task.waitForMe();
}
System.out.println("Successful modified NICS to " + portGroup);
}
private static VirtualDeviceConfigSpec[] getNICDeviceConfigSpec(VirtualMachine vm, String pgname) throws Exception
{
ArrayList<VirtualDeviceConfigSpec> updates = new ArrayList<VirtualDeviceConfigSpec>();
VirtualMachineConfigInfo vmConfigInfo = vm.getConfig();
VirtualDevice [] vds = vmConfigInfo.getHardware().getDevice();
for(int i=0;i<vds.length;i++)
{
if(vds[i] instanceof VirtualEthernetCard)
{
VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
nicSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
//System.out.println("Changing NIC with MAC: " + ((VirtualEthernetCard) vds[i]).getMacAddress());
VirtualEthernetCardNetworkBackingInfo oldbi = (VirtualEthernetCardNetworkBackingInfo) vds[i].getBacking();
VirtualEthernetCardNetworkBackingInfo bi = new VirtualEthernetCardNetworkBackingInfo();
bi.setDeviceName(pgname);
//System.out.println("New network name: " + bi.getDeviceName());
vds[i].setBacking(bi);
nicSpec.setDevice(vds[i]);
updates.add(nicSpec);
}
}
VirtualDeviceConfigSpec[] ret = new VirtualDeviceConfigSpec[updates.size()];
for(int x = 0; x < updates.size(); x++) {
ret[x] = updates.get(x);
}
return ret;
}
@MarcGrimme
Copy link

Do you have by any change another gist on how to add a new NIC. Specially the case when adding the backinginfo. Currently I have with some issues with the VirtualEthernetCardDistributedVirtualPortBackingInfo complaining about the uuid of the distributedVirtualSwitch.uuid. I would be interested to see how you add the backinginfo. Perhaps I'll get something from it ;-) .

Nevertheless thanks for sharing.

Marc.

@alesandreo
Copy link

When I run this code, I get com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo back as the class for getBacking, am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment