Java Code Examples for com.amazonaws.regions.Region#getRegion()

The following examples show how to use com.amazonaws.regions.Region#getRegion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private static AwsInstanceCloudConnector createConnector() {
    AWSCredentialsProvider baseCredentials = new ProfileCredentialsProvider("default");
    AWSSecurityTokenServiceAsync stsClient = new AmazonStsAsyncProvider(CONFIGURATION, baseCredentials).get();
    AWSCredentialsProvider credentialsProvider = new DataPlaneControllerCredentialsProvider(CONFIGURATION, stsClient, baseCredentials).get();

    Region currentRegion = Regions.getCurrentRegion();
    if (currentRegion == null) {
        currentRegion = Region.getRegion(Regions.US_EAST_1);
    }
    return new AwsInstanceCloudConnector(
            CONFIGURATION,
            AmazonEC2AsyncClientBuilder.standard()
                    .withRegion(currentRegion.getName())
                    .withCredentials(credentialsProvider)
                    .build(),
            AmazonAutoScalingAsyncClientBuilder.standard()
                    .withRegion(currentRegion.getName())
                    .withCredentials(credentialsProvider)
                    .build()
    );
}
 
Example 2
Source File: CloudFormationManagerTest.java    From AWS-MIMIC-IIItoOMOP with Apache License 2.0 6 votes vote down vote up
/**
 * Test of terminateCluster method, of class EMRManager.
 */
@Test
public void testTerminateStack() throws IOException {
    AmazonCloudFormationClient client = new AmazonCloudFormationClient();
    String name = "JUnitStack" + UUID.randomUUID().toString();
    CreateStackRequest createStackRequest = new CreateStackRequest();
    CloudFormationManager manager = new CloudFormationManager(Region.getRegion(Regions.US_WEST_2));
    
    client.setRegion(Region.getRegion(Regions.US_WEST_2));
    
    createStackRequest.setStackName(name);
    createStackRequest.setTemplateBody(IOUtils.toString(getClass().getResourceAsStream("cloudformation.template"), "UTF-8"));
    
    client.createStack(createStackRequest);
    
    manager.terminateStack(name);
    
    for(StackSummary stack : client.listStacks().getStackSummaries())
    {
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_COMPLETE")) continue;
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_FAILED")) continue;
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_IN_PROGRESS")) continue;
        
        if(stack.getStackName().equals(name)) fail(name +  " should have been deleted but status is: " + stack.getStackStatus());
    }
}
 
Example 3
Source File: KMSProviderBuilderMockTests.java    From aws-encryption-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testBareAliasMapping_withLegacyCtor() {
    MockKMSClient client = spy(new MockKMSClient());

    RegionalClientSupplier supplier = mock(RegionalClientSupplier.class);
    when(supplier.getClient(any())).thenReturn(client);

    String key1 = client.createKey().getKeyMetadata().getKeyId();
    client.createAlias(new CreateAliasRequest()
        .withAliasName("foo")
        .withTargetKeyId(key1)
    );

    KmsMasterKeyProvider mkp0 = new KmsMasterKeyProvider(
            client, Region.getRegion(Regions.DEFAULT_REGION), Arrays.asList("alias/foo")
    );

    new AwsCrypto().encryptData(mkp0, new byte[0]);
}
 
Example 4
Source File: Ec2MetadataRegionProviderTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void getRegion_availabilityZoneWithMatchingRegion_returnsRegion() throws Exception {
	// Arrange
	Ec2MetadataRegionProvider regionProvider = new Ec2MetadataRegionProvider() {

		@Override
		protected Region getCurrentRegion() {
			return Region.getRegion(Regions.EU_WEST_1);
		}
	};

	// Act
	Region region = regionProvider.getRegion();

	// Assert
	assertThat(region).isEqualTo(Region.getRegion(Regions.EU_WEST_1));
}
 
Example 5
Source File: MCAWS.java    From aws-big-data-blog with Apache License 2.0 5 votes vote down vote up
public static void listBucketItems(String bucketName) {
System.out.println( "Connecting to AWS" );
System.out.println( "Listing files in bucket "+ bucketName );
AmazonS3 s3 = new AmazonS3Client();
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
System.out.println("Listing buckets");
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
	.withBucketName(bucketName));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
	System.out.println(" - " + objectSummary.getKey() + "  " +
	"(size = " + objectSummary.getSize() + ")");
	}
System.out.println();
}
 
