use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext in project photon-model by vmware.
the class AWSResetService method startInstance.
private void startInstance(AmazonEC2AsyncClient client, DefaultAdapterContext c) {
StartInstancesRequest startRequest = new StartInstancesRequest();
startRequest.withInstanceIds(c.child.id);
client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
protected void handleError(Exception e) {
c.taskManager.patchTaskToFailure(e);
}
@Override
protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
if (e == null) {
c.taskManager.finishTask();
} else {
c.taskManager.patchTaskToFailure(e);
}
});
}
});
}
use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext in project photon-model by vmware.
the class AWSResetService method updateComputeState.
private void updateComputeState(DefaultAdapterContext c) {
ComputeState state = new ComputeState();
state.powerState = ComputeService.PowerState.ON;
Operation.createPatch(c.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.adapters.util.BaseAdapterContext.DefaultAdapterContext in project photon-model by vmware.
the class AWSResetService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
op.complete();
logInfo(() -> String.format("Handle operation %s for compute %s.", request.operation, request.resourceLink()));
if (request.isMockRequest) {
updateComputeState(new DefaultAdapterContext(this, request));
} else {
new DefaultAdapterContext(this, request).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;
}
reset(client, request, c);
});
});
}
}
use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext in project photon-model by vmware.
the class AWSComputeDiskDay2Service method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
try {
validateRequest(request);
op.complete();
} catch (Exception e) {
op.fail(e);
return;
}
// initialize context with baseAdapterContext and request information
DeferredResult<DiskContext> drDiskContext = new DefaultAdapterContext(this, request).populateBaseContext(BaseAdapterStage.VMDESC).thenApply(c -> {
DiskContext context = new DiskContext();
context.baseAdapterContext = c;
context.request = request;
return context;
});
Function<DiskContext, DeferredResult<DiskContext>> fn;
// handle resource operation
if (request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
fn = this::performAttachOperation;
} else if (request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
fn = this::performDetachOperation;
} else {
drDiskContext.thenApply(c -> {
Throwable err = new IllegalArgumentException(String.format("Unknown Operation %s for a disk", request.operation));
c.baseAdapterContext.taskManager.patchTaskToFailure(err);
return c;
});
return;
}
drDiskContext.thenCompose(this::setComputeState).thenCompose(this::setDiskState).thenCompose(this::setClient).thenCompose(fn).whenComplete((ctx, e) -> {
if (ctx.error == null) {
ctx.baseAdapterContext.taskManager.finishTask();
} else {
ctx.baseAdapterContext.taskManager.patchTaskToFailure(ctx.error);
}
});
}
use of com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext in project photon-model by vmware.
the class AWSPowerService method powerOff.
private void powerOff(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
OperationContext opContext = OperationContext.getOperationContext();
StopInstancesRequest request = new StopInstancesRequest();
request.withInstanceIds(c.child.id);
client.stopInstancesAsync(request, new AsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
public void onSuccess(StopInstancesRequest request, StopInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", 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);
}
});
}
Aggregations