Skip to content

Commit 31218b6

Browse files
committed
chore: fix part of generating Cfn for dynamodb
1 parent cb103ef commit 31218b6

File tree

4 files changed

+42
-511
lines changed

4 files changed

+42
-511
lines changed

lib/package/dynamodb/compileIamRoleToDynamodb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111

1212
return {
1313
tableName: serviceProxy.dynamodb.tableName,
14-
action: this.getDynamodbAction(serviceProxy.dynamodb)
14+
action: serviceProxy.dynamodb.action
1515
}
1616
})
1717
})

lib/package/dynamodb/compileIamRoleToDynamodb.test.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ describe('#compileIamRoleToDynamodb()', () => {
3131
path: '/dynamodb/v1',
3232
tableName: { Ref: 'mytable' },
3333
method: 'post',
34-
hashKey: 'id'
34+
action: 'PutItem',
35+
hashKey: {
36+
pathParam: 'id',
37+
attributeType: 'S'
38+
}
3539
}
3640
},
3741
{
@@ -55,16 +59,6 @@ describe('#compileIamRoleToDynamodb()', () => {
5559
queryStringParam: 'id'
5660
}
5761
}
58-
},
59-
{
60-
dynamodb: {
61-
path: '/dynamodb/v2',
62-
tableName: 'mytable',
63-
method: 'patch',
64-
hashKey: {
65-
queryStringParam: 'id'
66-
}
67-
}
6862
}
6963
]
7064
}
@@ -132,18 +126,6 @@ describe('#compileIamRoleToDynamodb()', () => {
132126
}
133127
]
134128
}
135-
},
136-
{
137-
Effect: 'Allow',
138-
Action: 'dynamodb:UpdateItem',
139-
Resource: {
140-
'Fn::Sub': [
141-
'arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tableName}',
142-
{
143-
tableName: 'mytable'
144-
}
145-
]
146-
}
147129
}
148130
]
149131
}

