use of software.amazon.awssdk.services.ec2.model.InstanceStateChange in project camel by apache.
the class AmazonEC2ClientMock method startInstances.
@Override
public StartInstancesResult startInstances(StartInstancesRequest startInstancesRequest) {
StartInstancesResult result = new StartInstancesResult();
if (startInstancesRequest.getInstanceIds().get(0).equals("test-1")) {
Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
InstanceStateChange sc = new InstanceStateChange();
InstanceState previousState = new InstanceState();
previousState.setCode(80);
previousState.setName(InstanceStateName.Stopped);
InstanceState newState = new InstanceState();
newState.setCode(16);
newState.setName(InstanceStateName.Running);
sc.setPreviousState(previousState);
sc.setCurrentState(newState);
sc.setInstanceId("test-1");
coll.add(sc);
result.setStartingInstances(coll);
} else {
throw new AmazonServiceException("The image-id doesn't exists");
}
return result;
}
use of software.amazon.awssdk.services.ec2.model.InstanceStateChange in project aws-doc-sdk-examples by awsdocs.
the class TerminateInstance method terminateEC2.
// snippet-start:[ec2.java2.terminate_instance]
public static void terminateEC2(Ec2Client ec2, String instanceID) {
try {
TerminateInstancesRequest ti = TerminateInstancesRequest.builder().instanceIds(instanceID).build();
TerminateInstancesResponse response = ec2.terminateInstances(ti);
List<InstanceStateChange> list = response.terminatingInstances();
for (int i = 0; i < list.size(); i++) {
InstanceStateChange sc = (list.get(i));
System.out.println("The ID of the terminated instance is " + sc.instanceId());
}
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.InstanceStateChange in project camel by apache.
the class AmazonEC2ClientMock method stopInstances.
@Override
public StopInstancesResult stopInstances(StopInstancesRequest stopInstancesRequest) {
StopInstancesResult result = new StopInstancesResult();
if (stopInstancesRequest.getInstanceIds().get(0).equals("test-1")) {
Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
InstanceStateChange sc = new InstanceStateChange();
InstanceState previousState = new InstanceState();
previousState.setCode(80);
previousState.setName(InstanceStateName.Running);
InstanceState newState = new InstanceState();
newState.setCode(16);
newState.setName(InstanceStateName.Stopped);
sc.setPreviousState(previousState);
sc.setCurrentState(newState);
sc.setInstanceId("test-1");
coll.add(sc);
result.setStoppingInstances(coll);
} else {
throw new AmazonServiceException("The image-id doesn't exists");
}
return result;
}
use of software.amazon.awssdk.services.ec2.model.InstanceStateChange in project camel by apache.
the class AmazonEC2ClientMock method terminateInstances.
@Override
public TerminateInstancesResult terminateInstances(TerminateInstancesRequest terminateInstancesRequest) {
TerminateInstancesResult result = new TerminateInstancesResult();
if (terminateInstancesRequest.getInstanceIds().contains("test-1")) {
Collection<InstanceStateChange> coll = new ArrayList<InstanceStateChange>();
InstanceStateChange sc = new InstanceStateChange();
InstanceState previousState = new InstanceState();
previousState.setCode(80);
previousState.setName(InstanceStateName.Running);
InstanceState newState = new InstanceState();
newState.setCode(16);
newState.setName(InstanceStateName.Terminated);
sc.setPreviousState(previousState);
sc.setCurrentState(newState);
sc.setInstanceId("test-1");
coll.add(sc);
result.setTerminatingInstances(coll);
} else {
throw new AmazonServiceException("The image-id doesn't exists");
}
return result;
}
use of software.amazon.awssdk.services.ec2.model.InstanceStateChange in project photon-model by vmware.
the class AWSUtils method waitForTransitionCompletion.
public static void waitForTransitionCompletion(ServiceHost host, List<InstanceStateChange> stateChangeList, final String desiredState, AmazonEC2AsyncClient client, BiConsumer<InstanceState, Exception> callback) {
InstanceStateChange stateChange = stateChangeList.get(0);
try {
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.withInstanceIds(stateChange.getInstanceId());
DescribeInstancesResult result = client.describeInstances(request);
Instance instance = result.getReservations().stream().flatMap(r -> r.getInstances().stream()).filter(i -> i.getInstanceId().equalsIgnoreCase(stateChange.getInstanceId())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s instance not found", stateChange.getInstanceId())));
String state = instance.getState().getName();
if (state.equals(desiredState)) {
callback.accept(instance.getState(), null);
} else {
host.schedule(() -> waitForTransitionCompletion(host, stateChangeList, desiredState, client, callback), 5, TimeUnit.SECONDS);
}
} catch (AmazonServiceException | IllegalArgumentException ase) {
callback.accept(null, ase);
}
}
Aggregations