Skip to content

Commit 0ecd9a7

Browse files
Added Compilation for sns
1 parent 6a60f53 commit 0ecd9a7

File tree

2 files changed

+282
-1
lines changed

2 files changed

+282
-1
lines changed

lib/package/sns/compileMethodsToSns.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ module.exports = {
6262
RequestTemplates: this.getSnsIntegrationRequestTemplates(http)
6363
}
6464

65+
if ("request" in http){
66+
if ("passThrough" in http.request){
67+
integration.PassthroughBehavior = http.request.passThrough
68+
}
69+
if ("contentHandling" in http.request){
70+
integration.ContentHandling = http.request.contentHandling
71+
}
72+
}
73+
6574
let integrationResponse
6675

6776
if (_.get(http.response, 'template.success')) {
@@ -146,6 +155,14 @@ module.exports = {
146155
}
147156
},
148157

158+
getSnsIntegrationRequestPassThrough(http, integration) {
159+
if ("request" in http) {
160+
161+
}
162+
const defaultRequestTemplates = this.getDefaultSnsRequestTemplates(http)
163+
return Object.assign(defaultRequestTemplates, _.get(http, ['request', 'template']))
164+
},
165+
149166
buildDefaultSnsRequestTemplate(http) {
150167
const { topicName } = http
151168

lib/package/sns/compileMethodsToSns.test.js

Lines changed: 265 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ describe('#compileMethodsToSns()', () => {
477477
)
478478
expect(
479479
resource.Properties.Integration.RequestTemplates['application/x-www-form-urlencoded'][
480-
'Fn::Join'
480+
'Fn::Join'
481481
]
482482
).to.be.deep.equal([
483483
'',
@@ -1000,4 +1000,268 @@ describe('#compileMethodsToSns()', () => {
10001000
}
10011001
])
10021002
})
1003+
1004+
it('should create corresponding resources when sns proxies are given with passthrou', () => {
1005+
serverlessApigatewayServiceProxy.validated = {
1006+
events: [
1007+
{
1008+
serviceName: 'sns',
1009+
http: {
1010+
topicName: 'myTopic',
1011+
path: 'sns',
1012+
method: 'post',
1013+
auth: {
1014+
authorizationType: 'NONE'
1015+
},
1016+
request: {
1017+
passThrough: 'WHEN_NO_TEMPLATES'
1018+
}
1019+
1020+
}
1021+
}
1022+
]
1023+
}
1024+
serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'
1025+
serverlessApigatewayServiceProxy.apiGatewayResources = {
1026+
sns: {
1027+
name: 'Sns',
1028+
resourceLogicalId: 'ApiGatewayResourceSns'
1029+
}
1030+
}
1031+
1032+
serverlessApigatewayServiceProxy.compileMethodsToSns()
1033+
1034+
1035+
it('should create corresponding resources when sns proxies are given a passThought option', () => {
1036+
serverlessApigatewayServiceProxy.validated = {
1037+
events: [
1038+
{
1039+
serviceName: 'sns',
1040+
http: {
1041+
topicName: 'myTopic',
1042+
path: 'sns',
1043+
method: 'post',
1044+
auth: {
1045+
authorizationType: 'NONE'
1046+
},
1047+
request: {
1048+
passThrough: 'WHEN_NO_TEMPLATES'
1049+
}
1050+
}
1051+
}
1052+
]
1053+
}
1054+
serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'
1055+
serverlessApigatewayServiceProxy.apiGatewayResources = {
1056+
sns: {
1057+
name: 'Sns',
1058+
resourceLogicalId: 'ApiGatewayResourceSns'
1059+
}
1060+
}
1061+
1062+
serverlessApigatewayServiceProxy.compileMethodsToSns()
1063+
console.log(serverless.service.provider.compiledCloudFormationTemplate.Resources)
1064+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
1065+
ApiGatewayMethodSnsPost: {
1066+
Type: 'AWS::ApiGateway::Method',
1067+
Properties: {
1068+
HttpMethod: 'POST',
1069+
RequestParameters: {},
1070+
AuthorizationType: 'NONE',
1071+
AuthorizationScopes: undefined,
1072+
AuthorizerId: undefined,
1073+
ApiKeyRequired: false,
1074+
ResourceId: { Ref: 'ApiGatewayResourceSns' },
1075+
RestApiId: { Ref: 'ApiGatewayRestApi' },
1076+
Integration: {
1077+
IntegrationHttpMethod: 'POST',
1078+
Type: 'AWS',
1079+
Credentials: { 'Fn::GetAtt': ['ApigatewayToSnsRole', 'Arn'] },
1080+
Uri: {
1081+
'Fn::Sub': 'arn:${AWS::Partition}:apigateway:${AWS::Region}:sns:path//'
1082+
},
1083+
PassthroughBehavior: 'WHEN_NO_TEMPLATES',
1084+
RequestParameters: {
1085+
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
1086+
},
1087+
RequestTemplates: {
1088+
'application/json': {
1089+
'Fn::Join': [
1090+
'',
1091+
[
1092+
"Action=Publish&Message=$util.urlEncode($input.body)&TopicArn=$util.urlEncode('",
1093+
{
1094+
'Fn::Sub': [
1095+
'arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${topicName}',
1096+
{ topicName: 'myTopic' }
1097+
]
1098+
},
1099+
"')"
1100+
]
1101+
]
1102+
},
1103+
'application/x-www-form-urlencoded': {
1104+
'Fn::Join': [
1105+
'',
1106+
[
1107+
"Action=Publish&Message=$util.urlEncode($input.body)&TopicArn=$util.urlEncode('",
1108+
{
1109+
'Fn::Sub': [
1110+
'arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${topicName}',
1111+
{ topicName: 'myTopic' }
1112+
]
1113+
},
1114+
"')"
1115+
]
1116+
]
1117+
}
1118+
},
1119+
IntegrationResponses: [
1120+
{
1121+
StatusCode: 200,
1122+
SelectionPattern: 200,
1123+
ResponseParameters: {},
1124+
ResponseTemplates: {}
1125+
},
1126+
{
1127+
StatusCode: 400,
1128+
SelectionPattern: 400,
1129+
ResponseParameters: {},
1130+
ResponseTemplates: {}
1131+
},
1132+
{
1133+
StatusCode: 500,
1134+
SelectionPattern: 500,
1135+
ResponseParameters: {},
1136+
ResponseTemplates: {}
1137+
}
1138+
]
1139+
},
1140+
MethodResponses: [
1141+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 200 },
1142+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 400 },
1143+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 500 }
1144+
]
1145+
}
1146+
}
1147+
})
1148+
})
1149+
})
1150+
1151+
it('should create corresponding resources when sns proxies are given a ContentHandling option', () => {
1152+
serverlessApigatewayServiceProxy.validated = {
1153+
events: [
1154+
{
1155+
serviceName: 'sns',
1156+
http: {
1157+
topicName: 'myTopic',
1158+
path: 'sns',
1159+
method: 'post',
1160+
auth: {
1161+
authorizationType: 'NONE'
1162+
},
1163+
request: {
1164+
passThrough: 'WHEN_NO_TEMPLATES',
1165+
contentHandling: 'CONVERT_TO_TEXT'
1166+
}
1167+
}
1168+
}
1169+
]
1170+
}
1171+
serverlessApigatewayServiceProxy.apiGatewayRestApiLogicalId = 'ApiGatewayRestApi'
1172+
serverlessApigatewayServiceProxy.apiGatewayResources = {
1173+
sns: {
1174+
name: 'Sns',
1175+
resourceLogicalId: 'ApiGatewayResourceSns'
1176+
}
1177+
}
1178+
1179+
serverlessApigatewayServiceProxy.compileMethodsToSns()
1180+
console.log(serverless.service.provider.compiledCloudFormationTemplate.Resources)
1181+
expect(serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
1182+
ApiGatewayMethodSnsPost: {
1183+
Type: 'AWS::ApiGateway::Method',
1184+
Properties: {
1185+
HttpMethod: 'POST',
1186+
RequestParameters: {},
1187+
AuthorizationType: 'NONE',
1188+
AuthorizationScopes: undefined,
1189+
AuthorizerId: undefined,
1190+
ApiKeyRequired: false,
1191+
ResourceId: { Ref: 'ApiGatewayResourceSns' },
1192+
RestApiId: { Ref: 'ApiGatewayRestApi' },
1193+
Integration: {
1194+
IntegrationHttpMethod: 'POST',
1195+
Type: 'AWS',
1196+
Credentials: { 'Fn::GetAtt': ['ApigatewayToSnsRole', 'Arn'] },
1197+
Uri: {
1198+
'Fn::Sub': 'arn:${AWS::Partition}:apigateway:${AWS::Region}:sns:path//'
1199+
},
1200+
PassthroughBehavior: 'WHEN_NO_TEMPLATES',
1201+
ContentHandling: 'CONVERT_TO_TEXT',
1202+
RequestParameters: {
1203+
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
1204+
},
1205+
RequestTemplates: {
1206+
'application/json': {
1207+
'Fn::Join': [
1208+
'',
1209+
[
1210+
"Action=Publish&Message=$util.urlEncode($input.body)&TopicArn=$util.urlEncode('",
1211+
{
1212+
'Fn::Sub': [
1213+
'arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${topicName}',
1214+
{ topicName: 'myTopic' }
1215+
]
1216+
},
1217+
"')"
1218+
]
1219+
]
1220+
},
1221+
'application/x-www-form-urlencoded': {
1222+
'Fn::Join': [
1223+
'',
1224+
[
1225+
"Action=Publish&Message=$util.urlEncode($input.body)&TopicArn=$util.urlEncode('",
1226+
{
1227+
'Fn::Sub': [
1228+
'arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:${topicName}',
1229+
{ topicName: 'myTopic' }
1230+
]
1231+
},
1232+
"')"
1233+
]
1234+
]
1235+
}
1236+
},
1237+
IntegrationResponses: [
1238+
{
1239+
StatusCode: 200,
1240+
SelectionPattern: 200,
1241+
ResponseParameters: {},
1242+
ResponseTemplates: {}
1243+
},
1244+
{
1245+
StatusCode: 400,
1246+
SelectionPattern: 400,
1247+
ResponseParameters: {},
1248+
ResponseTemplates: {}
1249+
},
1250+
{
1251+
StatusCode: 500,
1252+
SelectionPattern: 500,
1253+
ResponseParameters: {},
1254+
ResponseTemplates: {}
1255+
}
1256+
]
1257+
},
1258+
MethodResponses: [
1259+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 200 },
1260+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 400 },
1261+
{ ResponseParameters: {}, ResponseModels: {}, StatusCode: 500 }
1262+
]
1263+
}
1264+
}
1265+
})
1266+
})
10031267
})

0 commit comments

Comments
 (0)