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());
}
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());
}
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);
}
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);
}
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);
}
Aggregations