-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfn-inline-1.0.js
executable file
·298 lines (262 loc) · 10.5 KB
/
cfn-inline-1.0.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env node
// Copyright (c) 2021, Viasat, Inc
// License under MPL 2.0
'use strict'
//// vvv snip from here vvv ////
// Call f on every node of obj. Depth first unless bf is
// true. The arguments to f depend on the element type:
// - array element: f(val, idx, arr)
// - object key: f(key)
// - object value: f(val, key, keyIdx, keyList)
function walk(f, obj, bf=false) {
let x
if (!obj || typeof(obj) !== 'object') { return obj }
return bf
? Array.isArray(obj)
? obj.map((v, i, arr) => f(v, i, arr))
.map((v) => walk(f, v, bf))
: Object.keys(obj)
.map((k) => f(k))
.map((k, i, arr) => [k, f(obj[k], k, i, arr)])
.reduce((a, [k, v]) => (a[k] = walk(f, v, bf), a), {})
: Array.isArray(obj)
? obj.map((v, i, arr) => f(walk(f, v, bf), i, arr))
: Object.keys(obj).reduce((a, k, i, arr) =>
(x = f(walk(f, obj[k], bf), k, i, arr), a[f(k)] = x, a), {})
}
// Merge using f(v1, v2) for keys that occur in both maps
function mergeWith(f, a, b) {
return Object.entries(b).reduce((m, [k, v]) =>
(m[k] = k in m ? f(m[k], v) : v, m), Object.assign({}, a))
}
// Deep merge two maps
function deepMerge(a, b) {
return (a && a.constructor === Object) ? mergeWith(deepMerge, a, b) : b
}
// If obj contains 'Fn::Macro', invoke the macros using
// the current fragment and substitute resulting fragment
// Fn::Macro can contain one call (map) or multiple (list)
function doMacro(ns, event, context, obj) {
while (obj && typeof(obj) === 'object' && 'Fn::Macro' in obj) {
const {['Fn::Macro']: mc, ...rest} = obj // split out calls
let res = walk(v => v, rest) // deep copy
obj = (Array.isArray(mc) ? mc : [mc])
.reduce((res, call) =>
ns[call.Name](Object.assign({}, event, {
params: call.Parameters || {},
fragment: res
}), context),
res)
}
return obj
}
// If obj contains 'Fn::Function', call the first argument
// as a function using the remaining arguments.
function doFunction(ns, obj) {
if (obj && typeof(obj) === 'object' && 'Fn::Function' in obj) {
const [fname, ...fargs] = obj['Fn::Function']
return ns[fname](...fargs)
}
return obj
}
exports.handler = function(event, context, callback) {
console.log('event:', JSON.stringify(event))
try {
let frag = event.fragment
if (!('AWSTemplateFormatVersion' in frag)) {
throw new Error('AWSTemplateFormatVersion missing')
}
const initDef = frag.Metadata && frag.Metadata.JSInit
const macroDefs = frag.Metadata && frag.Metadata.JSMacros || {}
// Expose utility functions in global scope
Object.assign(global, {walk, mergeWith, deepMerge})
let ns = {} // user macro and function definitions
// Run initDef with exports set to ns
if (initDef) {
const initFn = new Function('event', 'context', 'exports', initDef)
initFn(event, context, ns)
}
// Instantiate macros in ns
Object.entries(macroDefs).forEach(([k, v]) =>
ns[k] = new Function('event', 'context', v))
// Do breadth first macro invocation (outside in). This
// is unlike standard CFN macros which are depth first,
// but more similar to Lisp macros.
frag = walk(doMacro.bind(null, ns, event, context),
[frag], true)[0]
// Do depth first invocation of functions
frag = walk(doFunction.bind(null, ns), [frag], false)[0]
// Response object with new fragment
console.log('new fragment:', JSON.stringify(frag))
callback(null, { requestId: event.requestId,
status: 'success',
fragment: frag })
} catch (e) {
console.error('caught error:', e)
callback(e)
}
}
//// ^^^ snip to here ^^^ ////
//// vvv Local testing only vvv ///
const assert = require('assert')
const { readFileSync } = require('fs')
const { yamlParse, yamlDump } = require('yaml-cfn')
const mode = process.argv[2]
const VERBOSE = process.env['VERBOSE']
/////////////////////////////////
function die(code, ...args) { console.error(...args); process.exit(code) }
function load(tPath, callback) {
const baseEvt = {
region: 'us-west-2',
accountId: 'some-account-id',
transformId: 'some-tx-id',
requestId: 'some-request-id',
params: {},
templateParameterValues: {}
}
const fragment = yamlParse(readFileSync(tPath, 'utf8'))
let tParams = {}
for (const [k, v] of Object.entries(fragment.Parameters || {})) {
const val = process.env[k] || v.Default
if ((!val) && val !== "") { die(2, `Parameter ${k} is required`) }
if (v.Type === 'CommaDelimitedList') {
tParams[k] = val ? val.split(/ *, */) : []
} else {
tParams[k] = val
}
}
const event = Object.assign({}, baseEvt,
{fragment, templateParameterValues: tParams})
exports.handler(event, {}, function (err, resp) {
if (err) {
callback(err, resp)
} else {
for (let k of ['JSInit', 'JSMacros', 'PyInit', 'PyMacros']) {
delete resp.fragment.Metadata[k]
}
callback(err, resp)
}
})
}
function loadTest(tPath, cPath) {
console.warn(`\nTESTING: expand of ${tPath} == ${cPath}`)
load(tPath, function (err, resp) {
if (err) {
console.warn('ERROR:', err)
console.warn("FAILURE")
process.exit(1)
}
let expected = yamlParse(readFileSync(cPath, 'utf8'))
let respFragment = Object.assign({}, resp.fragment)
// Remove stuff we don't want to check/show
delete respFragment.Metadata
let ignorer = v => typeof v === 'string' && v.startsWith('Ignore: ')
? 'IGNORED'
: v
expected = walk(ignorer, expected)
respFragment = walk(ignorer, respFragment)
try {
assert.deepEqual(respFragment, expected)
console.warn(`MATCH: expanded ${tPath} == ${cPath}`)
} catch (e) {
console.warn(`MISMATCH: expanded ${tPath} != ${cPath}`)
console.warn(JSON.stringify(respFragment))
console.warn(JSON.stringify(expected))
console.warn("FAILURE")
process.exit(1)
}
console.warn("SUCCESS")
})
}
if (!VERBOSE) {
console.log = (...a) => true
}
let res, log, frag, m1, m2
switch (mode) {
case 'load':
load(process.argv[3], function(err, resp) {
if (err) { process.exit(1) }
console.warn(yamlDump(resp.fragment))
})
break
case 'compare':
loadTest(process.argv[3], process.argv[4])
break
case 'test':
//
// walk tests
//
frag = {
region: 'us-west-2',
templateParameterValues: [
{abc: {def: 'ghi'}},
{jkl: {mno: 'pqr'}} ] }
log = []
console.warn("TESTING walk depth first")
console.warn("FRAG:", JSON.stringify(frag))
res = walk((v, ...a) => (log.push(v), v), frag, false)
console.warn("RES: ", JSON.stringify(res))
console.log("LOG:", JSON.stringify(log))
assert.deepEqual(frag, res)
assert.equal(log.length, 14)
assert.deepEqual(log, ["us-west-2","region","ghi","def",{"def":"ghi"},"abc",{"abc":{"def":"ghi"}},"pqr","mno",{"mno":"pqr"},"jkl",{"jkl":{"mno":"pqr"}},[{"abc":{"def":"ghi"}},{"jkl":{"mno":"pqr"}}],"templateParameterValues"])
console.warn("SUCCESS")
log = []
console.warn("\nTESTING walk breadth first")
console.warn("FRAG:", JSON.stringify(frag))
res = walk((v, ...a) => (log.push(v), v), frag, true)
console.warn("RES: ", JSON.stringify(res))
console.log("LOG:", JSON.stringify(log))
assert.deepEqual(frag, res)
assert.equal(log.length, 14)
assert.deepEqual(log, ["region","templateParameterValues","us-west-2",[{"abc":{"def":"ghi"}},{"jkl":{"mno":"pqr"}}],{"abc":{"def":"ghi"}},{"jkl":{"mno":"pqr"}},"abc",{"def":"ghi"},"def","ghi","jkl",{"mno":"pqr"},"mno","pqr"])
console.warn("SUCCESS")
frag = {
"Type": "Number",
"Value": {
"Fn::Function": [ "Add", 2, {
"Fn::Function": [ "Mult", 3, 4 ] } ] } }
log = []
console.warn("\nTESTING walk depth first")
console.warn("FRAG:", JSON.stringify(frag))
res = walk((v, ...a) => (log.push(v), v), frag, false)
console.warn("RES: ", JSON.stringify(res))
console.log("LOG:", JSON.stringify(log))
assert.deepEqual(frag, res)
assert.equal(log.length, 14)
assert.deepEqual(log, ["Number","Type","Add",2,"Mult",3,4,["Mult",3,4],"Fn::Function",{"Fn::Function":["Mult",3,4]},["Add",2,{"Fn::Function":["Mult",3,4]}],"Fn::Function",{"Fn::Function":["Add",2,{"Fn::Function":["Mult",3,4]}]},"Value"])
console.warn("SUCCESS")
//
// Merge tests
//
m1 = {a: 1, b: 2}
m2 = {a: 9, b: 98, c: 0}
console.warn("\nTESTING mergeWith add:", JSON.stringify(m1), JSON.stringify(m2))
res = mergeWith((a,b) => a+b, m1, m2)
console.warn("RES:", JSON.stringify(res))
assert.deepEqual(res, { a: 10, b: 100, c: 0 })
console.warn("SUCCESS")
m1 = {a: {b: 3, c: 4}}
m2 = {a: {b: 5}}
console.warn("\nTESTING deepMerge:", JSON.stringify(m1), JSON.stringify(m2))
res = deepMerge(m1, m2)
console.warn("RES:", JSON.stringify(res))
assert.deepEqual(res, { a: { b: 5, c: 4 } })
console.warn("SUCCESS")
//
// Before and After Compare tests
//
const tests = [['tests/t1.yaml', 'tests/t1-result.yaml'],
['tests/t2.yaml', 'tests/t2-result.yaml'],
['tests/t3.yaml', 'tests/t3-result.yaml'],
['tests/t4.yaml', 'tests/t4-result.yaml'],
['tests/t5.yaml', 'tests/t5-result.yaml']]
for (let [tPath, cPath] of tests) {
loadTest(tPath, cPath)
}
break
default:
console.error(`Unknown mode ${mode}`)
process.exit(1)
}
//// ^^^ Local testing only ^^^ ////