Example 6
Source File: MCAWS.java    From aws-big-data-blog with Apache License 2.0 5 votes vote down vote up
public static void putMirthS3(String bucketName, String key, String fileLocation, String fileContents) {
AmazonS3 s3 = new AmazonS3Client();
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
try {
s3.putObject(new PutObjectRequest(bucketName, key, createTmpFile(fileContents)));
} catch (Exception e) { System.out.println("ERROR ON TEXT FILE"); }
   }
 
Example 7
Source File: AWSClientFactory.java    From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 5 votes vote down vote up
public AWSClients getAwsClient(
        final String awsAccessKey,
        final String awsSecretKey,
        final String proxyHost,
        final int proxyPort,
        final String region,
        final String pluginUserAgentPrefix) {

    final Region awsRegion = Region.getRegion(Regions.fromName(region));
    final AWSClients aws;

    if (StringUtils.isEmpty(awsAccessKey) && StringUtils.isEmpty(awsSecretKey)) {
        aws = AWSClients.fromDefaultCredentialChain(
                awsRegion,
                proxyHost,
                proxyPort,
                pluginUserAgentPrefix);
    }
    else {
        aws = AWSClients.fromBasicCredentials(
                awsRegion,
                awsAccessKey,
                awsSecretKey,
                proxyHost,
                proxyPort,
                pluginUserAgentPrefix);
    }

    return aws;
}
 
Example 8
Source File: S3ClientProvider.java    From micro-server with Apache License 2.0 5 votes vote down vote up
@Bean
public AmazonS3Client getClient() {

    AWSCredentials credentials = getAwsCredentials();

    AmazonS3Client amazonS3Client = new AmazonS3Client(credentials, getClientConfiguration());

    if (s3Configuration.getRegion() != null) {
        Region region = Region.getRegion(Regions.fromName(s3Configuration.getRegion()));
        amazonS3Client.setRegion(region);
    }

    return amazonS3Client;
}
 
Example 9
Source File: ECSCloud.java    From amazon-ecs-plugin with MIT License 5 votes vote down vote up
public static Region getRegion(String regionName) {
    if (StringUtils.isNotEmpty(regionName)) {
        return RegionUtils.getRegion(regionName);
    } else {
        return Region.getRegion(Regions.US_EAST_1);
    }
}
 
Example 10
Source File: S3FeaturesDemoTest.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
public void init() throws Exception {
  /*
   * Create your credentials file at ~/.aws/credentials
   * (C:\Users\USER_NAME\.aws\credentials for Windows users) and save the
   * following lines after replacing the underlined values with your own.
   * 
   * [default] 
   * aws_access_key_id=YOUR_ACCESS_KEY_ID 
   * aws_secret_access_key=YOUR_SECRET_ACCESS_KEY
   */
  s3Client = new AmazonS3Client();
  Region region = Region.getRegion(Regions.US_WEST_2);
  //Region region = Region.getRegion(Regions.SA_EAST_1);
  s3Client.setRegion(region);
}
 
Example 11
Source File: AWSClientsTest.java    From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 5 votes vote down vote up
@Test
public void createsS3ClientUsingProxyHostAndPort() {
    // when
    final AWSClients awsClients = new AWSClients(Region.getRegion(Regions.US_WEST_2), mock(AWSCredentials.class), PROXY_HOST, PROXY_PORT, PLUGIN_VERSION, codePipelineClientFactory, s3ClientFactory);
    final AmazonS3 s3Client = awsClients.getS3Client(mock(AWSCredentialsProvider.class));

    // then
    assertEquals(expectedS3Client, s3Client);
    final ArgumentCaptor<ClientConfiguration> clientConfigurationCaptor = ArgumentCaptor.forClass(ClientConfiguration.class);
    verify(s3ClientFactory).getS3Client(any(AWSCredentialsProvider.class), clientConfigurationCaptor.capture());
    final ClientConfiguration clientConfiguration = clientConfigurationCaptor.getValue();
    assertEquals(PROXY_HOST, clientConfiguration.getProxyHost());
    assertEquals(PROXY_PORT, clientConfiguration.getProxyPort());
    verify(s3Client).setRegion(Region.getRegion(Regions.US_WEST_2));
}
 
Example 12
Source File: InfrastructureConfiguration.java    From chaos-lemur with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty("aws.accessKeyId")
AmazonEC2Client amazonEC2(@Value("${aws.accessKeyId}") String accessKeyId,
                          @Value("${aws.secretAccessKey}") String secretAccessKey,
                          @Value("${aws.region:us-east-1}") String regionName) {

    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(new BasicAWSCredentials(accessKeyId, secretAccessKey));
    Region region = Region.getRegion(Regions.fromName(regionName));
    amazonEC2Client.setEndpoint(region.getServiceEndpoint("ec2"));

    return amazonEC2Client;
}
 
Example 13
Source File: SNSQueueManagerImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 * Get the region
 */
private Region getRegion() {
    String regionName = fig.getPrimaryRegion();
    try {
        Regions regions = Regions.fromName(regionName);
        return Region.getRegion(regions);
    }
    catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("INVALID PRIMARY REGION FROM CONFIGURATION " + LegacyQueueFig.USERGRID_QUEUE_REGION_LOCAL + ": " + regionName, e);
    }
}
 
Example 14
Source File: JobManager.java    From soundwave with Apache License 2.0 5 votes vote down vote up
public JobManager(JobInfoStore jobInfoStore, CloudInstanceStore cloudInstanceStore,
                  CmdbInstanceStore cmdbInstanceStore) {
  ;
  this.jobInfoStore = jobInfoStore;
  this.ec2InstanceStore = cloudInstanceStore;
  this.cmdbInstanceStore = cmdbInstanceStore;
  this.awsRegion = Region.getRegion(
      Regions.fromName(Configuration.getProperties().getString("aws_region", "us-east-1")));

  int numOfSubscribers = Configuration.getProperties().getInt("num_subscriber", 1);
  logger.info("JobManager starts with {} subscribers", numOfSubscribers);
  for (int i = 0; i < numOfSubscribers; i++) {
    Ec2InstanceUpdateHandler
        handler =
        new Ec2InstanceUpdateHandler(cmdbInstanceStore, cloudInstanceStore,
            new EsDailySnapshotStoreFactory());

    SqsClient client = new SqsClient(handler);
    SqsTriggeredJobExecutor
        executor =
        new SqsTriggeredJobExecutor(Configuration.getProperties().getString("update_queue"),
            15, msg -> client.processMessage(msg));
    executor.setMinimumReprocessingDuration(30);
    executors.add(executor);
    this.onEc2HandlerCreate(handler);
  }

  //Add recurring jobs
  addRecurringJobs();
}
 
Example 15
Source File: S3BroadcastManager.java    From kickflip-android-sdk with Apache License 2.0 4 votes vote down vote up
public void setRegion(String regionStr) {
    if (regionStr == null || regionStr.equals("")) return;
    Region region = Region.getRegion(Regions.fromName(regionStr));
    mTransferManager.getAmazonS3Client().setRegion(region);
}
 
Example 16
Source File: KouplerMetrics.java    From koupler with MIT License 4 votes vote down vote up
public KouplerMetrics(KinesisEventProducer producer, KinesisProducerConfiguration config, String appName) {
    this(producer, appName);
    cloudWatch = new AmazonCloudWatchClient();
    Region region = Region.getRegion(Regions.fromName(config.getRegion()));
    cloudWatch.setRegion(region);
}
 
Example 17
Source File: ProducerBuilder.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
public ProducerBuilder withRegion(String regionName) {
	Region region = Region.getRegion(Regions.fromName(regionName));
	this.region = region;
	return this;
}
 
Example 18
Source File: DefaultCloudWatchFactory.java    From chassis with Apache License 2.0 4 votes vote down vote up
private Region getCloudWatchRegion(String region) {
    if ("default".equals(region)) {
        return Region.getRegion(Regions.DEFAULT_REGION);
    }
    return Region.getRegion(Regions.fromName(region));
}
 
Example 19
Source File: S3Client.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
public S3Client(){
  region = Region.getRegion(Regions.US_WEST_1);
}
 
Example 20
Source File: KmsMasterKeyProvider.java    From aws-encryption-sdk-java with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an instance of this object with default settings and configured to talk to the
 * {@link Regions#DEFAULT_REGION}.
 *
 * @deprecated The default region set by this constructor is subject to change. Use the builder method to construct
 * instances of this class for better control.
 */
@Deprecated
public KmsMasterKeyProvider(final AWSCredentialsProvider creds) {
    this(creds, Region.getRegion(Regions.DEFAULT_REGION), new ClientConfiguration(), Collections
            .<String> emptyList());
}