Search in sources :

Example 51 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project SimianArmy by Netflix.

the class TestInstanceJanitorCrawler method testInstancesWithNullIds.

@Test
public void testInstancesWithNullIds() {
    List<AutoScalingInstanceDetails> instanceDetailsList = createInstanceDetailsList();
    List<Instance> instanceList = createInstanceList();
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources();
    verifyInstanceList(resources, instanceDetailsList);
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Test(org.testng.annotations.Test)

Example 52 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project SimianArmy by Netflix.

the class TestInstanceJanitorCrawler method testInstancesWithResourceType.

@Test
public void testInstancesWithResourceType() {
    List<AutoScalingInstanceDetails> instanceDetailsList = createInstanceDetailsList();
    List<Instance> instanceList = createInstanceList();
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    for (AWSResourceType resourceType : AWSResourceType.values()) {
        List<Resource> resources = crawler.resources(resourceType);
        if (resourceType == AWSResourceType.INSTANCE) {
            verifyInstanceList(resources, instanceDetailsList);
        } else {
            Assert.assertTrue(resources.isEmpty());
        }
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Test(org.testng.annotations.Test)

Example 53 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project crate by crate.

the class AwsEc2SeedHostsProvider method fetchDynamicNodes.

private List<TransportAddress> fetchDynamicNodes() {
    final List<TransportAddress> dynamicHosts = new ArrayList<>();
    final DescribeInstancesResult descInstances;
    try (AmazonEc2Reference clientReference = awsEc2Service.client()) {
        // Query EC2 API based on AZ, instance state, and tag.
        // NOTE: we don't filter by security group during the describe instances request for two reasons:
        // 1. differences in VPCs require different parameters during query (ID vs Name)
        // 2. We want to use two different strategies: (all security groups vs. any security groups)
        descInstances = clientReference.client().describeInstances(buildDescribeInstancesRequest());
    } catch (final AmazonClientException e) {
        LOGGER.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
        LOGGER.debug("Full exception:", e);
        return dynamicHosts;
    }
    LOGGER.trace("finding seed nodes...");
    for (final Reservation reservation : descInstances.getReservations()) {
        for (final Instance instance : reservation.getInstances()) {
            // lets see if we can filter based on groups
            if (!groups.isEmpty()) {
                final List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
                final List<String> securityGroupNames = new ArrayList<>(instanceSecurityGroups.size());
                final List<String> securityGroupIds = new ArrayList<>(instanceSecurityGroups.size());
                for (final GroupIdentifier sg : instanceSecurityGroups) {
                    securityGroupNames.add(sg.getGroupName());
                    securityGroupIds.add(sg.getGroupId());
                }
                if (bindAnyGroup) {
                    // We check if we can find at least one group name or one group id in groups.
                    if (disjoint(securityGroupNames, groups) && disjoint(securityGroupIds, groups)) {
                        LOGGER.trace("filtering out instance {} based on groups {}, not part of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
                        // continue to the next instance
                        continue;
                    }
                } else {
                    // We need tp match all group names or group ids, otherwise we ignore this instance
                    if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
                        LOGGER.trace("filtering out instance {} based on groups {}, does not include all of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
                        // continue to the next instance
                        continue;
                    }
                }
            }
            String address = null;
            if (hostType.equals(PRIVATE_DNS)) {
                address = instance.getPrivateDnsName();
            } else if (hostType.equals(PRIVATE_IP)) {
                address = instance.getPrivateIpAddress();
            } else if (hostType.equals(PUBLIC_DNS)) {
                address = instance.getPublicDnsName();
            } else if (hostType.equals(PUBLIC_IP)) {
                address = instance.getPublicIpAddress();
            } else if (hostType.startsWith(TAG_PREFIX)) {
                // Reading the node host from its metadata
                final String tagName = hostType.substring(TAG_PREFIX.length());
                LOGGER.debug("reading hostname from [{}] instance tag", tagName);
                final List<Tag> tags = instance.getTags();
                for (final Tag tag : tags) {
                    if (tag.getKey().equals(tagName)) {
                        address = tag.getValue();
                        LOGGER.debug("using [{}] as the instance address", address);
                    }
                }
            } else {
                throw new IllegalArgumentException(hostType + " is unknown for discovery.ec2.host_type");
            }
            if (address != null) {
                try {
                    final TransportAddress[] addresses = transportService.addressesFromString(address);
                    for (int i = 0; i < addresses.length; i++) {
                        LOGGER.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]);
                        dynamicHosts.add(addresses[i]);
                    }
                } catch (final Exception e) {
                    final String finalAddress = address;
                    LOGGER.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", instance.getInstanceId(), finalAddress), e);
                }
            } else {
                LOGGER.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
            }
        }
    }
    LOGGER.debug("using dynamic transport addresses {}", dynamicHosts);
    return dynamicHosts;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.elasticsearch.common.transport.TransportAddress) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Collections.disjoint(java.util.Collections.disjoint) AmazonClientException(com.amazonaws.AmazonClientException) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tag(com.amazonaws.services.ec2.model.Tag)

Example 54 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project druid by druid-io.

the class EC2AutoScaler method idToIpLookup.

@Override
public List<String> idToIpLookup(List<String> nodeIds) {
    final List<String> retVal = FluentIterable.from(Lists.partition(nodeIds, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {

        @Override
        public Iterable<Reservation> apply(List<String> input) {
            return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", input))).getReservations();
        }
    }).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {

        @Override
        public Iterable<Instance> apply(Reservation reservation) {
            return reservation.getInstances();
        }
    }).transform(new Function<Instance, String>() {

        @Override
        public String apply(Instance instance) {
            return instance.getPrivateIpAddress();
        }
    }).toList();
    log.debug("Performing lookup: %s --> %s", nodeIds, retVal);
    return retVal;
}
Also used : Function(com.google.common.base.Function) Reservation(com.amazonaws.services.ec2.model.Reservation) FluentIterable(com.google.common.collect.FluentIterable) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) List(java.util.List) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 55 with Instance

use of software.amazon.awssdk.services.ec2.model.Instance in project incubator-gobblin by apache.

the class AWSSdkClient method getInstancesForGroup.

/**
 * Get list of EC2 {@link Instance}s for a auto scaling group
 *
 * @param groupName Auto scaling group name
 * @param status Instance status (eg. running)
 * @return List of EC2 instances found for the input auto scaling group
 */
public List<Instance> getInstancesForGroup(String groupName, String status) {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final DescribeInstancesResult instancesResult = amazonEC2.describeInstances(new DescribeInstancesRequest().withFilters(new Filter().withName("tag:aws:autoscaling:groupName").withValues(groupName)));
    final List<Instance> instances = new ArrayList<>();
    for (Reservation reservation : instancesResult.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (null == status || null == instance.getState() || status.equals(instance.getState().getName())) {
                instances.add(instance);
                LOGGER.info("Found instance: " + instance + " which qualified filter: " + status);
            } else {
                LOGGER.info("Found instance: " + instance + " but did not qualify for filter: " + status);
            }
        }
    }
    return instances;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)86 ArrayList (java.util.ArrayList)43 Reservation (com.amazonaws.services.ec2.model.Reservation)39 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)31 List (java.util.List)23 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)22 HashMap (java.util.HashMap)22 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18 Operation (com.vmware.xenon.common.Operation)18 Tag (com.amazonaws.services.ec2.model.Tag)17 Map (java.util.Map)17 Test (org.junit.Test)16 Filter (com.amazonaws.services.ec2.model.Filter)14 HashSet (java.util.HashSet)13 Volume (com.amazonaws.services.ec2.model.Volume)12 Utils (com.vmware.xenon.common.Utils)12 TimeUnit (java.util.concurrent.TimeUnit)12 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)11 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)11 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)10