use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class InstanceClient method mapNetworks.
private List<OvfNetworkMapping> mapNetworks(List<String> ovfNetworkNames, Document ovfDoc, List<NetworkInterfaceStateWithDetails> nics) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
List<OvfNetworkMapping> networks = new ArrayList<>();
if (ovfNetworkNames.isEmpty() || nics.isEmpty()) {
return networks;
}
CustomProperties custProp;
ManagedObjectReference moRef;
NetworkInterfaceStateWithDetails nic = nics.iterator().next();
if (nic.subnet != null) {
custProp = CustomProperties.of(nic.subnet);
} else {
custProp = CustomProperties.of(nic.network);
}
moRef = custProp.getMoRef(CustomProperties.MOREF);
if (moRef == null) {
moRef = this.finder.networkList("*").iterator().next().object;
}
final ManagedObjectReference finalMoRef = moRef;
ovfNetworkNames.forEach(n -> {
OvfNetworkMapping nm = new OvfNetworkMapping();
nm.setName(n);
nm.setNetwork(finalMoRef);
networks.add(nm);
});
return networks;
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class VmOverlay method findPublicIpV4Address.
/**
* If there is a nic with assignPublicIpAddress use the ip associated with that nic
* otherwise rely on previous logic where it tries to guess the "public" IP of a VM. IPv6
* addresses are excluded. It prefer routable
* addresses, then class A, then class B, then class C. Return null if not candidates.
*
* @return
*/
public String findPublicIpV4Address(List<NetworkInterfaceStateWithDetails> nics) {
// check if any of the nics is marked as assignPublicIpAddress
if (nics != null) {
boolean assignPublicIpAddress = false;
NetworkInterfaceStateWithDetails networkInterfaceStateWithDetails = nics.stream().filter(nic -> nic.description != null && nic.description.assignPublicIpAddress != null && nic.description.assignPublicIpAddress == true).findFirst().orElse(null);
if (networkInterfaceStateWithDetails != null) {
assignPublicIpAddress = true;
}
if (assignPublicIpAddress == true) {
String deviceKey = null;
deviceKey = getDeviceKey(networkInterfaceStateWithDetails);
if (deviceKey != null) {
Map<String, List<String>> mapNics2IpAddresses = getMapNic2IpV4Addresses();
if (mapNics2IpAddresses.containsKey(deviceKey)) {
List<String> ips = mapNics2IpAddresses.get(deviceKey);
return guessPublicIpV4Address(ips);
}
}
}
}
return guessPublicIpV4Address(getAllIps());
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class VmOverlayTest method findPublicIpAddressWithNicExternalId.
@Test
public void findPublicIpAddressWithNicExternalId() {
NetworkInterfaceStateWithDetails n1 = new NetworkInterfaceStateWithDetails();
NetworkInterfaceStateWithDetails n2 = new NetworkInterfaceStateWithDetails();
String mac1Address = "00:50:56:8b:54:bd";
String mac2Address = "98:87:fd:9e:ed:6d";
List<NetworkInterfaceStateWithDetails> nics = new ArrayList();
nics.add(n1);
n1.customProperties = new HashMap<>();
n1.customProperties.put(NIC_EXTERNAL_ID, Integer.toString(0));
nics.add(n2);
n2.customProperties = new HashMap<>();
n2.customProperties.put(NIC_EXTERNAL_ID, Integer.toString(0));
String publicIpAddress = this.overlay.findPublicIpV4Address(nics);
Assert.assertTrue("publicIpAddress is null", publicIpAddress != null);
// test with assignPublicIpAddress
n1.description = new NetworkInterfaceDescription();
n1.description.assignPublicIpAddress = true;
publicIpAddress = this.overlay.findPublicIpV4Address(nics);
Assert.assertTrue("publicIpAddress is null", publicIpAddress != null);
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class VmOverlayTest method findPublicIpAddress.
@Test
public void findPublicIpAddress() {
NetworkInterfaceStateWithDetails n1 = new NetworkInterfaceStateWithDetails();
NetworkInterfaceStateWithDetails n2 = new NetworkInterfaceStateWithDetails();
List<NetworkInterfaceStateWithDetails> nics = new ArrayList();
nics.add(n1);
nics.add(n2);
String publicIpAddress = this.overlay.findPublicIpV4Address(nics);
Assert.assertTrue("publicIpAddress is null", publicIpAddress != null);
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class InstanceClient method cloneOvfBasedTemplate.
private ManagedObjectReference cloneOvfBasedTemplate(ManagedObjectReference vmTempl, ManagedObjectReference datastore, ManagedObjectReference folder, ManagedObjectReference resourcePool, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws Exception {
String vmName = this.ctx.child.name;
Map<String, Object> props = this.get.entityProps(vmTempl, VimPath.vm_summary_config_numCpu, VimPath.vm_summary_config_memorySizeMB, VimPath.vm_snapshot, VimPath.vm_config_hardware_device, VimPath.vm_config_vAppConfig_property);
VirtualMachineSnapshotInfo snapshot = (VirtualMachineSnapshotInfo) props.get(VimPath.vm_snapshot);
ArrayOfVirtualDevice devices = (ArrayOfVirtualDevice) props.get(VimPath.vm_config_hardware_device);
VirtualDisk vd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).findFirst().orElse(null);
VirtualSCSIController scsiController = getFirstScsiController(devices);
Integer[] scsiUnit = findFreeScsiUnit(scsiController, devices.getVirtualDevice());
VirtualMachineRelocateDiskMoveOptions diskMoveOption = computeDiskMoveType();
boolean customizeImageDisk = false;
List<VirtualDeviceConfigSpec> newDisks = new ArrayList<>();
VirtualMachineRelocateSpecDiskLocator bootDiskLocator = null;
List<VirtualDisk> vDisks = null;
if (this.bootDisk != null) {
if (vd == null) {
String datastoreName = this.get.entityProp(datastore, VimPath.ds_summary_name);
String path = makePathToVmdkFile("ephemeral_disk", vmName);
String diskName = String.format(VM_PATH_FORMAT, datastoreName, path);
VirtualDeviceConfigSpec hdd = createHdd(scsiController.getKey(), scsiUnit[0], this.bootDisk, diskName, datastore, pbmSpec);
newDisks.add(hdd);
} else {
// strategy
if (this.imageDisks != null && !this.imageDisks.isEmpty()) {
vDisks = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).filter(d -> {
DiskStateExpanded ds = findMatchingImageDiskState(d, this.imageDisks);
return toKb(ds.capacityMBytes) > d.getCapacityInKB() || ds.customProperties != null;
}).collect(Collectors.toList());
if (vDisks.size() > 0) {
diskMoveOption = VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING;
logger.warn("Changing clone strategy to MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING, as there is disk resize requested");
customizeImageDisk = true;
bootDiskLocator = setProvisioningType(vDisks.get(0), datastore, pbmSpec);
}
}
}
}
VirtualCdrom vcd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualCdrom).map(d -> (VirtualCdrom) d).findFirst().orElse(null);
// add a cdrom so that ovf transport works
if (vcd == null) {
VirtualDevice ideController = getFirstIdeController(devices);
int ideUnit = findFreeUnit(ideController, devices.getVirtualDevice());
VirtualDeviceConfigSpec cdrom = createCdrom(ideController, ideUnit);
newDisks.add(cdrom);
} else {
VirtualDeviceConfigSpec cdrom = reconfigureCdrom(vcd);
newDisks.add(cdrom);
}
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
// even though this is a clone, hw config from the compute resource
// is takes precedence
spec.setNumCPUs((int) this.ctx.child.description.cpuCount);
spec.setMemoryMB(toMemoryMb(this.ctx.child.description.totalMemoryBytes));
String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null);
if (gt != null) {
spec.setGuestId(gt);
}
// set ovf environment
ArrayOfVAppPropertyInfo infos = (ArrayOfVAppPropertyInfo) props.get(// this.get.entityProp(vmTempl,
VimPath.vm_config_vAppConfig_property);
// VimPath.vm_config_vAppConfig_property);
populateVAppProperties(spec, infos);
populateCloudConfig(spec, infos);
recordTimestamp(spec.getExtraConfig());
// set the maximum snapshot limit if specified
final String snapshotLimit = CustomProperties.of(this.ctx.child).getString(CustomProperties.SNAPSHOT_MAXIMUM_LIMIT);
recordSnapshotLimit(spec.getExtraConfig(), snapshotLimit);
// add disks one at a time
for (VirtualDeviceConfigSpec newDisk : newDisks) {
spec.getDeviceChange().add(newDisk);
}
// configure network
VirtualPCIController pci = getFirstPciController(devices);
for (NetworkInterfaceStateWithDetails nicWithDetails : this.ctx.nics) {
VirtualDevice nic = createNic(nicWithDetails, pci.getControllerKey());
addDeviceToVm(spec, nic);
}
// remove any networks from the template
devices.getVirtualDevice().stream().filter(d -> VirtualEthernetCard.class.isAssignableFrom(d.getClass())).forEach(d -> addRemoveDeviceFromVm(spec, d));
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
if (pbmSpec != null) {
pbmSpec.stream().forEach(sp -> {
relocSpec.getProfile().add(sp);
});
}
relocSpec.setDatastore(datastore);
relocSpec.setFolder(folder);
relocSpec.setPool(resourcePool);
relocSpec.setDiskMoveType(diskMoveOption.value());
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setLocation(relocSpec);
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
if (snapshot != null) {
cloneSpec.setSnapshot(snapshot.getCurrentSnapshot());
}
cloneSpec.setConfig(spec);
if (bootDiskLocator != null) {
cloneSpec.getLocation().getDisk().add(bootDiskLocator);
}
ManagedObjectReference cloneTask = getVimPort().cloneVMTask(vmTempl, folder, vmName, cloneSpec);
TaskInfo info = waitTaskEnd(cloneTask);
if (info.getState() == TaskInfoState.ERROR) {
return VimUtils.rethrow(info.getError());
}
ManagedObjectReference vmMoref = (ManagedObjectReference) info.getResult();
// Apply boot disk customization if any, if done through full clone.
if (customizeImageDisk) {
ArrayOfVirtualDevice virtualDevices = this.get.entityProp(vmMoref, VimPath.vm_config_hardware_device);
reconfigureBootDisk(vmMoref, getCustomizationConfigSpecs(virtualDevices, this.imageDisks));
}
return vmMoref;
}
Aggregations