Review comments fixes
This commit is contained in:
Родитель
07e77fdebd
Коммит
4f1bc74a59
|
@ -51,7 +51,7 @@ export class VmOperations {
|
|||
var actionName: string = tl.getInput("action", true);
|
||||
var commonArgs: string = this.getCmdCommonArgs();
|
||||
var cmdArgsForAction: string = this.getCmdArgsForAction(actionName);
|
||||
var cmdArgs = "vmOpsTool " + cmdArgsForAction + commonArgs;
|
||||
var cmdArgs = "./vmOpsTool-1.0.jar " + cmdArgsForAction + commonArgs;
|
||||
util.log("Invoking command to perform vm operations ...\n");
|
||||
return tl.exec("java", cmdArgs)
|
||||
.then((code) => {
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
public class Constants {
|
||||
|
||||
// Command line switches/options
|
||||
public static final String vCenterUrl = "-vCenterUrl";
|
||||
public static final String vCenterUserName = "-vCenterUserName";
|
||||
public static final String vCenterPassword = "-vCenterPassword";
|
||||
public static final String vmList = "-vmList";
|
||||
public static final String snapshotOps = "-snapshotOps";
|
||||
public static final String snapshotName = "-snapshotName";
|
||||
public static final String restoreSnapshotAction = "restore";
|
||||
public static final String vmOpsTool = "vmOpsTool-1.0.jar";
|
||||
|
||||
public static final String taskId = "735d144e-55fe-44d6-b687-db9031b6e70b";
|
||||
}
|
|
@ -1,20 +1,50 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
import java.util.*;
|
||||
import javax.xml.ws.BindingProvider;
|
||||
import com.vmware.vim25.*;
|
||||
|
||||
import com.vmware.vim25.DynamicProperty;
|
||||
import com.vmware.vim25.ManagedObjectReference;
|
||||
import com.vmware.vim25.ObjectContent;
|
||||
import com.vmware.vim25.ObjectSpec;
|
||||
import com.vmware.vim25.ObjectUpdate;
|
||||
import com.vmware.vim25.PropertyChange;
|
||||
import com.vmware.vim25.PropertyChangeOp;
|
||||
import com.vmware.vim25.PropertyFilterSpec;
|
||||
import com.vmware.vim25.PropertyFilterUpdate;
|
||||
import com.vmware.vim25.PropertySpec;
|
||||
import com.vmware.vim25.RetrieveOptions;
|
||||
import com.vmware.vim25.RetrieveResult;
|
||||
import com.vmware.vim25.ServiceContent;
|
||||
import com.vmware.vim25.TaskInfoState;
|
||||
import com.vmware.vim25.TraversalSpec;
|
||||
import com.vmware.vim25.UpdateSet;
|
||||
import com.vmware.vim25.UserSession;
|
||||
import com.vmware.vim25.VimPortType;
|
||||
import com.vmware.vim25.VimService;
|
||||
import com.vmware.vim25.VirtualMachineConfigInfo;
|
||||
import com.vmware.vim25.VirtualMachineSnapshotInfo;
|
||||
import com.vmware.vim25.VirtualMachineSnapshotTree;
|
||||
import com.vmware.vim25.WaitOptions;
|
||||
|
||||
public class VMWareImpl implements IVMWare {
|
||||
|
||||
private static final String taskId = "735d144e-55fe-44d6-b687-db9031b6e70b";
|
||||
private VimService vimService;
|
||||
private VimPortType vimPort;
|
||||
private ServiceContent serviceContent;
|
||||
private UserSession userSession;
|
||||
private ManagedObjectReference rootFolder;
|
||||
|
||||
// vCenter Managed Object Types and MOR properties
|
||||
private final String virtualMachine = "VirtualMachine";
|
||||
private final String snapshot = "snapshot";
|
||||
private final String config = "config";
|
||||
|
||||
public String getCurrentSnapshot(String vmName, ConnectionData connData) throws Exception {
|
||||
System.out.println("Getting current snapshot name for virtual machine ( " + vmName + " )" );
|
||||
System.out.println("Getting current snapshot name for virtual machine ( " + vmName + " )");
|
||||
connect(connData);
|
||||
ManagedObjectReference vmMor = getVMMorByName(vmName);
|
||||
return getCurrentSnapshotName(vmMor, vmName);
|
||||
|
@ -23,7 +53,7 @@ public class VMWareImpl implements IVMWare {
|
|||
public void restoreSnapshot(String vmList, String snapshotName, ConnectionData connData) throws Exception {
|
||||
|
||||
connect(connData);
|
||||
String [] vmNames = vmList.split(",");
|
||||
String[] vmNames = vmList.split(",");
|
||||
String failedVmList = "";
|
||||
|
||||
for (String vmName : vmNames) {
|
||||
|
@ -33,24 +63,24 @@ public class VMWareImpl implements IVMWare {
|
|||
|
||||
try {
|
||||
vmMor = getVMMorByName(vmName);
|
||||
}
|
||||
catch(Exception exp) {
|
||||
} catch (Exception exp) {
|
||||
failedVmList += vmName;
|
||||
continue;
|
||||
}
|
||||
|
||||
ManagedObjectReference cpMor = getSnapshotReference(vmMor, vmName, snapshotName);
|
||||
ManagedObjectReference task = vimPort.revertToSnapshotTask(cpMor, null, true);
|
||||
|
||||
|
||||
if (waitAndGetTaskResult(task)) {
|
||||
System.out.printf("Successfully reverted to snapshot [%s] On virtual machine [%s]\n", snapshotName, vmName);
|
||||
System.out.printf("Successfully reverted to snapshot [%s] On virtual machine [%s]\n", snapshotName,
|
||||
vmName);
|
||||
} else {
|
||||
System.err.printf("Failed to revert snapshot [%s] on virtual machine [%s]\n", snapshotName, vmName) ;
|
||||
System.err.printf("Failed to revert snapshot [%s] on virtual machine [%s]\n", snapshotName, vmName);
|
||||
failedVmList += vmName;
|
||||
}
|
||||
}
|
||||
|
||||
if(!failedVmList.isEmpty()) {
|
||||
|
||||
if (!failedVmList.isEmpty()) {
|
||||
System.err.printf("Failed to revert snapshot [%s] on virtual machines [%s]\n", snapshotName, failedVmList);
|
||||
throw new Exception();
|
||||
}
|
||||
|
@ -58,11 +88,11 @@ public class VMWareImpl implements IVMWare {
|
|||
}
|
||||
|
||||
private ManagedObjectReference getVMMorByName(String vmName) throws Exception {
|
||||
Map<String, ManagedObjectReference> vmsMap = getObjectsInContainerByType(this.rootFolder, "VirtualMachine");
|
||||
Map<String, ManagedObjectReference> vmsMap = getObjectsInContainerByType(rootFolder, virtualMachine);
|
||||
|
||||
if(!vmsMap.containsKey(vmName))
|
||||
{
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_VmNotFound;TaskId=%s;]\n", taskId);
|
||||
if (!vmsMap.containsKey(vmName)) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_VmNotFound;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println("Virtual machine with name " + vmName + " not found.");
|
||||
throw new Exception("Virtual machine with name " + vmName + " not found.");
|
||||
}
|
||||
|
@ -74,29 +104,31 @@ public class VMWareImpl implements IVMWare {
|
|||
boolean retVal = false;
|
||||
|
||||
System.out.println("Waiting for operation completion.");
|
||||
Object[] result = waitForTaskResult(task, new String[]{"info.state", "info.error"}, new String[]{"state"},
|
||||
new Object[][]{new Object[]{TaskInfoState.SUCCESS, TaskInfoState.ERROR}});
|
||||
Object[] result = waitForTaskResult(task, new String[] { "info.state", "info.error" }, new String[] { "state" },
|
||||
new Object[][] { new Object[] { TaskInfoState.SUCCESS, TaskInfoState.ERROR } });
|
||||
|
||||
if (result[0].equals(TaskInfoState.SUCCESS)) {
|
||||
retVal = true;
|
||||
}
|
||||
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
private Object[] waitForTaskResult(ManagedObjectReference taskMor, String[] filterProps, String[] endWaitProps, Object[][] expectedVals)
|
||||
throws Exception {
|
||||
private Object[] waitForTaskResult(ManagedObjectReference taskMor, String[] filterProps, String[] endWaitProps,
|
||||
Object[][] expectedVals) throws Exception {
|
||||
try {
|
||||
|
||||
Object[] endVals = new Object[endWaitProps.length];
|
||||
Object[] filterVals = new Object[filterProps.length];
|
||||
String version = "";
|
||||
PropertyFilterSpec filterSpec = createPropFilterSpecForObject(taskMor, filterProps);
|
||||
ManagedObjectReference filterSpecRef = vimPort.createFilter(serviceContent.getPropertyCollector(), filterSpec, true);
|
||||
ManagedObjectReference filterSpecRef = vimPort.createFilter(serviceContent.getPropertyCollector(),
|
||||
filterSpec, true);
|
||||
boolean reached = false;
|
||||
|
||||
while (!reached) {
|
||||
UpdateSet updateSet = vimPort.waitForUpdatesEx(serviceContent.getPropertyCollector(), version, new WaitOptions());
|
||||
UpdateSet updateSet = vimPort.waitForUpdatesEx(serviceContent.getPropertyCollector(), version,
|
||||
new WaitOptions());
|
||||
if (updateSet == null || updateSet.getFilterSet() == null) {
|
||||
continue;
|
||||
}
|
||||
|
@ -104,12 +136,12 @@ public class VMWareImpl implements IVMWare {
|
|||
List<PropertyFilterUpdate> filterUpdateList = updateSet.getFilterSet();
|
||||
|
||||
for (PropertyFilterUpdate filterUpdate : filterUpdateList) {
|
||||
if(filterUpdate == null || filterUpdate.getObjectSet() == null) {
|
||||
if (filterUpdate == null || filterUpdate.getObjectSet() == null) {
|
||||
continue;
|
||||
}
|
||||
List<ObjectUpdate> objectUpdateList = filterUpdate.getObjectSet();
|
||||
for (ObjectUpdate objectUpdate : objectUpdateList) {
|
||||
if(objectUpdate == null || objectUpdate.getChangeSet() == null) {
|
||||
if (objectUpdate == null || objectUpdate.getChangeSet() == null) {
|
||||
continue;
|
||||
}
|
||||
List<PropertyChange> propChangeList = objectUpdate.getChangeSet();
|
||||
|
@ -130,9 +162,9 @@ public class VMWareImpl implements IVMWare {
|
|||
}
|
||||
vimPort.destroyPropertyFilter(filterSpecRef);
|
||||
return filterVals;
|
||||
}
|
||||
catch(Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_WaitForResultFailed;TaskId=%s;]\n", taskId);
|
||||
} catch (Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_WaitForResultFailed;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println("Failed to get operation result: " + exp.getMessage());
|
||||
throw exp;
|
||||
}
|
||||
|
@ -150,9 +182,11 @@ public class VMWareImpl implements IVMWare {
|
|||
}
|
||||
}
|
||||
|
||||
private ManagedObjectReference getSnapshotReference(ManagedObjectReference vmMor, String vmName, String snapshotName) throws Exception {
|
||||
private ManagedObjectReference getSnapshotReference(ManagedObjectReference vmMor, String vmName,
|
||||
String snapshotName) throws Exception {
|
||||
System.out.printf("Querying snapshot information for virtual machine (%s)\n", vmName);
|
||||
VirtualMachineSnapshotInfo vmSnapshotInfo = (VirtualMachineSnapshotInfo) getMorProperties(vmMor, new String[] { "snapshot" }).get("snapshot");
|
||||
VirtualMachineSnapshotInfo vmSnapshotInfo = (VirtualMachineSnapshotInfo) getMorProperties(vmMor,
|
||||
new String[] { snapshot }).get(snapshot);
|
||||
ManagedObjectReference snapshotMor = null;
|
||||
String snapshotNotFoundErr = "No snapshot found on virtual machine (" + vmName + ") with name " + snapshotName;
|
||||
|
||||
|
@ -161,12 +195,14 @@ public class VMWareImpl implements IVMWare {
|
|||
snapshotMor = findSnapshotInTree(vmRootSnapshotList, snapshotName);
|
||||
|
||||
if (snapshotMor == null) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_SnapshotNotFound;TaskId=%s;]\n", taskId);
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_SnapshotNotFound;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println(snapshotNotFoundErr);
|
||||
throw new Exception();
|
||||
}
|
||||
} else {
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_SnapshotNotFound;TaskId=%s;]\n", taskId);
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_SnapshotNotFound;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println(snapshotNotFoundErr);
|
||||
throw new Exception();
|
||||
}
|
||||
|
@ -175,7 +211,8 @@ public class VMWareImpl implements IVMWare {
|
|||
|
||||
private String getCurrentSnapshotName(ManagedObjectReference vmMor, String vmName) throws Exception {
|
||||
System.out.printf("Querying snapshot information for virtual machine (%s)\n", vmName);
|
||||
VirtualMachineSnapshotInfo vmSnapshotInfo = (VirtualMachineSnapshotInfo) getMorProperties(vmMor, new String[] { "snapshot" }).get("snapshot");
|
||||
VirtualMachineSnapshotInfo vmSnapshotInfo = (VirtualMachineSnapshotInfo) getMorProperties(vmMor,
|
||||
new String[] { snapshot }).get(snapshot);
|
||||
String currentSnapshotName = "";
|
||||
|
||||
if (vmSnapshotInfo != null) {
|
||||
|
@ -183,14 +220,14 @@ public class VMWareImpl implements IVMWare {
|
|||
List<VirtualMachineSnapshotTree> vmRootSnapshotList = vmSnapshotInfo.getRootSnapshotList();
|
||||
ManagedObjectReference currentSnapshotMor = vmSnapshotInfo.getCurrentSnapshot();
|
||||
currentSnapshotName = getCurrentSnapshotNameFromTree(vmRootSnapshotList, currentSnapshotMor);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
System.out.println("Snapshot info is null!!");
|
||||
}
|
||||
return currentSnapshotName;
|
||||
}
|
||||
|
||||
private String getCurrentSnapshotNameFromTree(List<VirtualMachineSnapshotTree> vmRootSnapshotList, ManagedObjectReference currentSnapshotMor) {
|
||||
private String getCurrentSnapshotNameFromTree(List<VirtualMachineSnapshotTree> vmRootSnapshotList,
|
||||
ManagedObjectReference currentSnapshotMor) {
|
||||
|
||||
String currentSnapshotName = "";
|
||||
if (vmRootSnapshotList == null) {
|
||||
|
@ -211,7 +248,8 @@ public class VMWareImpl implements IVMWare {
|
|||
return currentSnapshotName;
|
||||
}
|
||||
|
||||
private ManagedObjectReference findSnapshotInTree(List<VirtualMachineSnapshotTree> vmSnapshotList, String snapshotName) {
|
||||
private ManagedObjectReference findSnapshotInTree(List<VirtualMachineSnapshotTree> vmSnapshotList,
|
||||
String snapshotName) {
|
||||
ManagedObjectReference snapshotMor = null;
|
||||
|
||||
if (vmSnapshotList == null) {
|
||||
|
@ -236,9 +274,11 @@ public class VMWareImpl implements IVMWare {
|
|||
PropertyFilterSpec propFilterSpec = createPropFilterSpecForObject(vmMor, propList);
|
||||
RetrieveResult results = null;
|
||||
try {
|
||||
results = vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(), Arrays.asList(propFilterSpec), new RetrieveOptions());
|
||||
} catch(Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_RetriveObjectPropertiesFailed;TaskId=%s;]\n", taskId);
|
||||
results = vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(), Arrays.asList(propFilterSpec),
|
||||
new RetrieveOptions());
|
||||
} catch (Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_RetriveObjectPropertiesFailed;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println("Failed to properties for managed object : " + exp.getMessage());
|
||||
throw exp;
|
||||
}
|
||||
|
@ -270,23 +310,27 @@ public class VMWareImpl implements IVMWare {
|
|||
return propFilterSpec;
|
||||
}
|
||||
|
||||
private Map<String, ManagedObjectReference> getObjectsInContainerByType(ManagedObjectReference container, String morefType) throws Exception {
|
||||
PropertyFilterSpec propertyFilterSpecs = createRecursiveFilterSpec(container, morefType, new String [] {"config" });
|
||||
private Map<String, ManagedObjectReference> getObjectsInContainerByType(ManagedObjectReference container,
|
||||
String morefType) throws Exception {
|
||||
PropertyFilterSpec propertyFilterSpecs = createRecursiveFilterSpec(container, morefType,
|
||||
new String[] { config });
|
||||
|
||||
try {
|
||||
System.out.printf("Querying %s objects on vCenter server.\n", morefType);
|
||||
RetrieveResult results = vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(), Arrays.asList(propertyFilterSpecs), new RetrieveOptions());
|
||||
RetrieveResult results = vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(),
|
||||
Arrays.asList(propertyFilterSpecs), new RetrieveOptions());
|
||||
String token = null;
|
||||
final Map<String, ManagedObjectReference> morMap = new HashMap<String, ManagedObjectReference>();
|
||||
token = createMap(results, morMap);
|
||||
|
||||
while ( token != null && !token.isEmpty()) {
|
||||
while (token != null && !token.isEmpty()) {
|
||||
results = vimPort.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
|
||||
token = createMap(results, morMap);
|
||||
}
|
||||
return morMap;
|
||||
} catch (Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_QueryObjectsFailed;TaskId=%s;]\n", taskId);
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_QueryObjectsFailed;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println("Failed to fetch objects: " + exp.getMessage());
|
||||
throw exp;
|
||||
}
|
||||
|
@ -295,11 +339,12 @@ public class VMWareImpl implements IVMWare {
|
|||
private String createMap(final RetrieveResult results, final Map<String, ManagedObjectReference> morMap) {
|
||||
String token = null;
|
||||
|
||||
if(results != null) {
|
||||
if (results != null) {
|
||||
token = results.getToken();
|
||||
for (ObjectContent objectContent : results.getObjects()) {
|
||||
ManagedObjectReference mor = objectContent.getObj();
|
||||
VirtualMachineConfigInfo vmConfig = (VirtualMachineConfigInfo) objectContent.getPropSet().get(0).getVal();
|
||||
VirtualMachineConfigInfo vmConfig = (VirtualMachineConfigInfo) objectContent.getPropSet().get(0)
|
||||
.getVal();
|
||||
if (vmConfig != null && !vmConfig.isTemplate()) {
|
||||
morMap.put(vmConfig.getName(), mor);
|
||||
}
|
||||
|
@ -308,39 +353,45 @@ public class VMWareImpl implements IVMWare {
|
|||
return token;
|
||||
}
|
||||
|
||||
private PropertyFilterSpec createRecursiveFilterSpec(ManagedObjectReference container, String morefType, String[] filterProps) throws Exception {
|
||||
private PropertyFilterSpec createRecursiveFilterSpec(ManagedObjectReference container, String morefType,
|
||||
String[] filterProps) throws Exception {
|
||||
try {
|
||||
ManagedObjectReference viewManager = serviceContent.getViewManager();
|
||||
ManagedObjectReference containerView = vimPort.createContainerView(viewManager, container, Arrays.asList(morefType), true);
|
||||
ManagedObjectReference containerView = vimPort.createContainerView(viewManager, container,
|
||||
Arrays.asList(morefType), true);
|
||||
|
||||
// Create property specification to specify list properties to extract from given object type
|
||||
// Create property specification to specify list properties to
|
||||
// extract from given object type
|
||||
PropertySpec propSpec = new PropertySpec();
|
||||
propSpec.setAll(false);
|
||||
propSpec.getPathSet().addAll(Arrays.asList(filterProps));
|
||||
propSpec.setType(morefType);
|
||||
|
||||
// Create traversal specification to specify inventory navigation path
|
||||
// Create traversal specification to specify inventory navigation
|
||||
// path
|
||||
TraversalSpec traversalSpc = new TraversalSpec();
|
||||
traversalSpc.setName("view");
|
||||
traversalSpc.setPath("view");
|
||||
traversalSpc.setSkip(false);
|
||||
traversalSpc.setType("ContainerView");
|
||||
|
||||
// Create object specification to specify root location and associate a traversal spec
|
||||
// Create object specification to specify root location and
|
||||
// associate a traversal specification
|
||||
ObjectSpec objSpec = new ObjectSpec();
|
||||
objSpec.setObj(containerView);
|
||||
objSpec.setSkip(false);
|
||||
objSpec.getSelectSet().add(traversalSpc);
|
||||
|
||||
// Create property filter specification, then set property spec and object spec
|
||||
|
||||
// Create property filter specification, then set property spec and
|
||||
// object spec
|
||||
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
|
||||
propertyFilterSpec.getPropSet().add(propSpec);
|
||||
propertyFilterSpec.getObjectSet().add(objSpec);
|
||||
|
||||
return propertyFilterSpec;
|
||||
}
|
||||
catch(Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_CreateFilterSpecFailed;TaskId=%s;]\n", taskId);
|
||||
} catch (Exception exp) {
|
||||
System.out.printf("##vso[task.logissue type=error;code=PREREQ_CreateFilterSpecFailed;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.println("Failed to create filter spec: " + exp.getMessage());
|
||||
throw exp;
|
||||
}
|
||||
|
@ -348,7 +399,7 @@ public class VMWareImpl implements IVMWare {
|
|||
|
||||
public void connect(ConnectionData connData) throws Exception {
|
||||
try {
|
||||
if(!isSessionActive()) {
|
||||
if (!isSessionActive()) {
|
||||
System.out.println("No active session found.. establishing new session.");
|
||||
vimService = new VimService();
|
||||
vimPort = vimService.getVimPort();
|
||||
|
@ -359,15 +410,16 @@ public class VMWareImpl implements IVMWare {
|
|||
ManagedObjectReference serviceInstance = new ManagedObjectReference();
|
||||
serviceInstance.setType("ServiceInstance");
|
||||
serviceInstance.setValue("ServiceInstance");
|
||||
|
||||
|
||||
serviceContent = vimPort.retrieveServiceContent(serviceInstance);
|
||||
rootFolder = serviceContent.getRootFolder();
|
||||
userSession = vimPort.login(serviceContent.getSessionManager(), connData.userName,connData.password, null);
|
||||
userSession = vimPort.login(serviceContent.getSessionManager(), connData.userName, connData.password,
|
||||
null);
|
||||
}
|
||||
}
|
||||
catch (Exception exp) {
|
||||
} catch (Exception exp) {
|
||||
System.err.println("Failed to connect: " + exp.getMessage());
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_ConnectionFailed;TaskId=%s;]\n", taskId);
|
||||
System.out.printf("##vso[task.logissue type=error;code=USERINPUT_ConnectionFailed;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
throw exp;
|
||||
}
|
||||
System.out.println("Successfully established session with vCenter server.");
|
||||
|
@ -377,7 +429,7 @@ public class VMWareImpl implements IVMWare {
|
|||
private boolean isSessionActive() {
|
||||
System.out.println("Checking for active session...");
|
||||
if (userSession == null) {
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
long startTime = userSession.getLastActiveTime().toGregorianCalendar().getTime().getTime();
|
||||
return new Date().getTime() < startTime + 30 * 60 * 1000;
|
||||
|
|
|
@ -1,66 +1,68 @@
|
|||
|
||||
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class VmOpsTool {
|
||||
|
||||
private IVMWare vmWareImpl;
|
||||
private String taskId = "735d144e-55fe-44d6-b687-db9031b6e70b";
|
||||
|
||||
public VmOpsTool(IVMWare vmWareImpl) {
|
||||
this.vmWareImpl = vmWareImpl;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
new VmOpsTool(new VMWareImpl()).executeAction(args);
|
||||
}
|
||||
catch (Exception exp) {
|
||||
} catch (Exception exp) {
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the command line arguments and performs the vm operation
|
||||
* @param args command line arguments
|
||||
* @throws Exception on failure
|
||||
* parse the command line arguments and performs the vm operation
|
||||
*
|
||||
* @param args
|
||||
* command line arguments
|
||||
* @throws Exception
|
||||
* on failure
|
||||
*/
|
||||
public void executeAction(String[] args) throws Exception {
|
||||
|
||||
|
||||
Map<String, String> argsMap = parseCmdLine(args);
|
||||
|
||||
String vCenterUrl = argsMap.get("-vCenterUrl");
|
||||
String vCenterUserName = argsMap.get("-vCenterUserName");
|
||||
String vCenterPassword = argsMap.get("-vCenterPassword");
|
||||
String vmList = argsMap.get("-vmList");
|
||||
String vCenterUrl = argsMap.get(Constants.vCenterUrl);
|
||||
String vCenterUserName = argsMap.get(Constants.vCenterUserName);
|
||||
String vCenterPassword = argsMap.get(Constants.vCenterPassword);
|
||||
String vmList = argsMap.get(Constants.vmList);
|
||||
|
||||
ConnectionData connData = new ConnectionData(vCenterUrl, vCenterUserName, vCenterPassword);
|
||||
ConnectionData connData = new ConnectionData(vCenterUrl, vCenterUserName, vCenterPassword);
|
||||
|
||||
if(argsMap.containsKey("-snapshotOps")) {
|
||||
String actionName = argsMap.get("-snapshotOps");
|
||||
String snapshotName = argsMap.get("-snapshotName");
|
||||
if(actionName.equalsIgnoreCase("restore")) {
|
||||
if (argsMap.containsKey(Constants.snapshotOps)) {
|
||||
String actionName = argsMap.get(Constants.snapshotOps);
|
||||
String snapshotName = argsMap.get(Constants.snapshotName);
|
||||
if (actionName.equalsIgnoreCase(Constants.restoreSnapshotAction)) {
|
||||
System.out.printf("Initiating restore snapshot operation on vmList[%s]\n", vmList);
|
||||
vmWareImpl.restoreSnapshot(vmList, snapshotName, connData);
|
||||
}
|
||||
else {
|
||||
System.out.printf("##vso[task.logissue type=error;code=INFRA_InvalidSnapshotOperation;TaskId=%s;]\n", taskId );
|
||||
} else {
|
||||
System.out.printf("##vso[task.logissue type=error;code=INFRA_InvalidSnapshotOperation;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.printf("Invalid action name(%s) for snapshot operation\n", actionName);
|
||||
throw new Exception("Invalid action name(" + actionName + ") for snapshot operation");
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.printf("##vso[task.logissue type=error;code=INFRA_InvalidOperation;TaskId=%s;]\n", taskId );
|
||||
} else {
|
||||
System.out.printf("##vso[task.logissue type=error;code=INFRA_InvalidOperation;TaskId=%s;]\n",
|
||||
Constants.taskId);
|
||||
System.err.printf("Invalid action input for the operation.\n");
|
||||
throw new Exception("Invalid action input for the operation.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Duplicate keys, key missing, value missing all those cases we are not expecting
|
||||
* as the command construction is done by type script layer. Hence parse logic is not
|
||||
* handling any of these cases
|
||||
* @param cmdArgs array of input command line options
|
||||
* Duplicate keys, key missing, value missing all those cases we are not
|
||||
* expecting as the command construction is done by type script layer. Hence
|
||||
* parse logic is not handling any of these cases
|
||||
*
|
||||
* @param cmdArgs
|
||||
* array of input command line options
|
||||
* @return map of key value pairs of input parameters
|
||||
*/
|
||||
public static Map<String, String> parseCmdLine(String[] cmdArgs) {
|
||||
|
@ -70,13 +72,11 @@ public class VmOpsTool {
|
|||
|
||||
System.out.println("Parsing input parameters...");
|
||||
for (String arg : cmdArgs) {
|
||||
if(arg.equals("vmOpsTool")) {
|
||||
if (arg.equals(Constants.vmOpsTool)) {
|
||||
continue;
|
||||
}
|
||||
else if (arg.charAt(0) == '-') {
|
||||
} else if (arg.charAt(0) == '-') {
|
||||
key = arg;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
value = arg;
|
||||
argsMap.put(key, value);
|
||||
}
|
||||
|
|
|
@ -164,7 +164,7 @@ describe("runMain", (): void => {
|
|||
|
||||
var commonArgs = " -vCenterUrl \"http://localhost:8080\" -vCenterUserName \"dummydomain\\dummyuser\" -vCenterPassword \" pas\\\" w,o ;d\" ";
|
||||
var cmdArgsForAction = " -snapshotOps restore -snapshotName \"dummysnapshot\"";
|
||||
var cmdArgs = "vmOpsTool " + cmdArgsForAction + commonArgs;
|
||||
var cmdArgs = "./vmOpsTool-1.0.jar " + cmdArgsForAction + commonArgs;
|
||||
var actionName = "RestoreSnapshot";
|
||||
|
||||
it("Should return 0 on successful exection of the command", (done): void => {
|
||||
|
|
|
@ -7,6 +7,6 @@ public class VMWareImplPlatformTests extends VMWarePlatformTests {
|
|||
|
||||
@Override
|
||||
public String getvCenterUrl() {
|
||||
return "https://idcvstt-lab325.fareast.corp.microsoft.com/sdk/vimservice";
|
||||
return System.getenv("VCENTER_URL") + "/sdk/vimservice";
|
||||
}
|
||||
}
|
|
@ -6,41 +6,44 @@ import java.util.Map;
|
|||
import org.junit.Test;
|
||||
|
||||
public class VmOpsToolUnitTests {
|
||||
|
||||
|
||||
private InMemoryVMWareImpl vmWareImpl = new InMemoryVMWareImpl();
|
||||
private VmOpsTool vmOpsTool = new VmOpsTool(vmWareImpl);
|
||||
private String vCenterUrl = "https://localhost:8080/sdk/vimservice";
|
||||
private String vCenterUserName = "Administrator";
|
||||
private String vCenterPassword = "Password~1";
|
||||
|
||||
private String vmSnapshotName = "Snapshot1";
|
||||
|
||||
private ConnectionData connData = new ConnectionData(vCenterUrl, vCenterUserName, vCenterPassword);
|
||||
|
||||
@Test
|
||||
public void parseCmdArgsWithAllRequiredInputs() {
|
||||
String[] cmdArgs = {"vmOpsTool", "-vCenterUrl", "http://localhost:8080"};
|
||||
String[] cmdArgs = { Constants.vmOpsTool, Constants.vCenterUrl, vCenterUrl };
|
||||
|
||||
Map<String, String> argsMap = VmOpsTool.parseCmdLine(cmdArgs);
|
||||
|
||||
assertThat(argsMap.size()).isEqualTo(1);
|
||||
assertThat(argsMap.containsKey("vmOpsTool")).isEqualTo(false);
|
||||
assertThat(argsMap.containsKey("-vCenterUrl")).isEqualTo(true);
|
||||
assertThat(argsMap.get("-vCenterUrl")).isEqualTo("http://localhost:8080");
|
||||
assertThat(argsMap.containsKey(Constants.vmOpsTool)).isEqualTo(false);
|
||||
assertThat(argsMap.containsKey(Constants.vCenterUrl)).isEqualTo(true);
|
||||
assertThat(argsMap.get(Constants.vCenterUrl)).isEqualTo(vCenterUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeActionShouldRestoreSnapshotForRestoreOperation() throws Exception {
|
||||
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm2", "-snapshotOps", "restore", "Snapshot1");
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm2", Constants.snapshotOps, Constants.restoreSnapshotAction,
|
||||
vmSnapshotName);
|
||||
|
||||
vmOpsTool.executeAction(cmdArgs);
|
||||
|
||||
assertThat(vmWareImpl.getCurrentSnapshot("vm1", connData)).isEqualTo("Snapshot1");
|
||||
assertThat(vmWareImpl.getCurrentSnapshot("vm2", connData)).isEqualTo("Snapshot1");
|
||||
assertThat(vmWareImpl.getCurrentSnapshot("vm1", connData)).isEqualTo(vmSnapshotName);
|
||||
assertThat(vmWareImpl.getCurrentSnapshot("vm2", connData)).isEqualTo(vmSnapshotName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeActionShouldThrowForRestoreSnapshotFailureOnAVM() {
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm3", "-snapshotOps", "restore", "Snapshot1");
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm3", Constants.snapshotOps, Constants.restoreSnapshotAction,
|
||||
vmSnapshotName);
|
||||
Exception exp = null;
|
||||
|
||||
try {
|
||||
|
@ -50,38 +53,38 @@ public class VmOpsToolUnitTests {
|
|||
}
|
||||
|
||||
assertThat(exp).isNotNull();
|
||||
assertThat(vmWareImpl.getCurrentSnapshot("vm1", connData)).isEqualTo("Snapshot1");
|
||||
assertThat(vmWareImpl.getCurrentSnapshot("vm1", connData)).isEqualTo(vmSnapshotName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeActionInvalidSnapshotOperationShouldFail() {
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm2", "-snapshotOps", "invalid", "Snapshot1");
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm2", Constants.snapshotOps, "invalid", vmSnapshotName);
|
||||
Exception exp = null;
|
||||
|
||||
try {
|
||||
vmOpsTool.executeAction(cmdArgs);
|
||||
} catch (Exception e) {
|
||||
exp = e;
|
||||
}
|
||||
}
|
||||
assertThat(exp).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeActionForInvalidActionNameShouldFail() {
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm2", "-invalidOps", "restore", "Snapshot1");
|
||||
String[] cmdArgs = getCmdArgs("vm1, vm2", "-invalidOps", Constants.restoreSnapshotAction, vmSnapshotName);
|
||||
Exception exp = null;
|
||||
|
||||
try {
|
||||
vmOpsTool.executeAction(cmdArgs);
|
||||
} catch (Exception e) {
|
||||
exp = e;
|
||||
}
|
||||
}
|
||||
assertThat(exp).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeActionShouldIfRequiredInputIsNotPresent() {
|
||||
String[] cmdArgs = new String[] {"vmOpsTool"};
|
||||
String[] cmdArgs = new String[] { Constants.vmOpsTool };
|
||||
Exception exp = null;
|
||||
|
||||
try {
|
||||
|
@ -92,11 +95,11 @@ public class VmOpsToolUnitTests {
|
|||
|
||||
assertThat(exp).isNotNull();
|
||||
}
|
||||
|
||||
private String[] getCmdArgs(String vmList, String actionName , String actionOption, String snapshotName) {
|
||||
String[] cmdArgs = new String[] {"vmOpsTool", "-vCenterUrl", "http://localhost:8080",
|
||||
"-vCenterUserName", "dummyuser", "-vCenterPassword", "dummypassword", "-vmList",
|
||||
vmList, actionName, actionOption, "-snapshotName", snapshotName};
|
||||
|
||||
private String[] getCmdArgs(String vmList, String actionName, String actionOption, String snapshotName) {
|
||||
String[] cmdArgs = new String[] { Constants.vmOpsTool, Constants.vCenterUrl, vCenterUrl,
|
||||
Constants.vCenterUserName, "dummyuser", Constants.vCenterPassword, "dummypassword", Constants.vmList,
|
||||
vmList, actionName, actionOption, Constants.snapshotName, snapshotName };
|
||||
return cmdArgs;
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче