Search in sources :

Example 71 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class EddaInstanceJanitorCrawler method parseJsonElementToInstanceResource.

private Resource parseJsonElementToInstanceResource(String region, JsonNode jsonNode) {
    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 && BasicSimianArmyContext.GLOBAL_OWNER_TAGKEY.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;
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) JsonNode(org.codehaus.jackson.JsonNode) Date(java.util.Date)

Example 72 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class TestRDSJanitorResourceTracker method testGetResource.

@SuppressWarnings("unchecked")
@Test
public void testGetResource() {
    String id1 = "id-1";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    AWSResource result1 = mkResource(id1, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, false, fieldName, fieldValue);
    ArrayList<AWSResource> resources = new ArrayList<>();
    resources.add(result1);
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    Resource resource = tracker.getResource(id1);
    ArrayList<Resource> returnResources = new ArrayList<>();
    returnResources.add(resource);
    verifyResources(returnResources, id1, null, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, fieldName, fieldValue);
}
Also used : AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 73 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class TestRDSJanitorResourceTracker method testGetResources.

@SuppressWarnings("unchecked")
@Test
public void testGetResources() {
    String id1 = "id-1";
    String id2 = "id-2";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    AWSResource result1 = mkResource(id1, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, false, fieldName, fieldValue);
    AWSResource result2 = mkResource(id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, true, fieldName, fieldValue);
    ArrayList<AWSResource> resources = new ArrayList<>();
    resources.add(result1);
    resources.add(result2);
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    verifyResources(tracker.getResources(resourceType, state, region), id1, id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, fieldName, fieldValue);
}
Also used : AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 74 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class TestRDSJanitorResourceTracker method testInsertNewResource.

@SuppressWarnings("unchecked")
@Test
public void testInsertNewResource() {
    // mock the select query that is issued to see if the record already exists
    ArrayList<AWSResource> resources = new ArrayList<>();
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    String id = "i-12345678901234567";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    Resource resource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    ArgumentCaptor<Object> objCap = ArgumentCaptor.forClass(Object.class);
    ArgumentCaptor<String> sqlCap = ArgumentCaptor.forClass(String.class);
    when(tracker.getJdbcTemplate().update(sqlCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture())).thenReturn(1);
    tracker.addOrUpdate(resource);
    List<Object> args = objCap.getAllValues();
    Assert.assertEquals(sqlCap.getValue(), "insert into janitortable (resourceId,resourceType,region,ownerEmail,description,state,terminationReason,expectedTerminationTime,actualTerminationTime,notificationTime,launchTime,markTime,optOutOfJanitor,additionalFields) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    Assert.assertEquals(args.size(), 14);
    Assert.assertEquals(args.get(0).toString(), id);
    Assert.assertEquals(args.get(1).toString(), AWSResourceType.INSTANCE.toString());
    Assert.assertEquals(args.get(2).toString(), region);
    Assert.assertEquals(args.get(3).toString(), ownerEmail);
    Assert.assertEquals(args.get(4).toString(), description);
    Assert.assertEquals(args.get(5).toString(), state.toString());
    Assert.assertEquals(args.get(6).toString(), terminationReason);
    Assert.assertEquals(args.get(7).toString(), expectedTerminationTime.getTime() + "");
    Assert.assertEquals(args.get(8).toString(), "0");
    Assert.assertEquals(args.get(9).toString(), "0");
    Assert.assertEquals(args.get(10).toString(), "0");
    Assert.assertEquals(args.get(11).toString(), markTime.getTime() + "");
    Assert.assertEquals(args.get(12).toString(), "false");
    Assert.assertEquals(args.get(13).toString(), "{\"fieldName123\":\"fieldValue456\"}");
}
Also used : Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 75 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class TestRDSJanitorResourceTracker method testUpdateResource.

@SuppressWarnings("unchecked")
@Test
public void testUpdateResource() {
    String id = "i-12345678901234567";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    Resource resource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    // mock the select query that is issued to see if the record already exists
    ArrayList<Resource> resources = new ArrayList<>();
    resources.add(resource);
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    // update the ownerEmail
    ownerEmail = "owner2@test.com";
    Resource newResource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    ArgumentCaptor<Object> objCap = ArgumentCaptor.forClass(Object.class);
    ArgumentCaptor<String> sqlCap = ArgumentCaptor.forClass(String.class);
    when(tracker.getJdbcTemplate().update(sqlCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture())).thenReturn(1);
    tracker.addOrUpdate(newResource);
    List<Object> args = objCap.getAllValues();
    Assert.assertEquals(sqlCap.getValue(), "update janitortable set resourceType=?,region=?,ownerEmail=?,description=?,state=?,terminationReason=?,expectedTerminationTime=?,actualTerminationTime=?,notificationTime=?,launchTime=?,markTime=?,optOutOfJanitor=?,additionalFields=? where resourceId=? and region=?");
    Assert.assertEquals(args.size(), 15);
    Assert.assertEquals(args.get(0).toString(), AWSResourceType.INSTANCE.toString());
    Assert.assertEquals(args.get(1).toString(), region);
    Assert.assertEquals(args.get(2).toString(), ownerEmail);
    Assert.assertEquals(args.get(3).toString(), description);
    Assert.assertEquals(args.get(4).toString(), state.toString());
    Assert.assertEquals(args.get(5).toString(), terminationReason);
    Assert.assertEquals(args.get(6).toString(), expectedTerminationTime.getTime() + "");
    Assert.assertEquals(args.get(7).toString(), "0");
    Assert.assertEquals(args.get(8).toString(), "0");
    Assert.assertEquals(args.get(9).toString(), "0");
    Assert.assertEquals(args.get(10).toString(), markTime.getTime() + "");
    Assert.assertEquals(args.get(11).toString(), "false");
    Assert.assertEquals(args.get(12).toString(), "{\"fieldName123\":\"fieldValue456\"}");
    Assert.assertEquals(args.get(13).toString(), id);
    Assert.assertEquals(args.get(14).toString(), region);
}
Also used : Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Aggregations

AWSResource (com.netflix.simianarmy.aws.AWSResource)109 Resource (com.netflix.simianarmy.Resource)102 Test (org.testng.annotations.Test)89 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)64 DateTime (org.joda.time.DateTime)63 Date (java.util.Date)41 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 BeforeTest (org.testng.annotations.BeforeTest)9 JsonNode (org.codehaus.jackson.JsonNode)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)6 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)5 HashSet (java.util.HashSet)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 LinkedList (java.util.LinkedList)4 Tag (com.amazonaws.services.ec2.model.Tag)3 HashMap (java.util.HashMap)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)2 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)1 Instance (com.amazonaws.services.autoscaling.model.Instance)1