Search in sources :

Example 6 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project chassis by Kixeye.

the class ServerInstanceContextTest method testValidContext.

@Test
public void testValidContext() {
    final String region = Regions.US_WEST_2.getName();
    final String instanceId = RandomStringUtils.random(20, "abcdefghi");
    final String availabilityZone = region + RandomStringUtils.random(20, "abcdefghi");
    final String environment = RandomStringUtils.random(20, "abcdefghi");
    final String userData = new UserData(environment).toString();
    final String elbName = environment + "-Zookeeper";
    final String exhibitorUrl = "http://" + elbName + "-" + region + ".aws.amazon.com";
    final int exhibitorPort = 80;
    final String version = "1.0.0";
    Ec2MetadataClient ec2MetadataClient = EasyMock.createMock(Ec2MetadataClient.class);
    EasyMock.expect(ec2MetadataClient.getAvailabilityZone()).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return availabilityZone;
        }
    });
    EasyMock.expect(ec2MetadataClient.getInstanceId()).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return instanceId;
        }
    });
    EasyMock.expect(ec2MetadataClient.getUserData()).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return userData;
        }
    });
    EasyMock.expect(ec2MetadataClient.getPrivateIpAddress()).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return "127.0.0.1";
        }
    });
    EasyMock.expect(ec2MetadataClient.getPublicIpAddress()).andAnswer(new IAnswer<String>() {

        @Override
        public String answer() throws Throwable {
            return "127.0.0.1";
        }
    });
    AmazonEC2 amazonEC2 = EasyMock.createMock(AmazonEC2.class);
    EasyMock.expect(amazonEC2.describeRegions()).andAnswer(new IAnswer<DescribeRegionsResult>() {

        @Override
        public DescribeRegionsResult answer() throws Throwable {
            DescribeRegionsResult result = new DescribeRegionsResult();
            List<Region> regions = new ArrayList<>();
            for (Regions region : Regions.values()) {
                Region r = new Region();
                r.setRegionName(region.getName());
                regions.add(r);
            }
            result.setRegions(regions);
            return result;
        }
    });
    EasyMock.expect(amazonEC2.describeTags(EasyMock.anyObject(DescribeTagsRequest.class))).andAnswer(new IAnswer<DescribeTagsResult>() {

        @Override
        public DescribeTagsResult answer() throws Throwable {
            return new DescribeTagsResult();
        }
    });
    com.amazonaws.regions.Region r = com.amazonaws.regions.Region.getRegion(Regions.fromName(region));
    amazonEC2.setRegion(eqRegion(r));
    EasyMock.expectLastCall();
    AmazonElasticLoadBalancing amazonElasticLoadBalancing = EasyMock.createMock(AmazonElasticLoadBalancing.class);
    EasyMock.expect(amazonElasticLoadBalancing.describeLoadBalancers()).andAnswer(new IAnswer<DescribeLoadBalancersResult>() {

        @Override
        public DescribeLoadBalancersResult answer() throws Throwable {
            DescribeLoadBalancersResult result = new DescribeLoadBalancersResult();
            List<LoadBalancerDescription> loadBalancers = new ArrayList<>();
            LoadBalancerDescription lb1 = new LoadBalancerDescription();
            lb1.setDNSName(exhibitorUrl);
            lb1.setLoadBalancerName(elbName);
            List<ListenerDescription> listenerDescriptions = new ArrayList<>();
            ListenerDescription httpListenerDescription = new ListenerDescription();
            Listener httpListener = new Listener();
            httpListener.setProtocol("HTTP");
            httpListener.setLoadBalancerPort(exhibitorPort);
            httpListener.setInstancePort(8080);
            httpListenerDescription.setListener(httpListener);
            ListenerDescription httpsListenerDescription = new ListenerDescription();
            Listener httpsListener = new Listener();
            httpsListener.setProtocol("HTTPS");
            httpsListener.setLoadBalancerPort(443);
            httpsListener.setInstancePort(8080);
            httpsListenerDescription.setListener(httpListener);
            listenerDescriptions.add(httpListenerDescription);
            listenerDescriptions.add(httpsListenerDescription);
            lb1.setListenerDescriptions(listenerDescriptions);
            loadBalancers.add(lb1);
            result.setLoadBalancerDescriptions(loadBalancers);
            return result;
        }
    });
    amazonElasticLoadBalancing.setRegion(eqRegion(r));
    EasyMock.expectLastCall();
    EasyMock.replay(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
    ServerInstanceContext context = new ServerInstanceContext(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
    Assert.assertEquals(region, context.getRegion());
    Assert.assertEquals(instanceId, context.getInstanceId());
    Assert.assertEquals(availabilityZone, context.getAvailabilityZone());
    Assert.assertEquals(environment, context.getEnvironment());
    Assert.assertEquals(exhibitorUrl, context.getExhibitorHost());
    Assert.assertEquals(exhibitorPort, context.getExhibitorPort());
    Assert.assertNull(context.getAppName());
    EasyMock.verify(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
    EasyMock.reset(amazonEC2, amazonElasticLoadBalancing);
    amazonEC2.shutdown();
    EasyMock.expectLastCall();
    amazonElasticLoadBalancing.shutdown();
    EasyMock.expectLastCall();
    String name = RandomStringUtils.random(20, "abcdefghi");
    EasyMock.replay(amazonEC2, amazonElasticLoadBalancing);
    context.setAppName(name);
    context.setVersion(version);
    Assert.assertEquals(name, context.getAppName());
    context.shutdown();
    EasyMock.verify(ec2MetadataClient, amazonEC2, amazonElasticLoadBalancing);
}
Also used : DescribeTagsRequest(com.amazonaws.services.ec2.model.DescribeTagsRequest) DescribeTagsResult(com.amazonaws.services.ec2.model.DescribeTagsResult) Listener(com.amazonaws.services.elasticloadbalancing.model.Listener) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) ListenerDescription(com.amazonaws.services.elasticloadbalancing.model.ListenerDescription) ArrayList(java.util.ArrayList) List(java.util.List) LoadBalancerDescription(com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription) Regions(com.amazonaws.regions.Regions) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult) DescribeRegionsResult(com.amazonaws.services.ec2.model.DescribeRegionsResult) Region(com.amazonaws.services.ec2.model.Region) AmazonElasticLoadBalancing(com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancing) Test(org.junit.Test)

