Example usage for com.amazonaws.services.ec2.model AvailabilityZone getZoneName

List of usage examples for com.amazonaws.services.ec2.model AvailabilityZone getZoneName

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model AvailabilityZone getZoneName.

Prototype


public String getZoneName() 

Source Link

Document

The name of the Availability Zone.

Usage

From source file:AwsSdkSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    System.out.println("===========================================");
    System.out.println("Welcome to the AWS Java SDK!");
    System.out.println("===========================================");

    init();/*from w w w .ja  va2  s  .  co m*/

    try {
        /*
         * The Amazon EC2 client allows you to easily launch and configure
         * computing capacity in AWS datacenters.
         *
         * In this sample, we use the EC2 client to list the availability zones
         * in a region, and then list the instances running in those zones.
         */
        DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
        List<AvailabilityZone> availabilityZones = availabilityZonesResult.getAvailabilityZones();
        System.out.println("You have access to " + availabilityZones.size() + " availability zones:");
        for (AvailabilityZone zone : availabilityZones) {
            System.out.println(" - " + zone.getZoneName() + " (" + zone.getRegionName() + ")");
        }

        DescribeInstancesResult describeInstancesResult = ec2.describeInstances();
        Set<Instance> instances = new HashSet<Instance>();
        for (Reservation reservation : describeInstancesResult.getReservations()) {
            instances.addAll(reservation.getInstances());
        }

        System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");

        /*
         * The Amazon S3 client allows you to manage and configure buckets
         * and to upload and download data.
         *
         * In this sample, we use the S3 client to list all the buckets in
         * your account, and then iterate over the object metadata for all
         * objects in one bucket to calculate the total object count and
         * space usage for that one bucket. Note that this sample only
         * retrieves the object's metadata and doesn't actually download the
         * object's content.
         *
         * In addition to the low-level Amazon S3 client in the SDK, there
         * is also a high-level TransferManager API that provides
         * asynchronous management of uploads and downloads with an easy to
         * use API:
         *   http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/transfer/TransferManager.html
         */
        List<Bucket> buckets = s3.listBuckets();
        System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s).");

        if (buckets.size() > 0) {
            Bucket bucket = buckets.get(0);

            long totalSize = 0;
            long totalItems = 0;
            /*
             * The S3Objects and S3Versions classes provide convenient APIs
             * for iterating over the contents of your buckets, without
             * having to manually deal with response pagination.
             */
            for (S3ObjectSummary objectSummary : S3Objects.inBucket(s3, bucket.getName())) {
                totalSize += objectSummary.getSize();
                totalItems++;
            }

            System.out.println("The bucket '" + bucket.getName() + "' contains " + totalItems + " objects "
                    + "with a total size of " + totalSize + " bytes.");
        }
    } catch (AmazonServiceException ase) {
        /*
         * AmazonServiceExceptions represent an error response from an AWS
         * services, i.e. your request made it to AWS, but the AWS service
         * either found it invalid or encountered an error trying to execute
         * it.
         */
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        /*
         * AmazonClientExceptions represent an error that occurred inside
         * the client on the local host, either while trying to send the
         * request to AWS or interpret the response. For example, if no
         * network connection is available, the client won't be able to
         * connect to AWS to execute a request and will throw an
         * AmazonClientException.
         */
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:virtualIT.java

License:Open Source License

private String getAvailabilityzones() {
    /*********************************************
     * /*ww  w  .  j  a v  a  2  s  . c  o  m*/
      *  #2 Describe Availability Zones.
      *  
      *********************************************/
    System.out.println("#2 Describe Availability Zones.");
    DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
    List<AvailabilityZone> availableZones = availabilityZonesResult.getAvailabilityZones();
    for (Iterator<AvailabilityZone> n = availableZones.iterator(); n.hasNext();) {
        AvailabilityZone item = n.next();
        this.availableZones.add(item.getZoneName());
        System.out.println("You have access to " + item.getZoneName() + " Availability Zones.");
    }

    return this.availableZones.get(0).toString();

}

From source file:aws.example.ec2.DescribeRegionsAndZones.java

License:Open Source License

public static void main(String[] args) {
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeRegionsResult regions_response = ec2.describeRegions();

    for (Region region : regions_response.getRegions()) {
        System.out.printf("Found region %s " + "with endpoint %s", region.getRegionName(),
                region.getEndpoint());//from   w ww  .  j  a  v a 2s.co  m
    }

    DescribeAvailabilityZonesResult zones_response = ec2.describeAvailabilityZones();

    for (AvailabilityZone zone : zones_response.getAvailabilityZones()) {
        System.out.printf("Found availability zone %s " + "with status %s " + "in region %s",
                zone.getZoneName(), zone.getState(), zone.getRegionName());
    }
}

From source file:c3.ops.priam.defaultimpl.PriamConfiguration.java

License:Apache License

/**
 * Get the fist 3 available zones in the region
 */// w  w  w. j  ava2s.  c  o m
public void setDefaultRACList(String region) {
    AmazonEC2 client = new AmazonEC2Client(provider.getAwsCredentialProvider());
    client.setEndpoint("ec2." + region + ".amazonaws.com");
    DescribeAvailabilityZonesResult res = client.describeAvailabilityZones();
    List<String> zone = Lists.newArrayList();
    for (AvailabilityZone reg : res.getAvailabilityZones()) {
        if (reg.getState().equals("available"))
            zone.add(reg.getZoneName());
        if (zone.size() == 3)
            break;
    }
    //        DEFAULT_AVAILABILITY_ZONES =  StringUtils.join(zone, ",");
    DEFAULT_AVAILABILITY_ZONES = ImmutableList.copyOf(zone);
}

From source file:com.liferay.amazontools.AMIBuilder.java

License:Open Source License

protected boolean isZoneAvailable(String zoneName) {
    DescribeAvailabilityZonesResult describeAvailabilityZonesResult = amazonEC2Client
            .describeAvailabilityZones();

    List<AvailabilityZone> availabilityZones = describeAvailabilityZonesResult.getAvailabilityZones();

    for (AvailabilityZone availabilityZone : availabilityZones) {
        if (zoneName.equals(availabilityZone.getZoneName())) {
            return true;
        }/*ww w  .j  a  va  2 s .  c  o m*/
    }

    return false;
}

From source file:com.netflix.dynomitemanager.defaultimpl.DynomitemanagerConfiguration.java

License:Apache License

/**
 * Get the fist 3 available zones in the region
 *///  w  w  w . j a  v  a2  s .  c o  m
public void setDefaultRACList(String region) {
    AmazonEC2 client = new AmazonEC2Client(provider.getAwsCredentialProvider());
    client.setEndpoint("ec2." + region + ".amazonaws.com");
    DescribeAvailabilityZonesResult res = client.describeAvailabilityZones();
    List<String> zone = Lists.newArrayList();
    for (AvailabilityZone reg : res.getAvailabilityZones()) {
        if (reg.getState().equals("available"))
            zone.add(reg.getZoneName());
        if (zone.size() == 3)
            break;
    }
    // DEFAULT_AVAILABILITY_ZONES = StringUtils.join(zone, ",");
    DEFAULT_AVAILABILITY_ZONES = ImmutableList.copyOf(zone);
}

From source file:com.netflix.spinnaker.clouddriver.aws.security.DefaultAWSAccountInfoLookup.java

License:Apache License

@Override
public List<AWSRegion> listRegions(Collection<String> regionNames) {
    Set<String> nameSet = new HashSet<>(regionNames);
    AmazonEC2 ec2 = amazonClientProvider.getAmazonEC2(credentialsProvider, AmazonClientProvider.DEFAULT_REGION);

    DescribeRegionsRequest request = new DescribeRegionsRequest();
    if (!nameSet.isEmpty()) {
        request.withRegionNames(regionNames);
    }/*from ww  w  .j a v a 2  s .c o m*/
    List<Region> regions = ec2.describeRegions(request).getRegions();
    if (regions.size() != nameSet.size()) {
        Set<String> missingSet = new HashSet<>(nameSet);
        for (Region region : regions) {
            missingSet.remove(region.getRegionName());
        }
        throw new IllegalArgumentException(
                "Unknown region" + (missingSet.size() > 1 ? "s: " : ": ") + missingSet);
    }
    List<AWSRegion> awsRegions = new ArrayList<>(regions.size());
    for (Region region : regions) {
        ec2.setEndpoint(region.getEndpoint());
        List<AvailabilityZone> azs = ec2.describeAvailabilityZones().getAvailabilityZones();
        List<String> availabilityZoneNames = new ArrayList<>(azs.size());
        for (AvailabilityZone az : azs) {
            availabilityZoneNames.add(az.getZoneName());
        }

        awsRegions.add(new AWSRegion(region.getRegionName(), availabilityZoneNames));
    }
    return awsRegions;
}

From source file:com.norbl.cbp.ppe.AmiDescription.java

License:Open Source License

/**
 *
 * @param ec2Client//from  w  w w .  ja  v a 2 s  .  co m
 * @param zoneName
 * @return the zone that matches zoneName or <tt>null</tt>, which
 *  indicates that the zone is unspecified and any zone may be used.
 */
public static AvailabilityZone getAvailabilityZone(AmazonEC2Client ec2Client, String zoneName) {
    DescribeAvailabilityZonesResult r = ec2Client.describeAvailabilityZones();
    for (AvailabilityZone z : r.getAvailabilityZones()) {
        if (z.getZoneName().equals(zoneName))
            return (z);
    }
    return (null);
}

From source file:de.fischer.thotti.ec2.clients.EC2Validator.java

License:Apache License

private boolean isAvailabilityZoneKnownAtAWS(RegionType region) throws AWSRemoteServiceException {
    if (getRegionContext().awsAvailabilityZones == null)
        retrieveAWSAvailabilityZonesInformation();

    /*/*from   w ww .jav a2  s.c om*/
     * default means, we don't have any preference on
     * a availability zone.
     */
    if ("default".equals(region.getAvailibleZone()))
        return true;

    for (AvailabilityZone zone : getRegionContext().awsAvailabilityZones) {
        if (zone.getZoneName().equals(region.getAvailibleZone()))
            return true;
    }

    return false;
}

From source file:de.fischer.thotti.ec2.clients.EC2Validator.java

License:Apache License

private void retrieveAWSAvailabilityZonesInformation() throws AWSRemoteServiceException {
    try {//from ww  w  .  j a v  a 2 s . c o  m
        DescribeAvailabilityZonesResult result = getClient().describeAvailabilityZones();
        getRegionContext().awsAvailabilityZones = result.getAvailabilityZones();
    } catch (AmazonServiceException ase) {
        handleAmazonServiceException(ase);
    }

    if (logger.isDebugEnabled())
        for (AvailabilityZone r : getRegionContext().awsAvailabilityZones) {
            logger.debug("Found in region '{}' availability zone '{}'.",
                    new Object[] { getRegionContext().regionName, r.getZoneName() });
        }

}