Search in sources :

Example 1 with InstanceStateChange

use of com.amazonaws.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;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Example 2 with InstanceStateChange

use of com.amazonaws.services.ec2.model.InstanceStateChange in project photon-model by vmware.

the class AWSRemoteCleanup method triggerEC2Deletion.

private void triggerEC2Deletion(List<String> instanceIdsToBeDeleted, AmazonEC2 ec2Client) {
    if (instanceIdsToBeDeleted.isEmpty()) {
        return;
    }
    TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(instanceIdsToBeDeleted);
    TerminateInstancesResult terminateInstancesResult = ec2Client.terminateInstances(terminateInstancesRequest);
    terminateInstancesResult.getTerminatingInstances().stream().forEach(instanceStateChange -> {
        this.host.log("Terminating stale instance: %s", instanceStateChange.getInstanceId());
    });
}
Also used : TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Example 3 with InstanceStateChange

use of com.amazonaws.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);
    }
}
Also used : Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) TerminateInstancesResponse(software.amazon.awssdk.services.ec2.model.TerminateInstancesResponse) TerminateInstancesRequest(software.amazon.awssdk.services.ec2.model.TerminateInstancesRequest) InstanceStateChange(software.amazon.awssdk.services.ec2.model.InstanceStateChange)

Example 4 with InstanceStateChange

use of com.amazonaws.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;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Example 5 with InstanceStateChange

use of com.amazonaws.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;
}
Also used : InstanceState(com.amazonaws.services.ec2.model.InstanceState) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) InstanceStateChange(com.amazonaws.services.ec2.model.InstanceStateChange)

Aggregations

InstanceStateChange (com.amazonaws.services.ec2.model.InstanceStateChange)6 ArrayList (java.util.ArrayList)6 AmazonServiceException (com.amazonaws.AmazonServiceException)4 InstanceState (com.amazonaws.services.ec2.model.InstanceState)4 StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 Regions (com.amazonaws.regions.Regions)1 DEFAULT_BACKOFF_STRATEGY (com.amazonaws.retry.PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY)1 DEFAULT_RETRY_CONDITION (com.amazonaws.retry.PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION)1 RetryPolicy (com.amazonaws.retry.RetryPolicy)1 BackoffStrategy (com.amazonaws.retry.RetryPolicy.BackoffStrategy)1 AmazonCloudWatchAsyncClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient)1