public void deployOvfTemplate(final HttpInputs httpInputs, final VmInputs vmInputs, final String templatePath, final Map<String, String> ovfNetworkMap, final Map<String, String> ovfPropertyMap) throws Exception { final ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { final ImmutablePair<ManagedObjectReference, OvfCreateImportSpecResult> pair = createLeaseSetup(connectionResources, vmInputs, templatePath, ovfNetworkMap, ovfPropertyMap); final ManagedObjectReference httpNfcLease = pair.getLeft(); final OvfCreateImportSpecResult importSpecResult = pair.getRight(); final HttpNfcLeaseInfo httpNfcLeaseInfo = getHttpNfcLeaseInfoWhenReady(connectionResources, httpNfcLease); final List<HttpNfcLeaseDeviceUrl> deviceUrls = httpNfcLeaseInfo.getDeviceUrl(); final ProgressUpdater progressUpdater = executor.isParallel() ? new AsyncProgressUpdater(getDisksTotalNoBytes(importSpecResult), httpNfcLease, connectionResources) : new SyncProgressUpdater(getDisksTotalNoBytes(importSpecResult), httpNfcLease, connectionResources); executor.execute(progressUpdater); transferVmdkFiles(templatePath, importSpecResult, deviceUrls, progressUpdater); executor.shutdown(); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }
private void verifyMockInvocationsForCreateLeaseSetupTest(ImmutablePair<ManagedObjectReference, OvfCreateImportSpecResult> result, boolean withClustername) throws Exception { Assert.assertEquals(httpNfcLeaseMock, result.getLeft()); Assert.assertEquals(ovfCreateImportSpecResultMock, result.getRight()); verifyNew(VmUtils.class).withNoArguments(); if (withClustername) { verify(vmInputsMock, times(2)).getClusterName(); verify(vmUtilsMock, times(0)).getMorResourcePool(TEST_RESOURCE_POOL, connectionResourcesMock); verifyNew(MorObjectHandler.class).withNoArguments(); verify(morObjectHandlerMock).getSpecificMor(connectionResourcesMock, morRootFolderMock, "ClusterComputeResource", TEST_CLUSTER); verify(vmUtilsMock).getMorResourcePoolFromCluster(connectionResourcesMock, clusterMorMock, TEST_RESOURCE_POOL); } else { verify(vmInputsMock).getClusterName(); verify(vmUtilsMock).getMorResourcePool(TEST_RESOURCE_POOL, connectionResourcesMock); verify(vmUtilsMock, times(0)).getMorResourcePoolFromCluster(connectionResourcesMock, clusterMorMock, TEST_RESOURCE_POOL); } verify(vmUtilsMock).getMorHost(anyString(), any(ConnectionResources.class), any(ManagedObjectReference.class)); verify(vmUtilsMock).getMorDataStore(anyString(), any(ConnectionResources.class), any(ManagedObjectReference.class), any(VmInputs.class)); verify(vmUtilsMock).getMorFolder(anyString(), any(ConnectionResources.class)); verify(connectionResourcesMock).getVimPortType(); verify(vimPortTypeMock).createImportSpec(ovfManagerMock, OVF_TEMPLATE_AS_STRING, resourcePoolMock, datastoreMock, ovfCreateImportSpecParamsMock); }
private long getImportSizeBytes(OvfCreateImportSpecResult importResult) { List<OvfFileItem> items = importResult.getFileItem(); if (items == null) { return 0; } long totalBytes = 0; for (OvfFileItem fi : items) { totalBytes += fi.getSize(); } return totalBytes; }
protected ImmutablePair<ManagedObjectReference, OvfCreateImportSpecResult> createLeaseSetup( final ConnectionResources connectionResources, final VmInputs vmInputs, final String templatePath, final Map<String, String> ovfNetworkMap, final Map<String, String> ovfPropertyMap) throws Exception { final ManagedObjectReference ovfManager = getOvfManager(connectionResources); final VmUtils vmUtils = new VmUtils(); final ManagedObjectReference resourcePool; if (StringUtilities.isBlank(vmInputs.getClusterName())) { resourcePool = vmUtils.getMorResourcePool(vmInputs.getResourcePool(), connectionResources); } else { ManagedObjectReference clusterMor = new MorObjectHandler().getSpecificMor(connectionResources, connectionResources.getMorRootFolder(), ClusterParameter.CLUSTER_COMPUTE_RESOURCE.getValue(), vmInputs.getClusterName()); resourcePool = vmUtils.getMorResourcePoolFromCluster(connectionResources, clusterMor, vmInputs.getResourcePool()); } final ManagedObjectReference hostMor = vmUtils.getMorHost(vmInputs.getHostname(), connectionResources, null); final ManagedObjectReference datastoreMor = vmUtils.getMorDataStore(vmInputs.getDataStore(), connectionResources, null, vmInputs); final ManagedObjectReference folderMor = vmUtils.getMorFolder(vmInputs.getFolderName(), connectionResources); final List<OvfNetworkMapping> ovfNetworkMappings = getOvfNetworkMappings(ovfNetworkMap, connectionResources); final List<KeyValue> ovfPropertyMappings = getOvfPropertyMappings(ovfPropertyMap); final OvfCreateImportSpecResult importSpecResult = connectionResources.getVimPortType() .createImportSpec(ovfManager, getOvfTemplateAsString(templatePath), resourcePool, datastoreMor, getOvfCreateImportSpecParams(vmInputs, hostMor, ovfNetworkMappings, ovfPropertyMappings)); checkImportSpecResultForErrors(importSpecResult); final ManagedObjectReference httpNfcLease = OvfUtils.getHttpNfcLease(connectionResources, importSpecResult.getImportSpec(), resourcePool, hostMor, folderMor); return ImmutablePair.of(httpNfcLease, importSpecResult); }
private void checkImportSpecResultForErrors(OvfCreateImportSpecResult importSpecResult) throws Exception { if (0 < importSpecResult.getError().size()) { StringBuilder stringBuilder = new StringBuilder(); for (LocalizedMethodFault fault : importSpecResult.getError()) { stringBuilder.append(fault.getLocalizedMessage()).append(System.lineSeparator()); } throw new Exception(stringBuilder.toString()); } }
private void transferVmdkFiles(final String ovfPath, final OvfCreateImportSpecResult importSpecResult, final List<HttpNfcLeaseDeviceUrl> deviceUrls, final ProgressUpdater progressUpdater) throws Exception { for (HttpNfcLeaseDeviceUrl deviceUrl : deviceUrls) { final String deviceKey = deviceUrl.getImportKey(); for (OvfFileItem fileItem : importSpecResult.getFileItem()) { if (deviceKey.equals(fileItem.getDeviceId())) { final TransferVmdkTask transferVmdkTask = getTransferVmdkTask(ovfPath, progressUpdater, deviceUrl, fileItem); executor.execute(transferVmdkTask); break; } } } }
private long getDisksTotalNoBytes(final OvfCreateImportSpecResult importSpecResult) { long disksTotalNoBytes = 0; for (final OvfFileItem item : importSpecResult.getFileItem()) { if (item.getCimType() == DISK_DRIVE_CIM_TYPE) { disksTotalNoBytes += item.getSize(); } } return disksTotalNoBytes; }
@Test public void testCreateLeaseSetupWithClusterName() throws Exception { prepareMocksForCreateLeaseSetupTest(true); ImmutablePair<ManagedObjectReference, OvfCreateImportSpecResult> result = serviceSpy.createLeaseSetup(connectionResourcesMock, vmInputsMock, PATH, ovfNetworkMapMock, ovfPropertyMapMock); verifyMockInvocationsForCreateLeaseSetupTest(result, true); }
@Test public void testCreateLeaseSetupWithoutClusterName() throws Exception { prepareMocksForCreateLeaseSetupTest(false); ImmutablePair<ManagedObjectReference, OvfCreateImportSpecResult> result = serviceSpy.createLeaseSetup(connectionResourcesMock, vmInputsMock, PATH, ovfNetworkMapMock, ovfPropertyMapMock); verifyMockInvocationsForCreateLeaseSetupTest(result, false); }
private void prepareMocksForDeployOvfTemplate(boolean parallel) throws Exception { whenNew(ConnectionResources.class) .withArguments(httpInputsMock, vmInputsMock) .thenReturn(connectionResourcesMock); PowerMockito.doReturn(pair).when(serviceSpy, "createLeaseSetup", connectionResourcesMock, vmInputsMock, PATH, ovfNetworkMapMock, ovfPropertyMapMock); PowerMockito.doReturn(httpNfcLeaseInfoMock).when(serviceSpy, "getHttpNfcLeaseInfoWhenReady", any(ConnectionResources.class), any(ManagedObjectReference.class)); doReturn(deviceUrlsMock).when(httpNfcLeaseInfoMock).getDeviceUrl(); Whitebox.setInternalState(serviceSpy, "executor", executorMock); PowerMockito.doReturn(DISK_SIZE).when(serviceSpy, "getDisksTotalNoBytes", any(OvfCreateImportSpecResult.class)); whenNew(AsyncProgressUpdater.class).withArguments(DISK_SIZE, httpNfcLeaseMock, connectionResourcesMock) .thenReturn(asyncProgressUpdaterMock); whenNew(SyncProgressUpdater.class).withArguments(DISK_SIZE, httpNfcLeaseMock, connectionResourcesMock) .thenReturn(syncProgressUpdaterMock); if (parallel) { doNothing().when(executorMock).execute(asyncProgressUpdaterMock); PowerMockito.doNothing().when(serviceSpy, "transferVmdkFiles", PATH, ovfCreateImportSpecResultMock, deviceUrlsMock, asyncProgressUpdaterMock); } else { doNothing().when(executorMock).execute(syncProgressUpdaterMock); PowerMockito.doNothing().when(serviceSpy, "transferVmdkFiles", PATH, ovfCreateImportSpecResultMock, deviceUrlsMock, syncProgressUpdaterMock); } doNothing().when(executorMock).shutdown(); doReturn(parallel).when(executorMock).isParallel(); }
public static long calcTotalBytes(OvfCreateImportSpecResult ovfImportResult) { List<OvfFileItem> fileItemArr = ovfImportResult.getFileItem(); long totalBytes = 0; if (fileItemArr != null) { for (OvfFileItem fi : fileItemArr) { totalBytes += fi.getSize(); } } return totalBytes; }
public OvfCreateImportSpecResult createImportSpec(String ovfDescriptor, ResourcePool resourcePool, Datastore datastore, OvfCreateImportSpecParams cisp) throws TaskInProgress, VmConfigFault, ConcurrentAccess, FileFault, InvalidState, InvalidDatastore, RuntimeFault, RemoteException { return getVimService().createImportSpec(getMOR(), ovfDescriptor, resourcePool.getMOR(), datastore.getMOR(), cisp); }