/
parseResources.js
189 lines (151 loc) · 4.68 KB
/
parseResources.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const { entries, fromEntries, keys } = Object
const APIGATEWAY_INTEGRATION_TYPE_HTTP_PROXY = 'HTTP_PROXY'
const APIGATEWAY_ROOT_ID = 'RootResourceId'
const APIGATEWAY_TYPE_METHOD = 'AWS::ApiGateway::Method'
const APIGATEWAY_TYPE_RESOURCE = 'AWS::ApiGateway::Resource'
function getApiGatewayTemplateObjects(resources) {
const Resources = resources && resources.Resources
if (!Resources) {
return {}
}
const methodObjects = []
const pathObjects = []
entries(Resources).forEach(([key, value]) => {
const resourceObj = value || {}
const { Type } = resourceObj
const keyValuePair = [key, resourceObj]
if (Type === APIGATEWAY_TYPE_METHOD) {
methodObjects.push(keyValuePair)
} else if (Type === APIGATEWAY_TYPE_RESOURCE) {
pathObjects.push(keyValuePair)
}
})
return {
methodObjects: fromEntries(methodObjects),
pathObjects: fromEntries(pathObjects),
}
}
/* Example of a Resource Object
* "ApiGatewayResourceFavicon": {
* "Type": "AWS::ApiGateway::Resource",
* "Properties": {
* "ParentId": { "Fn::GetAtt": [ "ApiGatewayRestApi", "RootResourceId" ] },
* "PathPart": "favicon.ico",
* "RestApiId": { "Ref": "ApiGatewayRestApi" }
* }
* }
*/
/* Resource Helpers */
function isRoot(resourceId) {
return resourceId === APIGATEWAY_ROOT_ID
}
function getPathPart(resourceObj) {
if (!resourceObj || !resourceObj.Properties) {
return undefined
}
return resourceObj.Properties.PathPart
}
function getParentId(resourceObj) {
if (!resourceObj || !resourceObj.Properties) {
return undefined
}
const parentIdObj = resourceObj.Properties.ParentId || {}
const { Ref } = parentIdObj
if (Ref) return Ref
const getAtt = parentIdObj['Fn::GetAtt'] || []
return getAtt[1]
}
function getFullPath(pathObjects, resourceId) {
let currentId = resourceId
let currentObj
const arrResourceObjects = []
while (currentId && !isRoot(currentId)) {
currentObj = pathObjects[currentId]
arrResourceObjects.push(currentObj)
currentId = getParentId(currentObj)
}
const arrPath = arrResourceObjects.map(getPathPart).reverse()
if (arrPath.some((s) => !s)) {
return undefined
}
return `/${arrPath.join('/')}`
}
/* Example of an HTTP Proxy Method Object
* "ApiGatewayResourcePublicAnyProxyMethod": {
* "Type": "AWS::ApiGateway::Method",
* "Properties": {
* "ResourceId": { "Ref": "ApiGatewayResourcePublicAny" },
* "RestApiId": { "Ref": "ApiGatewayRestApi" },
* "AuthorizationType": "NONE",
* "HttpMethod": "GET",
* "MethodResponses": [ { "StatusCode": 200 } ],
* "RequestParameters": { "method.request.path.proxy": true },
* "Integration": {
* "Type": "HTTP_PROXY",
* "IntegrationHttpMethod": "GET",
* "Uri": "https://www.example.com/us-west-2/{proxy}", // Note that ${self:provider.region} is expanded
* "IntegrationResponses": [ { "StatusCode": 200 } ],
* "RequestParameters": { "integration.request.path.proxy": "method.request.path.proxy" },
* "PassthroughBehavior": "WHEN_NO_MATCH"
* }
* }
* }
*/
/* Method Helpers */
function getResourceId(methodObj) {
if (!methodObj || !methodObj.Properties) {
return undefined
}
if (!methodObj.Properties.ResourceId) {
return undefined
}
return methodObj.Properties.ResourceId.Ref
}
function getHttpMethod(methodObj) {
if (!methodObj || !methodObj.Properties) {
return undefined
}
return methodObj.Properties.HttpMethod
}
function getIntegrationObj(methodObj) {
if (!methodObj || !methodObj.Properties) {
return undefined
}
return methodObj.Properties.Integration
}
function templatePathToHapiPath(path) {
return path.replace('+', '')
}
function constructHapiInterface(pathObjects, methodObjects, methodId) {
// returns all info necessary so that routes can be added in index.js
const methodObj = methodObjects[methodId]
const resourceId = getResourceId(methodObj)
const Integration = getIntegrationObj(methodObj) || {}
const pathResource = getFullPath(pathObjects, resourceId)
const method = getHttpMethod(methodObj)
// let integrationType;
let proxyUri
if (!pathResource) {
return {}
}
const path = templatePathToHapiPath(pathResource)
if (Integration.Type === APIGATEWAY_INTEGRATION_TYPE_HTTP_PROXY) {
proxyUri = Integration.Uri
}
return {
isProxy: !!proxyUri,
method,
path,
pathResource,
proxyUri,
}
}
export default function parseResources(resources) {
const { methodObjects, pathObjects } = getApiGatewayTemplateObjects(resources)
return fromEntries(
keys(methodObjects).map((key) => [
key,
constructHapiInterface(pathObjects, methodObjects, key),
]),
)
}