use of com.vmware.vim25.ArrayOfVAppPropertyInfo 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.vim25.ArrayOfVAppPropertyInfo in project photon-model by vmware.
the class InstanceClient method populateVAppProperties.
private boolean populateVAppProperties(VirtualMachineConfigSpec spec, ArrayOfVAppPropertyInfo currentProps) {
if (this.bootDisk == null) {
return false;
}
boolean customizationsApplied = false;
int nextKey = 1;
if (currentProps != null) {
nextKey = currentProps.getVAppPropertyInfo().stream().mapToInt(VAppPropertyInfo::getKey).max().orElse(1);
nextKey++;
}
String ovfEnv = getFileItemByPath(this.bootDisk, OVF_PROPERTY_ENV);
if (ovfEnv != null) {
@SuppressWarnings("unchecked") Map<String, String> map = Utils.fromJson(ovfEnv, Map.class);
if (!map.isEmpty()) {
customizationsApplied = true;
VmConfigSpec configSpec = new VmConfigSpec();
configSpec.getOvfEnvironmentTransport().add(OvfDeployer.TRANSPORT_ISO);
if (currentProps == null) {
currentProps = new ArrayOfVAppPropertyInfo();
}
currentProps.getVAppPropertyInfo().forEach(pi -> {
if (map.containsKey(pi.getId())) {
VAppPropertySpec ps = new VAppPropertySpec();
ps.setOperation(ArrayUpdateOperation.EDIT);
pi.setValue(map.remove(pi.getId()));
ps.setInfo(pi);
configSpec.getProperty().add(ps);
}
});
// only new key/values
for (Entry<String, String> entry : map.entrySet()) {
VAppPropertyInfo pi = new VAppPropertyInfo();
pi.setId(entry.getKey());
pi.setType("string");
pi.setKey(nextKey++);
pi.setValue(entry.getValue());
VAppPropertySpec ps = new VAppPropertySpec();
ps.setOperation(ArrayUpdateOperation.ADD);
ps.setInfo(pi);
configSpec.getProperty().add(ps);
}
spec.setVAppConfig(configSpec);
}
}
return customizationsApplied;
}
use of com.vmware.vim25.ArrayOfVAppPropertyInfo in project photon-model by vmware.
the class InstanceClient method populateCloudConfig.
/**
* Puts the cloud-config user data in the OVF environment
*
* @param spec
* @param currentProps
*/
private boolean populateCloudConfig(VirtualMachineConfigSpec spec, ArrayOfVAppPropertyInfo currentProps) {
if (this.bootDisk == null) {
return false;
}
boolean customizationsApplied = false;
int nextKey = 1;
if (currentProps != null) {
nextKey = currentProps.getVAppPropertyInfo().stream().mapToInt(VAppPropertyInfo::getKey).max().orElse(1);
nextKey++;
}
VmConfigSpec configSpec = new VmConfigSpec();
configSpec.getOvfEnvironmentTransport().add(OvfDeployer.TRANSPORT_ISO);
String cloudConfig = getFileItemByPath(this.bootDisk, CLOUD_CONFIG_PROPERTY_USER_DATA);
if (cloudConfig != null) {
VAppPropertySpec propertySpec = new VAppPropertySpec();
VAppPropertyInfo userDataInfo = null;
if (currentProps != null) {
userDataInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(CLOUD_CONFIG_PROPERTY_USER_DATA)).findFirst().orElse(null);
if (userDataInfo == null) {
// try coreOS key
userDataInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(COREOS_CLOUD_CONFIG_PROPERTY_USER_DATA)).findFirst().orElse(null);
if (userDataInfo != null) {
VAppPropertyInfo coreosEncoding = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(COREOS_CLOUD_CONFIG_PROPERTY_USER_DATA_ENCODING)).findFirst().orElse(null);
if (coreosEncoding != null) {
VAppPropertySpec pSpec = new VAppPropertySpec();
coreosEncoding.setValue(CLOUD_CONFIG_BASE64_ENCODING);
pSpec.setOperation(ArrayUpdateOperation.EDIT);
pSpec.setInfo(coreosEncoding);
configSpec.getProperty().add(pSpec);
}
}
}
}
if (userDataInfo != null) {
propertySpec.setOperation(ArrayUpdateOperation.EDIT);
} else {
userDataInfo = new VAppPropertyInfo();
userDataInfo.setId(CLOUD_CONFIG_PROPERTY_USER_DATA);
userDataInfo.setType("string");
userDataInfo.setKey(nextKey++);
propertySpec.setOperation(ArrayUpdateOperation.ADD);
}
String encodedUserData = Base64.getEncoder().encodeToString(cloudConfig.getBytes());
userDataInfo.setValue(encodedUserData);
propertySpec.setInfo(userDataInfo);
configSpec.getProperty().add(propertySpec);
customizationsApplied = true;
}
String publicKeys = getFileItemByPath(this.bootDisk, CLOUD_CONFIG_PROPERTY_PUBLIC_KEYS);
if (publicKeys != null) {
VAppPropertySpec propertySpec = new VAppPropertySpec();
VAppPropertyInfo sshKeyInfo = null;
if (currentProps != null) {
sshKeyInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(CLOUD_CONFIG_PROPERTY_PUBLIC_KEYS)).findFirst().orElse(null);
}
if (sshKeyInfo != null) {
propertySpec.setOperation(ArrayUpdateOperation.EDIT);
} else {
sshKeyInfo = new VAppPropertyInfo();
sshKeyInfo.setType("string");
sshKeyInfo.setId(CLOUD_CONFIG_PROPERTY_PUBLIC_KEYS);
sshKeyInfo.setKey(nextKey++);
propertySpec.setOperation(ArrayUpdateOperation.ADD);
}
sshKeyInfo.setValue(publicKeys);
propertySpec.setInfo(sshKeyInfo);
configSpec.getProperty().add(propertySpec);
customizationsApplied = true;
}
String hostname = getFileItemByPath(this.bootDisk, CLOUD_CONFIG_PROPERTY_HOSTNAME);
if (hostname != null) {
VAppPropertySpec propertySpec = new VAppPropertySpec();
VAppPropertyInfo hostInfo = null;
if (currentProps != null) {
hostInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(CLOUD_CONFIG_PROPERTY_HOSTNAME)).findFirst().orElse(null);
if (hostInfo == null) {
// try coreOS key
hostInfo = currentProps.getVAppPropertyInfo().stream().filter(p -> p.getId().equals(COREOS_CLOUD_CONFIG_PROPERTY_HOSTNAME)).findFirst().orElse(null);
}
}
if (hostInfo != null) {
propertySpec.setOperation(ArrayUpdateOperation.EDIT);
} else {
hostInfo = new VAppPropertyInfo();
hostInfo.setId(CLOUD_CONFIG_PROPERTY_USER_DATA);
hostInfo.setType("string");
hostInfo.setKey(nextKey++);
propertySpec.setOperation(ArrayUpdateOperation.ADD);
}
hostInfo.setValue(hostname);
propertySpec.setInfo(hostInfo);
configSpec.getProperty().add(propertySpec);
customizationsApplied = true;
}
if (customizationsApplied) {
spec.setVAppConfig(configSpec);
}
return customizationsApplied;
}
use of com.vmware.vim25.ArrayOfVAppPropertyInfo 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