Search in sources :

Example 1 with StopInstancesRequest

use of com.amazonaws.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);
}
Also used : Message(org.apache.camel.Message) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest)

Example 2 with StopInstancesRequest

use of com.amazonaws.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);
}
Also used : StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) LinkedList(java.util.LinkedList)

Example 3 with StopInstancesRequest

use of com.amazonaws.services.ec2.model.StopInstancesRequest 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 4 with StopInstancesRequest

use of com.amazonaws.services.ec2.model.StopInstancesRequest in project aws-doc-sdk-examples by awsdocs.

the class StartStopInstance method stopInstance.

public static void stopInstance(String instance_id) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    DryRunSupportedRequest<StopInstancesRequest> dry_request = () -> {
        StopInstancesRequest request = new StopInstancesRequest().withInstanceIds(instance_id);
        return request.getDryRunRequest();
    };
    DryRunResult dry_response = ec2.dryRun(dry_request);
    if (!dry_response.isSuccessful()) {
        System.out.printf("Failed dry run to stop instance %s", instance_id);
        throw dry_response.getDryRunResponse();
    }
    StopInstancesRequest request = new StopInstancesRequest().withInstanceIds(instance_id);
    ec2.stopInstances(request);
    System.out.printf("Successfully stop instance %s", instance_id);
}
Also used : StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DryRunResult(com.amazonaws.services.ec2.model.DryRunResult)

Aggregations

StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 DryRunResult (com.amazonaws.services.ec2.model.DryRunResult)1 InstanceState (com.amazonaws.services.ec2.model.InstanceState)1 InstanceStateChange (com.amazonaws.services.ec2.model.InstanceStateChange)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Message (org.apache.camel.Message)1