n8n/packages/nodes-base/nodes/Aws/AwsSns.node.ts

164 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-10-04 18:06:26 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
2019-10-04 18:06:26 -07:00
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
2019-10-04 18:06:26 -07:00
} from 'n8n-workflow';
2019-10-15 10:21:48 -07:00
import { awsApiRequestSOAP } from './GenericFunctions';
2019-10-05 06:27:19 -07:00
2019-10-05 08:05:58 -07:00
export class AwsSns implements INodeType {
2019-10-04 18:06:26 -07:00
description: INodeTypeDescription = {
2019-10-05 08:05:58 -07:00
displayName: 'AWS SNS',
name: 'awsSns',
2019-10-04 18:06:26 -07:00
icon: 'file:sns.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["topic"]}}',
2019-10-05 08:05:58 -07:00
description: 'Sends data to AWS SNS',
2019-10-04 18:06:26 -07:00
defaults: {
2019-10-05 08:05:58 -07:00
name: 'AWS SNS',
2019-10-05 06:27:19 -07:00
color: '#FF9900',
2019-10-04 18:06:26 -07:00
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'aws',
required: true,
2020-10-22 06:46:03 -07:00
},
2019-10-04 18:06:26 -07:00
],
properties: [
2019-10-15 10:24:45 -07:00
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Publish',
value: 'publish',
description: 'Publish a message to a topic',
},
],
default: 'publish',
2019-10-15 10:24:45 -07:00
description: 'The operation to perform.',
},
2019-10-04 18:06:26 -07:00
{
displayName: 'Topic',
name: 'topic',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTopics',
},
2019-10-15 10:24:45 -07:00
displayOptions: {
show: {
operation: [
'publish',
],
},
},
2019-10-04 18:06:26 -07:00
options: [],
default: '',
required: true,
description: 'The topic you want to publish to',
},
{
displayName: 'Subject',
name: 'subject',
type: 'string',
2019-10-15 10:24:45 -07:00
displayOptions: {
show: {
operation: [
'publish',
],
},
},
2019-10-04 18:06:26 -07:00
default: '',
2019-10-15 10:21:48 -07:00
required: true,
2019-10-04 18:06:26 -07:00
description: 'Subject when the message is delivered to email endpoints',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
2019-10-15 10:24:45 -07:00
displayOptions: {
show: {
operation: [
'publish',
],
},
},
2019-10-04 18:06:26 -07:00
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 returnData: INodePropertyOptions[] = [];
let data;
2019-10-04 18:06:26 -07:00
try {
data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics');
2019-10-04 18:06:26 -07:00
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}
let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
if (!Array.isArray(topics)) {
// If user has only a single topic no array get returned so we make
// one manually to be able to process everything identically
topics = [topics];
}
for (const topic of topics) {
const topicArn = topic.TopicArn as string;
const topicName = topicArn.split(':')[5];
2019-10-05 06:27:19 -07:00
2019-10-04 18:06:26 -07:00
returnData.push({
name: topicName,
value: topicArn,
});
}
2019-10-04 18:06:26 -07:00
return returnData;
2020-10-22 06:46:03 -07:00
},
2019-10-04 18:06:26 -07:00
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
2019-10-15 10:21:48 -07:00
const params = [
'TopicArn=' + this.getNodeParameter('topic', i) as string,
'Subject=' + this.getNodeParameter('subject', i) as string,
'Message=' + this.getNodeParameter('message', i) as string,
];
2019-10-04 18:06:26 -07:00
let responseData;
2019-10-04 18:06:26 -07:00
try {
responseData = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=Publish&' + params.join('&'));
2019-10-04 18:06:26 -07:00
} catch (err) {
throw new Error(`AWS Error: ${err}`);
}
2019-10-15 10:21:48 -07:00
returnData.push({MessageId: responseData.PublishResponse.PublishResult.MessageId} as IDataObject);
2019-10-04 18:06:26 -07:00
}
return [this.helpers.returnJsonArray(returnData)];
}
}