use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.
the class AzurePowerServiceTest method assertVmCurrentPowerState.
private void assertVmCurrentPowerState(PowerState powerState) {
if (this.isMock) {
// return. Nothing provisioned on Azure so nothing to check
return;
}
try {
PowerState vmPowerState = PowerState.UNKNOWN;
VirtualMachineInner vm = AzureTestUtil.getAzureVirtualMachineWithExtension(this.computeManagementClient, azureVMName, azureVMName, EXPAND_INSTANCE_VIEW_PARAM);
for (InstanceViewStatus status : vm.instanceView().statuses()) {
if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_RUNNING)) {
vmPowerState = PowerState.ON;
} else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_STOPPED)) {
vmPowerState = PowerState.OFF;
}
}
assertEquals("VM current power state does not match expected value.", powerState, vmPowerState);
} catch (Exception e) {
fail("Unable to verify current Machine Power state on Azure");
e.printStackTrace();
}
}
use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.
the class GCPTestUtil method queryComputeStatesWithPowerState.
/**
* Method to query the number of local compute states with given power state.
* @param host The test service host.
* @param resourcePool The default resource pool.
* @param parentCompute The default compute host.
* @param powerState The given power state.
* @param instanceNames The assumed names of compute states.
*/
private static void queryComputeStatesWithPowerState(VerificationHost host, ResourcePoolState resourcePool, ComputeState parentCompute, PowerState powerState, Set<String> instanceNames) {
Query query = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, resourcePool.documentSelfLink).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, parentCompute.documentSelfLink).build();
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).setQuery(query).build();
host.sendRequest(QueryUtils.createQueryTaskOperation(host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(host.getUri()).setCompletion((o, e) -> {
if (e != null) {
host.log(Level.WARNING, String.format("Error: %s", e.getMessage()));
return;
}
QueryTask queryTask = o.getBody(QueryTask.class);
if (queryTask.results.documentCount > 0) {
queryTask.results.documents.values().forEach(s -> {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
if (computeState.powerState == powerState) {
instanceNames.remove(computeState.name);
}
});
}
}));
}
use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.
the class VSphereAdapterPowerService method handlePowerRequest.
private void handlePowerRequest(ProvisionContext ctx, ComputePowerRequest request) {
ctx.pool.submit(ctx.getAdapterManagementReference(), ctx.vSphereCredentials, (connection, ce) -> {
if (ctx.fail(ce)) {
return;
}
PowerStateClient client = new PowerStateClient(connection);
ManagedObjectReference vmMoRef;
try {
vmMoRef = getVmMoRef(ctx.child);
} catch (Exception e) {
ctx.failWithMessage("Cannot extract MoRef from compute state", e);
return;
}
if (vmMoRef == null) {
ctx.failWithMessage("No VM MoRef found in compute state");
return;
}
PowerState currentState;
try {
currentState = client.getPowerState(vmMoRef);
} catch (Exception e) {
// probably should ignore this error and assume power state has to change
ctx.failWithMessage("Cannot get current power state of vmMoRef" + VimUtils.convertMoRefToString(vmMoRef), e);
return;
}
if (currentState == request.powerState) {
ctx.mgr.finishTask();
return;
}
try {
long politenessDeadlineMicros = computeDeadline(ctx.task);
client.changePowerState(vmMoRef, request.powerState, request.powerTransition, politenessDeadlineMicros);
} catch (Exception e) {
ctx.failWithMessage("cannot change power state of vmMoRef " + VimUtils.convertMoRefToString(vmMoRef), e);
return;
}
patchComputeAndCompleteRequest(request, ctx);
});
}
use of com.vmware.photon.controller.model.resources.ComputeService.PowerState in project photon-model by vmware.
the class PowerStateClient method changePowerState.
/**
* @param vm
* @param targetState
* @param transition
* @param politenessDeadlineMicros Used only when doing a soft power-off,
* how long to be polite before doing hard power-off.
* @throws Exception
*/
public void changePowerState(ManagedObjectReference vm, PowerState targetState, PowerTransition transition, long politenessDeadlineMicros) throws Exception {
PowerState currentState = getPowerState(vm);
if (currentState == targetState) {
return;
}
ManagedObjectReference task;
if (targetState == PowerState.ON) {
task = getVimPort().powerOnVMTask(vm, null);
awaitTaskEnd(task);
return;
}
if (currentState == PowerState.ON && targetState == PowerState.SUSPEND) {
task = getVimPort().suspendVMTask(vm);
awaitTaskEnd(task);
return;
}
if (currentState == PowerState.SUSPEND && targetState == PowerState.OFF) {
// In SUSPENDED state only "Power.Off" and "Power.On" operations are valid, "Power.Off" corresponds to HardPowerOff
hardPowerOff(vm);
}
if (currentState == PowerState.ON && targetState == PowerState.OFF) {
if (transition == PowerTransition.SOFT) {
softPowerOff(vm, politenessDeadlineMicros);
} else {
hardPowerOff(vm);
}
}
}
Aggregations