Search in sources :

Example 16 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project druid by druid-io.

the class EC2AutoScalerTest method testIptoIdLookup.

@Test
public void testIptoIdLookup() throws Exception {
    EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
    final int n = 150;
    Assert.assertTrue(n <= 2 * EC2AutoScaler.MAX_AWS_FILTER_VALUES);
    List<String> ips = Lists.transform(ContiguousSet.create(Range.closedOpen(0, n), DiscreteDomain.integers()).asList(), Functions.toStringFunction());
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips.subList(0, EC2AutoScaler.MAX_AWS_FILTER_VALUES))))).andReturn(describeInstancesResult);
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips.subList(EC2AutoScaler.MAX_AWS_FILTER_VALUES, n))))).andReturn(describeInstancesResult);
    EasyMock.replay(amazonEC2Client);
    final Reservation[] chunk1 = new Reservation[EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk1, reservation);
    final Reservation[] chunk2 = new Reservation[n - EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk2, reservation);
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk1));
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk2));
    EasyMock.replay(describeInstancesResult);
    EasyMock.expect(reservation.getInstances()).andReturn(Arrays.asList(instance)).times(n);
    EasyMock.replay(reservation);
    List<String> ids = autoScaler.ipToIdLookup(ips);
    Assert.assertEquals(n, ids.size());
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) Test(org.junit.Test)

Example 17 with AmazonEC2Client

use of com.amazonaws.services.ec2.AmazonEC2Client in project druid by druid-io.

the class EC2AutoScalerTest method testIdToIpLookup.

@Test
public void testIdToIpLookup() throws Exception {
    EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
    final int n = 150;
    Assert.assertTrue(n <= 2 * EC2AutoScaler.MAX_AWS_FILTER_VALUES);
    List<String> ids = Lists.transform(ContiguousSet.create(Range.closedOpen(0, n), DiscreteDomain.integers()).asList(), Functions.toStringFunction());
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", ids.subList(0, EC2AutoScaler.MAX_AWS_FILTER_VALUES))))).andReturn(describeInstancesResult);
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", ids.subList(EC2AutoScaler.MAX_AWS_FILTER_VALUES, n))))).andReturn(describeInstancesResult);
    EasyMock.replay(amazonEC2Client);
    final Reservation[] chunk1 = new Reservation[EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk1, reservation);
    final Reservation[] chunk2 = new Reservation[n - EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk2, reservation);
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk1));
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk2));
    EasyMock.replay(describeInstancesResult);
    EasyMock.expect(reservation.getInstances()).andReturn(Collections.singletonList(instance)).times(n);
    EasyMock.replay(reservation);
    List<String> ips = autoScaler.idToIpLookup(ids);
    Assert.assertEquals(n, ips.size());
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) Test(org.junit.Test)

Example 18 with AmazonEC2Client

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

the class EC2Producer method deleteTags.

private void deleteTags(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    Collection tags;
    DeleteTagsRequest request = new DeleteTagsRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withResources(instanceIds);
    } else {
        throw new IllegalArgumentException("Instances Ids must be specified");
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS))) {
        tags = exchange.getIn().getHeader(EC2Constants.INSTANCES_TAGS, Collection.class);
        request.withTags(tags);
    } else {
        throw new IllegalArgumentException("Tags must be specified");
    }
    DeleteTagsResult result = new DeleteTagsResult();
    try {
        result = ec2Client.deleteTags(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Delete tags command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Delete tags [{}] on resources with Ids [{}] ", Arrays.toString(tags.toArray()), 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) DeleteTagsResult(com.amazonaws.services.ec2.model.DeleteTagsResult) DeleteTagsRequest(com.amazonaws.services.ec2.model.DeleteTagsRequest)

Example 19 with AmazonEC2Client

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

the class EC2Producer method terminateInstances.

private void terminateInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    TerminateInstancesRequest request = new TerminateInstancesRequest();
    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");
    }
    TerminateInstancesResult result;
    try {
        result = ec2Client.terminateInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Terminate Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Terminating 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) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Example 20 with AmazonEC2Client

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

the class EC2Producer method monitorInstances.

private void monitorInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    MonitorInstancesRequest request = new MonitorInstancesRequest();
    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");
    }
    MonitorInstancesResult result;
    try {
        result = ec2Client.monitorInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Monitor Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    LOG.trace("Start 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) MonitorInstancesResult(com.amazonaws.services.ec2.model.MonitorInstancesResult) MonitorInstancesRequest(com.amazonaws.services.ec2.model.MonitorInstancesRequest)

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