Search in sources :

Example 21 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project camel by apache.

the class EC2Producer method createAndRunInstance.

private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) {
    String ami;
    InstanceType instanceType;
    int minCount;
    int maxCount;
    boolean monitoring;
    String kernelId;
    boolean ebsOptimized;
    Collection securityGroups;
    String keyName;
    String clientToken;
    Placement placement;
    RunInstancesRequest request = new RunInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) {
        ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class);
        request.withImageId(ami);
    } else {
        throw new IllegalArgumentException("AMI must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) {
        instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class);
        request.withInstanceType(instanceType.toString());
    } else {
        throw new IllegalArgumentException("Instance Type must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) {
        minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class);
        request.withMinCount(minCount);
    } else {
        throw new IllegalArgumentException("Min instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) {
        maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class);
        request.withMaxCount(maxCount);
    } else {
        throw new IllegalArgumentException("Max instances count must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) {
        monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class);
        request.withMonitoring(monitoring);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) {
        kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class);
        request.withKernelId(kernelId);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) {
        ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class);
        request.withEbsOptimized(ebsOptimized);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) {
        securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class);
        request.withSecurityGroups(securityGroups);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) {
        keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class);
        request.withKeyName(keyName);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) {
        clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class);
        request.withClientToken(clientToken);
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) {
        placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class);
        request.withPlacement(placement);
    }
    RunInstancesResult result;
    try {
        result = ec2Client.runInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString());
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) Endpoint(org.apache.camel.Endpoint) Placement(com.amazonaws.services.ec2.model.Placement) RunInstancesResult(com.amazonaws.services.ec2.model.RunInstancesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) RunInstancesRequest(com.amazonaws.services.ec2.model.RunInstancesRequest) InstanceType(com.amazonaws.services.ec2.model.InstanceType)

Example 22 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project camel by apache.

the class EC2Producer method rebootInstances.

private void rebootInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    RebootInstancesRequest request = new RebootInstancesRequest();
    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");
    }
    try {
        LOG.trace("Rebooting instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
        ec2Client.rebootInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Reboot Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
}
Also used : RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection)

Example 23 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project camel by apache.

the class EC2Producer method unmonitorInstances.

private void unmonitorInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    UnmonitorInstancesRequest request = new UnmonitorInstancesRequest();
    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");
    }
    UnmonitorInstancesResult result;
    try {
        result = ec2Client.unmonitorInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Unmonitor Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Stop Monitoring instances with Ids [{}] ", Arrays.toString(instanceIds.toArray()));
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : Message(org.apache.camel.Message) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) UnmonitorInstancesResult(com.amazonaws.services.ec2.model.UnmonitorInstancesResult) UnmonitorInstancesRequest(com.amazonaws.services.ec2.model.UnmonitorInstancesRequest)

Example 24 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project camel by apache.

the class EC2Producer method describeInstances.

private void describeInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withInstanceIds(instanceIds);
    }
    DescribeInstancesResult result;
    try {
        result = ec2Client.describeInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Describe Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Message(org.apache.camel.Message) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 25 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project eureka by Netflix.

the class EIPManager method getEC2Service.

/**
     * Gets the EC2 service object to call AWS APIs.
     *
     * @return the EC2 service object to call AWS APIs.
     */
private AmazonEC2 getEC2Service() {
    String aWSAccessId = serverConfig.getAWSAccessId();
    String aWSSecretKey = serverConfig.getAWSSecretKey();
    AmazonEC2 ec2Service;
    if (null != aWSAccessId && !"".equals(aWSAccessId) && null != aWSSecretKey && !"".equals(aWSSecretKey)) {
        ec2Service = new AmazonEC2Client(new BasicAWSCredentials(aWSAccessId, aWSSecretKey));
    } else {
        ec2Service = new AmazonEC2Client(new InstanceProfileCredentialsProvider());
    }
    String region = clientConfig.getRegion();
    region = region.trim().toLowerCase();
    ec2Service.setEndpoint("ec2." + region + ".amazonaws.com");
    return ec2Service;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) InstanceProfileCredentialsProvider(com.amazonaws.auth.InstanceProfileCredentialsProvider) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Aggregations

AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)15 AmazonServiceException (com.amazonaws.AmazonServiceException)12 Collection (java.util.Collection)11 Message (org.apache.camel.Message)10 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)7 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)6 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)4 Instance (com.amazonaws.services.ec2.model.Instance)4 File (java.io.File)4 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)3 Reservation (com.amazonaws.services.ec2.model.Reservation)3 EC2AutoScaler (io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler)3 IOException (java.io.IOException)3 Date (java.util.Date)3 Test (org.junit.Test)3 InstanceProfileCredentialsProvider (com.amazonaws.auth.InstanceProfileCredentialsProvider)2 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)2 Filter (com.amazonaws.services.ec2.model.Filter)2 RunInstancesRequest (com.amazonaws.services.ec2.model.RunInstancesRequest)2