2019-10-04 18:06:26 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
import {
2020-10-01 05:01:39 -07:00
IDataObject ,
ILoadOptionsFunctions ,
2019-10-04 18:06:26 -07:00
INodeExecutionData ,
INodePropertyOptions ,
2020-10-01 05:01:39 -07:00
INodeType ,
INodeTypeDescription ,
2021-04-16 09:33:36 -07:00
NodeApiError ,
NodeOperationError ,
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 : [
{
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-06-03 10:23:49 -07:00
displayName : 'Topic Name or ID' ,
2019-10-04 18:06:26 -07:00
name : 'topic' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getTopics' ,
} ,
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
options : [ ] ,
default : '' ,
required : true ,
2022-08-01 13:47:55 -07:00
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>.' ,
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 ,
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 [ ] = [ ] ;
2021-04-16 09:33:36 -07:00
const data = await awsApiRequestSOAP . call ( this , 'sns' , 'GET' , '/?Action=ListTopics' ) ;
2019-10-04 18:06:26 -07:00
2019-10-16 02:18:39 -07:00
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-16 02:18:39 -07:00
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 ++ ) {
2021-07-19 23:58:54 -07:00
try {
const params = [
2022-08-01 13:47:55 -07:00
( 'TopicArn=' + this . getNodeParameter ( 'topic' , i ) ) as string ,
( 'Subject=' + this . getNodeParameter ( 'subject' , i ) ) as string ,
( 'Message=' + this . getNodeParameter ( 'message' , i ) ) as string ,
2021-07-19 23:58:54 -07:00
] ;
2019-10-04 18:06:26 -07:00
2022-08-01 13:47:55 -07: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 ) ] ;
}
}