@BeforeMethod @Override protected void setUp() throws Exception { super.setUp(); myIdxStorage = createTempDir(); FakeModel.instance().addDatacenter("dc"); FakeModel.instance().addFolder("cf").setParent("dc", Datacenter.class); FakeModel.instance().addResourcePool("rp").setParentFolder("cf"); FakeModel.instance().addVM("image1").setParentFolder("cf"); FakeModel.instance().addVM("image2").setParentFolder("cf"); FakeModel.instance().addVM("image_template").setParentFolder("cf"); FakeModel.instance().addVMSnapshot("image2", "snap"); FakeModel.instance().getCustomizationSpecs().put("someCustomization", new CustomizationSpec()); FakeModel.instance().getCustomizationSpecs().put("linux", new CustomizationSpec()); myFakeApiConnector = new FakeApiConnector(TEST_SERVER_UUID, PROFILE_ID, null); setInternalProperty("teamcity.vsphere.instance.status.update.delay.ms", "250"); myTaskManager = new MyExposingVmwareUpdateTaskManager(); }
public void customizeNic(String macAddress, String hostName, String address, SubnetState subnetState, CustomizationSpec template) { // remove existing mapping template.getNicSettingMap().removeIf(x -> Objects.equals(x.getMacAddress(), macAddress)); CustomizationAdapterMapping mapping = new CustomizationAdapterMapping(); mapping.setMacAddress(macAddress); CustomizationIPSettings adapter = new CustomizationIPSettings(); mapping.setAdapter(adapter); adapter.setSubnetMask(cidr2mask(subnetState.subnetCIDR)); adapter.getGateway().add(subnetState.gatewayAddress); adapter.setDnsDomain(subnetState.domain); CustomizationFixedIp ipGen = new CustomizationFixedIp(); ipGen.setIpAddress(address); adapter.setIp(ipGen); template.getNicSettingMap().add(mapping); if (isLinux()) { CustomizationLinuxPrep identity = new CustomizationLinuxPrep(); template.setIdentity(identity); identity.setDomain(subnetState.domain); CustomizationFixedName name = new CustomizationFixedName(); if (hostName == null || hostName.isEmpty()) { hostName = makeHostName(UriUtils.getLastPathSegment(this.state.documentSelfLink)); } name.setName(hostName); identity.setHostName(name); template.setOptions(new CustomizationLinuxOptions()); } }
public void customizeDns(List<String> dnsServers, List<String> dnsSearchDomains, CustomizationSpec template) { CustomizationGlobalIPSettings settings = new CustomizationGlobalIPSettings(); if (dnsServers != null) { settings.getDnsServerList().addAll(dnsServers); } if (dnsSearchDomains != null) { settings.getDnsSuffixList().addAll(dnsSearchDomains); } template.setGlobalIPSettings(settings); }
private VirtualMachineCloneSpec configureVirtualMachineCloneSpec(VirtualMachineRelocateSpec rSpec, String linuxName, boolean postConfiguration) throws Exception { VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); cloneSpec.setPowerOn(true); cloneSpec.setTemplate(false); //cloneSpec.setSnapshot(currentSnapshot.getMOR()); cloneSpec.setLocation(rSpec); if (postConfiguration) { CustomizationSpec customizationSpec = new CustomizationSpec(); CustomizationLinuxPrep linuxPrep = new CustomizationLinuxPrep(); CustomizationFixedName fixedName = new CustomizationFixedName(); fixedName.setName(linuxName); linuxPrep.setHostName(fixedName); linuxPrep.setDomain(""); linuxPrep.setHwClockUTC(true); //linuxPrep.setTimeZone("Etc/UTC"); customizationSpec.setIdentity(linuxPrep); customizationSpec.setGlobalIPSettings(new CustomizationGlobalIPSettings()); CustomizationAdapterMapping[] nicSettingMap = new CustomizationAdapterMapping[1]; nicSettingMap[0] = new CustomizationAdapterMapping(); nicSettingMap[0].adapter = new CustomizationIPSettings(); nicSettingMap[0].adapter.setIp(new CustomizationDhcpIpGenerator()); customizationSpec.setNicSettingMap(nicSettingMap); cloneSpec.setCustomization(customizationSpec); } return cloneSpec; }
@Override public CustomizationSpec getCustomizationSpec(final String name) throws VmwareCheckedCloudException { final CustomizationSpec spec = FakeModel.instance().getCustomizationSpec(name); if (spec == null){ throw new VmwareCheckedCloudException("Unable to get Customization Spec '" + name + "'"); } return spec; }
/** * Method used to connect to specified data center and customize the windows OS based virtual machine identified * by the inputs provided. * * @param httpInputs Object that has all the inputs necessary to made a connection to data center * @param vmInputs Object that has all the specific inputs necessary to identify the targeted virtual machine * @param guestInputs Object that has all specific inputs necessary to customize specified virtual machine * @return Map with String as key and value that contains returnCode of the operation, success message with task id * of the execution or failure message and the exception if there is one * @throws Exception */ public Map<String, String> customizeVM(HttpInputs httpInputs, VmInputs vmInputs, GuestInputs guestInputs, boolean isWin) throws Exception { ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs); try { ManagedObjectReference vmMor = new MorObjectHandler() .getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName()); if (vmMor != null) { CustomizationSpec customizationSpec = isWin ? new GuestConfigSpecs().getWinCustomizationSpec(guestInputs) : new GuestConfigSpecs().getLinuxCustomizationSpec(guestInputs); connectionResources.getVimPortType().checkCustomizationSpec(vmMor, customizationSpec); ManagedObjectReference task = connectionResources.getVimPortType().customizeVMTask(vmMor, customizationSpec); return new ResponseHelper(connectionResources, task) .getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully customized. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be customized."); } else { return ResponseUtils.getVmNotFoundResultsMap(vmInputs); } } catch (Exception ex) { return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE); } finally { if (httpInputs.isCloseSession()) { connectionResources.getConnection().disconnect(); clearConnectionFromContext(httpInputs.getGlobalSessionObject()); } } }
@Test public void customizeWinVMSuccess() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(vmMorMock); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getWinCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(taskMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, true)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testWinVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder() .withRebootOption("noreboot").withLicenseDataMode("perServer").build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, true); verifyConnection(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(guestConfigSpecsMock, times(1)).getWinCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, times(1)).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, times(1)).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, times(1)).getValue(); assertNotNull(results); assertEquals(0, Integer.parseInt(results.get("returnCode"))); assertEquals("Success: The [testWinVMName] VM was successfully customized. The taskId is: task-12345", results.get("returnResult")); }
@Test public void customizeWinVMFailure() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(vmMorMock); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getWinCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(taskMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, false)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testWinVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder() .withRebootOption("noreboot").withLicenseDataMode("perServer").build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, true); verifyConnection(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(guestConfigSpecsMock, times(1)).getWinCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, times(1)).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, times(1)).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, times(1)).getValue(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Failure: The [testWinVMName] VM could not be customized.", results.get("returnResult")); }
@Test public void customizeWinVMNotFound() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(null); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getWinCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(taskMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, true)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testWinVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder() .withRebootOption("noreboot").withLicenseDataMode("perServer").build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, true); verify(connectionResourcesMock, times(1)).getConnection(); verify(connectionMock, times(1)).disconnect(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(connectionResourcesMock, never()).getVimPortType(); verify(guestConfigSpecsMock, never()).getWinCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, never()).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, never()).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, never()).getValue(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Could not find the [testWinVMName] VM.", results.get("returnResult")); }
@Test public void customizeLinuxVMSuccess() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(vmMorMock); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getLinuxCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(taskMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, true)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testLinuxVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder().build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, false); verifyConnection(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(guestConfigSpecsMock, times(1)).getLinuxCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, times(1)).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, times(1)).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, times(1)).getValue(); assertNotNull(results); assertEquals(0, Integer.parseInt(results.get("returnCode"))); assertEquals("Success: The [testLinuxVMName] VM was successfully customized. The taskId is: task-12345", results.get("returnResult")); }
@Test public void customizeLinuxVMFailure() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(vmMorMock); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getLinuxCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(taskMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, false)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testLinuxVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder().build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, false); verifyConnection(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(guestConfigSpecsMock, times(1)).getLinuxCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, times(1)).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, times(1)).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, times(1)).getValue(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Failure: The [testLinuxVMName] VM could not be customized.", results.get("returnResult")); }
@Test public void customizeLinuxVMNotFound() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(null); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getLinuxCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(taskMock); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, true)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testLinuxVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder().build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, false); verify(connectionResourcesMock, times(1)).getConnection(); verify(connectionMock, times(1)).disconnect(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(connectionResourcesMock, never()).getVimPortType(); verify(guestConfigSpecsMock, never()).getLinuxCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, never()).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, never()).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, never()).getValue(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("Could not find the [testLinuxVMName] VM.", results.get("returnResult")); }
@Test public void customizeLinuxVMException() throws Exception { whenNew(MorObjectHandler.class).withNoArguments().thenReturn(morObjectHandlerMock); when(httpInputsMock.isCloseSession()).thenReturn(true); when(morObjectHandlerMock.getMor(any(ConnectionResources.class), anyString(), anyString())).thenReturn(vmMorMock); whenNew(GuestConfigSpecs.class).withNoArguments().thenReturn(guestConfigSpecsMock); when(guestConfigSpecsMock.getLinuxCustomizationSpec(any(GuestInputs.class))).thenReturn(customizationSpecMock); doNothing().when(vimPortMock).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); when(vimPortMock.customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class))).thenReturn(null); whenNew(ResponseHelper.class).withArguments(any(ConnectionResources.class), any(ManagedObjectReference.class)) .thenReturn(getResponseHelper(connectionResourcesMock, taskMock, true)); VmInputs vmInputs = new VmInputs.VmInputsBuilder().withVirtualMachineName("testLinuxVMName").build(); GuestInputs guestInputs = new GuestInputs.GuestInputsBuilder().build(); Map<String, String> results = guestService.customizeVM(httpInputsMock, vmInputs, guestInputs, false); verify(connectionResourcesMock, times(1)).getConnection(); verify(connectionMock, times(1)).disconnect(); verify(morObjectHandlerMock, times(1)).getMor(any(ConnectionResources.class), anyString(), anyString()); verify(connectionResourcesMock, times(2)).getVimPortType(); verify(guestConfigSpecsMock, times(1)).getLinuxCustomizationSpec(any(GuestInputs.class)); verify(vimPortMock, times(1)).checkCustomizationSpec(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(vimPortMock, times(1)).customizeVMTask(any(ManagedObjectReference.class), any(CustomizationSpec.class)); verify(taskMock, never()).getValue(); assertNotNull(results); assertEquals(-1, Integer.parseInt(results.get("returnCode"))); assertEquals("java.lang.NullPointerException", results.get("returnResult")); }
public ManagedObjectReference customizeGuest(ManagedObjectReference vm, CustomizationSpec spec) throws RuntimeFaultFaultMsg, CustomizationFaultFaultMsg { return connection.getVimPort().customizeVMTask(vm, spec); }
public Map<String, CustomizationSpec> getCustomizationSpecs() { return myCustomizationSpecs; }
public CustomizationSpec getCustomizationSpec(String name){ return myCustomizationSpecs.get(name); }
private void setAdapter(GuestInputs guestInputs, CustomizationSpec customizationSpec) { CustomizationAdapterMapping adapterMapping = getCustomizationAdapterMapping(guestInputs); List<CustomizationAdapterMapping> adaptersList = customizationSpec.getNicSettingMap(); adaptersList.add(adapterMapping); }
public CustomizationSpec getWinCustomizationSpec(GuestInputs guestInputs) { CustomizationSpec customizationSpec = new CustomizationSpec(); CustomizationSysprep customizationSysprep = getCustomizationSysprep(guestInputs); customizationSpec.setIdentity(customizationSysprep); CustomizationWinOptions winOptions = getCustomizationWinOptions(guestInputs); customizationSpec.setOptions(winOptions); customizationSpec.setGlobalIPSettings(new CustomizationGlobalIPSettings()); setAdapter(guestInputs, customizationSpec); return customizationSpec; }
public CustomizationSpec getLinuxCustomizationSpec(GuestInputs guestInputs) { CustomizationSpec customizationSpec = new CustomizationSpec(); CustomizationLinuxPrep customizationLinuxPrep = getCustomizationLinuxPrep(guestInputs); customizationSpec.setIdentity(customizationLinuxPrep); CustomizationGlobalIPSettings customizationGlobalIPSettings = new CustomizationGlobalIPSettings(); customizationSpec.setGlobalIPSettings(customizationGlobalIPSettings); setAdapter(guestInputs, customizationSpec); return customizationSpec; }
CustomizationSpec getCustomizationSpec(String name) throws VmwareCheckedCloudException;