protected List<CustomizationAdapterMapping> createCustomizationAdapterMappings( VmwareProcessClient vmwareProcessClient, VirtualMachine machine) { List<CustomizationAdapterMapping> nicSettingMap = new ArrayList<CustomizationAdapterMapping>(); // VirtualEthernetCardを取得 VirtualMachineConfigInfo configInfo = machine.getConfig(); for (VirtualDevice device : configInfo.getHardware().getDevice()) { if (device instanceof VirtualEthernetCard) { VirtualEthernetCard virtualEthernetCard = VirtualEthernetCard.class.cast(device); CustomizationAdapterMapping mapping = new CustomizationAdapterMapping(); CustomizationIPSettings settings = new CustomizationIPSettings(); // すべてのNICをDHCPにする CustomizationDhcpIpGenerator dhcpIp = new CustomizationDhcpIpGenerator(); settings.setIp(dhcpIp); mapping.setMacAddress(virtualEthernetCard.getMacAddress()); mapping.setAdapter(settings); nicSettingMap.add(mapping); } } return nicSettingMap; }
protected VirtualEthernetCard editEthernetCards(VirtualMachine machine, String networkName) { // ネットワークアダプタ E1000を使用する VirtualEthernetCard ethernetCard = new VirtualE1000(); // 分散ポートグループ情報取得 DistributedVirtualPortgroupInfo dvPortgroupInfo = getDVPortgroupInfo(machine, networkName); if (dvPortgroupInfo != null) { // 分散ポートグループの場合 VirtualEthernetCardDistributedVirtualPortBackingInfo nicBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); nicBacking.setPort(new DistributedVirtualSwitchPortConnection()); nicBacking.getPort().setPortgroupKey(dvPortgroupInfo.getPortgroupKey()); nicBacking.getPort().setSwitchUuid(dvPortgroupInfo.getSwitchUuid()); ethernetCard.setBacking(nicBacking); } else { // 標準ポートグループの場合 VirtualEthernetCardNetworkBackingInfo backingInfo = new VirtualEthernetCardNetworkBackingInfo(); backingInfo.setDeviceName(networkName); ethernetCard.setBacking(backingInfo); } return ethernetCard; }
protected boolean checkSameNormalNetwork(VirtualEthernetCard ethernetCard1, VirtualEthernetCard ethernetCard2) { if (!(ethernetCard1.getBacking() instanceof VirtualEthernetCardNetworkBackingInfo)) { return false; } if (!(ethernetCard2.getBacking() instanceof VirtualEthernetCardNetworkBackingInfo)) { return false; } VirtualEthernetCardNetworkBackingInfo backingInfo1 = VirtualEthernetCardNetworkBackingInfo.class .cast(ethernetCard1.getBacking()); VirtualEthernetCardNetworkBackingInfo backingInfo2 = VirtualEthernetCardNetworkBackingInfo.class .cast(ethernetCard2.getBacking()); return StringUtils.equals(backingInfo1.getDeviceName(), backingInfo2.getDeviceName()); }
protected boolean checkSameDistributedNetwork(VirtualEthernetCard ethernetCard1, VirtualEthernetCard ethernetCard2) { if (!(ethernetCard1.getBacking() instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo)) { return false; } if (!(ethernetCard2.getBacking() instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo)) { return false; } VirtualEthernetCardDistributedVirtualPortBackingInfo backingInfo1 = VirtualEthernetCardDistributedVirtualPortBackingInfo.class .cast(ethernetCard1.getBacking()); VirtualEthernetCardDistributedVirtualPortBackingInfo backingInfo2 = VirtualEthernetCardDistributedVirtualPortBackingInfo.class .cast(ethernetCard2.getBacking()); return StringUtils.equals(backingInfo1.getPort().getPortgroupKey(), backingInfo2.getPort().getPortgroupKey()); }
static VirtualDeviceConfigSpec createNicSpec(String netName, String nicName) throws Exception { VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); nicSpec.setOperation(VirtualDeviceConfigSpecOperation.add); VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(netName); Description info = new Description(); info.setLabel(nicName); info.setSummary(netName); nic.setDeviceInfo(info); // type: "generated", "manual", "assigned" by VC nic.setAddressType("generated"); nic.setBacking(nicBacking); nic.setKey(0); nicSpec.setDevice(nic); return nicSpec; }
VirtualDeviceConfigSpec getNicSpecs(String fileName, List<VirtualDevice> virtualDevicesList, VirtualDeviceConfigSpecOperation operation, String addressType, Integer key, String parameter, VmInputs vmInputs) { VirtualDeviceConfigSpec nicSpecs = new VirtualDeviceConfigSpec(); VirtualEthernetCard nic; if (Operation.ADD.toString().equalsIgnoreCase(parameter)) { nic = getEth(fileName, addressType, key); return getNicOpSpec(nicSpecs, operation, nic); } else { nic = findVirtualDevice(VirtualEthernetCard.class, virtualDevicesList, vmInputs); if (nic != null) { return getNicOpSpec(nicSpecs, operation, nic); } } throw new RuntimeException("No nic named: [" + vmInputs.getUpdateValue() + "] can be found."); }
public Pair<Integer, VirtualDevice> getNicDeviceIndex(String networkNamePrefix) throws Exception { List<VirtualDevice> nics = getNicDevices(true); int index = 0; String attachedNetworkSummary; String dvPortGroupName; for (VirtualDevice nic : nics) { attachedNetworkSummary = ((VirtualEthernetCard)nic).getDeviceInfo().getSummary(); if (attachedNetworkSummary.startsWith(networkNamePrefix)) { return new Pair<Integer, VirtualDevice>(new Integer(index), nic); } else if (attachedNetworkSummary.endsWith("DistributedVirtualPortBackingInfo.summary") || attachedNetworkSummary.startsWith("DVSwitch")) { dvPortGroupName = getDvPortGroupName((VirtualEthernetCard)nic); if (dvPortGroupName != null && dvPortGroupName.startsWith(networkNamePrefix)) { s_logger.debug("Found a dvPortGroup already associated with public NIC."); return new Pair<Integer, VirtualDevice>(new Integer(index), nic); } } index++; } return new Pair<Integer, VirtualDevice>(new Integer(-1), null); }
public static VirtualDevice prepareNicOpaque(VirtualMachineMO vmMo, VirtualEthernetCardType deviceType, String portGroupName, String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception { assert(vmMo.getRunningHost().hasOpaqueNSXNetwork()); VirtualEthernetCard nic = createVirtualEthernetCard(deviceType); VirtualEthernetCardOpaqueNetworkBackingInfo nicBacking = new VirtualEthernetCardOpaqueNetworkBackingInfo(); nicBacking.setOpaqueNetworkId("br-int"); nicBacking.setOpaqueNetworkType("nsx.network"); nic.setBacking(nicBacking); nic.setAddressType("Manual"); nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart)); nic.setMacAddress(macAddress); nic.setKey(-contextNumber); return nic; }
public static VirtualDevice prepareNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String portGroupName, String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception { VirtualEthernetCard nic = createVirtualEthernetCard(deviceType); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(portGroupName); nicBacking.setNetwork(morNetwork); nic.setBacking(nicBacking); nic.setAddressType("Manual"); nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart)); nic.setMacAddress(macAddress); nic.setKey(-contextNumber); return nic; }
public static VirtualDevice prepareDvNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String dvPortGroupName, String dvSwitchUuid, String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception { VirtualEthernetCard nic = createVirtualEthernetCard(deviceType); final VirtualEthernetCardDistributedVirtualPortBackingInfo dvPortBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); final DistributedVirtualSwitchPortConnection dvPortConnection = new DistributedVirtualSwitchPortConnection(); dvPortConnection.setSwitchUuid(dvSwitchUuid); dvPortConnection.setPortgroupKey(morNetwork.getValue()); dvPortBacking.setPort(dvPortConnection); nic.setBacking(dvPortBacking); nic.setAddressType("Manual"); nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart)); nic.setMacAddress(macAddress); nic.setKey(-contextNumber); return nic; }
public static int getNumberOfNICs(VMwareClient vmw, ManagedObjectReference vmwInstance) throws Exception { logger.debug(""); VirtualMachineConfigInfo configInfo = (VirtualMachineConfigInfo) vmw .getServiceUtil().getDynamicProperty(vmwInstance, "config"); List<VirtualEthernetCard> vmNics = getNetworkAdapter(configInfo); return vmNics.size(); }
/** * Replaces the NICs in the given VM. * * @param vmw * connected VMware client entity * @param paramHandler * entity which holds all properties of the instance * @param vmwInstance * the virtual machine that gets reconfigured */ public static void configureNetworkAdapter(VMwareClient vmw, VirtualMachineConfigSpec vmConfigSpec, VMPropertyHandler paramHandler, ManagedObjectReference vmwInstance) throws Exception { logger.debug(""); VirtualMachineConfigInfo configInfo = (VirtualMachineConfigInfo) vmw .getServiceUtil().getDynamicProperty(vmwInstance, "config"); List<VirtualEthernetCard> vmNics = getNetworkAdapter(configInfo); int numberOfNICs = Integer.parseInt(paramHandler .getServiceSetting(VMPropertyHandler.TS_NUMBER_OF_NICS)); if (numberOfNICs != vmNics.size()) { throw new Exception( "the number of NICs in virtual machine does not match the service parameter. VM: " + configInfo.getName() + " NICs: " + vmNics.size() + " " + VMPropertyHandler.TS_NUMBER_OF_NICS + ": " + numberOfNICs); } for (int i = 1; i <= numberOfNICs; i++) { String newNetworkName = paramHandler.getNetworkAdapter(i); VirtualEthernetCard vmNic = vmNics.get(i - 1); String vmNetworkName = getNetworkName(vmw, vmwInstance, i); if (newNetworkName != null && newNetworkName.length() > 0 && !newNetworkName.equals(vmNetworkName)) { ManagedObjectReference newNetworkRef = getNetworkFromHost(vmw, vmwInstance, newNetworkName); replaceNetworkAdapter(vmConfigSpec, vmNic, newNetworkRef, newNetworkName); } else { connectNIC(vmConfigSpec, vmNic, vmNetworkName); } } }
public static List<VirtualEthernetCard> getNetworkAdapter( VirtualMachineConfigInfo configInfo) { List<VirtualEthernetCard> nics = new ArrayList<VirtualEthernetCard>(); List<VirtualDevice> devices = configInfo.getHardware().getDevice(); for (VirtualDevice vd : devices) { if (vd instanceof VirtualEthernetCard) { nics.add((VirtualEthernetCard) vd); } } return nics; }
public List<VirtualEthernetCard> getNics() { ArrayOfVirtualDevice dev = (ArrayOfVirtualDevice) getOrDefault( VimPath.vm_config_hardware_device, null); if (dev == null) { return Collections.emptyList(); } return dev.getVirtualDevice().stream() .filter(d -> d instanceof VirtualEthernetCard) .map(d -> (VirtualEthernetCard) d) .collect(Collectors.toList()); }
public String getPrimaryMac() { for (VirtualEthernetCard dev : getNics()) { return dev.getMacAddress(); } return null; }
private String getPrimaryIPv4Address(VirtualEthernetCard nic, Map<String, List<String>> nicMACToIPv4Addresses) { if (nicMACToIPv4Addresses == null) { return null; } String macAddress = nic.getMacAddress(); List<String> ipv4Addresses = nicMACToIPv4Addresses.get(macAddress); if (ipv4Addresses != null && ipv4Addresses.size() > 0) { return ipv4Addresses.get(0); } return null; }
private VirtualEthernetCard createNic(NetworkInterfaceStateWithDetails nicWithDetails, Integer controllerKey) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualEthernetCard nic = new VirtualE1000(); nic.setAddressType(VirtualEthernetCardMacType.GENERATED.value()); nic.setKey(-1); nic.setControllerKey(controllerKey); // Currently the network backing information is stored in both places subnet and network // If it were to exist at one state object, then it would reduce complexity further. // Question: Is it acceptable for querying subnet first and network later? Or the order // should be reversed? QueryConfigTargetRequest queryConfigTargetRequest = new QueryConfigTargetRequest(this.get, getVimPort(), this.ctx.computeMoRef); VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(nicWithDetails.subnet, queryConfigTargetRequest); if (deviceBackingInfo == null) { deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(nicWithDetails.network, queryConfigTargetRequest); } nic.setBacking(deviceBackingInfo); return nic; }
protected List<VirtualEthernetCard> createEthernetCards(VmwareProcessClient vmwareProcessClient, Long instanceNo, VirtualMachine machine) { Instance instance = instanceDao.read(instanceNo); List<VmwareNetwork> vmwareNetworks = vmwareNetworkDao.readByFarmNo(instance.getFarmNo()); // ネットワーク名の取得 PlatformVmware platformVmware = platformVmwareDao.read(instance.getPlatformNo()); String publicNetworkName = platformVmware.getPublicNetwork(); String privateNetworkName = platformVmware.getPrivateNetwork(); for (VmwareNetwork vmwareNetwork : vmwareNetworks) { if (BooleanUtils.isTrue(vmwareNetwork.getPublicNetwork())) { publicNetworkName = vmwareNetwork.getNetworkName(); } else { privateNetworkName = vmwareNetwork.getNetworkName(); } } // イーサネット設定の作成 List<VirtualEthernetCard> ethernetCards = new ArrayList<VirtualEthernetCard>(); // Public側イーサネット設定 if (StringUtils.isNotEmpty(publicNetworkName)) { VirtualEthernetCard publicEthernetCard = editEthernetCards(machine, publicNetworkName); ethernetCards.add(publicEthernetCard); } // Private側イーサネット設定 VirtualEthernetCard privateEthernetCard = editEthernetCards(machine, privateNetworkName); ethernetCards.add(privateEthernetCard); return ethernetCards; }
protected Map<String, String> createPublicNetworkData(Long instanceNo, VirtualEthernetCard ethernetCard) { VmwareAddress vmwareAddress = vmwareAddressDao.readByInstanceNo(instanceNo); if (vmwareAddress == null) { // VmwareAddressがない場合は動的IP return createDhcpNetworkData(ethernetCard); } if (BooleanUtils.isTrue(vmwareAddress.getEnabled())) { // VmwareAddressが有効な場合は静的IP return createStaticNetworkData(ethernetCard, vmwareAddress); } else { // VmwareAddressが無効な場合は動的IP return createDhcpNetworkData(ethernetCard); } }
protected Map<String, String> createStaticNetworkData(VirtualEthernetCard ethernetCard, VmwareAddress vmwareAddress) { Map<String, String> map = new HashMap<String, String>(); String macAddress = ethernetCard.getMacAddress().toUpperCase(); map.put("BootProto", "static"); map.put("Mac", macAddress); map.put("IP", vmwareAddress.getIpAddress()); map.put("Netmask", vmwareAddress.getSubnetMask()); map.put("Gateway", vmwareAddress.getDefaultGateway()); return map; }
protected static String getVirtualMachineMacAddress( VirtualMachineConfigInfo vmConfigInfo, DistributedVirtualPortgroup portGroup) { VirtualDevice devices[] = vmConfigInfo.getHardware().getDevice(); for (VirtualDevice device : devices) { // Assuming only one interface if (device instanceof VirtualEthernetCard) { VirtualDeviceBackingInfo backingInfo = device.getBacking(); if (backingInfo == null) continue; // Is it backed by the distributed virtual port group? if (backingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) { VirtualEthernetCardDistributedVirtualPortBackingInfo dvpBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backingInfo; if ((dvpBackingInfo.getPort() == null) || (dvpBackingInfo.getPort().getPortgroupKey() == null)) continue; if (dvpBackingInfo.getPort().getPortgroupKey(). equals(portGroup.getKey())) { String vmMac = ((VirtualEthernetCard) device). getMacAddress(); return vmMac; } } } } s_logger.error("dvPg: " + portGroup.getName() + " vmConfig: " + vmConfigInfo + " MAC Address NOT found"); return null; }
private VirtualEthernetCard getEth(String fileName, String addressType, Integer key) { VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(fileName); VirtualEthernetCard nic = new VirtualPCNet32(); nic.setBacking(nicBacking); nic.setAddressType(addressType); nic.setKey(key); return nic; }
private VirtualDeviceConfigSpec getNicOpSpec(VirtualDeviceConfigSpec nicSpecs, VirtualDeviceConfigSpecOperation operation, VirtualEthernetCard nic) { nicSpecs.setOperation(operation); nicSpecs.setDevice(nic); return nicSpecs; }
private List<VirtualDevice> getNicDevices(boolean sorted) throws Exception { List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); List<VirtualDevice> nics = new ArrayList<VirtualDevice>(); if (devices != null) { for (VirtualDevice device : devices) { if (device instanceof VirtualEthernetCard) { nics.add(device); } } } if (sorted) { Collections.sort(nics, new Comparator<VirtualDevice>() { @Override public int compare(VirtualDevice arg0, VirtualDevice arg1) { int unitNumber0 = arg0.getUnitNumber() != null ? arg0.getUnitNumber().intValue() : -1; int unitNumber1 = arg1.getUnitNumber() != null ? arg1.getUnitNumber().intValue() : -1; if (unitNumber0 < unitNumber1) return -1; else if (unitNumber0 > unitNumber1) return 1; return 0; } }); } return nics; }
public String getDvPortGroupName(VirtualEthernetCard nic) throws Exception { VirtualEthernetCardDistributedVirtualPortBackingInfo dvpBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo)nic.getBacking(); DistributedVirtualSwitchPortConnection dvsPort = dvpBackingInfo.getPort(); String dvPortGroupKey = dvsPort.getPortgroupKey(); ManagedObjectReference dvPortGroupMor = new ManagedObjectReference(); dvPortGroupMor.setValue(dvPortGroupKey); dvPortGroupMor.setType("DistributedVirtualPortgroup"); return (String)_context.getVimClient().getDynamicProperty(dvPortGroupMor, "name"); }
private VirtualDevice findVirtualNicDevice(VirtualMachineMO vmMo, String mac) throws Exception { VirtualDevice[] nics = vmMo.getNicDevices(); for (VirtualDevice nic : nics) { if (nic instanceof VirtualEthernetCard) { if (((VirtualEthernetCard)nic).getMacAddress().equals(mac)) return nic; } } return null; }
private void tearDownVm(VirtualMachineMO vmMo) throws Exception { if (vmMo == null) return; boolean hasSnapshot = false; hasSnapshot = vmMo.hasSnapshot(); if (!hasSnapshot) vmMo.tearDownDevices(new Class<?>[] {VirtualDisk.class, VirtualEthernetCard.class}); else vmMo.tearDownDevices(new Class<?>[] {VirtualEthernetCard.class}); vmMo.ensureScsiDeviceController(); }
private ManagedObjectReference deployOvf(URI ovfUri) throws Exception { OvfDeployer deployer = new OvfDeployer(this.connection); CustomProperties cust = CustomProperties.of(this.ctx.child.description); URI archiveUri = cust.getUri(OvfParser.PROP_OVF_ARCHIVE_URI); if (archiveUri != null) { logger.info("Prefer ova {} uri to ovf {}", archiveUri, ovfUri); OvfRetriever retriever = deployer.getRetriever(); ovfUri = retriever.downloadIfOva(archiveUri); } ManagedObjectReference folder = getVmFolder(); List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk); ManagedObjectReference ds = getDataStoreForDisk(this.bootDisk, pbmSpec); ManagedObjectReference resourcePool = getResourcePool(); String vmName = "pmt-" + deployer.getRetriever().hash(ovfUri); GetMoRef get = new GetMoRef(this.connection); ManagedObjectReference vm = findTemplateByName(vmName, get); if (vm == null) { String config = cust.getString(OvfParser.PROP_OVF_CONFIGURATION); Lock lock = getLock(vmName); lock.lock(); try { vm = findTemplateByName(vmName, get); if (vm == null) { OvfParser parser = new OvfParser(); Document ovfDoc = parser.retrieveDescriptor(ovfUri); List<OvfNetworkMapping> networks = mapNetworks(parser.extractNetworks(ovfDoc), ovfDoc, this.ctx.nics); vm = deployer.deployOvf(ovfUri, getHost(), folder, vmName, networks, ds, Collections.emptyList(), config, resourcePool); logger.info("Removing NICs from deployed template: {} ({})", vmName, vm.getValue()); ArrayOfVirtualDevice devices = get.entityProp(vm, VimPath.vm_config_hardware_device); if (devices != null) { VirtualMachineConfigSpec reconfig = new VirtualMachineConfigSpec(); for (VirtualDevice device : devices.getVirtualDevice()) { if (device instanceof VirtualEthernetCard) { VirtualDeviceConfigSpec spec = new VirtualDeviceConfigSpec(); spec.setDevice(device); spec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE); reconfig.getDeviceChange().add(spec); } } ManagedObjectReference reconfigTask = getVimPort() .reconfigVMTask(vm, reconfig); VimUtils.waitTaskEnd(this.connection, reconfigTask); } ManagedObjectReference snapshotTask = getVimPort() .createSnapshotTask(vm, "initial", null, false, false); VimUtils.waitTaskEnd(this.connection, snapshotTask); } } catch (Exception e) { logger.warn("Error deploying Ovf for template [" + vmName + "],reason:", e); vm = awaitVM(vmName, folder, get); } finally { lock.unlock(); } } if (!isSameDatastore(ds, vm, get)) { // make sure the original VM template is ready Object snapshot = get.entityProp(vm, VimPath.vm_snapshot); if (snapshot == null) { vm = awaitVM(vmName, folder, get); } vm = replicateVMTemplate(resourcePool, ds, pbmSpec, folder, vmName, vm, get); } return cloneOvfBasedTemplate(vm, ds, folder, resourcePool, pbmSpec); }
protected Map<String, String> createDhcpNetworkData(VirtualEthernetCard ethernetCard) { Map<String, String> map = new HashMap<String, String>(); map.put("BootProto", "dhcp"); map.put("Mac", ethernetCard.getMacAddress().toUpperCase()); return map; }
static VirtualDeviceConfigSpec getNICDeviceConfigSpec( VirtualMachine vm, String op, String name) throws Exception { VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); VirtualMachineConfigInfo vmConfigInfo = vm.getConfig(); if("add".equalsIgnoreCase(op) && doesNetworkNameExist(vm, name)) { nicSpec.setOperation(VirtualDeviceConfigSpecOperation.add); VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(name); nic.setAddressType("generated"); nic.setBacking(nicBacking); nic.setKey(4); nicSpec.setDevice(nic); return nicSpec; } else if("remove".equalsIgnoreCase(op)) { VirtualDevice [] vds = vmConfigInfo.getHardware().getDevice(); nicSpec.setOperation( VirtualDeviceConfigSpecOperation.remove); for(int i=0;i<vds.length;i++) { if((vds[i] instanceof VirtualEthernetCard) && (vds[i].getDeviceInfo().getLabel().equalsIgnoreCase( name))) { nicSpec.setDevice(vds[i]); return nicSpec; } } } return null; }
private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final IpAddressTO ipAddressTO) throws Exception { // TODO : probably need to set traffic shaping Pair<ManagedObjectReference, String> networkInfo = null; VirtualSwitchType vSwitchType = VirtualSwitchType.StandardVirtualSwitch; if (_publicTrafficInfo != null) { vSwitchType = _publicTrafficInfo.getVirtualSwitchType(); } /** FIXME We have no clue which network this nic is on and that means that we can't figure out the BroadcastDomainType * so we assume that it's VLAN for now */ if (VirtualSwitchType.StandardVirtualSwitch == vSwitchType) { networkInfo = HypervisorHostHelper.prepareNetwork(_publicTrafficInfo.getVirtualSwitchName(), "cloud.public", vmMo.getRunningHost(), vlanId, ipAddressTO.getNetworkRate(), null, _opsTimeout, true, BroadcastDomainType.Vlan, null, null); } else { networkInfo = HypervisorHostHelper.prepareNetwork(_publicTrafficInfo.getVirtualSwitchName(), "cloud.public", vmMo.getRunningHost(), vlanId, null, ipAddressTO.getNetworkRate(), null, _opsTimeout, vSwitchType, _portsPerDvPortGroup, null, false, BroadcastDomainType.Vlan, _vsmCredentials, null); } int nicIndex = allocPublicNicIndex(vmMo); try { VirtualDevice[] nicDevices = vmMo.getNicDevices(); VirtualEthernetCard device = (VirtualEthernetCard)nicDevices[nicIndex]; if (VirtualSwitchType.StandardVirtualSwitch == vSwitchType) { VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(networkInfo.second()); nicBacking.setNetwork(networkInfo.first()); device.setBacking(nicBacking); } else { HostMO hostMo = vmMo.getRunningHost(); DatacenterMO dataCenterMo = new DatacenterMO(hostMo.getContext(), hostMo.getHyperHostDatacenter()); device.setBacking(dataCenterMo.getDvPortBackingInfo(networkInfo)); } VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1]; VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec(); deviceConfigSpec.setDevice(device); deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT); vmConfigSpec.getDeviceChange().add(deviceConfigSpec); if (!vmMo.configureVm(vmConfigSpec)) { throw new Exception("Failed to configure devices when plugPublicNic"); } } catch (Exception e) { // restore allocation mask in case of exceptions String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); int nicMasks = Integer.parseInt(nicMasksStr); nicMasks &= ~(1 << nicIndex); vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); throw e; } }