Skip to content

Instantly share code, notes, and snippets.

@unicolet
Created October 27, 2012 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unicolet/3963532 to your computer and use it in GitHub Desktop.
Save unicolet/3963532 to your computer and use it in GitHub Desktop.
Script to reconfigure a vSphere vm first network adapter
import java.net.URL
import com.vmware.vim25.*
import com.vmware.vim25.mo.*
/*
Example invocation:
groovy -cp lib/dom4j-1.6.1.jar:lib/vijava50120120518.jar Sample.groovy 1 2 3 websphere "Network adapter 1" DMZ
*/
if(args.length != 6) {
System.out.println("Usage: java <program> <url> <username> <password> <vmname> <name> <newname>");
System.out.println("name - network name");
System.out.println("name - new network name");
System.exit(0);
}
String url = args[0];
String usr = args[1];
String pwd = args[2];
String vmname = args[3];
String name = args[4];
String newname = args[5];
def start = System.currentTimeMillis()
def si = new ServiceInstance(new URL(url), usr, pwd, true)
def end = System.currentTimeMillis()
println "["+url+"] time to login: " + (end-start)
def rootFolder = si.getRootFolder()
VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);
if(vm==null) {
System.out.println("No VM " + vmname + " found");
si.getServerConnection().logout();
return;
}
def networkReference = doesNetworkNameExist(vm, newname)
if(!doesNetworkNameExist(vm, newname)) {
System.out.println("No network " + newname + " found");
si.getServerConnection().logout();
return;
}
String result = null;
VirtualDeviceConfigSpec nicSpec = getNICDeviceConfigSpec(vm, name, newname, networkReference);
if(nicSpec!=null) {
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec[] changes=[nicSpec];
vmConfigSpec.setDeviceChange(changes);
Task task = vm.reconfigVM_Task(vmConfigSpec);
result = task.waitForMe();
if(result==Task.SUCCESS) {
System.out.println("Done with NIC for VM:" + vmname);
} else {
System.out.println("Failed with NIC for VM:" + vmname);
}
} else {
println "Nic not found"
}
si.getServerConnection().logout()
def getNICDeviceConfigSpec(VirtualMachine vm, String name, String newname, ManagedObjectReference networkReference ) throws Exception {
VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
VirtualMachineConfigInfo vmConfigInfo = vm.getConfig();
VirtualDevice [] vds = vmConfigInfo.getHardware().getDevice();
nicSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
for(int i=0;i<vds.length;i++) {
if((vds[i] instanceof VirtualEthernetCard) && (vds[i].getDeviceInfo().getLabel().equalsIgnoreCase(name))) {
VirtualEthernetCardNetworkBackingInfo nicBacking=vds[i].getBacking();
println "Found device, requesting change, old network is: "+nicBacking.getDeviceName()
nicBacking.setDeviceName(newname);
nicBacking.network = networkReference;
nicSpec.setDevice(vds[i]);
return nicSpec;
}
}
return null;
}
def doesNetworkNameExist(VirtualMachine vm, String netName) throws Exception {
VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime();
EnvironmentBrowser envBrowser = vm.getEnvironmentBrowser();
ManagedObjectReference hmor = vmRuntimeInfo.getHost();
HostSystem host = new HostSystem(vm.getServerConnection(), hmor);
ConfigTarget cfg = envBrowser.queryConfigTarget();
VirtualMachineNetworkInfo[] nets = cfg.getNetwork();
for (int i = 0; nets!=null && i < nets.length; i++) {
NetworkSummary netSummary = nets[i].getNetwork();
if (netSummary.isAccessible() &&
netSummary.getName().equalsIgnoreCase(netName)) {
return netSummary.getNetwork();
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment