Example usage for com.amazonaws.regions Region getName

List of usage examples for com.amazonaws.regions Region getName

Introduction

In this page you can find the example usage for com.amazonaws.regions Region getName.

Prototype

public String getName() 

Source Link

Document

The unique system ID for this region; ex: "us-east-1".

Usage

From source file:io.macgyver.plugin.cloud.aws.scanner.GraphNodeGarbageCollector.java

License:Apache License

public GraphNodeGarbageCollector region(Region region) {
    return region(region.getName());
}

From source file:io.macgyver.plugin.cloud.aws.scanner.SecurityGroupScanner.java

License:Apache License

@Override
public void scan(Region region) {

    AmazonEC2Client client = getAWSServiceClient().createEC2Client(region.getName());

    DescribeSecurityGroupsResult result = client.describeSecurityGroups();

    long now = System.currentTimeMillis();
    GraphNodeGarbageCollector gc = newGarbageCollector().region(region).label("AwsSecurityGroup");
    result.getSecurityGroups().forEach(sg -> {

        ObjectNode g = convertAwsObject(sg, region);

        // non-VPC security groups don't have a VPC
        String vpcId = Strings.nullToEmpty(sg.getVpcId());
        String cypher = "merge (sg:AwsSecurityGroup {aws_account: {a}, aws_region: {r}, aws_vpcId: {vpcId}, aws_groupId: {groupId}}) set sg+={props}, sg.updateTs={now} return sg";

        JsonNode xx = getNeoRxClient().execCypher(cypher, "vpcId", vpcId, "groupId", sg.getGroupId(), "props",
                g, "now", now, "a", getAccountId(), "r", region.getName()).toBlocking().first();

        gc.updateEarliestTimestamp(xx);//from  ww  w  .j av a2  s  .c  o m
        if (!vpcId.isEmpty()) {
            cypher = "match (v:AwsVpc {aws_vpcId: {vpcId}}), (sg:AwsSecurityGroup {aws_groupId:{groupId}, aws_vpcId: {vpcId}}) merge (sg)-[:RESIDES_IN]->(v)";
            getNeoRxClient().execCypher(cypher, "vpcId", vpcId, "groupId", sg.getGroupId());
        }
    });

    gc.invoke();

}

From source file:io.macgyver.plugin.cloud.aws.scanner.VPCScanner.java

License:Apache License

@Override
public void scan(Region region) {

    AmazonEC2Client c = getAWSServiceClient().createEC2Client(region);

    DescribeVpcsResult result = c.describeVpcs();

    GraphNodeGarbageCollector gc = newGarbageCollector().region(region).label("AwsVpc");
    NeoRxClient neoRx = getNeoRxClient();
    Preconditions.checkNotNull(neoRx);//w  ww . j av  a  2  s  . c  o  m

    result.getVpcs().forEach(it -> {
        try {
            ObjectNode n = convertAwsObject(it, region);

            String cypher = "merge (x:AwsVpc {aws_arn:{aws_arn}}) set x+={props} set x.updateTs=timestamp() return x";

            String mapToSubnetCypher = "match (y:AwsSubnet {aws_vpcId:{aws_vpcId}}), "
                    + "(x:AwsVpc {aws_arn:{aws_arn}}) "
                    + "merge (x)-[r:CONTAINS]->(y) set r.updateTs=timestamp()";

            neoRx.execCypher(cypher, "aws_arn", n.path("aws_arn").asText(), "props", n)
                    .forEach(gc.MERGE_ACTION);
            neoRx.execCypher(mapToSubnetCypher, "aws_arn", n.path("aws_arn").asText(), "aws_vpcId",
                    n.path("aws_vpcId").asText());
        } catch (RuntimeException e) {
            logger.warn("problem scanning VPC", e);
        }
    });

    String mapAccountCypher = "match (x:AwsAccount {aws_account:{aws_account}}), (y:AwsVpc {aws_account:{aws_account}}) "
            + "merge (x)-[r:OWNS]->(y) set r.updateTs=timestamp()";
    String mapRegionCypher = "match (x:AwsVpc {aws_region:{aws_region}}), (y:AwsRegion {aws_regionName:{aws_region}, aws_account:{aws_account}}) "
            + "merge (x)-[r:RESIDES_IN]->(y) set r.updateTs=timestamp()";

    neoRx.execCypher(mapAccountCypher, "aws_account", getAccountId());
    neoRx.execCypher(mapRegionCypher, "aws_region", region.getName(), "aws_account", getAccountId());
    gc.invoke();
}

From source file:io.prestosql.plugin.hive.s3.PrestoS3FileSystem.java

License:Apache License

