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; }
/** 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); }
public ManagedObjectReference getMorDataStore(String dataStoreName, ConnectionResources connectionResources, ManagedObjectReference vmMor, VmInputs vmInputs) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ManagedObjectReference dataStore = null; if (isNotBlank(dataStoreName)) { ManagedObjectReference cloneHostMor = getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor); ConfigTarget configTarget = getHostConfigTarget(connectionResources, cloneHostMor); List<VirtualMachineDatastoreInfo> dataStoreInfoList = configTarget.getDatastore(); for (VirtualMachineDatastoreInfo dataStoreInfo : dataStoreInfoList) { if (vmInputs.getCloneDataStore().equals(dataStoreInfo.getDatastore().getName())) { dataStore = getDataStoreRef(vmInputs.getCloneDataStore(), dataStoreInfoList); break; } } if (dataStore == null) { throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_FOUND); } } else { ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); String vmPathName = virtualMachineSummary.getConfig().getVmPathName(); dataStoreName = vmPathName.substring(1, vmPathName.indexOf(Constants.RIGHT_SQUARE_BRACKET)); dataStore = getDataStore(dataStoreName, connectionResources, vmMor); break; } break; } } return dataStore; }
ConfigTarget getHostConfigTarget(ConnectionResources connectionResources, ManagedObjectReference hostMor) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg { ManagedObjectReference environmentBrowserMor = new MorObjectHandler() .getEnvironmentBrowser(connectionResources, ManagedObjectType.ENVIRONMENT_BROWSER.getValue()); ConfigTarget configTarget = connectionResources.getVimPortType().queryConfigTarget(environmentBrowserMor, hostMor); if (configTarget == null) { throw new RuntimeException(ErrorMessages.CONFIG_TARGET_NOT_FOUND_IN_COMPUTE_RESOURCE); } return configTarget; }
/** * Backing info for distributed virtual switch port or portgroup */ private static VirtualEthernetCardDistributedVirtualPortBackingInfo getDistributedPortBackingInfo( CustomProperties props, QueryConfigTargetRequest queryConfigTargetRequest) { DistributedVirtualSwitchPortConnection port = new DistributedVirtualSwitchPortConnection(); String portGroupKey = props.getString(DvsProperties.PORT_GROUP_KEY); String dvsUuid = props.getString(DvsProperties.DVS_UUID); if (StringUtil.isNullOrEmpty(dvsUuid)) { // NSX-V sets the value to a list of dvPortGroupsKeys as the logical switch is // created in a transport zone which could be associated with multiple clusters. // Hence dvPortGroup is created per cluster. The configTarget will filter based on // the cluster where machine is being provisioned. Type listType = new TypeToken<ArrayList<String>>(){}.getType(); final List<String> portGroupIds = Utils.fromJson(portGroupKey, listType); // NSX-V doesn't have UUID information in its API response DistributedVirtualPortgroupInfo info = null; try { ConfigTarget configTarget = queryConfigTargetRequest.getConfigTarget(); info = configTarget.getDistributedVirtualPortgroup() .stream() .filter(d -> { return portGroupIds.contains(d.getPortgroupKey()); }) .findFirst() .orElse(null); } catch (Exception e) { logger.error("getDistributedPortBackingInfo::Failed to get dvportgroup info.", e); } if (info == null) { throw new IllegalArgumentException("getDistributedPortBackingInfo::The port group " + "information is not found for key: " + portGroupKey); } portGroupKey = info.getPortgroupKey(); dvsUuid = info.getSwitchUuid(); } port.setPortgroupKey(portGroupKey); port.setSwitchUuid(dvsUuid); VirtualEthernetCardDistributedVirtualPortBackingInfo backing = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); backing.setPort(port); return backing; }
public ConfigTarget queryConfigTarget(HostSystem host) throws RuntimeFault, RemoteException { return getVimService().queryConfigTarget(getMOR(), host==null? null : host.getMOR()); }
/** * Get {@link ConfigTarget} aka information about physical devices that are used to back * virtual device. */ public ConfigTarget getConfigTarget() throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { return this.vimPortType.queryConfigTarget(this.getEnvironmentBrowserReference(), null); }