From b4d2a1a1cbda1962f4be440acd6c972dac8b9022 Mon Sep 17 00:00:00 2001 From: Subrahmanyam Mandavilli Date: Wed, 4 May 2016 11:46:59 +0530 Subject: [PATCH] Java layer changes for adding customization spec --- src/Tools/vmOpsTool/Constants.java | 1 + src/Tools/vmOpsTool/IVMWare.java | 13 ++++++------ src/Tools/vmOpsTool/VMWareImpl.java | 16 +++++++++++--- src/Tools/vmOpsTool/VmOpsTool.java | 4 +++- tests/Tools/vmOpsTool/InMemoryVMWareImpl.java | 3 +-- .../Tools/vmOpsTool/VMWarePlatformTests.java | 21 ++++++++++--------- tests/Tools/vmOpsTool/VmOpsToolUnitTests.java | 6 ++++-- 7 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Tools/vmOpsTool/Constants.java b/src/Tools/vmOpsTool/Constants.java index 158a00b..deca6d7 100644 --- a/src/Tools/vmOpsTool/Constants.java +++ b/src/Tools/vmOpsTool/Constants.java @@ -20,6 +20,7 @@ public class Constants { public static final String CREATE_SNAPSHOT_ACTION = "create"; public static final String VM_OPS_TOOL = "vmOpsTool-1.0.jar"; public static final String DESCRIPTION = "-description"; + public static final String CUSTOMIZATIONSPEC = "-customizationspec"; public static final String QUIESCE_VM_FS = "-quiesceVmFs"; public static final String SAVE_VM_MEMORY = "-saveVmMemory"; public static final String DELETE_SNAPSHOT_ACTION = "delete"; diff --git a/src/Tools/vmOpsTool/IVMWare.java b/src/Tools/vmOpsTool/IVMWare.java index f720658..ce26158 100644 --- a/src/Tools/vmOpsTool/IVMWare.java +++ b/src/Tools/vmOpsTool/IVMWare.java @@ -67,15 +67,16 @@ public interface IVMWare { boolean isVMPoweredOn(String vmName, boolean defaultValue, ConnectionData connData) throws Exception; /** - * @param templateName name of the virtual machine template to be cloned - * @param vmName name of the virtual machine - * @param computeType type of the compute esxi/cluster/resourcepool - * @param computeName name of the compute resouce - * @param description optional description for create operation + * @param templateName name of the virtual machine template to be cloned + * @param vmName name of the virtual machine + * @param computeType type of the compute esxi/cluster/resourcepool + * @param computeName name of the compute resouce + * @param description optional description for create operation + * @param customizationSpec name of the customization specification to be used during clone vm * @param connData connection information for vCenter * @throws Exception */ - void cloneVMFromTemplate(String templateName, String vmName, String computeType, String computeName, String datastore, String description, ConnectionData connData) throws Exception; + void cloneVMFromTemplate(String templateName, String vmName, String computeType, String computeName, String datastore, String customizationSpec, String description, ConnectionData connData) throws Exception; /** * @param vmName name of the virtual machine diff --git a/src/Tools/vmOpsTool/VMWareImpl.java b/src/Tools/vmOpsTool/VMWareImpl.java index dd54c63..fd9e26f 100644 --- a/src/Tools/vmOpsTool/VMWareImpl.java +++ b/src/Tools/vmOpsTool/VMWareImpl.java @@ -67,12 +67,13 @@ public class VMWareImpl implements IVMWare { } public void cloneVMFromTemplate(String templateName, String vmName, String computeType, - String computeName, String targetDS, String description, ConnectionData connData) throws Exception { + String computeName, String targetDS, String customizationSpec, + String description, ConnectionData connData) throws Exception { connect(connData); System.out.println(String.format("Finding template [ %s ] on vCenter server.", templateName)); ManagedObjectReference templateMor = getMorByName(targetDCMor, templateName, VIRTUAL_MACHINE, true); ManagedObjectReference targetVmFolder = (ManagedObjectReference) getMorProperties(targetDCMor, new String[]{VM_FOLDER}).get(VM_FOLDER); - VirtualMachineCloneSpec cloneSpec = getVirtualMachineCloneSpec(computeType, computeName, targetDS, description); + VirtualMachineCloneSpec cloneSpec = getVirtualMachineCloneSpec(computeType, computeName, targetDS, customizationSpec, description); System.out.println(String.format("Creating new virtual machine [ %s ] using template [ %s ].", vmName, templateName)); ManagedObjectReference task = vimPort.cloneVMTask(templateMor, targetVmFolder, vmName, cloneSpec); @@ -573,12 +574,21 @@ public class VMWareImpl implements IVMWare { } } - private VirtualMachineCloneSpec getVirtualMachineCloneSpec(String computeType, String computeName, String targetDS, String description) throws Exception { + private VirtualMachineCloneSpec getVirtualMachineCloneSpec(String computeType, String computeName, String targetDS, + String customizationSpec, String description) throws Exception { VirtualMachineRelocateSpec relocSpec = getVirtualMachineRelocationSpec(computeType, computeName, targetDS); VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); configSpec.setAnnotation(description); VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); + + if(!customizationSpec.isEmpty()){ + System.out.println(String.format("Fetching customization specification with name [ %s ].", customizationSpec)); + CustomizationSpecItem customizationSpecItem = vimPort.getCustomizationSpec( + serviceContent.getCustomizationSpecManager(), customizationSpec); + cloneSpec.setCustomization(customizationSpecItem.getSpec()); + } + cloneSpec.setConfig(configSpec); cloneSpec.setLocation(relocSpec); cloneSpec.setPowerOn(false); diff --git a/src/Tools/vmOpsTool/VmOpsTool.java b/src/Tools/vmOpsTool/VmOpsTool.java index b42aaf1..3e47a3a 100644 --- a/src/Tools/vmOpsTool/VmOpsTool.java +++ b/src/Tools/vmOpsTool/VmOpsTool.java @@ -179,10 +179,12 @@ public class VmOpsTool { String computeType = argsMap.get(Constants.COMPUTE_TYPE); String computeName = argsMap.get(Constants.COMPUTE_NAME); String datastore = argsMap.get(Constants.DATASTORE); + String customizationspec = argsMap.get(Constants.CUSTOMIZATIONSPEC); String description = argsMap.get(Constants.DESCRIPTION); try { - vmwareFactory.call().cloneVMFromTemplate(templateName, vmName, computeType, computeName, datastore, description, connData); + vmwareFactory.call().cloneVMFromTemplate(templateName, vmName, computeType, computeName, datastore, + customizationspec, description, connData); } catch (Exception exp) { System.out.println(exp.getMessage() != null ? exp.getMessage() : "Unknown error occurred."); failedVm = vmName + " "; diff --git a/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java b/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java index b98dd08..e1047de 100644 --- a/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java +++ b/tests/Tools/vmOpsTool/InMemoryVMWareImpl.java @@ -165,7 +165,7 @@ public class InMemoryVMWareImpl implements IVMWare { } public synchronized void cloneVMFromTemplate(String templateName, String vmName, String computeType, String computeName, - String datastore, String description, ConnectionData connData) throws Exception { + String datastore, String customizationSpec, String description, ConnectionData connData) throws Exception { if (vmName.equals("VMNameThatFailsInClone")) { throw new Exception("Clone VM from template operation failed for VMNameThatFailsInClone"); } @@ -186,7 +186,6 @@ public class InMemoryVMWareImpl implements IVMWare { throw new Exception("Invalid compute resource"); } - vmSnapshotInfo.put(vmName, new ArrayList<>()); vmActiveSnapshot.put(vmName, ""); } diff --git a/tests/Tools/vmOpsTool/VMWarePlatformTests.java b/tests/Tools/vmOpsTool/VMWarePlatformTests.java index 8338abb..35ce64c 100644 --- a/tests/Tools/vmOpsTool/VMWarePlatformTests.java +++ b/tests/Tools/vmOpsTool/VMWarePlatformTests.java @@ -10,7 +10,8 @@ public abstract class VMWarePlatformTests { private ConnectionData connData = new ConnectionData(vCenterUrl, vCenterUserName, vCenterPassword, defaultTargetDC, true); private IVMWare vmWareImpl = getVmWareImpl(); private String snapshotOne = "Snapshot1"; - private String templateName = "Ubuntu"; + private String ubuntuTemplate = "Ubuntu"; + String linuxCustomizationSpec = "Linux Spec"; private String snapshotTwo = "Snapshot2"; public abstract IVMWare getVmWareImpl(); @@ -35,7 +36,7 @@ public abstract class VMWarePlatformTests { String description = "Creating new VM from ubuntuVM template on ESXi host"; connData.setTargetDC(targetDC); - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); assertThat(vmWareImpl.isVMExists(newVmName, connData)).isEqualTo(true); @@ -54,7 +55,7 @@ public abstract class VMWarePlatformTests { String description = "Creating new VM from ubuntuVM template on cluster"; connData.setTargetDC(targetDC); - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); assertThat(vmWareImpl.isVMExists(newVmName, connData)).isEqualTo(true); @@ -73,7 +74,7 @@ public abstract class VMWarePlatformTests { String description = "Creating new VM from ubuntuVM template on resource pool"; connData.setTargetDC(targetDC); - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); assertThat(vmWareImpl.isVMExists(newVmName, connData)).isEqualTo(true); @@ -94,7 +95,7 @@ public abstract class VMWarePlatformTests { Exception exp = null; try { - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); } catch (Exception e) { exp = e; } @@ -114,7 +115,7 @@ public abstract class VMWarePlatformTests { Exception exp = null; try { - vmWareImpl.cloneVMFromTemplate(nonExistingTemplate, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(nonExistingTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); } catch (Exception e) { exp = e; } @@ -133,7 +134,7 @@ public abstract class VMWarePlatformTests { Exception exp = null; try { - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); } catch (Exception e) { exp = e; } @@ -152,7 +153,7 @@ public abstract class VMWarePlatformTests { Exception exp = null; try { - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); } catch (Exception e) { exp = e; } @@ -171,7 +172,7 @@ public abstract class VMWarePlatformTests { Exception exp = null; try { - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); } catch (Exception e) { exp = e; } @@ -190,7 +191,7 @@ public abstract class VMWarePlatformTests { Exception exp = null; try { - vmWareImpl.cloneVMFromTemplate(templateName, newVmName, computeType, computeName, datastore, description, connData); + vmWareImpl.cloneVMFromTemplate(ubuntuTemplate, newVmName, computeType, computeName, datastore, linuxCustomizationSpec, description, connData); } catch (Exception e) { exp = e; } diff --git a/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java b/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java index a28e1e5..87eb439 100644 --- a/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java +++ b/tests/Tools/vmOpsTool/VmOpsToolUnitTests.java @@ -47,7 +47,8 @@ public class VmOpsToolUnitTests { @Test public void executeActionInParallelShouldSucceedForCloneAndDeleteVMActionWithValidInputs() throws Exception { String[] cmdArgs = getCmdArgs("newVM1, newVM2", Constants.CLONE_TEMPLATE, "dummyTemplate", - Constants.COMPUTE_TYPE, "DummyCompute", Constants.COMPUTE_NAME, "DummyName", Constants.DESCRIPTION, "Dummy description"); + Constants.COMPUTE_TYPE, "DummyCompute", Constants.COMPUTE_NAME, "DummyName", + Constants.CUSTOMIZATIONSPEC, "Dummy Customization Spec", Constants.DESCRIPTION, "Dummy description"); vmOpsTool.executeActionOnVmsInParallel(cmdArgs); @@ -140,7 +141,8 @@ public class VmOpsToolUnitTests { @Test public void executeActionInParallelShouldThrowForCloneAndDeleteVMFailureOnAVM() throws Exception { String[] cmdArgs = getCmdArgs("newVM1, VMNameThatFailsInClone", Constants.CLONE_TEMPLATE, "dummyTemplate", - Constants.COMPUTE_TYPE, "DummyCompute", Constants.COMPUTE_NAME, "DummyName", Constants.DESCRIPTION, "Dummy description"); + Constants.COMPUTE_TYPE, "DummyCompute", Constants.COMPUTE_NAME, "DummyName", + Constants.CUSTOMIZATIONSPEC, "Dummy Customization Spec", Constants.DESCRIPTION, "Dummy description"); Exception exp = null;