2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import type {
|
2020-10-01 05:01:39 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
2019-10-04 18:06:26 -07:00
|
|
|
INodeExecutionData,
|
2022-11-28 03:27:20 -08:00
|
|
|
INodeListSearchItems,
|
|
|
|
INodeListSearchResult,
|
2020-10-01 05:01:39 -07:00
|
|
|
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',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:sns.svg',
|
2019-10-04 18:06:26 -07:00
|
|
|
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-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',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2019-10-15 10:24:45 -07:00
|
|
|
options: [
|
2022-11-28 03:27:20 -08:00
|
|
|
{
|
|
|
|
name: 'Create',
|
|
|
|
value: 'create',
|
|
|
|
description: 'Create a topic',
|
|
|
|
action: 'Create a topic',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Delete',
|
|
|
|
value: 'delete',
|
|
|
|
description: 'Delete a topic',
|
|
|
|
action: 'Delete a topic',
|
|
|
|
},
|
2019-10-15 10:24:45 -07:00
|
|
|
{
|
|
|
|
name: 'Publish',
|
|
|
|
value: 'publish',
|
|
|
|
description: 'Publish a message to a topic',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Publish a message to a topic',
|
2019-10-15 10:24:45 -07:00
|
|
|
},
|
|
|
|
],
|
2019-10-16 02:18:39 -07:00
|
|
|
default: 'publish',
|
2019-10-15 10:24:45 -07:00
|
|
|
},
|
2019-10-04 18:06:26 -07:00
|
|
|
{
|
2022-11-28 03:27:20 -08:00
|
|
|
displayName: 'Name',
|
|
|
|
name: 'name',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: ['create'],
|
|
|
|
},
|
2019-10-04 18:06:26 -07:00
|
|
|
},
|
2022-11-28 03:27:20 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Display Name',
|
|
|
|
name: 'displayName',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'The display name to use for a topic with SMS subscriptions',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Fifo Topic',
|
|
|
|
name: 'fifoTopic',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
description:
|
|
|
|
'Whether the topic you want to create is a FIFO (first-in-first-out) topic',
|
|
|
|
},
|
|
|
|
],
|
2019-10-15 10:24:45 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-11-28 03:27:20 -08:00
|
|
|
operation: ['create'],
|
2019-10-15 10:24:45 -07:00
|
|
|
},
|
|
|
|
},
|
2022-11-28 03:27:20 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Topic',
|
|
|
|
name: 'topic',
|
|
|
|
type: 'resourceLocator',
|
|
|
|
default: { mode: 'list', value: '' },
|
2019-10-04 18:06:26 -07:00
|
|
|
required: true,
|
2022-11-28 03:27:20 -08:00
|
|
|
modes: [
|
|
|
|
{
|
|
|
|
displayName: 'From List',
|
|
|
|
name: 'list',
|
|
|
|
type: 'list',
|
|
|
|
placeholder: 'Select a topic...',
|
|
|
|
typeOptions: {
|
|
|
|
searchListMethod: 'listTopics',
|
|
|
|
searchable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'By URL',
|
|
|
|
name: 'url',
|
|
|
|
type: 'string',
|
|
|
|
placeholder:
|
|
|
|
'https://us-east-1.console.aws.amazon.com/sns/v3/home?region=us-east-1#/topic/arn:aws:sns:us-east-1:777777777777:your_topic',
|
|
|
|
validation: [
|
|
|
|
{
|
|
|
|
type: 'regex',
|
|
|
|
properties: {
|
|
|
|
regex:
|
|
|
|
'https:\\/\\/[0-9a-zA-Z\\-_]+\\.console\\.aws\\.amazon\\.com\\/sns\\/v3\\/home\\?region\\=[0-9a-zA-Z\\-_]+\\#\\/topic\\/arn:aws:sns:[0-9a-zA-Z\\-_]+:[0-9]+:[0-9a-zA-Z\\-_]+(?:\\/.*|)',
|
|
|
|
errorMessage: 'Not a valid AWS SNS Topic URL',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
extractValue: {
|
|
|
|
type: 'regex',
|
|
|
|
regex:
|
|
|
|
'https:\\/\\/[0-9a-zA-Z\\-_]+\\.console\\.aws\\.amazon\\.com\\/sns\\/v3\\/home\\?region\\=[0-9a-zA-Z\\-_]+\\#\\/topic\\/(arn:aws:sns:[0-9a-zA-Z\\-_]+:[0-9]+:[0-9a-zA-Z\\-_]+)(?:\\/.*|)',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'ID',
|
|
|
|
name: 'id',
|
|
|
|
type: 'string',
|
|
|
|
validation: [
|
|
|
|
{
|
|
|
|
type: 'regex',
|
|
|
|
properties: {
|
|
|
|
regex: 'arn:aws:sns:[0-9a-zA-Z\\-_]+:[0-9]+:[0-9a-zA-Z\\-_]+',
|
|
|
|
errorMessage: 'Not a valid AWS SNS Topic ARN',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
placeholder: 'arn:aws:sns:your-aws-region:777777777777:your_topic',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
operation: ['publish', 'delete'],
|
|
|
|
},
|
|
|
|
},
|
2019-10-04 18:06:26 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Subject',
|
|
|
|
name: 'subject',
|
|
|
|
type: 'string',
|
2019-10-15 10:24:45 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['publish'],
|
2019-10-15 10:24:45 -07:00
|
|
|
},
|
|
|
|
},
|
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: {
|
2022-08-01 13:47:55 -07:00
|
|
|
operation: ['publish'],
|
2019-10-15 10:24:45 -07:00
|
|
|
},
|
|
|
|
},
|
2019-10-04 18:06:26 -07:00
|
|
|
required: true,
|
|
|
|
default: '',
|
|
|
|
description: 'The message you want to send',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
2022-11-28 03:27:20 -08:00
|
|
|
listSearch: {
|
|
|
|
async listTopics(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
filter?: string,
|
|
|
|
paginationToken?: string,
|
|
|
|
): Promise<INodeListSearchResult> {
|
|
|
|
const returnData: INodeListSearchItems[] = [];
|
|
|
|
const params = paginationToken ? `NextToken=${encodeURIComponent(paginationToken)}` : '';
|
|
|
|
|
|
|
|
const data = await awsApiRequestSOAP.call(
|
|
|
|
this,
|
|
|
|
'sns',
|
|
|
|
'GET',
|
|
|
|
'/?Action=ListTopics&' + params,
|
|
|
|
);
|
2019-10-04 18:06:26 -07:00
|
|
|
|
2019-10-16 02:18:39 -07:00
|
|
|
let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
|
2022-11-28 03:27:20 -08:00
|
|
|
const nextToken = data.ListTopicsResponse.ListTopicsResult.NextToken;
|
|
|
|
|
|
|
|
if (nextToken) {
|
|
|
|
paginationToken = nextToken as string;
|
|
|
|
} else {
|
|
|
|
paginationToken = undefined;
|
|
|
|
}
|
2019-10-16 02:18:39 -07:00
|
|
|
|
|
|
|
if (!Array.isArray(topics)) {
|
|
|
|
topics = [topics];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const topic of topics) {
|
|
|
|
const topicArn = topic.TopicArn as string;
|
2022-11-28 03:27:20 -08:00
|
|
|
const arnParsed = topicArn.split(':');
|
|
|
|
const topicName = arnParsed[5];
|
|
|
|
const awsRegion = arnParsed[3];
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (filter && !topicName.includes(filter)) {
|
2022-11-28 03:27:20 -08:00
|
|
|
continue;
|
|
|
|
}
|
2019-10-05 06:27:19 -07:00
|
|
|
|
2019-10-04 18:06:26 -07:00
|
|
|
returnData.push({
|
|
|
|
name: topicName,
|
|
|
|
value: topicArn,
|
2022-11-28 03:27:20 -08:00
|
|
|
url: `https://${awsRegion}.console.aws.amazon.com/sns/v3/home?region=${awsRegion}#/topic/${topicArn}`,
|
2019-10-04 18:06:26 -07:00
|
|
|
});
|
|
|
|
}
|
2022-11-28 03:27:20 -08:00
|
|
|
return { results: returnData, paginationToken };
|
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[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2019-10-04 18:06:26 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
2022-11-28 03:27:20 -08:00
|
|
|
if (operation === 'create') {
|
|
|
|
let name = this.getNodeParameter('name', i) as string;
|
|
|
|
const fifoTopic = this.getNodeParameter('options.fifoTopic', i, false) as boolean;
|
|
|
|
const displayName = this.getNodeParameter('options.displayName', i, '') as string;
|
|
|
|
const params: string[] = [];
|
2019-10-04 18:06:26 -07:00
|
|
|
|
2022-11-28 03:27:20 -08:00
|
|
|
if (fifoTopic && !name.endsWith('.fifo')) {
|
|
|
|
name = `${name}.fifo`;
|
|
|
|
}
|
|
|
|
|
|
|
|
params.push(`Name=${name}`);
|
|
|
|
|
|
|
|
if (fifoTopic) {
|
|
|
|
params.push('Attributes.entry.1.key=FifoTopic');
|
|
|
|
params.push('Attributes.entry.1.value=true');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (displayName) {
|
|
|
|
params.push('Attributes.entry.2.key=DisplayName');
|
|
|
|
params.push(`Attributes.entry.2.value=${displayName}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseData = await awsApiRequestSOAP.call(
|
|
|
|
this,
|
|
|
|
'sns',
|
|
|
|
'GET',
|
|
|
|
'/?Action=CreateTopic&' + params.join('&'),
|
|
|
|
);
|
|
|
|
returnData.push({
|
|
|
|
TopicArn: responseData.CreateTopicResponse.CreateTopicResult.TopicArn,
|
|
|
|
} as IDataObject);
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
const topic = this.getNodeParameter('topic', i, undefined, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
2022-12-02 12:54:28 -08:00
|
|
|
const params = ['TopicArn=' + topic];
|
2022-11-28 03:27:20 -08:00
|
|
|
|
|
|
|
await awsApiRequestSOAP.call(
|
|
|
|
this,
|
|
|
|
'sns',
|
|
|
|
'GET',
|
|
|
|
'/?Action=DeleteTopic&' + params.join('&'),
|
|
|
|
);
|
|
|
|
// response of delete is the same no matter if topic was deleted or not
|
|
|
|
returnData.push({ success: true } as IDataObject);
|
|
|
|
}
|
|
|
|
if (operation === 'publish') {
|
|
|
|
const topic = this.getNodeParameter('topic', i, undefined, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
|
|
|
|
|
|
|
const params = [
|
2022-12-02 12:54:28 -08:00
|
|
|
'TopicArn=' + topic,
|
2023-01-13 09:11:56 -08:00
|
|
|
'Subject=' + (this.getNodeParameter('subject', i) as string),
|
|
|
|
'Message=' + (this.getNodeParameter('message', i) as string),
|
2022-11-28 03:27:20 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
const responseData = await awsApiRequestSOAP.call(
|
|
|
|
this,
|
|
|
|
'sns',
|
|
|
|
'GET',
|
|
|
|
'/?Action=Publish&' + params.join('&'),
|
|
|
|
);
|
|
|
|
returnData.push({
|
|
|
|
MessageId: responseData.PublishResponse.PublishResult.MessageId,
|
|
|
|
} as IDataObject);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2019-10-04 18:06:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|