public DistributedVirtualPortgroupInfo getDVPortgroupInfo(VirtualMachine machine, String networkName) { HostSystem host = new HostSystem(machine.getServerConnection(), machine.getRuntime().getHost()); ComputeResource computeResource = (ComputeResource) host.getParent(); EnvironmentBrowser envBrowser = computeResource.getEnvironmentBrowser(); DistributedVirtualPortgroupInfo dvPortgroupInfo = null; try { ConfigTarget configTarget = envBrowser.queryConfigTarget(host); // 分散ポートグループの場合 if (configTarget.getDistributedVirtualPortgroup() != null) { // 分散ポートグループ情報を取得 dvPortgroupInfo = findDVPortgroupInfo(configTarget.getDistributedVirtualPortgroup(), networkName); } } catch (RemoteException e) { throw new RuntimeException(e); } return dvPortgroupInfo; }
private List<String> getValidCdromOnHost() throws InvalidProperty, RuntimeFault, RemoteException { List<String> result = new ArrayList<String>(); EnvironmentBrowser envBrower = vm.getEnvironmentBrowser(); ConfigTarget configTarget; try { configTarget = envBrower.queryConfigTarget(null); } catch (Exception ex) { throw new RuntimeException("Error in getting Cdrom devices from host."); } if(configTarget != null && configTarget.cdRom != null) { for(VirtualMachineCdromInfo cdromInfo : configTarget.cdRom) { result.add(cdromInfo.name); } } return result; }
static boolean 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(host); 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 true; } } return false; }
static DatastoreSummary findDatastoreSummary(VirtualMachine vm, String dsName) throws Exception { DatastoreSummary dsSum = null; VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime(); EnvironmentBrowser envBrowser = vm.getEnvironmentBrowser(); ManagedObjectReference hmor = vmRuntimeInfo.getHost(); if(hmor == null) { System.out.println("No Datastore found"); return null; } ConfigTarget configTarget = envBrowser.queryConfigTarget(new HostSystem(vm.getServerConnection(), hmor)); VirtualMachineDatastoreInfo[] dis = configTarget.getDatastore(); for (int i=0; dis!=null && i<dis.length; i++) { dsSum = dis[i].getDatastore(); if (dsSum.isAccessible() && dsName.equals(dsSum.getName())) { break; } } return dsSum; }
static VirtualDevice[] getDefaultDevices(VirtualMachine vm) throws Exception { VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime(); EnvironmentBrowser envBrowser = vm.getEnvironmentBrowser(); ManagedObjectReference hmor = vmRuntimeInfo.getHost(); VirtualMachineConfigOption cfgOpt = envBrowser.queryConfigOption(null, new HostSystem(vm.getServerConnection(), hmor)); VirtualDevice[] defaultDevs = null; if (cfgOpt != null) { defaultDevs = cfgOpt.getDefaultDevice(); if (defaultDevs == null) { throw new Exception("No Datastore found in ComputeResource"); } } else { throw new Exception("No VirtualHardwareInfo found in ComputeResource"); } return defaultDevs; }
/** Create a new virtual network adapter on the VM * Your MAC address should start with 00:50:56 */ public void createNetworkAdapter(VirtualNetworkAdapterType type, String networkName, String macAddress, boolean wakeOnLan, boolean startConnected) throws InvalidProperty, RuntimeFault, RemoteException, InterruptedException { VirtualMachinePowerState powerState = vm.getRuntime().getPowerState(); String vmVerStr = vm.getConfig().getVersion(); int vmVer = Integer.parseInt(vmVerStr.substring(vmVerStr.length()-2)); if((powerState == VirtualMachinePowerState.suspended) || (powerState == VirtualMachinePowerState.suspended && vmVer < 7)) { throw new InvalidPowerState(); } HostSystem host = new HostSystem(vm.getServerConnection(), vm.getRuntime().getHost()); ComputeResource cr = (ComputeResource) host.getParent(); EnvironmentBrowser envBrowser = cr.getEnvironmentBrowser(); ConfigTarget configTarget = envBrowser.queryConfigTarget(host); VirtualMachineConfigOption vmCfgOpt = envBrowser.queryConfigOption(null, host); type = validateNicType(vmCfgOpt.getGuestOSDescriptor(), vm.getConfig().getGuestId(), type); VirtualDeviceConfigSpec nicSpec = createNicSpec(type, networkName, macAddress, wakeOnLan, startConnected, configTarget); VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); vmConfigSpec.setDeviceChange(new VirtualDeviceConfigSpec []{nicSpec}); Task task = vm.reconfigVM_Task(vmConfigSpec); task.waitForTask(200, 100); }