Package com.netflix.simianarmy

Examples of com.netflix.simianarmy.Resource


                Set<String> processedIds = Sets.newHashSet();
                for (Iterator<JsonNode> it = batchResult.getElements(); it.hasNext();) {
                    JsonNode elem = it.next();
                    JsonNode data = elem.get("data");
                    String volumeId = data.get("volumeId").getTextValue();
                    Resource resource = idToResource.get(volumeId);
                    JsonNode attachments = data.get("attachments");

                    if (!(attachments.isArray() && attachments.size() > 0)) {
                        continue;
                    }
                    JsonNode attachment = attachments.get(0);

                    JsonNode ltime = elem.get("ltime");
                    if (ltime == null || ltime.isNull()) {
                        continue;
                    }
                    DateTime detachTime = new DateTime(ltime.asLong());
                    processedIds.add(volumeId);
                    setAttachmentInfo(volumeId, attachment, detachTime, resource);
                }

                for (Map.Entry<String, Resource> volumeEntry : idToResource.entrySet()) {
                    String id = volumeEntry.getKey();
                    if (!processedIds.contains(id)) {
                        Resource resource = volumeEntry.getValue();
                        LOGGER.info(String.format("Volume %s never was attached, use createTime %s as the detachTime",
                                id, resource.getLaunchTime()));
                        setAttachmentInfo(id, null, new DateTime(resource.getLaunchTime().getTime()), resource);
                    }
                }
            }
        }
    }
View Full Code Here


            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;
    }
View Full Code Here

        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;
    }
View Full Code Here

        Validate.notNull(jsonNode);

        String instanceId = jsonNode.get("instanceId").getTextValue();
        long launchTime = jsonNode.get("launchTime").getLongValue();

        Resource resource = new AWSResource().withId(instanceId).withRegion(region)
                .withResourceType(AWSResourceType.INSTANCE)
                .withLaunchTime(new Date(launchTime));

        JsonNode publicDnsName = jsonNode.get("publicDnsName");
        String description = String.format("type=%s; host=%s",
                jsonNode.get("instanceType").getTextValue(),
                publicDnsName == null ? "" : publicDnsName.getTextValue());
        resource.setDescription(description);

        String owner = getOwnerEmailForResource(resource);
        resource.setOwnerEmail(owner);
        JsonNode tags = jsonNode.get("tags");
        String asgName = null;
        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);
                if ("aws:autoscaling:groupName".equals(key)) {
                    asgName = value;
                } else if (owner == null && "owner".equals(key)) {
                    resource.setOwnerEmail(value);
                }
            }
            resource.setDescription(description.toString());
        }
        // If we cannot find ASG name in tags, use the map for the ASG name
        if (asgName == null) {
            asgName = instanceToAsg.get(instanceId);
            if (asgName != null) {
                LOGGER.debug(String.format("Failed to find ASG name in tags of %s, use the ASG name %s from map",
                        instanceId, asgName));
            }
        }
        if (asgName != null) {
            resource.setAdditionalField(InstanceJanitorCrawler.INSTANCE_FIELD_ASG_NAME, asgName);
        }
        ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").get("name").getTextValue());
        String imageId = jsonNode.get("imageId").getTextValue();
        resource.setAdditionalField("imageId", imageId);
        return resource;
    }
View Full Code Here

        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;
    }
View Full Code Here

        Validate.notNull(jsonNode);

        String asgName = jsonNode.get("autoScalingGroupName").getTextValue();
        long createdTime = jsonNode.get("createdTime").getLongValue();

        Resource resource = new AWSResource().withId(asgName).withRegion(region)
                .withResourceType(AWSResourceType.ASG)
                .withLaunchTime(new Date(createdTime));

        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);
        }
        JsonNode maxSize = jsonNode.get("maxSize");
        if (maxSize != null) {
            resource.setAdditionalField(ASG_FIELD_MAX_SIZE, String.valueOf(maxSize.getIntValue()));
        }
        // Adds instances and ELBs as additional fields.
        JsonNode instances = jsonNode.get("instances");
        resource.setDescription(String.format("%d instances", instances.size()));
        List<String> instanceIds = Lists.newArrayList();
        for (Iterator<JsonNode> it = instances.getElements(); it.hasNext();) {
            instanceIds.add(it.next().get("instanceId").getTextValue());
        }
        resource.setAdditionalField(ASG_FIELD_INSTANCES, StringUtils.join(instanceIds, ","));
        JsonNode elbs = jsonNode.get("loadBalancerNames");
        List<String> elbNames = Lists.newArrayList();
        for (Iterator<JsonNode> it = elbs.getElements(); it.hasNext();) {
            elbNames.add(it.next().getTextValue());
        }
        resource.setAdditionalField(ASG_FIELD_ELBS, StringUtils.join(elbNames, ","));

        JsonNode lc = jsonNode.get("launchConfigurationName");
        if (lc != null) {
            String lcName = lc.getTextValue();
            Long lcCreationTime = lcNameToCreationTime.get(lcName);
            resource.setAdditionalField(ASG_FIELD_LC_NAME, lcName);
            if (lcCreationTime != null) {
                resource.setAdditionalField(ASG_FIELD_LC_CREATION_TIME, String.valueOf(lcCreationTime));
            }
        }
        // sets the field for the time when the ASG's traffic is suspended from ELB
        JsonNode suspendedProcesses = jsonNode.get("suspendedProcesses");
        for (Iterator<JsonNode> it = suspendedProcesses.getElements(); it.hasNext();) {
            JsonNode sp = it.next();
            if ("AddToLoadBalancer".equals(sp.get("processName").getTextValue())) {
                String suspensionTime = getSuspensionTimeString(sp.get("suspensionReason").getTextValue());
                if (suspensionTime != null) {
                    LOGGER.info(String.format("Suspension time of ASG %s is %s",
                            asgName, suspensionTime));
                    resource.setAdditionalField(ASG_FIELD_SUSPENSION_TIME, suspensionTime);
                    break;
                }
            }
        }
        Long lastChangeTime = regionToAsgToLastChangeTime.get(region).get(asgName);
        if (lastChangeTime != null) {
            resource.setAdditionalField(ASG_FIELD_LAST_CHANGE_TIME, String.valueOf(lastChangeTime));
        }
        return resource;

    }
View Full Code Here

            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);
View Full Code Here

        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));
            }
            if (instance.getState() != null) {
                ((AWSResource) instanceResource).setAWSResourceState(instance.getState().getName());
            }
            resources.add(instanceResource);
View Full Code Here

    private Resource parseJsonElementToSnapshotResource(String region, JsonNode jsonNode) {
        Validate.notNull(jsonNode);
        long startTime = jsonNode.get("startTime").asLong();

        Resource resource = new AWSResource().withId(jsonNode.get("snapshotId").getTextValue()).withRegion(region)
                .withResourceType(AWSResourceType.EBS_SNAPSHOT)
                .withLaunchTime(new Date(startTime));
        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);
            }
        }
        JsonNode description = jsonNode.get("description");
        if (description != null) {
            resource.setDescription(description.getTextValue());
        }
        ((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());
        Collection<String> amis = snapshotToAMIs.get(resource.getId());
        if (amis != null) {
            resource.setAdditionalField(SNAPSHOT_FIELD_AMIS, StringUtils.join(amis, ","));
        }
        resource.setOwnerEmail(getOwnerEmailForResource(resource));
        return resource;
    }
View Full Code Here

        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;
    }
View Full Code Here

TOP

Related Classes of com.netflix.simianarmy.Resource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.