Example 7 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project incubator-gobblin by apache.

the class AWSSdkClient method getAvailabilityZones.

/**
 * Get availability zones in an Amazon AWS region
 *
 * @return List of availability zones
 */
public List<AvailabilityZone> getAvailabilityZones() {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final DescribeAvailabilityZonesResult describeAvailabilityZonesResult = amazonEC2.describeAvailabilityZones();
    final List<AvailabilityZone> availabilityZones = describeAvailabilityZonesResult.getAvailabilityZones();
    LOGGER.info("Found: " + availabilityZones.size() + " availability zone");
    return availabilityZones;
}
Also used : AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

Example 8 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project incubator-gobblin by apache.

the class GobblinAWSClusterLauncher method setupGobblinCluster.

/**
 * Setup the Gobblin AWS cluster.
 *
 * @throws IOException If there's anything wrong setting up the AWS cluster
 */
@VisibleForTesting
String setupGobblinCluster() throws IOException {
    final String uuid = UUID.randomUUID().toString();
    // Create security group
    // TODO: Make security group restrictive
    final String securityGroupName = "GobblinSecurityGroup_" + uuid;
    this.awsSdkClient.createSecurityGroup(securityGroupName, "Gobblin cluster security group");
    this.awsSdkClient.addPermissionsToSecurityGroup(securityGroupName, "0.0.0.0/0", "tcp", 0, 65535);
    // Create key value pair
    final String keyName = "GobblinKey_" + uuid;
    final String material = this.awsSdkClient.createKeyValuePair(keyName);
    LOGGER.debug("Material is: " + material);
    FileUtils.writeStringToFile(new File(keyName + ".pem"), material);
    // Get all availability zones in the region. Currently, we will only use first
    final List<AvailabilityZone> availabilityZones = this.awsSdkClient.getAvailabilityZones();
    // Launch Cluster Master
    final String clusterId = launchClusterMaster(uuid, keyName, securityGroupName, availabilityZones.get(0));
    // Launch WorkUnit runners
    launchWorkUnitRunners(uuid, keyName, securityGroupName, availabilityZones.get(0));
    return clusterId;
}
Also used : AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project GNS by MobilityFirst.

the class AWSEC2 method getAvailabilityZones.

/**
   * Returns a list of strings of all the availability zones in the current region.
   *
   * @param ec2
   * @return a list of zone strings
   */