private AmazonS3 createAmazonS3Client(Configuration hadoopConfig, ClientConfiguration clientConfig) {
    Optional<EncryptionMaterialsProvider> encryptionMaterialsProvider = createEncryptionMaterialsProvider(
            hadoopConfig);/*  w w  w.j  av  a2  s  .c o  m*/
    AmazonS3Builder<? extends AmazonS3Builder, ? extends AmazonS3> clientBuilder;

    String signerType = hadoopConfig.get(S3_SIGNER_TYPE);
    if (signerType != null) {
        clientConfig.withSignerOverride(signerType);
    }

    if (encryptionMaterialsProvider.isPresent()) {
        clientBuilder = AmazonS3EncryptionClient.encryptionBuilder().withCredentials(credentialsProvider)
                .withEncryptionMaterials(encryptionMaterialsProvider.get())
                .withClientConfiguration(clientConfig).withMetricsCollector(METRIC_COLLECTOR);
    } else {
        clientBuilder = AmazonS3Client.builder().withCredentials(credentialsProvider)
                .withClientConfiguration(clientConfig).withMetricsCollector(METRIC_COLLECTOR);
    }

    boolean regionOrEndpointSet = false;

    // use local region when running inside of EC2
    if (pinS3ClientToCurrentRegion) {
        Region region = Regions.getCurrentRegion();
        if (region != null) {
            clientBuilder = clientBuilder.withRegion(region.getName());
            regionOrEndpointSet = true;
        }
    }

    String endpoint = hadoopConfig.get(S3_ENDPOINT);
    if (endpoint != null) {
        clientBuilder = clientBuilder.withEndpointConfiguration(new EndpointConfiguration(endpoint, null));
        regionOrEndpointSet = true;
    }

    if (isPathStyleAccess) {
        clientBuilder = clientBuilder.enablePathStyleAccess();
    }

    if (!regionOrEndpointSet) {
        clientBuilder = clientBuilder.withRegion(US_EAST_1);
        clientBuilder.setForceGlobalBucketAccessEnabled(true);
    }

    return clientBuilder.build();
}

From source file:net.smartcosmos.plugin.service.aws.queue.AwsQueueService.java

License:Apache License

@Override
public void create(String queueName) {
    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region region = assignRegion(sqs);

    try {//from   w w w. j av  a 2s.c  o m
        CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
        String assignedUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
        LOG.debug("Assigned URL for queue named {} in region {}: {}",
                new Object[] { queueName, region.getName(), assignedUrl });

        onlineFlag = true;
    } catch (AmazonClientException e) {
        if (e.getCause() != null && e.getCause().getClass() == UnknownHostException.class) {
            // Queue most definitely does not exist!
            LOG.error("AWS host is unreachable: {}", new Object[] { e.getCause().getMessage() });
        } else {
            throw e;
        }
    }
}

From source file:net.smartcosmos.plugin.service.aws.queue.AwsQueueService.java

License:Apache License

@Override
public boolean exists(String queueName) {
    boolean existsFlag = false;

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region region = assignRegion(sqs);

    try {/*from ww  w .ja va  2s . co  m*/
        sqs.getQueueUrl(queueName);
        existsFlag = true;
        onlineFlag = true;

        LOG.info("Queue named {} in region {} exists", new Object[] { queueName, region.getName() });
    } catch (QueueDoesNotExistException e) {
        // Queue most definitely does not exist!
        LOG.info("Queue named {} in region {} does not exist", new Object[] { queueName, region.getName() });
    } catch (AmazonClientException e) {
        if (e.getCause() != null && e.getCause().getClass() == UnknownHostException.class) {
            // Queue most definitely does not exist!
            LOG.error("AWS host is unreachable: {}", new Object[] { e.getCause().getMessage() });
        } else {
            throw e;
        }
    }

    return existsFlag;
}

From source file:net.smartcosmos.plugin.service.aws.queue.AwsQueueService.java

License:Apache License

