use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef in project photon-model by vmware.
the class OvfDeployer method deployOvf.
public ManagedObjectReference deployOvf(URI ovfUri, ManagedObjectReference host, ManagedObjectReference vmFolder, String vmName, List<OvfNetworkMapping> networks, ManagedObjectReference datastore, Collection<KeyValue> ovfProps, String deploymentConfig, ManagedObjectReference resourcePool) throws Exception {
String ovfDescriptor = getRetriever().retrieveAsString(ovfUri);
OvfCreateImportSpecParams params = new OvfCreateImportSpecParams();
params.setHostSystem(host);
params.setLocale("US");
params.setEntityName(vmName);
if (deploymentConfig == null) {
deploymentConfig = "";
}
params.setDeploymentOption(deploymentConfig);
params.getNetworkMapping().addAll(networks);
params.setDiskProvisioning(OvfCreateImportSpecParamsDiskProvisioningType.THIN.name());
if (ovfProps != null) {
params.getPropertyMapping().addAll(ovfProps);
}
ManagedObjectReference ovfManager = this.connection.getServiceContent().getOvfManager();
OvfCreateImportSpecResult importSpecResult = getVimPort().createImportSpec(ovfManager, ovfDescriptor, resourcePool, datastore, params);
if (!importSpecResult.getError().isEmpty()) {
return VimUtils.rethrow(importSpecResult.getError().get(0));
}
long totalBytes = getImportSizeBytes(importSpecResult);
ManagedObjectReference lease = getVimPort().importVApp(resourcePool, importSpecResult.getImportSpec(), vmFolder, host);
LeaseProgressUpdater leaseUpdater = new LeaseProgressUpdater(this.connection, lease, totalBytes);
GetMoRef get = new GetMoRef(this.connection);
HttpNfcLeaseInfo httpNfcLeaseInfo;
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
try {
leaseUpdater.awaitReady();
logger.info("Lease ready");
// start updating the lease
leaseUpdater.start(executorService);
httpNfcLeaseInfo = get.entityProp(lease, PROP_INFO);
List<HttpNfcLeaseDeviceUrl> deviceUrls = httpNfcLeaseInfo.getDeviceUrl();
String ip = this.connection.getURI().getHost();
String basePath = extractBasePath(ovfUri);
for (HttpNfcLeaseDeviceUrl deviceUrl : deviceUrls) {
String deviceKey = deviceUrl.getImportKey();
for (OvfFileItem ovfFileItem : importSpecResult.getFileItem()) {
if (deviceKey.equals(ovfFileItem.getDeviceId())) {
logger.debug("Importing device id: {}", deviceKey);
String sourceUri = computeDiskSourceUri(basePath, ovfFileItem);
String uploadUri = makUploadUri(ip, deviceUrl);
uploadVmdkFile(ovfFileItem, sourceUri, uploadUri, leaseUpdater, this.ovfRetriever.getClient());
logger.info("Completed uploading VMDK file {}", sourceUri);
}
}
}
// complete lease
leaseUpdater.complete();
} catch (Exception e) {
leaseUpdater.abort(VimUtils.convertExceptionToFault(e));
logger.info("Error importing ovf", e);
throw e;
} finally {
executorService.shutdown();
}
httpNfcLeaseInfo = get.entityProp(lease, PROP_INFO);
ManagedObjectReference entity = httpNfcLeaseInfo.getEntity();
// as this is an OVF it makes sense to enable the OVF transport
// only the guestInfo is enabled by default
VmConfigSpec spec = new VmConfigSpec();
spec.getOvfEnvironmentTransport().add(TRANSPORT_GUESTINFO);
spec.getOvfEnvironmentTransport().add(TRANSPORT_ISO);
VirtualMachineConfigSpec reconfig = new VirtualMachineConfigSpec();
reconfig.setVAppConfig(spec);
ManagedObjectReference reconfigTask = getVimPort().reconfigVMTask(entity, reconfig);
VimUtils.waitTaskEnd(this.connection, reconfigTask);
return entity;
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef in project photon-model by vmware.
the class CreatePortgroupFlow method createPortgroupInVsphere.
private ConnectionCallback createPortgroupInVsphere(DeferredResult<Void> result) {
return (connection, error) -> {
if (error != null) {
result.fail(error);
return;
}
// extract moref of the parent DVS switch
ManagedObjectReference dvsRef = CustomProperties.of(this.networkState).getMoRef(CustomProperties.MOREF);
DVPortgroupConfigSpec pgSpec = createDefaultPortgroupSpec();
ManagedObjectReference task;
try {
task = connection.getVimPort().createDVPortgroupTask(dvsRef, pgSpec);
} catch (Exception e) {
result.fail(e);
return;
}
TaskInfo taskInfo;
try {
taskInfo = VimUtils.waitTaskEnd(connection, task);
} catch (Exception e) {
result.fail(e);
return;
}
if (taskInfo.getState() != TaskInfoState.SUCCESS) {
IllegalStateException e = new IllegalStateException(taskInfo.getError().getLocalizedMessage());
result.fail(e);
return;
}
ManagedObjectReference pg = (ManagedObjectReference) taskInfo.getResult();
AssertUtil.assertNotNull(pg, "MoRef of dvPortGroup");
String pgKey = null;
String dvsUuid = null;
try {
GetMoRef get = new GetMoRef(connection);
Map<String, Object> propValues = get.entityProps(pg, VimPath.pg_config_key, VimPath.pg_config_distributedVirtualSwitch);
pgKey = (String) propValues.get(VimPath.pg_config_key);
ManagedObjectReference parentDvSwitch = (ManagedObjectReference) propValues.get(VimPath.pg_config_distributedVirtualSwitch);
if (parentDvSwitch != null) {
dvsUuid = get.entityProp(parentDvSwitch, VimPath.dvs_uuid);
}
} catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg ignore) {
getService().logWarning("Cannot retrieve dvPortGroup properties of [%s]: %s", VimUtils.convertMoRefToString(pg), ignore.getLocalizedMessage());
}
// store the moref as custom property
CustomProperties.of(this.subnetState).put(CustomProperties.MOREF, pg).put(DvsProperties.PORT_GROUP_KEY, pgKey).put(DvsProperties.DVS_UUID, dvsUuid);
OperationContext.setFrom(getOperationContext());
Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getService().getHost(), this.subnetState.documentSelfLink)).setBody(this.subnetState).setCompletion((o, e) -> {
if (e != null) {
result.fail(e);
return;
}
result.complete(null);
getTaskManager().patchTask(TaskStage.FINISHED);
}).sendWith(getService());
};
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef in project photon-model by vmware.
the class BaseVSphereAdapterTest method fetchVirtualDisk.
/**
* Get the reference to Virtual Disk from VM.
*/
protected VirtualDisk fetchVirtualDisk(ComputeState vm, GetMoRef get) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
ManagedObjectReference vmMoRef = CustomProperties.of(vm).getMoRef(MOREF);
ArrayOfVirtualDevice devices = get.entityProp(vmMoRef, VimPath.vm_config_hardware_device);
VirtualDisk vd = devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).findFirst().orElse(null);
return vd;
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef in project photon-model by vmware.
the class BaseVSphereAdapterTest method fetchAllVirtualDisks.
/**
* Get the reference to Virtual Disk from VM.
*/
protected List<VirtualDisk> fetchAllVirtualDisks(ComputeState vm, GetMoRef get) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
ManagedObjectReference vmMoRef = CustomProperties.of(vm).getMoRef(MOREF);
ArrayOfVirtualDevice devices = get.entityProp(vmMoRef, VimPath.vm_config_hardware_device);
return devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualDisk).map(d -> (VirtualDisk) d).collect(Collectors.toList());
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef in project photon-model by vmware.
the class TestVSphereLibraryProvisionTaskWithStorage method deployFromLibraryWithAdditionalDisks.
@Test
public void deployFromLibraryWithAdditionalDisks() throws Throwable {
ComputeService.ComputeState vm = provisionVMAndGetState(true, true);
try {
if (vm == null) {
return;
}
// Verify that the disk is resized
BasicConnection connection = createConnection();
GetMoRef get = new GetMoRef(connection);
List<VirtualDisk> virtualDisks = fetchAllVirtualDisks(vm, get);
assertEquals(3, virtualDisks.size());
assertEquals(3, vm.diskLinks.size());
List<DeferredResult<DiskService.DiskState>> disks = vm.diskLinks.stream().map(link -> {
Operation getOp = Operation.createGet(this.host, link).setReferer(this.host.getReferer());
return this.host.sendWithDeferredResult(getOp, DiskService.DiskState.class);
}).collect(Collectors.toList());
DeferredResult.allOf(disks).thenAccept(diskStates -> diskStates.stream().forEach(ds -> {
assertNotNull(ds.customProperties);
assertNotNull(ds.sourceImageReference);
assertNotNull(ds.customProperties.get(PROVIDER_DISK_UNIQUE_ID));
}));
} finally {
if (vm != null) {
deleteVmAndWait(vm);
}
}
}
Aggregations