Merge pull request #51 from Microsoft/users/subraman/poweroffvm

Added power off operation
This commit is contained in:
Subrahmanyam Mandavilli 2016-02-25 00:20:57 +05:30
Родитель d2a4e31d75 f27c215be3
Коммит 3fd6e220dc
11 изменённых файлов: 80 добавлений и 7 удалений

Просмотреть файл

@ -12,7 +12,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 1,
"Minor": 2,
"Patch": 0
},
"demands": [
@ -44,6 +44,7 @@
"Delete Snapshot of Virtual Machines": "Delete Snapshot of Virtual Machines",
"Power On Virtual Machines": "Power on Virtual Machines",
"Shutdown Virtual Machines": "Shutdown Virtual Machines",
"Power off Virtual Machines": "Power off Virtual Machines",
"Delete Virtual Machines": "Delete Virtual Machines"
}
},

Просмотреть файл

@ -78,6 +78,9 @@ export class VmOperations {
case "Shutdown Virtual Machines":
cmdArgs += " -powerops shutdown";
break;
case "Power off Virtual Machines":
cmdArgs += " -powerops poweroff";
break;
default:
tl.error("Invalid action name : " + actionName);
tl.exit(1);

Просмотреть файл

@ -26,6 +26,7 @@ public class Constants {
public static final String DELETE_VM_ACTION = "delete";
public static final String POWER_ON_VM_ACTION = "poweron";
public static final String SHUTDOWN_VM_ACTION = "shutdown";
public static final String POWER_OFF_VM_ACTION = "poweroff";
public static final String TASK_ID = "735d144e-55fe-44d6-b687-db9031b6e70b";
public static final int START_STOP_MAX_WAIT_IN_MINUTES = 5;

Просмотреть файл

@ -102,4 +102,11 @@ public interface IVMWare {
* @throws Exception
*/
void shutdownVM(String vmName, ConnectionData connData) throws Exception;
/**
* @param vmName name of the virtual machine
* @param connData vCenter connection information
* @throws Exception
*/
void powerOffVM(String vmName, ConnectionData connData) throws Exception;
}

Просмотреть файл

@ -166,6 +166,21 @@ public class VMWareImpl implements IVMWare {
System.out.printf("Virtual machine [%s] is already shutdowned.\n", vmName);
}
public void powerOffVM(String vmName, ConnectionData connData) throws Exception {
connect(connData);
if (isVMPoweredOn(vmName, connData)) {
ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false);
ManagedObjectReference powerOffTask = vimPort.powerOffVMTask(vmMor);
System.out.printf("Waiting for virtual machine [%s] to power off.\n", vmName);
if (!waitAndGetTaskResult(powerOffTask)) {
throw new Exception(
String.format("Failed to power off virtual machine [%s].\n", vmName));
}
System.out.printf("Successfully powered off the virtual machine [%s].\n", vmName);
}
System.out.printf("Virtual machine [%s] is already powered off.\n", vmName);
}
public void deleteVM(String vmName, ConnectionData connData) throws Exception {
connect(connData);
ManagedObjectReference vmMor = getMorByName(targetDCMor, vmName, VIRTUAL_MACHINE, false);

Просмотреть файл

@ -137,6 +137,9 @@ public class VmOpsTool {
case Constants.SHUTDOWN_VM_ACTION:
vmwareFactory.call().shutdownVM(vmName, connData);
break;
case Constants.POWER_OFF_VM_ACTION:
vmwareFactory.call().powerOffVM(vmName, connData);
break;
default:
System.out.printf(
"##vso[task.logissue type=error;code=INFRA_InvalidPowerOperation;TaskId=%s;]\n",

Просмотреть файл

@ -2,7 +2,7 @@
"manifestVersion": 1,
"id": "vmwareapp",
"name": "VMware Resource Deployment",
"version": "0.1.0",
"version": "0.2.0",
"publisher": "ms-vscs-rm",
"description": "Connect to a VMware vCenter Server, and easily provision VMs, and perform actions on them like snapshot, revert snapshot, start, stop etc.",
"public": true,

Просмотреть файл

@ -237,6 +237,12 @@ describe("getCmdArgsForAction", (): void => {
cmdArgs.should.contain("-powerops shutdown");
});
it("Should construct command action for power off vm operation", (): void => {
var cmdArgs = vmOperations.VmOperations.getCmdArgsForAction("Power off Virtual Machines");
cmdArgs.should.contain("-powerops poweroff");
});
it("Should throw on failure to read snapshot name for restore/create/delete snapshot action", (): void => {
getInputStub.withArgs("snapshotName", true).throws();

Просмотреть файл

@ -25,6 +25,7 @@ public class InMemoryVMWareImpl implements IVMWare {
vmSnapshotInfo.put("vm2", snapshotList);
vmSnapshotInfo.put("startandstopubuntu", snapshotList);
vmSnapshotInfo.put("startandstopwindows", snapshotList);
vmSnapshotInfo.put("poweronandpoweroff", snapshotList);
String activeSnapshot = "Snapshot2";
vmActiveSnapshot.put("win2012r2", activeSnapshot);
@ -37,6 +38,7 @@ public class InMemoryVMWareImpl implements IVMWare {
vmActiveSnapshot.put("vm2", activeSnapshot);
vmActiveSnapshot.put("startandstopubuntu", activeSnapshot);
vmActiveSnapshot.put("startandstopwindows", activeSnapshot);
vmActiveSnapshot.put("poweronandpoweroff", activeSnapshot);
String vmState = "Paused";
vmStateInformation.put("win2012r2", vmState);
@ -49,6 +51,7 @@ public class InMemoryVMWareImpl implements IVMWare {
vmStateInformation.put("vm2", vmState);
vmStateInformation.put("startandstopubuntu", vmState);
vmStateInformation.put("startandstopwindows", vmState);
vmStateInformation.put("poweronandpoweroff", vmState);
}
public synchronized void createSnapshot(String vmName, String snapshotName, boolean saveVMMemory, boolean quiesceFs,
@ -121,6 +124,10 @@ public class InMemoryVMWareImpl implements IVMWare {
}
}
public void powerOffVM(String vmName, ConnectionData connData) throws Exception {
shutdownVM(vmName, connData);
}
public String getCurrentSnapshot(String vmName, ConnectionData connData) throws Exception {
String currentSnapshotName;

Просмотреть файл

@ -279,6 +279,19 @@ public abstract class VMWarePlatformTests {
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false);
}
@Test
public void powerOnAndPowerOffVmShouldSucceed() throws Exception {
String vmName = "powerOnAndPowerOff";
String targetDC = "fareastdc";
connData.setTargetDC(targetDC);
vmWareImpl.powerOnVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(true);
vmWareImpl.powerOffVM(vmName, connData);
assertThat(vmWareImpl.isVMPoweredOn(vmName, connData)).isEqualTo(false);
}
// Common for restore/delete snapshot operations
@Test
public void restoreOrDeleteSnapshotShouldThrowIfSnapshotDoesNotExist() {

Просмотреть файл

@ -63,7 +63,7 @@ public class VmOpsToolUnitTests {
}
@Test
public void executeActionInParallelShouldSucceedForStartAndStopVMActionWithValidInputs() throws Exception {
public void executeActionInParallelShouldSucceedForPowerOnShutdownAndPowerOffVMActionWithValidInputs() throws Exception {
String[] cmdArgs = getCmdArgs("vm1, vm2", Constants.POWER_OPS, Constants.POWER_ON_VM_ACTION);
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
@ -71,17 +71,22 @@ public class VmOpsToolUnitTests {
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(true);
assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(true);
cmdArgs = getCmdArgs("vm1, vm2", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION);
cmdArgs = getCmdArgs("vm1", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION);
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(false);
cmdArgs = getCmdArgs("vm2", Constants.POWER_OPS, Constants.POWER_OFF_VM_ACTION);
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(false);
}
@Test
public void executeActionInParallelShouldThrowForStartAndStopVMActionFailureOnAVM() throws Exception {
String[] cmdArgs = getCmdArgs("vm1, VmThatFailsInStart", Constants.POWER_OPS, Constants.POWER_ON_VM_ACTION);
public void executeActionInParallelShouldThrowForPowerOnShutdownAndPowerOffVMActionFailureOnAVM() throws Exception {
String[] cmdArgs = getCmdArgs("vm1, vm2, VmThatFailsInPowerOn", Constants.POWER_OPS, Constants.POWER_ON_VM_ACTION);
Exception exp = null;
@ -94,7 +99,7 @@ public class VmOpsToolUnitTests {
assertThat(exp).isNotNull();
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(true);
cmdArgs = getCmdArgs("vm1, VmThatFailsInStop", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION);
cmdArgs = getCmdArgs("vm1, VmThatFailsInShutdown", Constants.POWER_OPS, Constants.SHUTDOWN_VM_ACTION);
exp = null;
try {
@ -105,6 +110,18 @@ public class VmOpsToolUnitTests {
assertThat(exp).isNotNull();
assertThat(vmWareImpl.isVMPoweredOn("vm1", connData)).isEqualTo(false);
cmdArgs = getCmdArgs("vm2, VmThatFailsInPowerOff", Constants.POWER_OPS, Constants.POWER_OFF_VM_ACTION);
exp = null;
try {
vmOpsTool.executeActionOnVmsInParallel(cmdArgs);
} catch (Exception e) {
exp = e;
}
assertThat(exp).isNotNull();
assertThat(vmWareImpl.isVMPoweredOn("vm2", connData)).isEqualTo(false);
}
@Test