use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class EddaEBSSnapshotJanitorCrawler method parseJsonElementToSnapshotResource.
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;
}
use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class EddaEBSVolumeJanitorCrawler method getBatchUrl.
private String getBatchUrl(String region, List<Resource> batch) {
StringBuilder batchUrl = new StringBuilder(eddaClient.getBaseUrl(region) + "/aws/volumes/");
boolean isFirst = true;
for (Resource resource : batch) {
if (!isFirst) {
batchUrl.append(',');
} else {
isFirst = false;
}
batchUrl.append(resource.getId());
}
batchUrl.append(";data.state=in-use;_since=0;_expand;_meta:" + "(ltime,data:(volumeId,attachments:(deleteOnTermination,instanceId)))");
return batchUrl.toString();
}
use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method updateReferenceTimeByLaunchConfig.
private void updateReferenceTimeByLaunchConfig(String region, List<Resource> batch, long since) {
LOGGER.info(String.format("Getting the last reference time by launch config for batch of size %d", batch.size()));
String batchUrl = getLaunchConfigBatchUrl(region, batch, since);
JsonNode batchResult = null;
Map<String, Resource> idToResource = Maps.newHashMap();
for (Resource resource : batch) {
idToResource.put(resource.getId(), resource);
}
try {
batchResult = eddaClient.getJsonNodeFromUrl(batchUrl);
} catch (IOException e) {
LOGGER.error("Failed to get response for the batch.", e);
}
if (batchResult == null || !batchResult.isArray()) {
throw new RuntimeException(String.format("Failed to get valid document from %s, got: %s", batchUrl, batchResult));
}
for (Iterator<JsonNode> it = batchResult.getElements(); it.hasNext(); ) {
JsonNode elem = it.next();
JsonNode data = elem.get("data");
String imageId = data.get("imageId").getTextValue();
String launchConfigurationName = data.get("launchConfigurationName").getTextValue();
JsonNode ltimeNode = elem.get("ltime");
if (ltimeNode != null && !ltimeNode.isNull()) {
long ltime = ltimeNode.asLong();
Resource ami = idToResource.get(imageId);
String lastRefTimeByLC = ami.getAdditionalField(AMI_FIELD_LAST_LC_REF_TIME);
if (lastRefTimeByLC == null || Long.parseLong(lastRefTimeByLC) < ltime) {
LOGGER.info(String.format("The last time that the image %s was referenced by launch config %s is %d", imageId, launchConfigurationName, ltime));
ami.setAdditionalField(AMI_FIELD_LAST_LC_REF_TIME, String.valueOf(ltime));
}
}
}
}
use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class EddaImageJanitorCrawler method parseJsonElementToresource.
private Resource parseJsonElementToresource(String region, JsonNode jsonNode) {
Validate.notNull(jsonNode);
String imageId = jsonNode.get("imageId").getTextValue();
Resource resource = new AWSResource().withId(imageId).withRegion(region).withResourceType(AWSResourceType.IMAGE);
Long creationTime = imageIdToCreationTime.get(imageId);
if (creationTime != null) {
resource.setLaunchTime(new Date(creationTime));
}
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 descNode = jsonNode.get("description");
if (descNode != null && !descNode.isNull()) {
String description = descNode.getTextValue();
resource.setDescription(description);
String ancestorImageId = getBaseAmiIdFromDescription(description);
if (ancestorImageId != null && !ancestorImageIds.contains(ancestorImageId)) {
LOGGER.info(String.format("Found base AMI id %s from description '%s'", ancestorImageId, description));
ancestorImageIds.add(ancestorImageId);
}
}
((AWSResource) resource).setAWSResourceState(jsonNode.get("state").getTextValue());
String owner = getOwnerEmailForResource(resource);
if (owner != null) {
resource.setOwnerEmail(owner);
}
return resource;
}
use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.
the class EddaInstanceJanitorCrawler method refreshOwnerByImage.
private void refreshOwnerByImage(String region, List<Resource> resources) {
HashSet<String> imageIds = new HashSet<>();
for (Resource resource : resources) {
if (resource.getOwnerEmail() == null) {
imageIds.add(resource.getAdditionalField("imageId"));
}
}
if (imageIds.size() > 0) {
HashMap<String, String> imageToOwner = new HashMap<>();
String baseurl = eddaClient.getBaseUrl(region) + "/aws/images/";
Iterator<String> itr = imageIds.iterator();
long leftToQuery = imageIds.size();
while (leftToQuery > 0) {
long batchcount = leftToQuery > MAX_IMAGE_IDS_PER_QUERY ? MAX_IMAGE_IDS_PER_QUERY : leftToQuery;
leftToQuery -= batchcount;
ArrayList<String> batch = new ArrayList<>();
for (int i = 0; i < batchcount; i++) {
batch.add(itr.next());
}
String url = baseurl;
url += StringUtils.join(batch, ',');
url += ";tags.key=owner;public=false;_expand:(imageId,tags:(owner))";
JsonNode imageJsonNode = null;
try {
imageJsonNode = eddaClient.getJsonNodeFromUrl(url);
} catch (Exception e) {
LOGGER.error(String.format("Failed to get Json node from edda for AMIs in region %s.", region), e);
}
if (imageJsonNode != null) {
for (Iterator<JsonNode> it = imageJsonNode.getElements(); it.hasNext(); ) {
JsonNode image = it.next();
String imageId = image.get("imageId").getTextValue();
JsonNode tags = image.get("tags");
for (Iterator<JsonNode> tagIt = tags.getElements(); tagIt.hasNext(); ) {
JsonNode tag = tagIt.next();
if (tag.get(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY) != null) {
imageToOwner.put(imageId, tag.get(BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY).getTextValue());
break;
}
}
}
}
}
if (imageToOwner.size() > 0) {
for (Resource resource : resources) {
if (resource.getOwnerEmail() == null && imageToOwner.get(resource.getAdditionalField("imageId")) != null) {
resource.setOwnerEmail(imageToOwner.get(resource.getAdditionalField("imageId")));
LOGGER.info(String.format("Found owner %s for instance %s in AMI %s", resource.getOwnerEmail(), resource.getId(), resource.getAdditionalField("imageId")));
}
}
}
}
}
Aggregations