package com.vmware.samples.vm; import java.net.*; import java.rmi.RemoteException; import java.util.*; import java.io.*; import com.vmware.vim.*; import com.vmware.apputils.*; import com.vmware.apputils.vim.*; /** *
*This sample implements a function that
*
*
*Parameters:
*DatacenterName  [required] : DatacenterName
*vmPath          [required] : A path to the VM inventory
*DatastoreName   [required] : Datastore for VM
*CloneName       [required] : Name of the Clone
*IPAddress		 [required] : IP address of the cloned VM
*
*
*Command: To clone a virtual machine
*run.bat com.vmware.samples.vm.VMClone --url 
*--username  --password  --DatacenterName 
*--vmPath   --CloneName  --IPAddress 
*
*/ public class VMCloneSetDataStoreSetIP { static VimPortType service; // All Methods private static ServiceContent content; private static AppUtil cb = null; private String[][] typeInfo; Log log = new Log(); ServiceUtil svc = new ServiceUtil(); private static OptionSpec[] constructOptions() { OptionSpec [] useroptions = new OptionSpec[5]; useroptions[0] = new OptionSpec("DatacenterName","String",1 ,"Name of the Datacenter" ,null); useroptions[1] = new OptionSpec("vmPath","String",1, "Path to the VM inventory", null); useroptions[2] = new OptionSpec("CloneName","String",1, "Name of the Clone", null); useroptions[3] = new OptionSpec("DatastoreName","String",0, "Name of the Datastore", null); useroptions[4] = new OptionSpec("IPAddress","String",0, "IP address of the clone", null); return useroptions; } private void VMCloneSetDataStoreSetIP() throws Exception{ String cloneName = cb.get_option("CloneName"); String vmPath = cb.get_option("vmPath"); String datacenterName= cb.get_option("DatacenterName"); String datastoreName= cb.get_option("DatastoreName"); String ipAddress = cb.get_option("IPAddress"); com.vmware.apputils.vim.ServiceConnection S1 = cb.getConnection(); content = S1.getServiceContent(); service = S1.getService(); // Find the Datacenter reference by using findByInventoryPath(). ManagedObjectReference datacenterRef = S1.getService().findByInventoryPath(content.getSearchIndex(), datacenterName); if (datacenterRef == null) { System.out.println("The specified datacenter is not found"); return; } // Find the virtual machine folder for this datacenter. ManagedObjectReference vmFolderRef = (ManagedObjectReference) getObjectProperty(datacenterRef, "vmFolder"); if (vmFolderRef == null) { System.out.println("The virtual machine is not found"); return; } ManagedObjectReference vmRef = S1.getService().findByInventoryPath(content.getSearchIndex(), vmPath); if (vmRef == null) { System.out.println("The virtual machine is not found"); return; } VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec(); VirtualMachineConfigInfo vmConfigInfo = (VirtualMachineConfigInfo) getObjectProperty(vmRef, "config"); VirtualMachineRuntimeInfo vmRuntimeInfo = (VirtualMachineRuntimeInfo) getObjectProperty(vmRef, "runtime"); ManagedObjectReference hostRef = vmRuntimeInfo.getHost(); if (hostRef == null) { System.out.println("Host not found for the template"); } else { ManagedObjectReference hostParentCompResRef = (ManagedObjectReference) getObjectProperty(hostRef, "parent"); if(hostParentCompResRef.getType().compareTo("ComputeResource") == 0) { // ArrayOfManagedObjectReference datastores = (ArrayOfManagedObjectReference)getObjectProperty(hostParentCompResRef, "datastore"); ManagedObjectReference [] datastoreList = datastores.getManagedObjectReference(); ManagedObjectReference datastoreRef = null; ManagedObjectReference datastoreAllRef = null; boolean dsFound = false; for (int i = 0; i < datastoreList.length; i++) { datastoreAllRef = datastoreList[i]; DatastoreInfo dsInfo = (DatastoreInfo)getObjectProperty(datastoreAllRef, "info"); if (dsInfo.getName().equals(datastoreName)) { datastoreRef = datastoreAllRef; dsFound = true; relocSpec.setDatastore(datastoreRef); break; } } if(dsFound == false){ System.out.println("No datastore specified or Specified Datastore not found on the host"); } ManagedObjectReference resourcePoolRootRef = (ManagedObjectReference) getObjectProperty(hostParentCompResRef, "resourcePool"); if (vmConfigInfo.isTemplate()) { if (resourcePoolRootRef == null) { System.out.println("Resource pool not found for the template"); return; } else { relocSpec.setPool(resourcePoolRootRef); } } else { System.out.println("Parent of Host system was not of type ComputeResource"); } } } CustomizationSpec custSpec = new CustomizationSpec(); CustomizationAdapterMapping[] custAdapMapList = new CustomizationAdapterMapping[1]; CustomizationAdapterMapping custAdapMap = new CustomizationAdapterMapping(); CustomizationIPSettings custIPSettings = new CustomizationIPSettings(); CustomizationFixedIp custFixedIp = new CustomizationFixedIp(); custFixedIp.setIpAddress(ipAddress); custIPSettings.setIp(custFixedIp); custAdapMap.setAdapter(custIPSettings); custAdapMapList[0] = custAdapMap; custSpec.setNicSettingMap(custAdapMapList); CustomizationGlobalIPSettings custGlobalIPSetting = new CustomizationGlobalIPSettings(); custSpec.setGlobalIPSettings(custGlobalIPSetting); CustomizationSysprep custSysprep = new CustomizationSysprep(); CustomizationGuiUnattended guiUnattended = new CustomizationGuiUnattended(); // guiUnattended.setAutoLogon(true); guiUnattended.setAutoLogonCount(0); guiUnattended.setTimeZone(190); CustomizationPassword custPasswd = new CustomizationPassword(); custPasswd.setPlainText(true); custPasswd.setValue("user"); // CustomizationIdentification custIdentification = new CustomizationIdentification(); // custIdentification.setDomainAdmin("user"); custIdentification.setDomainAdminPassword(custPasswd); custIdentification.setJoinDomain("ibm"); // CustomizationUserData custUserData = new CustomizationUserData(); CustomizationFixedName custFixedName = new CustomizationFixedName(); custFixedName.setName("mycomputer"); custUserData.setComputerName(custFixedName); custUserData.setFullName("stephanl"); custUserData.setOrgName("ibm"); custUserData.setProductId("XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"); custSysprep.setUserData(custUserData); custSysprep.setGuiUnattended(guiUnattended); custSysprep.setIdentification(custIdentification); custSpec.setIdentity(custSysprep); cloneSpec.setLocation(relocSpec); cloneSpec.setPowerOn(false); cloneSpec.setTemplate(false); cloneSpec.setCustomization(custSpec); String clonedName = cloneName; System.out.println("Launching clone task to create a clone: " + clonedName); try { ManagedObjectReference cloneTask = service.cloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec); String status = cb.getServiceUtil().waitForTask(cloneTask); if(status.equalsIgnoreCase("failure")) { System.out.println("Failure -: Virtual Machine cannot be cloned"); } if(status.equalsIgnoreCase("sucess")) { System.out.println("Virtual Machine Cloned successfully."); } } catch(Exception e) { if(e instanceof org.apache.axis.AxisFault) { org.apache.axis.AxisFault fault = (org.apache.axis.AxisFault)e; org.w3c.dom.Element [] errors = fault.getFaultDetails(); for(int i=0; i