lib/package/dynamodb/compileMethodsToDynamodb.js

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = {
66
compileMethodsToDynamodb() {
77
this.validated.events.forEach((event) => {
88
if (event.serviceName == 'dynamodb') {
9-
event.http.action = this.getDynamodbAction(event.http)
109
const resourceId = this.getResourceId(event.http.path)
1110
const resourceName = this.getResourceName(event.http.path)
1211

@@ -44,25 +43,6 @@ module.exports = {
4443
})
4544
},
4645

47-
getDynamodbAction(http) {
48-
if (!_.has(http, 'action')) {
49-
switch (http.method.toUpperCase()) {
50-
case 'PUT':
51-
return 'PutItem'
52-
case 'POST':
53-
return 'PutItem'
54-
case 'GET':
55-
return 'GetItem'
56-
case 'DELETE':
57-
return 'DeleteItem'
58-
case 'PATCH':
59-
return 'UpdateItem'
60-
}
61-
}
62-
63-
return http.action
64-
},
65-
6646
getDynamodbMethodIntegration(http) {
6747
const integration = {
6848
IntegrationHttpMethod: 'POST',
@@ -153,20 +133,6 @@ module.exports = {
153133
attributeValue: `$input.params().querystring.${http.hashKey.queryStringParam}`
154134
}
155135
}
156-
157-
if (http.hashKey.bodyParam) {
158-
return {
159-
key: http.hashKey.bodyParam,
160-
attributeType: http.hashKey.attributeType,
161-
attributeValue: `$util.parseJson($input.body).${http.hashKey.bodyParam}`
162-
}
163-
}
164-
165-
return {
166-
key: http.hashKey,
167-
attributeType: 'S',
168-
attributeValue: '$context.requestId'
169-
}
170136
},
171137

172138
getDynamodbObjectRangekeyParameter(http) {
@@ -185,30 +151,9 @@ module.exports = {
185151
attributeValue: `$input.params().querystring.${http.rangeKey.queryStringParam}`
186152
}
187153
}
188-
189-
if (http.rangeKey.bodyParam) {
190-
return {
191-
key: http.rangeKey.bodyParam,
192-
attributeType: http.rangeKey.attributeType,
193-
attributeValue: `$util.parseJson($input.body).${http.rangeKey.bodyParam}`
194-
}
195-
}
196-
197-
return {
198-
key: http.rangeKey,
199-
attributeType: 'S',
200-
attributeValue: '$context.requestId'
201-
}
202154
},
203155

204156
getDefaultDynamodbResponseTemplates(http) {
205-
if (http.action === 'PutItem' && _.isString(http.hashKey)) {
206-
return {
207-
'application/json': this.getPutItemDefaultDynamodbResponseTemplate(http),
208-
'application/x-www-form-urlencoded': this.getPutItemDefaultDynamodbResponseTemplate(http)
209-
}
210-
}
211-
212157
if (http.action === 'GetItem') {
213158
return {
214159
'application/json': this.getGetItemDefaultDynamodbResponseTemplate(),
@@ -223,19 +168,6 @@ module.exports = {
223168
return `#set($item = $input.path('$.Item')){#foreach($key in $item.keySet())#set ($value = $item.get($key))#foreach( $type in $value.keySet())"$key":"$value.get($type)"#if($foreach.hasNext()),#end#end#if($foreach.hasNext()),#end#end}`
224169
},
225170

226-
getPutItemDefaultDynamodbResponseTemplate(http) {
227-
const objectRequestParam = this.getDynamodbObjectHashkeyParameter(http)
228-
return {
229-
'Fn::Sub': [
230-
'{"${HashKey}": "${HashAttributeValue}"}',
231-
{
232-
HashKey: `${objectRequestParam.key}`,
233-
HashAttributeValue: `${objectRequestParam.attributeValue}`
234-
}
235-
]
236-
}
237-
},
238-
239171
buildDefaultDynamodbRequestTemplate(http) {
240172
switch (http.action) {
241173
case 'PutItem':
@@ -244,8 +176,6 @@ module.exports = {
244176
return this.buildDefaultDynamodbGetItemRequestTemplate(http)
245177
case 'DeleteItem':
246178
return this.buildDefaultDynamodbDeleteItemRequestTemplate(http)
247-
case 'UpdateItem':
248-
return this.buildDefaultDynamodbUpdateItemRequestTemplate(http)
249179
}
250180
},
251181

@@ -275,37 +205,6 @@ module.exports = {
275205
}
276206
},
277207

278-
buildDefaultDynamodbUpdateItemRequestTemplate(http) {
279-
const fuSubValues = {
280-
TableName: http.tableName
281-
}
282-
283-
let requestTemplate = '{"TableName": "${TableName}","Key":{'
284-
if (_.has(http, 'hashKey')) {
285-
requestTemplate += '"${HashKey}": {"${HashAttributeType}": "${HashAttributeValue}"}'
286-
Object.assign(fuSubValues, this.getDynamodbHashkeyFnSubValues(http))
287-
}
288-
289-
if (_.has(http, 'rangeKey')) {
290-
requestTemplate += ',"${RangeKey}": {"${RangeAttributeType}": "${RangeAttributeValue}"}'
291-
Object.assign(fuSubValues, this.getDynamodbRangekeyFnSubValues(http))
292-
}
293-
294-
requestTemplate += '}'
295-
requestTemplate += `
296-
#set ($body = $input.path('$'))
297-
,"UpdateExpression":"$body.UpdateExpression"
298-
,"ExpressionAttributeValues":$input.json('$.ExpressionAttributeValues')
299-
#if ($body.ExpressionAttributeNames != "")
300-
,"ExpressionAttributeNames":$input.json('$.ExpressionAttributeNames')
301-
#end
302-
`
303-
requestTemplate += '}'
304-
return {
305-
'Fn::Sub': [`${requestTemplate}`, fuSubValues]
306-
}
307-
},
308-
309208
buildDefaultDynamodbGetItemRequestTemplate(http) {
310209
const fuSubValues = {
311210
TableName: http.tableName

0 commit comments

Comments
 (0)