use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class VmOverlayTest method findPublicIpAddressWithMacAddresses.
@Test
public void findPublicIpAddressWithMacAddresses() {
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_MAC_ADDRESS, mac1Address);
nics.add(n2);
n2.customProperties = new HashMap<>();
n2.customProperties.put(NIC_MAC_ADDRESS, mac2Address);
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);
Assert.assertTrue("publicIpAddress is null", publicIpAddress.equals("192.168.1.10"));
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class VSphereAdapterInstanceService method createUpdateIPOperationsForComputeAndNics.
private List<Operation> createUpdateIPOperationsForComputeAndNics(String computeLink, String ip, Map<String, List<String>> ipV4Addresses, ProvisionContext ctx) {
List<Operation> updateIpAddressOperations = new ArrayList<>();
if (ip != null) {
ComputeState state = new ComputeState();
state.address = ip;
// update compute
Operation updateIpAddress = Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), computeLink)).setBody(state);
updateIpAddressOperations.add(updateIpAddress);
}
if (ipV4Addresses != null) {
int sizeIpV4Addresses = ipV4Addresses.size();
for (NetworkInterfaceStateWithDetails nic : ctx.nics) {
String deviceKey = null;
deviceKey = VmOverlay.getDeviceKey(nic);
if (deviceKey == null && nic.deviceIndex < sizeIpV4Addresses) {
deviceKey = Integer.toString(nic.deviceIndex);
}
if (deviceKey != null) {
List<String> ipsV4 = ipV4Addresses.containsKey(deviceKey) ? ipV4Addresses.get(deviceKey) : Collections.emptyList();
if (ipsV4.size() > 0) {
NetworkInterfaceState patchNic = new NetworkInterfaceState();
// if nic has multiple ip addresses for ipv4 only pick 1st ip address
patchNic.address = ipsV4.get(0);
Operation updateAddressNetWorkInterface = Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), nic.documentSelfLink)).setBody(patchNic);
updateIpAddressOperations.add(updateAddressNetWorkInterface);
} else {
log(Level.WARNING, "Address is not going to be updated in network " + "interface state: [%], deviceKey: [%s] was not " + "found in " + "ipV4Addresses: " + "[%s]", nic.documentSelfLink, deviceKey, ipV4Addresses.keySet());
}
} else {
log(Level.WARNING, "Address is not going to be updated in network interface " + "state: [%s] deviceKey is null", nic.documentSelfLink);
}
}
}
return updateIpAddressOperations;
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class VSphereAdapterInstanceService method handleCreateInstance.
private void handleCreateInstance(ProvisionContext ctx) {
ctx.pool.submit(ctx.getAdapterManagementReference(), ctx.vSphereCredentials, (connection, ce) -> {
if (ctx.fail(ce)) {
return;
}
try {
InstanceClient client = new InstanceClient(connection, ctx);
ComputeState state;
if (ctx.templateMoRef != null) {
state = client.createInstanceFromTemplate(ctx.templateMoRef);
} else if (ctx.image != null) {
ManagedObjectReference moRef = CustomProperties.of(ctx.image).getMoRef(CustomProperties.MOREF);
if (moRef != null) {
// the image is backed by a template VM
state = client.createInstanceFromTemplate(moRef);
} else {
// library item
state = client.createInstanceFromLibraryItem(ctx.image);
}
} else if (ctx.snapshotMoRef != null) {
state = client.createInstanceFromSnapshot();
} else {
state = client.createInstance();
}
if (state == null) {
// assume they will patch the task if they have provisioned the vm
return;
}
// populate state, MAC address being very important
VmOverlay vmOverlay = client.enrichStateFromVm(state);
Operation[] finishTask = new Operation[1];
for (NetworkInterfaceStateWithDetails nic : ctx.nics) {
// request guest customization while vm of powered off
SubnetState subnet = nic.subnet;
if (subnet != null && nic.description != null && nic.description.assignment == IpAssignment.STATIC) {
CustomizationClient cc = new CustomizationClient(connection, ctx.child, vmOverlay.getGuestId());
CustomizationSpec template = new CustomizationSpec();
cc.customizeNic(vmOverlay.getPrimaryMac(), ctx.child.hostName, nic.address, subnet, template);
cc.customizeDns(subnet.dnsServerAddresses, subnet.dnsSearchDomains, template);
ManagedObjectReference task = cc.customizeGuest(client.getVm(), template);
TaskInfo taskInfo = VimUtils.waitTaskEnd(connection, task);
if (taskInfo.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(taskInfo.getError());
}
}
}
// power on machine before enrichment
if (ctx.child.powerState == PowerState.ON) {
new PowerStateClient(connection).changePowerState(client.getVm(), PowerState.ON, null, 0);
state.powerState = PowerState.ON;
Operation op = ctx.mgr.createTaskPatch(TaskStage.FINISHED);
Boolean awaitIp = CustomProperties.of(ctx.child).getBoolean(ComputeProperties.CUSTOM_PROP_COMPUTE_AWAIT_IP, true);
if (awaitIp) {
Runnable runnable = createCheckForIpTask(ctx.pool, op, client.getVm(), connection.createUnmanagedCopy(), ctx.child.documentSelfLink, ctx);
ctx.pool.schedule(runnable, IP_CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);
} else {
finishTask[0] = op;
}
} else {
// only finish the task without waiting for IP
finishTask[0] = ctx.mgr.createTaskPatch(TaskStage.FINISHED);
}
updateNicsAfterProvisionSuccess(vmOverlay.getNics(), ctx);
updateDiskLinksAfterProvisionSuccess(state, vmOverlay.getDisks(), ctx);
state.lifecycleState = LifecycleState.READY;
// Find the host link where the computed is provisioned and patch the
// compute state.
queryHostDocumentAndUpdateCompute(ctx, vmOverlay.getHost()).thenCompose(computeState -> {
ComputeState hostState = computeState.iterator().next();
CustomProperties.of(state).put(VC_UUID, CustomProperties.of(hostState).getString(VC_UUID)).put(DATACENTER_SELF_LINK, CustomProperties.of(hostState).getString(DATACENTER_SELF_LINK)).put(COMPUTE_HOST_LINK_PROP_NAME, hostState.documentSelfLink);
return createComputeResourcePatch(state, ctx.computeReference);
}).whenComplete((o, e) -> {
if (e != null) {
ctx.fail(e);
return;
}
if (finishTask.length > 0) {
finishTask[0].sendWith(this);
}
});
} catch (Exception e) {
ctx.fail(e);
}
});
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class InstanceClient method customizeAfterClone.
private void customizeAfterClone() throws Exception {
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
// is takes precedence
if (this.ctx.child.description.cpuCount > 0) {
spec.setNumCPUs((int) this.ctx.child.description.cpuCount);
}
String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null);
if (gt != null) {
spec.setGuestId(gt);
}
if (this.ctx.child.description.totalMemoryBytes > 0) {
spec.setMemoryMB(toMemoryMb(this.ctx.child.description.totalMemoryBytes));
}
recordTimestamp(spec.getExtraConfig());
// set ovf environment
ArrayOfVAppPropertyInfo infos = this.get.entityProp(this.vm, VimPath.vm_config_vAppConfig_property);
populateCloudConfig(spec, infos);
// remove nics and attach to proper networks if nics are configured
ArrayOfVirtualDevice devices = null;
if (this.ctx.nics != null && this.ctx.nics.size() > 0) {
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualEthernetCard).forEach(nic -> {
VirtualDeviceConfigSpec removeNicChange = new VirtualDeviceConfigSpec();
removeNicChange.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
removeNicChange.setDevice(nic);
spec.getDeviceChange().add(removeNicChange);
});
for (NetworkInterfaceStateWithDetails niState : this.ctx.nics) {
VirtualDevice nic = createNic(niState, null);
addDeviceToVm(spec, nic);
}
}
// Find whether it has HDD disk
if (this.imageDisks != null && !this.imageDisks.isEmpty()) {
// Iterate over each of the VirtualDisk and reconfigure if needed
if (devices == null) {
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
}
spec.getDeviceChange().addAll(getCustomizationConfigSpecs(devices, this.imageDisks));
}
// set the maximum snapshot limit if specified
final String snapshotLimit = CustomProperties.of(this.ctx.child).getString(CustomProperties.SNAPSHOT_MAXIMUM_LIMIT);
recordSnapshotLimit(spec.getExtraConfig(), snapshotLimit);
ManagedObjectReference task = getVimPort().reconfigVMTask(this.vm, spec);
TaskInfo info = waitTaskEnd(task);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
// If there are any data disks then attach then to the VM
if (this.dataDisks != null && !this.dataDisks.isEmpty()) {
attachDisks(this.dataDisks, false);
}
// If there are any external disks, then attach then to the VM
attachExternalDisks();
}
use of com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails in project photon-model by vmware.
the class InstanceClient method buildVirtualMachineConfigSpec.
/**
* Creates a spec used to create the VM.
*
* @param datastoreName
* @return
* @throws InvalidPropertyFaultMsg
* @throws FinderException
* @throws RuntimeFaultFaultMsg
*/
private VirtualMachineConfigSpec buildVirtualMachineConfigSpec(String datastoreName) throws InvalidPropertyFaultMsg, FinderException, RuntimeFaultFaultMsg {
String displayName = this.ctx.child.name;
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.setName(displayName);
spec.setNumCPUs((int) this.ctx.child.description.cpuCount);
spec.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST_64.value());
spec.setMemoryMB(toMemoryMb(this.ctx.child.description.totalMemoryBytes));
VirtualMachineFileInfo files = new VirtualMachineFileInfo();
// Use a full path to the config file to avoid creating a VM with the same name
String path = String.format("[%s] %s/%s.vmx", datastoreName, displayName, displayName);
files.setVmPathName(path);
spec.setFiles(files);
for (NetworkInterfaceStateWithDetails ni : this.ctx.nics) {
VirtualDevice nic = createNic(ni, null);
addDeviceToVm(spec, nic);
}
VirtualDevice scsi = createScsiController();
addDeviceToVm(spec, scsi);
return spec;
}
Aggregations