@Test public void getVMDetailsSuccess() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(null); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMorMock, true)); ObjectContent[] objectContents = getObjectContents(); when(GetObjectProperties.getObjectProperties(any(ConnectionResources.class), any(ManagedObjectReference.class), any(String[].class))).thenReturn(objectContents); Map<String, String> results = new VmService().getVMDetails(httpInputsMock, new VmInputs.VmInputsBuilder().withHostname("hostname").build()); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); assertNotNull(results); assertEquals(0, Integer.parseInt(results.get("returnCode"))); assertTrue(results.get("returnResult").contains("\"vmId\":\"vm-123\"")); assertTrue(results.get("returnResult").contains("\"numCPUs\":\"3\"")); assertTrue(results.get("returnResult").contains("\"numEths\":\"4\"")); assertTrue(results.get("returnResult").contains("\"numDisks\":\"2\"")); assertTrue(results.get("returnResult").contains("\"vmUuid\":\"a3e76177-5020-41a3-ac2a-59c6303c8415\"")); assertTrue(results.get("returnResult").contains("\"isTemplate\":\"true\"")); assertTrue(results.get("returnResult").contains("\"virtualMachineFullName\":\"Ubuntu Linux (64-bit)\"")); assertTrue(results.get("returnResult").contains("\"dataStore\":\"AbCdEf123-vc6-2\"")); assertTrue(results.get("returnResult").contains("\"vmMemorySize\":\"8192\"")); assertTrue(results.get("returnResult").contains("\"vmPathName\":\"[AbCdEf123-vc6-2] Ubuntu64/Ubuntu64.vmx\"")); assertTrue(results.get("returnResult").contains("\"ipAddress\":\"127.0.0.1\"")); }
public Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); if (objectContents != null) { Map<String, String> vmDetails = new HashMap<>(); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.getConfig(); ResponseUtils.addDataToVmDetailsMap(vmDetails, virtualMachineSummary, virtualMachineConfigSummary); } } String responseJson = ResponseUtils.getJsonString(vmDetails); return ResponseUtils.getResultsMap(responseJson, Outputs.RETURN_CODE_SUCCESS); } else { return ResponseUtils.getResultsMap("Could not retrieve the details for: [" + vmInputs.getVirtualMachineName() + "] VM.", Outputs.RETURN_CODE_FAILURE); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }
VmService { public Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); if (objectContents != null) { Map<String, String> vmDetails = new HashMap<>(); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.getConfig(); ResponseUtils.addDataToVmDetailsMap(vmDetails, virtualMachineSummary, virtualMachineConfigSummary); } } String responseJson = ResponseUtils.getJsonString(vmDetails); return ResponseUtils.getResultsMap(responseJson, Outputs.RETURN_CODE_SUCCESS); } else { return ResponseUtils.getResultsMap("Could not retrieve the details for: [" + vmInputs.getVirtualMachineName() + "] VM.", Outputs.RETURN_CODE_FAILURE); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } } }
VmService { public Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); if (objectContents != null) { Map<String, String> vmDetails = new HashMap<>(); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.getConfig(); ResponseUtils.addDataToVmDetailsMap(vmDetails, virtualMachineSummary, virtualMachineConfigSummary); } } String responseJson = ResponseUtils.getJsonString(vmDetails); return ResponseUtils.getResultsMap(responseJson, Outputs.RETURN_CODE_SUCCESS); } else { return ResponseUtils.getResultsMap("Could not retrieve the details for: [" + vmInputs.getVirtualMachineName() + "] VM.", Outputs.RETURN_CODE_FAILURE); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }  }
VmService { public Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); if (objectContents != null) { Map<String, String> vmDetails = new HashMap<>(); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.getConfig(); ResponseUtils.addDataToVmDetailsMap(vmDetails, virtualMachineSummary, virtualMachineConfigSummary); } } String responseJson = ResponseUtils.getJsonString(vmDetails); return ResponseUtils.getResultsMap(responseJson, Outputs.RETURN_CODE_SUCCESS); } else { return ResponseUtils.getResultsMap("Could not retrieve the details for: [" + vmInputs.getVirtualMachineName() + "] VM.", Outputs.RETURN_CODE_FAILURE); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }  Map<String, String> getOsDescriptors(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> createVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> deleteVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOnVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOffVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> listVMsAndTemplates(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> updateVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs); }
VmService { public Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[]{ManagedObjectType.SUMMARY.getValue()}); if (objectContents != null) { Map<String, String> vmDetails = new HashMap<>(); for (ObjectContent objectItem : objectContents) { List<DynamicProperty> vmProperties = objectItem.getPropSet(); for (DynamicProperty propertyItem : vmProperties) { VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal(); VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.getConfig(); ResponseUtils.addDataToVmDetailsMap(vmDetails, virtualMachineSummary, virtualMachineConfigSummary); } } String responseJson = ResponseUtils.getJsonString(vmDetails); return ResponseUtils.getResultsMap(responseJson, Outputs.RETURN_CODE_SUCCESS); } else { return ResponseUtils.getResultsMap("Could not retrieve the details for: [" + vmInputs.getVirtualMachineName() + "] VM.", Outputs.RETURN_CODE_FAILURE); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }  Map<String, String> getOsDescriptors(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> createVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> deleteVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOnVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> powerOffVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> listVMsAndTemplates(HttpInputs httpInputs, VmInputs vmInputs, String delimiter); Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> updateVM(HttpInputs httpInputs, VmInputs vmInputs); Map<String, String> cloneVM(HttpInputs httpInputs, VmInputs vmInputs);  }