use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler 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.awsadapter.util.AWSAsyncHandler in project photon-model by vmware.
the class AWSUtils method setDeleteOnTerminateAttribute.
public static DeferredResult<DiskService.DiskState> setDeleteOnTerminateAttribute(AmazonEC2AsyncClient client, String instanceId, Map<String, Pair<String, Boolean>> deleteDiskMapByDeviceName, OperationContext opCtx) {
List<InstanceBlockDeviceMappingSpecification> instanceBlockDeviceMappingSpecificationList = deleteDiskMapByDeviceName.entrySet().stream().map(entry -> new InstanceBlockDeviceMappingSpecification().withDeviceName(entry.getKey()).withEbs(new EbsInstanceBlockDeviceSpecification().withDeleteOnTermination(entry.getValue().right).withVolumeId(entry.getValue().left))).collect(Collectors.toList());
DeferredResult<DiskService.DiskState> modifyInstanceAttrDr = new DeferredResult();
ModifyInstanceAttributeRequest modifyInstanceAttrReq = new ModifyInstanceAttributeRequest().withInstanceId(instanceId).withAttribute(InstanceAttributeName.BlockDeviceMapping).withBlockDeviceMappings(instanceBlockDeviceMappingSpecificationList);
AWSAsyncHandler<ModifyInstanceAttributeRequest, ModifyInstanceAttributeResult> modifyInstanceAttrHandler = new AWSAsyncHandler<ModifyInstanceAttributeRequest, ModifyInstanceAttributeResult>() {
@Override
protected void handleError(Exception exception) {
OperationContext.restoreOperationContext(opCtx);
modifyInstanceAttrDr.fail(exception);
}
@Override
protected void handleSuccess(ModifyInstanceAttributeRequest request, ModifyInstanceAttributeResult result) {
OperationContext.restoreOperationContext(opCtx);
modifyInstanceAttrDr.complete(new DiskService.DiskState());
}
};
client.modifyInstanceAttributeAsync(modifyInstanceAttrReq, modifyInstanceAttrHandler);
return modifyInstanceAttrDr;
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler in project photon-model by vmware.
the class AWSComputeDiskDay2Service method startInstance.
/**
* start the instance and on success updates the disk and compute state to reflect the detach information.
*/
private void startInstance(AmazonEC2AsyncClient client, DiskContext c, DeferredResult<DiskContext> dr, OperationContext opCtx) {
StartInstancesRequest startRequest = new StartInstancesRequest();
startRequest.withInstanceIds(c.baseAdapterContext.child.id);
client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
protected void handleError(Exception e) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start the instance %s. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
}
@Override
protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
if (e != null) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Instance %s failed to reach " + "running state. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
return;
}
logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully started the " + "instance %s", result.getStartingInstances().get(0).getInstanceId()));
updateComputeAndDiskState(dr, c, opCtx);
});
}
});
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler in project photon-model by vmware.
the class AWSComputeDiskDay2Service method performDetachOperation.
private DeferredResult<DiskContext> performDetachOperation(DiskContext context) {
DeferredResult<DiskContext> dr = new DeferredResult<>();
try {
validateDetachInfo(context.diskState);
if (context.request.isMockRequest) {
updateComputeAndDiskState(dr, context, null);
return dr;
}
String instanceId = context.computeState.id;
if (instanceId == null || !instanceId.startsWith(AWS_INSTANCE_ID_PREFIX)) {
return logAndGetFailedDr(context, "compute id cannot be empty");
}
String diskId = context.diskState.id;
if (diskId == null || !diskId.startsWith(AWS_VOLUME_ID_PREFIX)) {
return logAndGetFailedDr(context, "disk id cannot be empty");
}
// stop the instance, detach the disk and then start the instance.
if (context.baseAdapterContext.child.powerState.equals(ComputeService.PowerState.ON)) {
StopInstancesRequest stopRequest = new StopInstancesRequest();
stopRequest.withInstanceIds(context.baseAdapterContext.child.id);
context.amazonEC2Client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
protected void handleError(Exception e) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start compute. %s", Utils.toString(e)));
OperationContext.restoreOperationContext(this.opContext);
context.error = e;
dr.complete(context);
}
@Override
protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
OperationContext.restoreOperationContext(this.opContext);
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", context.amazonEC2Client, (is, e) -> {
if (e != null) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to stop " + "the compute. %s", Utils.toString(e)));
context.error = e;
dr.complete(context);
return;
}
logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully stopped " + "the instance %s", instanceId));
// detach disk from the instance.
detachVolume(context, dr, instanceId, diskId, true);
});
}
});
} else {
detachVolume(context, dr, instanceId, diskId, false);
}
} catch (Exception e) {
context.error = e;
return DeferredResult.completed(context);
}
return dr;
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler in project photon-model by vmware.
the class AWSResetService method reset.
private void reset(AmazonEC2AsyncClient client, ResourceOperationRequest pr, DefaultAdapterContext c) {
if (!c.child.powerState.equals(ComputeService.PowerState.ON)) {
logWarning(() -> String.format("Cannot perform a reset on this EC2 instance. " + "The machine should be in powered on state"));
c.taskManager.patchTaskToFailure(new IllegalStateException("Incorrect power state. Expected the machine " + "to be powered on "));
return;
}
// The stop action for reset is a force stop. So we use the withForce method to set the force parameter to TRUE
// This is similar to unplugging the machine from the power circuit.
// The OS and the applications are forcefully stopped.
StopInstancesRequest stopRequest = new StopInstancesRequest();
stopRequest.withInstanceIds(c.child.id).withForce(Boolean.TRUE);
client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {
@Override
protected void handleError(Exception e) {
c.taskManager.patchTaskToFailure(e);
}
@Override
protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
if (e != null) {
onError(e);
return;
}
// Instances will be started only if they're successfully stopped
startInstance(client, c);
});
}
});
}
Aggregations