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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations