fix(AWS SNS Node): Pagination Issue

This commit is contained in:
Michael Kret 2022-11-28 13:27:20 +02:00 committed by GitHub
parent 14f81c2725
commit d96d1610ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 304 additions and 60 deletions

View file

@ -3,7 +3,8 @@ import {
IDataObject, IDataObject,
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions, INodeListSearchItems,
INodeListSearchResult,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -37,6 +38,18 @@ export class AwsSns implements INodeType {
type: 'options', type: 'options',
noDataExpression: true, noDataExpression: true,
options: [ options: [
{
name: 'Create',
value: 'create',
description: 'Create a topic',
action: 'Create a topic',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a topic',
action: 'Delete a topic',
},
{ {
name: 'Publish', name: 'Publish',
value: 'publish', value: 'publish',
@ -47,22 +60,106 @@ export class AwsSns implements INodeType {
default: 'publish', default: 'publish',
}, },
{ {
displayName: 'Topic Name or ID', displayName: 'Name',
name: 'topic', name: 'name',
type: 'options', type: 'string',
typeOptions: { required: true,
loadOptionsMethod: 'getTopics', default: '',
},
displayOptions: { displayOptions: {
show: { show: {
operation: ['publish'], operation: ['create'],
}, },
}, },
options: [], },
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Display Name',
name: 'displayName',
type: 'string',
default: '', default: '',
required: true, description: 'The display name to use for a topic with SMS subscriptions',
},
{
displayName: 'Fifo Topic',
name: 'fifoTopic',
type: 'boolean',
default: false,
description: description:
'The topic you want to publish to. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.', 'Whether the topic you want to create is a FIFO (first-in-first-out) topic',
},
],
displayOptions: {
show: {
operation: ['create'],
},
},
},
{
displayName: 'Topic',
name: 'topic',
type: 'resourceLocator',
default: { mode: 'list', value: '' },
required: true,
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'],
},
},
}, },
{ {
displayName: 'Subject', displayName: 'Subject',
@ -97,32 +194,52 @@ export class AwsSns implements INodeType {
}; };
methods = { methods = {
loadOptions: { listSearch: {
// Get all the available topics to display them to user so that he can async listTopics(
// select them easily this: ILoadOptionsFunctions,
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { filter?: string,
const returnData: INodePropertyOptions[] = []; paginationToken?: string,
const data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics'); ): Promise<INodeListSearchResult> {
const returnData: INodeListSearchItems[] = [];
const params = paginationToken ? `NextToken=${encodeURIComponent(paginationToken)}` : '';
const data = await awsApiRequestSOAP.call(
this,
'sns',
'GET',
'/?Action=ListTopics&' + params,
);
let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member; let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
const nextToken = data.ListTopicsResponse.ListTopicsResult.NextToken;
if (nextToken) {
paginationToken = nextToken as string;
} else {
paginationToken = undefined;
}
if (!Array.isArray(topics)) { 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]; topics = [topics];
} }
for (const topic of topics) { for (const topic of topics) {
const topicArn = topic.TopicArn as string; const topicArn = topic.TopicArn as string;
const topicName = topicArn.split(':')[5]; const arnParsed = topicArn.split(':');
const topicName = arnParsed[5];
const awsRegion = arnParsed[3];
if (filter && topicName.includes(filter) === false) {
continue;
}
returnData.push({ returnData.push({
name: topicName, name: topicName,
value: topicArn, value: topicArn,
url: `https://${awsRegion}.console.aws.amazon.com/sns/v3/home?region=${awsRegion}#/topic/${topicArn}`,
}); });
} }
return { results: returnData, paginationToken };
return returnData;
}, },
}, },
}; };
@ -130,11 +247,64 @@ export class AwsSns implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
try { try {
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[] = [];
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;
const params = [('TopicArn=' + topic) as string];
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 = [ const params = [
('TopicArn=' + this.getNodeParameter('topic', i)) as string, ('TopicArn=' + topic) as string,
('Subject=' + this.getNodeParameter('subject', i)) as string, ('Subject=' + this.getNodeParameter('subject', i)) as string,
('Message=' + this.getNodeParameter('message', i)) as string, ('Message=' + this.getNodeParameter('message', i)) as string,
]; ];
@ -148,6 +318,7 @@ export class AwsSns implements INodeType {
returnData.push({ returnData.push({
MessageId: responseData.PublishResponse.PublishResult.MessageId, MessageId: responseData.PublishResponse.PublishResult.MessageId,
} as IDataObject); } as IDataObject);
}
} catch (error) { } catch (error) {
if (this.continueOnFail()) { if (this.continueOnFail()) {
returnData.push({ error: error.message }); returnData.push({ error: error.message });

View file

@ -2,7 +2,8 @@ import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
import { import {
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodePropertyOptions, INodeListSearchItems,
INodeListSearchResult,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
IWebhookResponseData, IWebhookResponseData,
@ -44,55 +45,123 @@ export class AwsSnsTrigger implements INodeType {
], ],
properties: [ properties: [
{ {
displayName: 'Topic Name or ID', displayName: 'Topic',
name: 'topic', name: 'topic',
type: 'options', type: 'resourceLocator',
description: default: { mode: 'list', value: '' },
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
required: true, required: true,
modes: [
{
displayName: 'From List',
name: 'list',
type: 'list',
placeholder: 'Select a topic...',
typeOptions: { typeOptions: {
loadOptionsMethod: 'getTopics', searchListMethod: 'listTopics',
searchable: true,
}, },
default: '', },
{
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',
},
],
}, },
], ],
}; };
methods = { methods = {
loadOptions: { listSearch: {
// Get all the available topics to display them to user so that he can async listTopics(
// select them easily this: ILoadOptionsFunctions,
async getTopics(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { filter?: string,
const returnData: INodePropertyOptions[] = []; paginationToken?: string,
const data = await awsApiRequestSOAP.call(this, 'sns', 'GET', '/?Action=ListTopics'); ): Promise<INodeListSearchResult> {
const returnData: INodeListSearchItems[] = [];
const params = paginationToken ? `NextToken=${encodeURIComponent(paginationToken)}` : '';
const data = await awsApiRequestSOAP.call(
this,
'sns',
'GET',
'/?Action=ListTopics&' + params,
);
let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member; let topics = data.ListTopicsResponse.ListTopicsResult.Topics.member;
const nextToken = data.ListTopicsResponse.ListTopicsResult.NextToken;
if (nextToken) {
paginationToken = nextToken as string;
} else {
paginationToken = undefined;
}
if (!Array.isArray(topics)) { 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]; topics = [topics];
} }
for (const topic of topics) { for (const topic of topics) {
const topicArn = topic.TopicArn as string; const topicArn = topic.TopicArn as string;
const topicName = topicArn.split(':')[5]; const arnParsed = topicArn.split(':');
const topicName = arnParsed[5];
const awsRegion = arnParsed[3];
if (filter && topicName.includes(filter) === false) {
continue;
}
returnData.push({ returnData.push({
name: topicName, name: topicName,
value: topicArn, value: topicArn,
url: `https://${awsRegion}.console.aws.amazon.com/sns/v3/home?region=${awsRegion}#/topic/${topicArn}`,
}); });
} }
return returnData; return { results: returnData, paginationToken };
}, },
}, },
}; };
// @ts-ignore //@ts-expect-error because of webhook
webhookMethods = { webhookMethods = {
default: { default: {
async checkExists(this: IHookFunctions): Promise<boolean> { async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const topic = this.getNodeParameter('topic') as string; const topic = this.getNodeParameter('topic', undefined, {
extractValue: true,
}) as string;
if (webhookData.webhookId === undefined) { if (webhookData.webhookId === undefined) {
return false; return false;
} }
@ -127,7 +196,9 @@ export class AwsSnsTrigger implements INodeType {
async create(this: IHookFunctions): Promise<boolean> { async create(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node'); const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default') as string; const webhookUrl = this.getNodeWebhookUrl('default') as string;
const topic = this.getNodeParameter('topic') as string; const topic = this.getNodeParameter('topic', undefined, {
extractValue: true,
}) as string;
if (webhookUrl.includes('%20')) { if (webhookUrl.includes('%20')) {
throw new NodeOperationError( throw new NodeOperationError(
@ -175,7 +246,9 @@ export class AwsSnsTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject(); const req = this.getRequestObject();
const topic = this.getNodeParameter('topic') as string; const topic = this.getNodeParameter('topic', undefined, {
extractValue: true,
}) as string;
const body = jsonParse<{ Type: string; TopicArn: string; Token: string }>( const body = jsonParse<{ Type: string; TopicArn: string; Token: string }>(
req.rawBody.toString(), req.rawBody.toString(),