Search in sources :

Example 61 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class TestOldDetachedVolumeRule method testUnavailableVolume.

@Test
public void testUnavailableVolume() {
    Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME);
    ((AWSResource) resource).setAWSResourceState("stopped");
    OldDetachedVolumeRule rule = new OldDetachedVolumeRule(new TestMonkeyCalendar(), 0, 0);
    Assert.assertTrue(rule.isValid(resource));
    Assert.assertNull(resource.getExpectedTerminationTime());
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Test(org.testng.annotations.Test)

Example 62 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class TestOldDetachedVolumeRule method testResourceWithExpectedTerminationTimeSet.

@Test
public void testResourceWithExpectedTerminationTimeSet() {
    DateTime now = DateTime.now();
    Date oldTermDate = new Date(now.plusDays(10).getMillis());
    String oldTermReason = "Foo";
    int ageThreshold = 5;
    Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(new Date(now.minusDays(ageThreshold + 1).getMillis()));
    ((AWSResource) resource).setAWSResourceState("available");
    Date lastDetachTime = new Date(now.minusDays(ageThreshold + 1).getMillis());
    String metaTag = VolumeTaggingMonkey.makeMetaTag(null, null, lastDetachTime);
    resource.setTag(JanitorMonkey.JANITOR_META_TAG, metaTag);
    int retentionDays = 4;
    OldDetachedVolumeRule rule = new OldDetachedVolumeRule(new TestMonkeyCalendar(), ageThreshold, retentionDays);
    resource.setExpectedTerminationTime(oldTermDate);
    resource.setTerminationReason(oldTermReason);
    Assert.assertFalse(rule.isValid(resource));
    Assert.assertEquals(oldTermDate, resource.getExpectedTerminationTime());
    Assert.assertEquals(oldTermReason, resource.getTerminationReason());
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 63 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class ASGJanitorCrawler method getASGResources.

private List<Resource> getASGResources(String... asgNames) {
    AWSClient awsClient = getAWSClient();
    List<LaunchConfiguration> launchConfigurations = awsClient.describeLaunchConfigurations();
    for (LaunchConfiguration lc : launchConfigurations) {
        nameToLaunchConfig.put(lc.getLaunchConfigurationName(), lc);
    }
    List<Resource> resources = new LinkedList<Resource>();
    for (AutoScalingGroup asg : awsClient.describeAutoScalingGroups(asgNames)) {
        Resource asgResource = new AWSResource().withId(asg.getAutoScalingGroupName()).withResourceType(AWSResourceType.ASG).withRegion(awsClient.region()).withLaunchTime(asg.getCreatedTime());
        for (TagDescription tag : asg.getTags()) {
            asgResource.setTag(tag.getKey(), tag.getValue());
        }
        asgResource.setDescription(String.format("%d instances", asg.getInstances().size()));
        asgResource.setOwnerEmail(getOwnerEmailForResource(asgResource));
        if (asg.getStatus() != null) {
            ((AWSResource) asgResource).setAWSResourceState(asg.getStatus());
        }
        Integer maxSize = asg.getMaxSize();
        if (maxSize != null) {
            asgResource.setAdditionalField(ASG_FIELD_MAX_SIZE, String.valueOf(maxSize));
        }
        // Adds instances and ELBs as additional fields.
        List<String> instances = new ArrayList<String>();
        for (Instance instance : asg.getInstances()) {
            instances.add(instance.getInstanceId());
        }
        asgResource.setAdditionalField(ASG_FIELD_INSTANCES, StringUtils.join(instances, ","));
        asgResource.setAdditionalField(ASG_FIELD_ELBS, StringUtils.join(asg.getLoadBalancerNames(), ","));
        String lcName = asg.getLaunchConfigurationName();
        LaunchConfiguration lc = nameToLaunchConfig.get(lcName);
        if (lc != null) {
            asgResource.setAdditionalField(ASG_FIELD_LC_NAME, lcName);
        }
        if (lc != null && lc.getCreatedTime() != null) {
            asgResource.setAdditionalField(ASG_FIELD_LC_CREATION_TIME, String.valueOf(lc.getCreatedTime().getTime()));
        }
        // sets the field for the time when the ASG's traffic is suspended from ELB
        for (SuspendedProcess sp : asg.getSuspendedProcesses()) {
            if ("AddToLoadBalancer".equals(sp.getProcessName())) {
                String suspensionTime = getSuspensionTimeString(sp.getSuspensionReason());
                if (suspensionTime != null) {
                    LOGGER.info(String.format("Suspension time of ASG %s is %s", asg.getAutoScalingGroupName(), suspensionTime));
                    asgResource.setAdditionalField(ASG_FIELD_SUSPENSION_TIME, suspensionTime);
                    break;
                }
            }
        }
        resources.add(asgResource);
    }
    return resources;
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Instance(com.amazonaws.services.autoscaling.model.Instance) TagDescription(com.amazonaws.services.autoscaling.model.TagDescription) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) ArrayList(java.util.ArrayList) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) LinkedList(java.util.LinkedList) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) AWSResource(com.netflix.simianarmy.aws.AWSResource) SuspendedProcess(com.amazonaws.services.autoscaling.model.SuspendedProcess)

