Skip to content

Commit

Permalink
Added SNS publishing capability
Browse files Browse the repository at this point in the history
  • Loading branch information
sdavids13 committed Apr 24, 2016
1 parent cce79ff commit 3971b90
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Current Features / Supported AWS Products
* Send messages
* Delete messages
* Read messages
* SNS
* Publish message

Requirements
------------
Expand Down Expand Up @@ -319,6 +321,26 @@ task viewMessages(type: AmazonSQSMessageConsumerTask) {

Look [SQS example](samples/09-sqs) for more information.

### SNS tasks
```
apply plugin: "jp.classmethod.aws.sns"
task publishMessage(type: AmazonSNSPublishMessageTask) {
topicArn 'arn:aws:sns:us-east-1:000000000000:gradle-aws-plugin-sns-topic'
message 'Test body'
subject 'Optional test subject'
}
task publishJsonMessage(type: AmazonSNSPublishMessageTask) {
topicArn 'arn:aws:sns:us-east-1:000000000000:gradle-aws-plugin-sns-topic'
message JsonOutput.toJson(['default': 'Default message body.',
'email' : 'Email message body.',
'sms': 'SMS message body.'])
messageStructure 'json'
}
```
Look [SNS example](samples/10-sns) for more information.

License
-------
Copyright (C) 2013-2015 [Classmethod, Inc.](http://classmethod.jp/)
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies {
compile "com.amazonaws:aws-java-sdk-lambda:$awsJavaSdkVersion"
compile "com.amazonaws:aws-java-sdk-iam:$awsJavaSdkVersion"
compile "com.amazonaws:aws-java-sdk-sqs:$awsJavaSdkVersion"
compile "com.amazonaws:aws-java-sdk-sns:$awsJavaSdkVersion"

compile "jp.xet.spar-wings:spar-wings-awscli-config:$sparWingsVersion"

Expand Down
32 changes: 32 additions & 0 deletions samples/10-sns/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// -*- coding: utf-8; mode: groovy -*-

import groovy.json.JsonOutput
import jp.classmethod.aws.gradle.sns.AmazonSNSPublishMessageTask;

buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }

}
dependencies {
classpath "jp.classmethod.aws:gradle-aws-plugin:0.+"
}
}

apply plugin: "jp.classmethod.aws.sns"

task publishMessage(type: AmazonSNSPublishMessageTask) {
topicArn 'arn:aws:sns:us-east-1:000000000000:gradle-aws-plugin-sns-topic'
message 'Test body'
subject 'Optional test subject'
}

task publishJsonMessage(type: AmazonSNSPublishMessageTask) {
topicArn 'arn:aws:sns:us-east-1:000000000000:gradle-aws-plugin-sns-topic'
message JsonOutput.toJson(['default': 'Default message body.',
'email' : 'Email message body.',
'sms': 'SMS message body.'])
messageStructure 'json'
}
28 changes: 28 additions & 0 deletions src/main/java/jp/classmethod/aws/gradle/sns/AmazonSNSPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2013-2016 Classmethod, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.classmethod.aws.gradle.sns;

import jp.classmethod.aws.gradle.AwsPlugin;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class AmazonSNSPlugin implements Plugin<Project> {

public void apply(Project project) {
project.getPluginManager().apply(AwsPlugin.class);
project.getExtensions().create(AmazonSNSPluginExtension.NAME, AmazonSNSPluginExtension.class, project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2013-2016 Classmethod, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.classmethod.aws.gradle.sns;

import com.amazonaws.services.sns.AmazonSNSClient;
import jp.classmethod.aws.gradle.common.BaseRegionAwarePluginExtension;
import org.gradle.api.Project;

public class AmazonSNSPluginExtension extends BaseRegionAwarePluginExtension<AmazonSNSClient> {

public static final String NAME = "sns";

public AmazonSNSPluginExtension(Project project) {
super(project, AmazonSNSClient.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2013-2016 Classmethod, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.classmethod.aws.gradle.sns;

import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.model.PublishRequest;
import lombok.Getter;
import lombok.Setter;
import org.gradle.api.GradleException;
import org.gradle.api.internal.ConventionTask;
import org.gradle.api.tasks.TaskAction;

public class AmazonSNSPublishMessageTask extends ConventionTask {

@Getter @Setter
private String topicArn;

@Getter @Setter
private String message;

@Getter @Setter
private String subject;

@Getter @Setter
private String messageStructure;

public AmazonSNSPublishMessageTask() {
setDescription("Publish message to SNS");
setGroup("AWS");
}

@TaskAction
public void publishMessage() {
String topicArn = getTopicArn();
String message = getMessage();
String subject = getSubject();
String messageStructure = getMessageStructure();

if (topicArn == null) throw new GradleException("Must specify SNS topicArn");
if (message == null) throw new GradleException("Must provide message to send to SNS");

AmazonSNSPluginExtension ext = getProject().getExtensions().getByType(AmazonSNSPluginExtension.class);
AmazonSNS sns = ext.getClient();

PublishRequest request = new PublishRequest().withTopicArn(topicArn).withMessage(message);
if (subject != null) {
request.setSubject(subject);
}
if (messageStructure != null) {
request.setMessageStructure(messageStructure);
}
sns.publish(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2013-2016 Classmethod, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

implementation-class=jp.classmethod.aws.gradle.sns.AmazonSNSPlugin

0 comments on commit 3971b90

Please sign in to comment.