use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.
the class TestOldDetachedVolumeRule method testUnavailableVolume.
@Test
public void testUnavailableVolume() {
Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME);
((AWSResource) resource).setAWSResourceState("stopped");
OldDetachedVolumeRule rule = new OldDetachedVolumeRule(new TestMonkeyCalendar(), 0, 0);
Assert.assertTrue(rule.isValid(resource));
Assert.assertNull(resource.getExpectedTerminationTime());
}
use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.
the class TestOldDetachedVolumeRule method testResourceWithExpectedTerminationTimeSet.
@Test
public void testResourceWithExpectedTerminationTimeSet() {
DateTime now = DateTime.now();
Date oldTermDate = new Date(now.plusDays(10).getMillis());
String oldTermReason = "Foo";
int ageThreshold = 5;
Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(new Date(now.minusDays(ageThreshold + 1).getMillis()));
((AWSResource) resource).setAWSResourceState("available");
Date lastDetachTime = new Date(now.minusDays(ageThreshold + 1).getMillis());
String metaTag = VolumeTaggingMonkey.makeMetaTag(null, null, lastDetachTime);
resource.setTag(JanitorMonkey.JANITOR_META_TAG, metaTag);
int retentionDays = 4;
OldDetachedVolumeRule rule = new OldDetachedVolumeRule(new TestMonkeyCalendar(), ageThreshold, retentionDays);
resource.setExpectedTerminationTime(oldTermDate);
resource.setTerminationReason(oldTermReason);
Assert.assertFalse(rule.isValid(resource));
Assert.assertEquals(oldTermDate, resource.getExpectedTerminationTime());
Assert.assertEquals(oldTermReason, resource.getTerminationReason());
}
use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.
the class ASGJanitorCrawler method getASGResources.
private List<Resource> getASGResources(String... asgNames) {
AWSClient awsClient = getAWSClient();
List<LaunchConfiguration> launchConfigurations = awsClient.describeLaunchConfigurations();
for (LaunchConfiguration lc : launchConfigurations) {
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);
}
return resources;
}
use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.
the class EBSVolumeJanitorCrawler method getVolumeResources.
private List<Resource> getVolumeResources(String... volumeIds) {
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;
}
use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.
the class ELBJanitorCrawler method getELBResources.
private List<Resource> getELBResources(String... elbNames) {
List<Resource> resources = new LinkedList<Resource>();
AWSClient awsClient = getAWSClient();
for (LoadBalancerDescription elb : awsClient.describeElasticLoadBalancers(elbNames)) {
Resource resource = new AWSResource().withId(elb.getLoadBalancerName()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.ELB).withLaunchTime(elb.getCreatedTime());
resource.setOwnerEmail(getOwnerEmailForResource(resource));
resources.add(resource);
List<Instance> instances = elb.getInstances();
if (instances == null || 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 (Instance instance : instances) {
String instanceId = instance.getInstanceId();
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));
}
for (TagDescription tagDescription : awsClient.describeElasticLoadBalancerTags(resource.getId())) {
for (Tag tag : tagDescription.getTags()) {
LOGGER.debug(String.format("Adding tag %s = %s to resource %s", tag.getKey(), tag.getValue(), resource.getId()));
resource.setTag(tag.getKey(), tag.getValue());
}
}
}
Map<String, List<String>> elbtoASGMap = buildELBtoASGMap();
for (Resource resource : resources) {
List<String> asgList = elbtoASGMap.get(resource.getId());
if (asgList != null && asgList.size() > 0) {
resource.setAdditionalField("referencedASGCount", "" + asgList.size());
String asgStr = StringUtils.join(asgList, ",");
resource.setDescription(resource.getDescription() + ", ASGS=" + asgStr);
LOGGER.debug(String.format("Resource ELB %s is referenced by ASGs %s", resource.getId(), asgStr));
} else {
resource.setAdditionalField("referencedASGCount", "0");
resource.setDescription(resource.getDescription() + ", ASGS=none");
LOGGER.debug(String.format("No ASGs found for ELB %s", resource.getId()));
}
}
return resources;
}
Aggregations