Search in sources :

Example 1 with AWSResource

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

the class EBSSnapshotJanitorCrawler method getSnapshotResources.

private List<Resource> getSnapshotResources(String... snapshotIds) {
    refreshSnapshotToAMIs();
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    for (Snapshot snapshot : awsClient.describeSnapshots(snapshotIds)) {
        Resource snapshotResource = new AWSResource().withId(snapshot.getSnapshotId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.EBS_SNAPSHOT).withLaunchTime(snapshot.getStartTime()).withDescription(snapshot.getDescription());
        for (Tag tag : snapshot.getTags()) {
            LOGGER.debug(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), snapshotResource.getId()));
            snapshotResource.setTag(tag.getKey(), tag.getValue());
        }
        snapshotResource.setOwnerEmail(getOwnerEmailForResource(snapshotResource));
        ((AWSResource) snapshotResource).setAWSResourceState(snapshot.getState());
        Collection<String> amis = snapshotToAMIs.get(snapshotResource.getId());
        if (amis != null) {
            snapshotResource.setAdditionalField(SNAPSHOT_FIELD_AMIS, StringUtils.join(amis, ","));
        }
        resources.add(snapshotResource);
    }
    return resources;
}
Also used : Snapshot(com.amazonaws.services.ec2.model.Snapshot) 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 2 with AWSResource

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

the class LaunchConfigJanitorCrawler method getLaunchConfigResources.

private List<Resource> getLaunchConfigResources(String... launchConfigNames) {
    List<Resource> resources = Lists.newArrayList();
    AWSClient awsClient = getAWSClient();
    Set<String> usedLCs = Sets.newHashSet();
    for (AutoScalingGroup asg : awsClient.describeAutoScalingGroups()) {
        usedLCs.add(asg.getLaunchConfigurationName());
    }
    for (LaunchConfiguration launchConfiguration : awsClient.describeLaunchConfigurations(launchConfigNames)) {
        String lcName = launchConfiguration.getLaunchConfigurationName();
        Resource lcResource = new AWSResource().withId(lcName).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.LAUNCH_CONFIG).withLaunchTime(launchConfiguration.getCreatedTime());
        lcResource.setOwnerEmail(getOwnerEmailForResource(lcResource));
        lcResource.setAdditionalField(LAUNCH_CONFIG_FIELD_USED_BY_ASG, String.valueOf(usedLCs.contains(lcName)));
        resources.add(lcResource);
    }
    return resources;
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient)

Example 3 with AWSResource

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

the class EddaEBSVolumeJanitorCrawler method parseJsonElementToVolumeResource.

private Resource parseJsonElementToVolumeResource(String region, JsonNode jsonNode) {
    Validate.notNull(jsonNode);
    long createTime = jsonNode.get("createTime").asLong();
    Resource resource = new AWSResource().withId(jsonNode.get("volumeId").getTextValue()).withRegion(region).withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(new Date(createTime));
    JsonNode tags = jsonNode.get("tags");
    StringBuilder description = new StringBuilder();
    JsonNode size = jsonNode.get("size");
    description.append(String.format("size=%s", size == null ? "unknown" : size.getIntValue()));
    if (tags == null || !tags.isArray() || tags.size() == 0) {
        LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
    } else {
        for (Iterator<JsonNode> it = tags.getElements(); it.hasNext(); ) {
            JsonNode tag = it.next();
            String key = tag.get("key").getTextValue();
            String value = tag.get("value").getTextValue();
            description.append(String.format("; %s=%s", key, value));
            resource.setTag(key, value);
            if (key.equals(PURPOSE)) {
                resource.setAdditionalField(PURPOSE, value);
            }
        }
        resource.setDescription(description.toString());
    }
    ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode)

Example 4 with AWSResource

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

the class EddaELBJanitorCrawler method parseJsonElementToELBResource.

private Resource parseJsonElementToELBResource(String region, JsonNode jsonNode) {
    Validate.notNull(jsonNode);
    String elbName = jsonNode.get("loadBalancerName").getTextValue();
    long launchTime = jsonNode.get("createdTime").getLongValue();
    Resource resource = new AWSResource().withId(elbName).withRegion(region).withResourceType(AWSResourceType.ELB).withLaunchTime(new Date(launchTime));
    String dnsName = jsonNode.get("DNSName").getTextValue();
    resource.setAdditionalField("DNSName", dnsName);
    JsonNode tags = jsonNode.get("tags");
    if (tags == null || !tags.isArray() || tags.size() == 0) {
        LOGGER.debug(String.format("No tags is found for %s", resource.getId()));
    } else {
        for (Iterator<JsonNode> it = tags.getElements(); it.hasNext(); ) {
            JsonNode tag = it.next();
            String key = tag.get("key").getTextValue();
            String value = tag.get("value").getTextValue();
            resource.setTag(key, value);
        }
    }
    String owner = getOwnerEmailForResource(resource);
    if (owner != null) {
        resource.setOwnerEmail(owner);
    }
    LOGGER.debug(String.format("Owner of ELB Resource %s (ELB DNS: %s) is %s", resource.getId(), resource.getAdditionalField("DNSName"), resource.getOwnerEmail()));
    JsonNode instances = jsonNode.get("instances");
    if (instances == null || !instances.isArray() || 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 (Iterator<JsonNode> it = instances.getElements(); it.hasNext(); ) {
            JsonNode instance = it.next();
            String instanceId = instance.get("instanceId").getTextValue();
            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));
    }
    return resource;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode)

Example 5 with AWSResource

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

the class EddaLaunchConfigJanitorCrawler method getLaunchConfigResourcesInRegion.

private List<Resource> getLaunchConfigResourcesInRegion(String region, String... launchConfigNames) {
    String url = eddaClient.getBaseUrl(region) + "/aws/launchConfigurations;";
    if (launchConfigNames != null && launchConfigNames.length != 0) {
        url += StringUtils.join(launchConfigNames, ',');
        LOGGER.info(String.format("Getting launch configurations in region %s for %d ids", region, launchConfigNames.length));
    } else {
        LOGGER.info(String.format("Getting all launch configurations in region %s", region));
    }
    url += ";_expand:(launchConfigurationName,createdTime)";
    JsonNode jsonNode = null;
    try {
        jsonNode = eddaClient.getJsonNodeFromUrl(url);
    } catch (Exception e) {
        LOGGER.error(String.format("Failed to get Jason node from edda for instances in region %s.", region), e);
    }
    if (jsonNode == null || !jsonNode.isArray()) {
        throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", url, jsonNode));
    }
    List<Resource> resources = Lists.newArrayList();
    Set<String> usedLCs = getLaunchConfigsInUse(region);
    for (Iterator<JsonNode> it = jsonNode.getElements(); it.hasNext(); ) {
        JsonNode launchConfiguration = it.next();
        String lcName = launchConfiguration.get("launchConfigurationName").getTextValue();
        Resource lcResource = new AWSResource().withId(lcName).withRegion(region).withResourceType(AWSResourceType.LAUNCH_CONFIG).withLaunchTime(new Date(launchConfiguration.get("createdTime").getLongValue()));
        lcResource.setOwnerEmail(getOwnerEmailForResource(lcResource));
        lcResource.setAdditionalField(LAUNCH_CONFIG_FIELD_USED_BY_ASG, String.valueOf(usedLCs.contains(lcName)));
        resources.add(lcResource);
    }
    return resources;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

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