Implement AWS SNS node

This commit is contained in:
Matheus Cansian 2019-10-04 22:06:26 -03:00
parent f2ceb8aa88
commit ec9d65b2e0
3 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1,152 @@
import {
SNS,
config,
AWSError,
} from 'aws-sdk';
import { IExecuteFunctions } from 'n8n-core';
import {
INodeTypeDescription,
INodeExecutionData,
INodeType,
INodePropertyOptions,
ILoadOptionsFunctions,
IDataObject
} from 'n8n-workflow';
export class Sns implements INodeType {
description: INodeTypeDescription = {
displayName: 'Amazon SNS',
name: 'amazonSns',
icon: 'file:sns.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["topic"]}}',
description: 'Sends data to Amazon SNS',
defaults: {
name: 'Amazon SNS',
color: '#BB2244',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'aws',
required: true,
}
],
properties: [
{
displayName: 'Topic',
name: 'topic',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTopics',
},
options: [],
default: '',
required: true,
description: 'The topic you want to publish to',
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
default: '',
description: 'Subject when the message is delivered to email endpoints',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
required: true,
typeOptions: {
alwaysOpenEditWindow: true,
},
default: '',
description: 'The message you want to send',
},
],
};
methods = {
loadOptions: {
// Get all the available topics to display them to user so that he can
// select them easily
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const credentials = this.getCredentials('aws');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
config.update({
region: `${credentials.region}`,
accessKeyId: `${credentials.accessKeyId}`,
secretAccessKey: `${credentials.secretAccessKey}`,
});
const returnData: INodePropertyOptions[] = [];
let sns = new SNS();
try {
var data = await sns.listTopics({}).promise();
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}
for (let topic of data.Topics!) {
let topicArn = topic.TopicArn!;
let topicName = topicArn.split(':')[5];
returnData.push({
name: topicName,
value: topicArn,
});
}
return returnData;
}
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const credentials = this.getCredentials('aws');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let topicArn: string;
let subject: string;
let message: string;
config.update({
region: `${credentials.region}`,
accessKeyId: `${credentials.accessKeyId}`,
secretAccessKey: `${credentials.secretAccessKey}`,
});
let sns = new SNS();
for (let i = 0; i < items.length; i++) {
topicArn = this.getNodeParameter('topic', i) as string;
subject = this.getNodeParameter('subject', i) as string;
message = this.getNodeParameter('message', i) as string;
let params = {
Message: message,
Subject: subject,
TopicArn: topicArn,
};
try {
var responseData = await sns.publish(params).promise();
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}
returnData.push(responseData as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View file

@ -56,6 +56,7 @@
"dist/nodes/Airtable/Airtable.node.js",
"dist/nodes/Asana/Asana.node.js",
"dist/nodes/Asana/AsanaTrigger.node.js",
"dist/nodes/Aws/Sns.node.js",
"dist/nodes/Chargebee/Chargebee.node.js",
"dist/nodes/Chargebee/ChargebeeTrigger.node.js",
"dist/nodes/Cron.node.js",
@ -124,6 +125,7 @@
"typescript": "~3.5.2"
},
"dependencies": {
"aws-sdk": "^2.543.0",
"basic-auth": "^2.0.1",
"cron": "^1.6.0",
"glob-promise": "^3.4.0",