Example 64 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class EBSVolumeJanitorCrawler method getVolumeResources.

private List<Resource> getVolumeResources(String... volumeIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    for (Volume volume : awsClient.describeVolumes(volumeIds)) {
        Resource volumeResource = new AWSResource().withId(volume.getVolumeId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(volume.getCreateTime());
        for (Tag tag : volume.getTags()) {
            LOGGER.info(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), volumeResource.getId()));
            volumeResource.setTag(tag.getKey(), tag.getValue());
        }
        volumeResource.setOwnerEmail(getOwnerEmailForResource(volumeResource));
        volumeResource.setDescription(getVolumeDescription(volume));
        ((AWSResource) volumeResource).setAWSResourceState(volume.getState());
        resources.add(volumeResource);
    }
    return resources;
}
Also used : Volume(com.amazonaws.services.ec2.model.Volume) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 65 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class ELBJanitorCrawler method getELBResources.

private List<Resource> getELBResources(String... elbNames) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    for (LoadBalancerDescription elb : awsClient.describeElasticLoadBalancers(elbNames)) {
        Resource resource = new AWSResource().withId(elb.getLoadBalancerName()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.ELB).withLaunchTime(elb.getCreatedTime());
        resource.setOwnerEmail(getOwnerEmailForResource(resource));
        resources.add(resource);
        List<Instance> instances = elb.getInstances();
        if (instances == null || instances.size() == 0) {
            resource.setAdditionalField("instanceCount", "0");
            resource.setDescription("instances=none");
            LOGGER.debug(String.format("No instances found for ELB %s", resource.getId()));
        } else {
            resource.setAdditionalField("instanceCount", "" + instances.size());
            ArrayList<String> instanceList = new ArrayList<String>(instances.size());
            LOGGER.debug(String.format("Found %d instances for ELB %s", instances.size(), resource.getId()));
            for (Instance instance : instances) {
                String instanceId = instance.getInstanceId();
                instanceList.add(instanceId);
            }
            String instancesStr = StringUtils.join(instanceList, ",");
            resource.setDescription(String.format("instances=%s", instances));
            LOGGER.debug(String.format("Resource ELB %s has instances %s", resource.getId(), instancesStr));
        }
        for (TagDescription tagDescription : awsClient.describeElasticLoadBalancerTags(resource.getId())) {
            for (Tag tag : tagDescription.getTags()) {
                LOGGER.debug(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), resource.getId()));
                resource.setTag(tag.getKey(), tag.getValue());
            }
        }
    }
    Map<String, List<String>> elbtoASGMap = buildELBtoASGMap();
    for (Resource resource : resources) {
        List<String> asgList = elbtoASGMap.get(resource.getId());
        if (asgList != null && asgList.size() > 0) {
            resource.setAdditionalField("referencedASGCount", "" + asgList.size());
            String asgStr = StringUtils.join(asgList, ",");
            resource.setDescription(resource.getDescription() + ", ASGS=" + asgStr);
            LOGGER.debug(String.format("Resource ELB %s is referenced by ASGs %s", resource.getId(), asgStr));
        } else {
            resource.setAdditionalField("referencedASGCount", "0");
            resource.setDescription(resource.getDescription() + ", ASGS=none");
            LOGGER.debug(String.format("No ASGs found for ELB %s", resource.getId()));
        }
    }
    return resources;
}
Also used : Instance(com.amazonaws.services.elasticloadbalancing.model.Instance) TagDescription(com.amazonaws.services.elasticloadbalancing.model.TagDescription) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) AWSResource(com.netflix.simianarmy.aws.AWSResource) Tag(com.amazonaws.services.elasticloadbalancing.model.Tag) LoadBalancerDescription(com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription)

Aggregations

AWSResource (com.netflix.simianarmy.aws.AWSResource)109 Resource (com.netflix.simianarmy.Resource)102 Test (org.testng.annotations.Test)89 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)64 DateTime (org.joda.time.DateTime)63 Date (java.util.Date)41 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 BeforeTest (org.testng.annotations.BeforeTest)9 JsonNode (org.codehaus.jackson.JsonNode)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)6 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)5 HashSet (java.util.HashSet)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 LinkedList (java.util.LinkedList)4 Tag (com.amazonaws.services.ec2.model.Tag)3 HashMap (java.util.HashMap)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)2 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)1 Instance (com.amazonaws.services.autoscaling.model.Instance)1