use of software.amazon.awssdk.services.ec2.model.StopInstancesRequest in project GNS by MobilityFirst.
the class AWSEC2 method stopInstance.
/**
* Stop an instance
*
* @param ec2
* @param createdInstanceId
*/
public static void stopInstance(AmazonEC2 ec2, String createdInstanceId) {
System.out.println("Stopping Instance:" + createdInstanceId);
List<String> instanceIds = new LinkedList<>();
instanceIds.add(createdInstanceId);
StopInstancesRequest stopIR = new StopInstancesRequest(instanceIds);
ec2.stopInstances(stopIR);
}
use of software.amazon.awssdk.services.ec2.model.StopInstancesRequest in project camel by apache.
the class EC2Producer method stopInstances.
private void stopInstances(AmazonEC2Client ec2Client, Exchange exchange) {
Collection instanceIds;
StopInstancesRequest request = new StopInstancesRequest();
if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
request.withInstanceIds(instanceIds);
} else {
throw new IllegalArgumentException("Instances Ids must be specified");
}
StopInstancesResult result;
try {
result = ec2Client.stopInstances(request);
} catch (AmazonServiceException ase) {
LOG.trace("Stop Instances command returned the error code {}", ase.getErrorCode());
throw ase;
}
LOG.trace("Stopping instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
Message message = getMessageForResponse(exchange);
message.setBody(result);
}
use of software.amazon.awssdk.services.ec2.model.StopInstancesRequest 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 software.amazon.awssdk.services.ec2.model.StopInstancesRequest in project aws-doc-sdk-examples by awsdocs.
the class StartStopInstance method stopInstance.
// snippet-end:[ec2.java2.start_stop_instance.start]
// snippet-start:[ec2.java2.start_stop_instance.stop]
public static void stopInstance(Ec2Client ec2, String instanceId) {
StopInstancesRequest request = StopInstancesRequest.builder().instanceIds(instanceId).build();
ec2.stopInstances(request);
System.out.printf("Successfully stopped instance %s", instanceId);
}
use of software.amazon.awssdk.services.ec2.model.StopInstancesRequest 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