Search in sources :

Example 1 with Region

use of software.amazon.awssdk.services.ec2.model.Region in project jackrabbit-oak by apache.

the class S3Backend method init.

public void init(CachingDataStore store, String homeDir, Properties prop) throws DataStoreException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        this.store = store;
        s3ReqDecorator = new S3RequestDecorator(prop);
        s3service = Utils.openService(prop);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = prop.getProperty(S3Constants.S3_BUCKET);
            // Alternately check if the 'container' property is set
            if (Strings.isNullOrEmpty(bucket)) {
                bucket = prop.getProperty(S3Constants.S3_CONTAINER);
            }
        }
        String region = prop.getProperty(S3Constants.S3_REGION);
        Region s3Region = null;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }
        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }
        int writeThreads = 10;
        String writeThreadsStr = prop.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, (ThreadPoolExecutor) Executors.newFixedThreadPool(writeThreads, new NamedThreadFactory("s3-transfer-manager-worker")));
        int asyncWritePoolSize = 10;
        String maxConnsStr = prop.getProperty(S3Constants.S3_MAX_CONNS);
        if (maxConnsStr != null) {
            asyncWritePoolSize = Integer.parseInt(maxConnsStr) - writeThreads;
        }
        asyncWriteExecuter = (ThreadPoolExecutor) Executors.newFixedThreadPool(asyncWritePoolSize, new NamedThreadFactory("s3-write-worker"));
        String renameKeyProp = prop.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.debug("  error ", e);
        Map<String, String> filteredMap = Maps.newHashMap();
        if (prop != null) {
            filteredMap = Maps.filterKeys(Maps.fromProperties(prop), new Predicate<String>() {

                @Override
                public boolean apply(String input) {
                    return !input.equals(S3Constants.ACCESS_KEY) && !input.equals(S3Constants.SECRET_KEY);
                }
            });
        }
        throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) NamedThreadFactory(org.apache.jackrabbit.core.data.util.NamedThreadFactory) AmazonClientException(com.amazonaws.AmazonClientException) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) S3RequestDecorator(org.apache.jackrabbit.oak.blob.cloud.s3.S3RequestDecorator) Region(com.amazonaws.services.s3.model.Region) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Map(java.util.Map)

Example 2 with Region

use of software.amazon.awssdk.services.ec2.model.Region in project jackrabbit-oak by apache.

the class S3Backend method init.

public void init() throws DataStoreException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        s3ReqDecorator = new S3RequestDecorator(properties);
        s3service = Utils.openService(properties);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = properties.getProperty(S3Constants.S3_BUCKET);
            // Alternately check if the 'container' property is set
            if (Strings.isNullOrEmpty(bucket)) {
                bucket = properties.getProperty(S3Constants.S3_CONTAINER);
            }
        }
        String region = properties.getProperty(S3Constants.S3_REGION);
        Region s3Region;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }
        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }
        int writeThreads = 10;
        String writeThreadsStr = properties.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, Executors.newFixedThreadPool(writeThreads, new NamedThreadFactory("s3-transfer-manager-worker")));
        String renameKeyProp = properties.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.error("Error ", e);
        Map<String, Object> filteredMap = Maps.newHashMap();
        if (properties != null) {
            filteredMap = Maps.filterKeys(Utils.asMap(properties), new Predicate<String>() {

                @Override
                public boolean apply(String input) {
                    return !input.equals(S3Constants.ACCESS_KEY) && !input.equals(S3Constants.SECRET_KEY);
                }
            });
        }
        throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) NamedThreadFactory(org.apache.jackrabbit.core.data.util.NamedThreadFactory) AmazonClientException(com.amazonaws.AmazonClientException) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) Region(com.amazonaws.services.s3.model.Region) Map(java.util.Map)

Example 3 with Region

use of software.amazon.awssdk.services.ec2.model.Region 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 4 with Region

use of software.amazon.awssdk.services.ec2.model.Region in project aws-doc-sdk-examples by awsdocs.

the class ReleaseAddress method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "   <allocId>\n\n" + "Where:\n" + "   allocId - an allocation ID value that you can obtain from the AWS Console. \n\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String allocId = args[0];
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    releaseEC2Address(ec2, allocId);
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Example 5 with Region

use of software.amazon.awssdk.services.ec2.model.Region in project aws-doc-sdk-examples by awsdocs.

the class TerminateInstance method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "   <instanceId>\n\n" + "Where:\n" + "   instanceId - an instance id value that you can obtain from the AWS Console. \n\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String instanceId = args[0];
    Region region = Region.US_EAST_1;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    terminateEC2(ec2, instanceId);
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Aggregations

Region (software.amazon.awssdk.regions.Region)20 Ec2Client (software.amazon.awssdk.services.ec2.Ec2Client)20 AmazonClientException (com.amazonaws.AmazonClientException)3 AmazonServiceException (com.amazonaws.AmazonServiceException)3 DescribeRegionsResult (com.amazonaws.services.ec2.model.DescribeRegionsResult)3 Region (com.amazonaws.services.ec2.model.Region)3 Region (com.amazonaws.services.s3.model.Region)3 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)3 IOException (java.io.IOException)3 Date (java.util.Date)3 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)3 NamedThreadFactory (org.apache.jackrabbit.core.data.util.NamedThreadFactory)3 Regions (com.amazonaws.regions.Regions)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 Map (java.util.Map)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)1 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)1 DescribeRegionsRequest (com.amazonaws.services.ec2.model.DescribeRegionsRequest)1