Search in sources :

Example 81 with Instance

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

the class TestInstanceInVPC method setUp.

@BeforeMethod
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    List<Instance> instanceList = Lists.newArrayList();
    Instance instance = new Instance().withInstanceId(VPC_INSTANCE_ID).withVpcId("12345");
    instanceList.add(instance);
    doReturn(instanceList).when(instanceInVPC).getAWSInstances(REGION, VPC_INSTANCE_ID);
    List<Instance> instanceList2 = Lists.newArrayList();
    Instance instance2 = new Instance().withInstanceId(INSTANCE_ID);
    instanceList2.add(instance2);
    doReturn(instanceList2).when(instanceInVPC).getAWSInstances(REGION, INSTANCE_ID);
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 82 with Instance

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

the class TestInstanceJanitorCrawler method testInstancesWithIds.

@Test
public void testInstancesWithIds() {
    List<AutoScalingInstanceDetails> instanceDetailsList = createInstanceDetailsList();
    List<Instance> instanceList = createInstanceList();
    String[] ids = { "i-12345678901234560", "i-12345678901234561" };
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList, ids);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources(ids);
    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 83 with Instance

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

the class TestInstanceJanitorCrawler method testInstancesNotExistingInASG.

@Test
public void testInstancesNotExistingInASG() {
    List<AutoScalingInstanceDetails> instanceDetailsList = Collections.emptyList();
    List<Instance> instanceList = createInstanceList();
    AWSClient awsMock = createMockAWSClient(instanceDetailsList, instanceList);
    InstanceJanitorCrawler crawler = new InstanceJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources();
    Assert.assertEquals(resources.size(), instanceList.size());
}
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 84 with Instance

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

the class AWSClient method listAttachedVolumes.

@Override
public List<String> listAttachedVolumes(String instanceId, boolean includeRoot) {
    Validate.notEmpty(instanceId);
    LOGGER.info(String.format("Listing volumes attached to instance %s in region %s.", instanceId, region));
    try {
        List<String> volumeIds = new ArrayList<String>();
        for (Instance instance : describeInstances(instanceId)) {
            String rootDeviceName = instance.getRootDeviceName();
            for (InstanceBlockDeviceMapping ibdm : instance.getBlockDeviceMappings()) {
                EbsInstanceBlockDevice ebs = ibdm.getEbs();
                if (ebs == null) {
                    continue;
                }
                String volumeId = ebs.getVolumeId();
                if (Strings.isNullOrEmpty(volumeId)) {
                    continue;
                }
                if (!includeRoot && rootDeviceName != null && rootDeviceName.equals(ibdm.getDeviceName())) {
                    continue;
                }
                volumeIds.add(volumeId);
            }
        }
        return volumeIds;
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
            throw new NotFoundException("AWS instance " + instanceId + " not found", e);
        }
        throw e;
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.netflix.simianarmy.NotFoundException)

Example 85 with Instance

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

the class InstanceJanitorCrawler method getInstanceResources.

private List<Resource> getInstanceResources(String... instanceIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    Map<String, AutoScalingInstanceDetails> idToASGInstance = new HashMap<String, AutoScalingInstanceDetails>();
    for (AutoScalingInstanceDetails instanceDetails : awsClient.describeAutoScalingInstances(instanceIds)) {
        idToASGInstance.put(instanceDetails.getInstanceId(), instanceDetails);
    }
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        Resource instanceResource = new AWSResource().withId(instance.getInstanceId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.INSTANCE).withLaunchTime(instance.getLaunchTime());
        for (Tag tag : instance.getTags()) {
            instanceResource.setTag(tag.getKey(), tag.getValue());
        }
        String description = String.format("type=%s; host=%s", instance.getInstanceType(), instance.getPublicDnsName() == null ? "" : instance.getPublicDnsName());
        instanceResource.setDescription(description);
        instanceResource.setOwnerEmail(getOwnerEmailForResource(instanceResource));
        String asgName = getAsgName(instanceResource, idToASGInstance);
        if (asgName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_ASG_NAME, asgName);
            LOGGER.info(String.format("instance %s has a ASG tag name %s.", instanceResource.getId(), asgName));
        }
        String opsworksStackName = getOpsWorksStackName(instanceResource);
        if (opsworksStackName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_OPSWORKS_STACK_NAME, opsworksStackName);
            LOGGER.info(String.format("instance %s is part of an OpsWorks stack named %s.", instanceResource.getId(), opsworksStackName));
        }
        if (instance.getState() != null) {
            ((AWSResource) instanceResource).setAWSResourceState(instance.getState().getName());
        }
        resources.add(instanceResource);
    }
    return resources;
}
Also used : HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) AWSResource(com.netflix.simianarmy.aws.AWSResource) 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) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

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