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