@Override
public String send(final IQueueRequest queueRequest) {
    Preconditions.checkNotNull(queueRequest.getMessageBody(), "messageBody must not be null");
    Preconditions.checkNotNull(queueRequest.getQueueName(), "queueName must not be null");

    if (onlineFlag) {
        final Region region = assignRegion(sqsAsyncClient);

        String targetQueueName = fetchQueueName(queueRequest);

        String queueUrl = sqsAsyncClient.getQueueUrl(new GetQueueUrlRequest(targetQueueName)).getQueueUrl();
        LOG.debug("Queue URL for queue named {} in region {}: {}", queueRequest.getQueueName(),
                region.getName(), queueUrl);

        SendMessageRequest request = new SendMessageRequest(queueUrl, queueRequest.getMessageBody());

        if (null != queueRequest.getMessageAttributes()) {
            for (Map.Entry<String, String> entry : queueRequest.getMessageAttributes().entrySet()) {
                if (entry.getValue() != null && entry.getValue().length() > 0) {
                    if (entry.getKey().equals(Field.LAST_MODIFIED_TIMESTAMP_FIELD)
                            || entry.getKey().equals(Field.FILE_CONTENT_LENGTH)) {
                        request.addMessageAttributesEntry(entry.getKey(), new MessageAttributeValue()
                                .withDataType("Number").withStringValue(entry.getValue()));
                    } else {
                        request.addMessageAttributesEntry(entry.getKey(), new MessageAttributeValue()
                                .withDataType("String").withStringValue(entry.getValue()));
                    }/*from ww w .  j a  v a2  s  .  c om*/
                }
            }
        }

        if (queueRequest.getMoniker() != null && queueRequest.getMoniker().length() > 0) {
            request.addMessageAttributesEntry(Field.MONIKER_FIELD, new MessageAttributeValue()
                    .withDataType("String").withStringValue(queueRequest.getMoniker()));
        }

        final String enqueueUrn = "urn:uuid:" + UUID.randomUUID().toString();

        request.addMessageAttributesEntry(Field.QUEUE_URN_FIELD,
                new MessageAttributeValue().withDataType("String.URN").withStringValue(enqueueUrn));

        request.addMessageAttributesEntry(Field.ENTITY_REFERENCE_TYPE,
                new MessageAttributeValue().withDataType("String.EntityReferenceType")
                        .withStringValue(queueRequest.getEntityReferenceType().toString()));

        request.addMessageAttributesEntry(Field.REFERENCE_URN_FIELD, new MessageAttributeValue()
                .withDataType("String.ReferenceUrn").withStringValue(queueRequest.getReferenceUrn()));

        AwsQueueAsyncHandler asyncHandler = new AwsQueueAsyncHandler(queueRequest, region, enqueueUrn);
        sqsAsyncClient.sendMessageAsync(request, asyncHandler);
        return enqueueUrn;

    } else {
        throw new AmazonClientException("AWS Queue Service is not online");
    }
}

From source file:org.dspace.storage.bitstore.S3BitStoreService.java

License:BSD License

/**
 * Initialize the asset store/*from ww w  . j  a v  a  2s .co m*/
 * S3 Requires:
 *  - access key
 *  - secret key
 *  - bucket name
 */
public void init() throws IOException {
    if (StringUtils.isBlank(getAwsAccessKey()) || StringUtils.isBlank(getAwsSecretKey())) {
        log.warn("Empty S3 access or secret");
    }

    // init client
    AWSCredentials awsCredentials = new BasicAWSCredentials(getAwsAccessKey(), getAwsSecretKey());
    s3Service = new AmazonS3Client(awsCredentials);

    // bucket name
    if (StringUtils.isEmpty(bucketName)) {
        bucketName = "dspace-asset-" + ConfigurationManager.getProperty("dspace.hostname");
        log.warn("S3 BucketName is not configured, setting default: " + bucketName);
    }

    try {
        if (!s3Service.doesBucketExist(bucketName)) {
            s3Service.createBucket(bucketName);
            log.info("Creating new S3 Bucket: " + bucketName);
        }
    } catch (Exception e) {
        log.error(e);
        throw new IOException(e);
    }

    // region
    if (StringUtils.isNotBlank(awsRegionName)) {
        try {
            Regions regions = Regions.fromName(awsRegionName);
            Region region = Region.getRegion(regions);
            s3Service.setRegion(region);
            log.info("S3 Region set to: " + region.getName());
        } catch (IllegalArgumentException e) {
            log.warn("Invalid aws_region: " + awsRegionName);
        }
    }

    log.info("AWS S3 Assetstore ready to go! bucket:" + bucketName);
}

From source file:org.duracloud.s3storage.S3ProviderUtil.java

License:Apache License

private static AmazonS3 newS3Client(String accessKey, String secretKey, Region region) {
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    try {/*from w w w  .  j a v a 2  s  .  c  o  m*/
        String awsRegion = null;
        if (region != null) {
            awsRegion = region.getName();
        } else {
            awsRegion = System.getProperty(AWS_REGION.name());
        }
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(awsRegion)
                .build();
        return s3Client;
    } catch (AmazonServiceException e) {
        String err = "Could not create connection to Amazon S3 due " + "to error: " + e.getMessage();
        throw new StorageException(err, e, RETRY);
    }
}

From source file:org.lendingclub.mercator.aws.AWSScanner.java

License:Apache License

public ObjectNode convertAwsObject(Object x, Region region) {
    ObjectNode n = JsonConverter.newInstance(getAccountId(), region).toJson(x, null);
    Optional<String> arn = computeArn(n);
    if (arn.isPresent()) {
        n.put(AWS_ARN_ATTRIBUTE, arn.get());
    }/*from   w  w  w. ja  v a 2  s .co m*/
    if (region != null) {
        n.put(AWS_REGION_ATTRIBUTE, region.getName());
    }
    n.put(AWS_ACCOUNT_ATTRIBUTE, getAccountId());
    return n;

}