public static List<String> getAvailabilityZones(AmazonEC2 ec2) {
    ArrayList<String> result = new ArrayList<>();
    DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
    for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) {
        result.add(zone.getZoneName());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

Example 10 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project GNS by MobilityFirst.

the class AWSStatusCheck method main.

/**
   *
   * @param args
   * @throws Exception
   */
public static void main(String[] args) throws Exception {
    init();
    /*
     * Amazon EC2
     */
    for (String endpoint : endpoints) {
        try {
            ec2.setEndpoint(endpoint);
            System.out.println("**** Endpoint: " + endpoint);
            DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
            System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size() + " Availability Zones.");
            for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) {
                System.out.println(zone.getZoneName());
            }
            DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
            List<Reservation> reservations = describeInstancesRequest.getReservations();
            Set<Instance> instances = new HashSet<Instance>();
            System.out.println("Instances: ");
            for (Reservation reservation : reservations) {
                for (Instance instance : reservation.getInstances()) {
                    instances.add(instance);
                    System.out.println(instance.getPublicDnsName() + " is " + instance.getState().getName());
                }
            }
            System.out.println("Security groups: ");
            DescribeSecurityGroupsResult describeSecurityGroupsResult = ec2.describeSecurityGroups();
            for (SecurityGroup securityGroup : describeSecurityGroupsResult.getSecurityGroups()) {
                System.out.println(securityGroup.getGroupName());
            }
        //System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
        } catch (AmazonServiceException ase) {
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }
        /*
       * Amazon SimpleDB
       *
       */
        try {
            ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100);
            ListDomainsResult sdbResult = sdb.listDomains(sdbRequest);
            int totalItems = 0;
            for (String domainName : sdbResult.getDomainNames()) {
                DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName);
                DomainMetadataResult domainMetadata = sdb.domainMetadata(metadataRequest);
                totalItems += domainMetadata.getItemCount();
            }
            System.out.println("You have " + sdbResult.getDomainNames().size() + " Amazon SimpleDB domain(s)" + "containing a total of " + totalItems + " items.");
        } catch (AmazonServiceException ase) {
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }
        /*
       * Amazon S3
       *.
       */
        try {
            List<Bucket> buckets = s3.listBuckets();
            long totalSize = 0;
            int totalItems = 0;
            for (Bucket bucket : buckets) {
                /*
           * In order to save bandwidth, an S3 object listing does not
           * contain every object in the bucket; after a certain point the
           * S3ObjectListing is truncated, and further pages must be
           * obtained with the AmazonS3Client.listNextBatchOfObjects()
           * method.
           */
                ObjectListing objects = s3.listObjects(bucket.getName());
                do {
                    for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                        totalSize += objectSummary.getSize();
                        totalItems++;
                    }
                    objects = s3.listNextBatchOfObjects(objects);
                } while (objects.isTruncated());
            }
            System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s), " + "containing " + 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());
        }
    }
}
Also used : ListDomainsResult(com.amazonaws.services.simpledb.model.ListDomainsResult) Instance(com.amazonaws.services.ec2.model.Instance) DomainMetadataResult(com.amazonaws.services.simpledb.model.DomainMetadataResult) AmazonClientException(com.amazonaws.AmazonClientException) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DomainMetadataRequest(com.amazonaws.services.simpledb.model.DomainMetadataRequest) Reservation(com.amazonaws.services.ec2.model.Reservation) Bucket(com.amazonaws.services.s3.model.Bucket) ListDomainsRequest(com.amazonaws.services.simpledb.model.ListDomainsRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) HashSet(java.util.HashSet)

Aggregations

AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)10 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)5 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)4 HashMap (java.util.HashMap)4 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)3 DescribeRegionsResult (com.amazonaws.services.ec2.model.DescribeRegionsResult)3 SpotPrice (com.amazonaws.services.ec2.model.SpotPrice)3 Subnet (com.amazonaws.services.ec2.model.Subnet)3 List (java.util.List)3 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)2 DescribeAvailabilityZonesRequest (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesRequest)2 DescribeSpotPriceHistoryResult (com.amazonaws.services.ec2.model.DescribeSpotPriceHistoryResult)2 Region (com.amazonaws.services.ec2.model.Region)2 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)2 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)2 Region (com.sequenceiq.cloudbreak.cloud.model.Region)2 Collections.singletonList (java.util.Collections.singletonList)2 Test (org.junit.Test)2