Search in sources :

Example 66 with Region

use of com.amazonaws.services.ec2.model.Region 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;
}
Also used : Volume(com.amazonaws.services.ec2.model.Volume) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 67 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class InstanceJanitorCrawler method getInstanceResources.

private List<Resource> getInstanceResources(String... instanceIds) {
    List<Resource> resources = new LinkedList<Resource>();
    AWSClient awsClient = getAWSClient();
    Map<String, AutoScalingInstanceDetails> idToASGInstance = new HashMap<String, AutoScalingInstanceDetails>();
    for (AutoScalingInstanceDetails instanceDetails : awsClient.describeAutoScalingInstances(instanceIds)) {
        idToASGInstance.put(instanceDetails.getInstanceId(), instanceDetails);
    }
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        Resource instanceResource = new AWSResource().withId(instance.getInstanceId()).withRegion(getAWSClient().region()).withResourceType(AWSResourceType.INSTANCE).withLaunchTime(instance.getLaunchTime());
        for (Tag tag : instance.getTags()) {
            instanceResource.setTag(tag.getKey(), tag.getValue());
        }
        String description = String.format("type=%s; host=%s", instance.getInstanceType(), instance.getPublicDnsName() == null ? "" : instance.getPublicDnsName());
        instanceResource.setDescription(description);
        instanceResource.setOwnerEmail(getOwnerEmailForResource(instanceResource));
        String asgName = getAsgName(instanceResource, idToASGInstance);
        if (asgName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_ASG_NAME, asgName);
            LOGGER.info(String.format("instance %s has a ASG tag name %s.", instanceResource.getId(), asgName));
        }
        String opsworksStackName = getOpsWorksStackName(instanceResource);
        if (opsworksStackName != null) {
            instanceResource.setAdditionalField(INSTANCE_FIELD_OPSWORKS_STACK_NAME, opsworksStackName);
            LOGGER.info(String.format("instance %s is part of an OpsWorks stack named %s.", instanceResource.getId(), opsworksStackName));
        }
        if (instance.getState() != null) {
            ((AWSResource) instanceResource).setAWSResourceState(instance.getState().getName());
        }
        resources.add(instanceResource);
    }
    return resources;
}
Also used : HashMap(java.util.HashMap) Instance(com.amazonaws.services.ec2.model.Instance) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) AutoScalingInstanceDetails(com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Tag(com.amazonaws.services.ec2.model.Tag) LinkedList(java.util.LinkedList)

Example 68 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class InstanceInVPC method checkInstancesInVPC.

private Set<String> checkInstancesInVPC(String region, Collection<String> instances) {
    Set<String> failedInstances = Sets.newHashSet();
    for (String instanceId : instances) {
        for (Instance awsInstance : getAWSInstances(region, instanceId)) {
            if (awsInstance.getVpcId() == null) {
                LOGGER.info(String.format("Instance %s is not in a virtual private cloud", instanceId));
                failedInstances.add(instanceId);
            }
        }
    }
    return failedInstances;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance)

Example 69 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class InstanceTooOld method getInstanceLaunchTimes.

/**
 * Gets the launch time (in milliseconds) for a list of instance ids of the same region. The default
 * implementation is using an AWS client. The method can be overridden in subclasses to get the instance
 * launch times differently.
 * @param region
 *      the region of the instances
 * @param instanceIds
 *      the instance ids, all instances should be in the same region.
 * @return
 *      the map from instance id to the launch time in milliseconds
 */
protected Map<String, Long> getInstanceLaunchTimes(String region, String... instanceIds) {
    Map<String, Long> result = Maps.newHashMap();
    if (instanceIds == null || instanceIds.length == 0) {
        return result;
    }
    AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        if (instance.getLaunchTime() != null) {
            result.put(instance.getInstanceId(), instance.getLaunchTime().getTime());
        } else {
            LOGGER.warn(String.format("No launch time found for instance %s", instance.getInstanceId()));
        }
    }
    return result;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AWSClient(com.netflix.simianarmy.client.aws.AWSClient)

Example 70 with Region

use of com.amazonaws.services.ec2.model.Region in project SimianArmy by Netflix.

the class TestInstanceInVPC method setUp.

@BeforeMethod
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    List<Instance> instanceList = Lists.newArrayList();
    Instance instance = new Instance().withInstanceId(VPC_INSTANCE_ID).withVpcId("12345");
    instanceList.add(instance);
    doReturn(instanceList).when(instanceInVPC).getAWSInstances(REGION, VPC_INSTANCE_ID);
    List<Instance> instanceList2 = Lists.newArrayList();
    Instance instance2 = new Instance().withInstanceId(INSTANCE_ID);
    instanceList2.add(instance2);
    doReturn(instanceList2).when(instanceInVPC).getAWSInstances(REGION, INSTANCE_ID);
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)24 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)22 Instance (com.amazonaws.services.ec2.model.Instance)20 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)15 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)14 AmazonServiceException (com.amazonaws.AmazonServiceException)11 Reservation (com.amazonaws.services.ec2.model.Reservation)9 Tag (com.amazonaws.services.ec2.model.Tag)8 IOException (java.io.IOException)8 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)7 List (java.util.List)7 Map (java.util.Map)7 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)6 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)6 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)6 Test (org.junit.Test)6 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)5 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)5