Search in sources :

Example 31 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project aws-doc-sdk-examples by awsdocs.

the class FindRunningInstances method findRunningEC2Instances.

// snippet-start:[ec2.java2.running_instances.main]
public static void findRunningEC2Instances(Ec2Client ec2) {
    try {
        String nextToken = null;
        do {
            Filter filter = Filter.builder().name("instance-state-name").values("running").build();
            DescribeInstancesRequest request = DescribeInstancesRequest.builder().filters(filter).build();
            DescribeInstancesResponse response = ec2.describeInstances(request);
            for (Reservation reservation : response.reservations()) {
                for (Instance instance : reservation.instances()) {
                    System.out.printf("Found Reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.instanceId(), instance.imageId(), instance.instanceType(), instance.state().name(), instance.monitoring().state());
                    System.out.println("");
                }
            }
            nextToken = response.nextToken();
        } while (nextToken != null);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Reservation(software.amazon.awssdk.services.ec2.model.Reservation) Filter(software.amazon.awssdk.services.ec2.model.Filter) Instance(software.amazon.awssdk.services.ec2.model.Instance) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DescribeInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)

Example 32 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project aws-doc-sdk-examples by awsdocs.

the class DescribeInstances method main.

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    boolean done = false;
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    while (!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);
        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                System.out.printf("Found instance with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState());
            }
        }
        request.setNextToken(response.getNextToken());
        if (response.getNextToken() == null) {
            done = true;
        }
    }
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 33 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project aws-doc-sdk-examples by awsdocs.

the class FindRunningInstances method main.

public static void main(String[] args) {
    // snippet-start:[ec2.java1.running_instances.main]
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    try {
        // Create the Filter to use to find running instances
        Filter filter = new Filter("instance-state-name");
        filter.withValues("running");
        // Create a DescribeInstancesRequest
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        request.withFilters(filter);
        // Find the running instances
        DescribeInstancesResult response = ec2.describeInstances(request);
        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                // Print out the results
                System.out.printf("Found reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState());
            }
        }
        System.out.print("Done");
    } catch (SdkClientException e) {
        e.getStackTrace();
    }
// snippet-end:[ec2.java1.running_instances.main]
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) SdkClientException(com.amazonaws.SdkClientException) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 34 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project SimianArmy by Netflix.

the class InstanceInSecurityGroup method getInstanceSecurityGroups.

/**
 * Gets the security groups 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 security groups 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 list of security group names the instance has
 */
protected Map<String, List<String>> getInstanceSecurityGroups(String region, String... instanceIds) {
    Map<String, List<String>> result = Maps.newHashMap();
    if (instanceIds == null || instanceIds.length == 0) {
        return result;
    }
    AWSClient awsClient = new AWSClient(region, awsCredentialsProvider);
    for (Instance instance : awsClient.describeInstances(instanceIds)) {
        // Ignore instances that are in VPC
        if (StringUtils.isNotEmpty(instance.getVpcId())) {
            LOGGER.info(String.format("Instance %s is in VPC and is ignored.", instance.getInstanceId()));
            continue;
        }
        if (!"running".equals(instance.getState().getName())) {
            LOGGER.info(String.format("Instance %s is not running, state is %s.", instance.getInstanceId(), instance.getState().getName()));
            continue;
        }
        List<String> sgs = Lists.newArrayList();
        for (GroupIdentifier groupId : instance.getSecurityGroups()) {
            sgs.add(groupId.getGroupName());
        }
        result.put(instance.getInstanceId(), sgs);
    }
    return result;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) List(java.util.List) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier)

Example 35 with Instance

use of com.google.api.services.notebooks.v1.model.Instance in project SimianArmy by Netflix.

the class AWSClient method describeInstances.

/**
 * Describe a set of specific instances.
 *
 * @param instanceIds the instance ids
 * @return the instances
 */
public List<Instance> describeInstances(String... instanceIds) {
    if (instanceIds == null || instanceIds.length == 0) {
        LOGGER.info(String.format("Getting all EC2 instances in region %s.", region));
    } else {
        LOGGER.info(String.format("Getting EC2 instances for %d ids in region %s.", instanceIds.length, region));
    }
    List<Instance> instances = new LinkedList<Instance>();
    AmazonEC2 ec2Client = ec2Client();
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    if (instanceIds != null) {
        request.withInstanceIds(Arrays.asList(instanceIds));
    }
    DescribeInstancesResult result = ec2Client.describeInstances(request);
    for (Reservation reservation : result.getReservations()) {
        instances.addAll(reservation.getInstances());
    }
    LOGGER.info(String.format("Got %d EC2 instances in region %s.", instances.size(), region));
    return instances;
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)185 Reservation (com.amazonaws.services.ec2.model.Reservation)84 ArrayList (java.util.ArrayList)82 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)71 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)48 List (java.util.List)48 Tag (com.amazonaws.services.ec2.model.Tag)41 Test (org.junit.jupiter.api.Test)38 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)36 Map (java.util.Map)36 Collectors (java.util.stream.Collectors)32 HashMap (java.util.HashMap)26 Filter (com.amazonaws.services.ec2.model.Filter)25 Set (java.util.Set)23 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)22 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)20 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)18 InstanceState (com.amazonaws.services.ec2.model.InstanceState)18 Volume (com.amazonaws.services.ec2.model.Volume)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)18