Search in sources :

Example 11 with Reservation

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

the class EC2AutoScalerTest method setUp.

@Before
public void setUp() throws Exception {
    amazonEC2Client = EasyMock.createMock(AmazonEC2Client.class);
    describeInstancesResult = EasyMock.createMock(DescribeInstancesResult.class);
    reservation = EasyMock.createMock(Reservation.class);
    instance = new Instance().withInstanceId(INSTANCE_ID).withLaunchTime(new Date()).withImageId(AMI_ID).withPrivateIpAddress(IP);
    managementConfig = new SimpleWorkerResourceManagementConfig().setWorkerPort(8080).setWorkerVersion("");
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) Date(java.util.Date) Before(org.junit.Before)

Example 12 with Reservation

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

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

use of com.amazonaws.services.ec2.model.Reservation in project elasticsearch by elastic.

the class AmazonEC2Mock method describeInstances.

@Override
public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest) throws AmazonServiceException, AmazonClientException {
    Collection<Instance> filteredInstances = new ArrayList<>();
    logger.debug("--> mocking describeInstances");
    for (Instance instance : instances) {
        boolean tagFiltered = false;
        boolean instanceFound = false;
        Map<String, List<String>> expectedTags = new HashMap<>();
        Map<String, List<String>> instanceTags = new HashMap<>();
        for (Tag tag : instance.getTags()) {
            List<String> tags = instanceTags.get(tag.getKey());
            if (tags == null) {
                tags = new ArrayList<>();
                instanceTags.put(tag.getKey(), tags);
            }
            tags.add(tag.getValue());
        }
        for (Filter filter : describeInstancesRequest.getFilters()) {
            // If we have the same tag name and one of the values, we add the instance
            if (filter.getName().startsWith("tag:")) {
                tagFiltered = true;
                String tagName = filter.getName().substring(4);
                // if we have more than one value for the same key, then the key is appended with .x
                Pattern p = Pattern.compile("\\.\\d+", Pattern.DOTALL);
                Matcher m = p.matcher(tagName);
                if (m.find()) {
                    int i = tagName.lastIndexOf(".");
                    tagName = tagName.substring(0, i);
                }
                List<String> tags = expectedTags.get(tagName);
                if (tags == null) {
                    tags = new ArrayList<>();
                    expectedTags.put(tagName, tags);
                }
                tags.addAll(filter.getValues());
            }
        }
        if (tagFiltered) {
            logger.debug("--> expected tags: [{}]", expectedTags);
            logger.debug("--> instance tags: [{}]", instanceTags);
            instanceFound = true;
            for (Map.Entry<String, List<String>> expectedTagsEntry : expectedTags.entrySet()) {
                List<String> instanceTagValues = instanceTags.get(expectedTagsEntry.getKey());
                if (instanceTagValues == null) {
                    instanceFound = false;
                    break;
                }
                for (String expectedValue : expectedTagsEntry.getValue()) {
                    boolean valueFound = false;
                    for (String instanceTagValue : instanceTagValues) {
                        if (instanceTagValue.equals(expectedValue)) {
                            valueFound = true;
                        }
                    }
                    if (valueFound == false) {
                        instanceFound = false;
                    }
                }
            }
        }
        if (tagFiltered == false || instanceFound) {
            logger.debug("--> instance added");
            filteredInstances.add(instance);
        } else {
            logger.debug("--> instance filtered");
        }
    }
    return new DescribeInstancesResult().withReservations(new Reservation().withInstances(filteredInstances));
}
Also used : Pattern(java.util.regex.Pattern) Instance(com.amazonaws.services.ec2.model.Instance) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with Reservation

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

the class AmazonEC2ClientMock method describeInstances.

@Override
public DescribeInstancesResult describeInstances(DescribeInstancesRequest describeInstancesRequest) {
    DescribeInstancesResult result = new DescribeInstancesResult();
    if (describeInstancesRequest.getInstanceIds().isEmpty()) {
        Collection<Reservation> list = new ArrayList<Reservation>();
        Reservation res = new Reservation();
        res.setOwnerId("1");
        res.setRequesterId("user-test");
        res.setReservationId("res-1");
        Collection<Instance> instances = new ArrayList();
        Instance ins = new Instance();
        ins.setImageId("id-1");
        ins.setInstanceType(InstanceType.T2Micro);
        ins.setInstanceId("instance-1");
        instances.add(ins);
        Instance ins1 = new Instance();
        ins1.setImageId("id-2");
        ins1.setInstanceType(InstanceType.T2Micro);
        ins1.setInstanceId("instance-2");
        instances.add(ins1);
        res.setInstances(instances);
        list.add(res);
        result.setReservations(list);
    } else {
        if (describeInstancesRequest.getInstanceIds().contains("instance-1")) {
            Collection<Reservation> list = new ArrayList<Reservation>();
            Reservation res = new Reservation();
            res.setOwnerId("1");
            res.setRequesterId("user-test");
            res.setReservationId("res-1");
            Collection<Instance> instances = new ArrayList();
            Instance ins = new Instance();
            ins.setImageId("id-1");
            ins.setInstanceType(InstanceType.T2Micro);
            ins.setInstanceId("instance-1");
            instances.add(ins);
            res.setInstances(instances);
            list.add(res);
            result.setReservations(list);
        }
    }
    return result;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList)

Aggregations

Reservation (com.amazonaws.services.ec2.model.Reservation)15 Instance (com.amazonaws.services.ec2.model.Instance)14 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)10 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)7 Filter (com.amazonaws.services.ec2.model.Filter)6 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)3 Function (com.google.common.base.Function)3 EC2AutoScaler (io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler)3 List (java.util.List)3 Test (org.junit.Test)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)2 RunInstancesResult (com.amazonaws.services.ec2.model.RunInstancesResult)2 Tag (com.amazonaws.services.ec2.model.Tag)2 FluentIterable (com.google.common.collect.FluentIterable)2 Date (java.util.Date)2