Example usage for com.amazonaws.services.sns AmazonSNS createTopic

List of usage examples for com.amazonaws.services.sns AmazonSNS createTopic

Introduction

In this page you can find the example usage for com.amazonaws.services.sns AmazonSNS createTopic.

Prototype

CreateTopicResult createTopic(String name);

Source Link

Document

Simplified method form for invoking the CreateTopic operation.

Usage

From source file:com.easarrive.aws.plugins.common.service.impl.SNSService.java

License:Open Source License

/**
 * {@inheritDoc}//from  ww w.  j  av  a 2  s .c o m
 */
@Override
public CreateTopicResult createTopic(AmazonSNS client, SNSTopicRequest topicRequest) {
    if (topicRequest == null) {
        return null;
    }
    // create a new SNS topic
    CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicRequest.getSnsName());
    CreateTopicResult result = client.createTopic(createTopicRequest);

    return result;
}

From source file:com.netflix.spinnaker.clouddriver.aws.lifecycle.InstanceTerminationLifecycleAgent.java

License:Apache License

private static String ensureTopicExists(AmazonSNS amazonSNS, ARN topicARN, List<String> allAccountIds,
        ARN queueARN) {/*from   w w  w.  j  a va2s  .  c o m*/
    topicARN.arn = amazonSNS.createTopic(topicARN.name).getTopicArn();

    amazonSNS.setTopicAttributes(new SetTopicAttributesRequest().withTopicArn(topicARN.arn)
            .withAttributeName("Policy").withAttributeValue(buildSNSPolicy(topicARN, allAccountIds).toJson()));

    amazonSNS.subscribe(topicARN.arn, "sqs", queueARN.arn);

    return topicARN.arn;
}

From source file:com.netflix.spinnaker.clouddriver.aws.lifecycle.LaunchFailureNotificationAgent.java

License:Apache License

/**
 * Ensure that the topic exists and has a policy granting all accounts permission to publish messages to it
 *//*from w  w  w. ja v  a 2  s.  co  m*/
private static String ensureTopicExists(AmazonSNS amazonSNS, ARN topicARN, List<String> allAccountIds,
        ARN queueARN) {
    topicARN.arn = amazonSNS.createTopic(topicARN.name).getTopicArn();

    amazonSNS.setTopicAttributes(new SetTopicAttributesRequest().withTopicArn(topicARN.arn)
            .withAttributeName("Policy").withAttributeValue(buildSNSPolicy(topicARN, allAccountIds).toJson()));

    amazonSNS.subscribe(topicARN.arn, "sqs", queueARN.arn);

    return topicARN.arn;
}

From source file:com.oneops.antenna.senders.aws.SNSService.java

License:Apache License

/**
 * Sends using the sns publisher/*from   w  w w. ja  v a 2 s .  co  m*/
 */
@Override
public boolean postMessage(NotificationMessage nMsg, BasicSubscriber subscriber) {

    SNSSubscriber sub;
    if (subscriber instanceof SNSSubscriber) {
        sub = (SNSSubscriber) subscriber;
    } else {
        throw new ClassCastException("invalid subscriber " + subscriber.getClass().getName());
    }

    SNSMessage msg = buildSNSMessage(nMsg);
    AmazonSNS sns = new AmazonSNSClient(new BasicAWSCredentials(sub.getAwsAccessKey(), sub.getAwsSecretKey()));
    if (sub.getSnsEndpoint() != null) {
        sns.setEndpoint(sub.getSnsEndpoint());
    }

    CreateTopicRequest tRequest = new CreateTopicRequest();
    tRequest.setName(msg.getTopicName());
    CreateTopicResult result = sns.createTopic(tRequest);

    PublishRequest pr = new PublishRequest(result.getTopicArn(), msg.getTxtMessage())
            .withSubject(msg.getSubject());

    try {
        PublishResult pubresult = sns.publish(pr);
        logger.info("Published msg with id - " + pubresult.getMessageId());
    } catch (AmazonClientException ace) {
        logger.error(ace.getMessage());
        return false;
    }
    return true;
}

From source file:io.konig.maven.CreateAwsSnsTopicAction.java

License:Apache License

public AwsDeployment from(String path) throws Exception {
    String cfTemplatePresent = System.getProperty("cfTemplatePresent");
    if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) {
        try {/*from w w  w.  ja v  a  2 s. co  m*/
            File file = deployment.file(path);
            ObjectMapper mapper = new ObjectMapper();
            S3Bucket bucket = mapper.readValue(file, S3Bucket.class);
            deployment.verifyAWSCredentials();
            String envtName = "";
            if (System.getProperty("environmentName") != null) {
                envtName = System.getProperty("environmentName");
            }
            String bucketName = StringUtils.replaceOnce(bucket.getBucketName(), "${environmentName}", envtName);
            TopicConfiguration notificationConfig = bucket.getNotificationConfiguration()
                    .getTopicConfiguration();
            if (notificationConfig != null && notificationConfig.getTopic() != null) {
                Topic topic = notificationConfig.getTopic();
                Regions regions = Regions.fromName(topic.getRegion());
                AmazonSNS sns = AmazonSNSClientBuilder.standard().withCredentials(deployment.getCredential())
                        .withRegion(regions).build();
                CreateTopicResult result = sns.createTopic(topic.getResourceName());
                deployment.setResponse("Topic with ARN : " + result.getTopicArn() + " is created");

                Policy policy = new Policy().withStatements(new Statement(Effect.Allow)
                        .withPrincipals(Principal.AllUsers).withActions(SNSActions.Publish)
                        .withResources(new Resource(result.getTopicArn()))
                        .withConditions(new ArnCondition(ArnComparisonType.ArnEquals,
                                ConditionFactory.SOURCE_ARN_CONDITION_KEY, "arn:aws:s3:*:*:" + bucketName)));

                sns.setTopicAttributes(
                        new SetTopicAttributesRequest(result.getTopicArn(), "Policy", policy.toJson()));
            } else {
                deployment.setResponse("No topic is configured to the S3 Bucket");
            }

        } catch (Exception e) {
            throw e;
        }
    } else {
        deployment.setResponse("Topic will be created through cloud formation template");
    }
    return deployment;
}

From source file:net.smartcosmos.plugin.service.aws.notification.AwsNotificationService.java

License:Apache License

@Override
public String createTopic(INotificationEndpoint notificationEndpoint) {
    Preconditions.checkArgument((notificationEndpoint != null), "Notification endpoint must not be null");

    Preconditions.checkArgument((notificationEndpoint.getTopicArn() == null),
            "Notification already has a notification URL defined");

    AmazonSNS sns = new AmazonSNSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sns.setRegion(usEast1);//from w  w  w. j  a  va 2 s.  c  o  m

    String topicArn = null;

    try {
        String topicName = stripUrnUuidPrefix(notificationEndpoint.getAccount());

        LOG.info("Topic Name Assigned: " + topicName);

        CreateTopicRequest request = new CreateTopicRequest(topicName);
        CreateTopicResult result = sns.createTopic(request);

        topicArn = result.getTopicArn();

        //
        // Event
        //
        INotificationResultObject<IAccount> nro = new NotificationResultObject<>(EntityReferenceType.Account,
                notificationEndpoint.getAccount(), result.getTopicArn());
        IEventService eventService = context.getServiceFactory()
                .getEventService(notificationEndpoint.getAccount());
        eventService.recordEvent(EventType.NotificationEnroll, notificationEndpoint.getAccount(), null, nro);

    } finally {
        sns.shutdown();
    }

    return topicArn;
}