use of com.vmware.photon.controller.model.adapterapi.ComputePowerRequest in project photon-model by vmware.
the class AzurePowerService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ComputePowerRequest request = op.getBody(ComputePowerRequest.class);
op.complete();
AzurePowerContext ctx = new AzurePowerContext(this, request);
if (request.isMockRequest) {
updateComputeState(ctx);
} else {
ctx.populateBaseContext(BaseAdapterStage.VMDESC).whenComplete((ignoreCtx, e) -> {
if (e != null) {
logSevere("Error populating base context during Azure power state operation %s for resource %s failed with error %s", request.powerState, request.resourceReference, Utils.toString(e));
ctx.finishExceptionally(e);
return;
}
ctx.vmName = ctx.child.name != null ? ctx.child.name : ctx.child.id;
ctx.rgName = AzureUtils.getResourceGroupName(ctx);
applyPowerOperation(ctx);
});
}
}
use of com.vmware.photon.controller.model.adapterapi.ComputePowerRequest in project photon-model by vmware.
the class AWSPowerService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ComputePowerRequest pr = op.getBody(ComputePowerRequest.class);
op.complete();
if (pr.isMockRequest) {
updateComputeState(pr, new DefaultAdapterContext(this, pr));
} else {
new DefaultAdapterContext(this, pr).populateBaseContext(BaseAdapterStage.VMDESC).whenComplete((c, e) -> {
this.clientManager.getOrCreateEC2ClientAsync(c.endpointAuth, c.child.description.regionId, this).whenComplete((client, t) -> {
if (t != null) {
c.taskManager.patchTaskToFailure(t);
return;
}
applyPowerOperation(client, pr, c);
});
});
}
}
use of com.vmware.photon.controller.model.adapterapi.ComputePowerRequest in project photon-model by vmware.
the class AWSPowerService method updateComputeState.
private void updateComputeState(ComputePowerRequest pr, DefaultAdapterContext c) {
ComputeState state = new ComputeState();
state.powerState = pr.powerState;
if (OFF.equals(pr.powerState)) {
// clear IP address in case of power-off
state.address = "";
}
Operation.createPatch(pr.resourceReference).setBody(state).setCompletion((o, e) -> {
if (e != null) {
c.taskManager.patchTaskToFailure(e);
return;
}
c.taskManager.finishTask();
}).sendWith(this);
}
use of com.vmware.photon.controller.model.adapterapi.ComputePowerRequest in project photon-model by vmware.
the class AWSPowerService method powerOn.
private void powerOn(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
OperationContext opContext = OperationContext.getOperationContext();
StartInstancesRequest request = new StartInstancesRequest();
request.withInstanceIds(c.child.id);
client.startInstancesAsync(request, new AsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
public void onSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
OperationContext.restoreOperationContext(opContext);
if (e != null) {
onError(e);
return;
}
updateComputeState(pr, c);
});
}
@Override
public void onError(Exception e) {
OperationContext.restoreOperationContext(opContext);
c.taskManager.patchTaskToFailure(e);
}
});
}
use of com.vmware.photon.controller.model.adapterapi.ComputePowerRequest in project photon-model by vmware.
the class AWSPowerServiceTest method changePowerState.
private void changePowerState(ComputeDescription cd, String computeLink, PowerState powerState) {
String taskLink = UUID.randomUUID().toString();
ComputePowerRequest powerRequest = new ComputePowerRequest();
powerRequest.isMockRequest = this.isMock;
powerRequest.powerState = powerState;
powerRequest.resourceReference = UriUtils.buildUri(this.host, computeLink);
powerRequest.taskReference = UriUtils.buildUri(this.host, taskLink);
TestContext ctx = this.host.testCreate(2);
createTaskResultListener(this.host, taskLink, (u) -> {
if (u.getAction() != Action.PATCH) {
return false;
}
ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
if (TaskState.isFailed(response.taskInfo)) {
ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
} else if (TaskState.isFinished(response.taskInfo)) {
ctx.completeIteration();
}
return true;
});
Operation powerOp = Operation.createPatch(cd.powerAdapterReference).setBody(powerRequest).setReferer("/boza").setCompletion((o, e) -> {
if (e != null) {
ctx.failIteration(e);
return;
}
ctx.completeIteration();
});
this.host.send(powerOp);
ctx.await();
ComputeState compute = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeLink));
assertEquals(powerState, compute.powerState);
if (PowerState.OFF.equals(powerState)) {
assertTrue(compute.address.isEmpty());
}
}
Aggregations