@suss/contract-terraform 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -3
- package/dist/index.cjs +541 -34
- package/dist/index.d.cts +122 -5
- package/dist/index.d.ts +122 -5
- package/dist/index.js +538 -34
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -5,42 +5,398 @@ import hcl2 from "hcl2-parser";
|
|
|
5
5
|
import semver from "semver";
|
|
6
6
|
import {
|
|
7
7
|
messageBusBinding,
|
|
8
|
+
metricBinding,
|
|
8
9
|
namePatternFromSub,
|
|
9
10
|
storageBinding
|
|
10
11
|
} from "@suss/behavioral-ir";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
|
|
13
|
+
// src/filterQuery.ts
|
|
14
|
+
var OPERATORS = ["!=", ">=", "<=", "=", ">", "<", ":"];
|
|
15
|
+
var KEYWORDS = /* @__PURE__ */ new Set(["AND", "OR", "NOT"]);
|
|
16
|
+
function filterValuesFor(query, key) {
|
|
17
|
+
return filterTerms(query).filter((term) => term.key === key && term.operator === "=").map((term) => term.value);
|
|
18
|
+
}
|
|
19
|
+
function filterTerms(query) {
|
|
20
|
+
if (query.type === "term") {
|
|
21
|
+
return [query];
|
|
22
|
+
}
|
|
23
|
+
if (query.type === "call") {
|
|
17
24
|
return [];
|
|
18
25
|
}
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
if (query.type === "negation") {
|
|
27
|
+
return filterTerms(query.operand);
|
|
28
|
+
}
|
|
29
|
+
return query.operands.flatMap(filterTerms);
|
|
30
|
+
}
|
|
31
|
+
function filterCalls(query) {
|
|
32
|
+
if (query.type === "call") {
|
|
33
|
+
return [query];
|
|
34
|
+
}
|
|
35
|
+
if (query.type === "term") {
|
|
21
36
|
return [];
|
|
22
37
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
if (query.type === "negation") {
|
|
39
|
+
return filterCalls(query.operand);
|
|
40
|
+
}
|
|
41
|
+
return query.operands.flatMap(filterCalls);
|
|
42
|
+
}
|
|
43
|
+
function parseFilterQuery(source) {
|
|
44
|
+
const tokens = tokenize(source);
|
|
45
|
+
if (!tokens.ok) {
|
|
46
|
+
return { ok: false, reason: tokens.reason };
|
|
47
|
+
}
|
|
48
|
+
const reader = new TokenReader(tokens.tokens);
|
|
49
|
+
const query = reader.readExpression();
|
|
50
|
+
if (query === null) {
|
|
51
|
+
return { ok: false, reason: reader.reason };
|
|
52
|
+
}
|
|
53
|
+
if (!reader.atEnd()) {
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
reason: `nothing joins the term at "${reader.rest()}"`
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return { ok: true, query };
|
|
60
|
+
}
|
|
61
|
+
function tokenize(source) {
|
|
62
|
+
const tokens = [];
|
|
63
|
+
let at = 0;
|
|
64
|
+
while (at < source.length) {
|
|
65
|
+
const char = source[at];
|
|
66
|
+
if (/\s/.test(char) || char === ",") {
|
|
67
|
+
at += 1;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (char === "(" || char === ")") {
|
|
71
|
+
tokens.push({ type: char === "(" ? "open" : "close", text: char });
|
|
72
|
+
at += 1;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (char === '"' || char === "'") {
|
|
76
|
+
const quoted = readQuoted(source, at, char);
|
|
77
|
+
if (quoted === null) {
|
|
78
|
+
return { ok: false, reason: `a quote is never closed at ${at}` };
|
|
30
79
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
const previous = tokens[tokens.length - 1];
|
|
81
|
+
if (previous !== void 0 && isPartialKey(previous)) {
|
|
82
|
+
previous.text += quoted.value;
|
|
83
|
+
} else {
|
|
84
|
+
tokens.push({ type: "string", text: quoted.value });
|
|
85
|
+
}
|
|
86
|
+
at = quoted.next;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const operator = OPERATORS.find(
|
|
90
|
+
(candidate) => source.startsWith(candidate, at)
|
|
91
|
+
);
|
|
92
|
+
if (operator !== void 0) {
|
|
93
|
+
tokens.push({ type: "operator", text: operator });
|
|
94
|
+
at += operator.length;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const word = readWord(source, at);
|
|
98
|
+
if (word === null) {
|
|
99
|
+
return { ok: false, reason: `nothing readable at "${char}"` };
|
|
34
100
|
}
|
|
101
|
+
tokens.push({ type: "word", text: word.value });
|
|
102
|
+
at = word.next;
|
|
35
103
|
}
|
|
36
|
-
return
|
|
104
|
+
return { ok: true, tokens };
|
|
105
|
+
}
|
|
106
|
+
function readQuoted(source, start, quote) {
|
|
107
|
+
let value = "";
|
|
108
|
+
let at = start + 1;
|
|
109
|
+
while (at < source.length) {
|
|
110
|
+
const char = source[at];
|
|
111
|
+
if (char === "\\" && at + 1 < source.length) {
|
|
112
|
+
value += source[at + 1];
|
|
113
|
+
at += 2;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (char === quote) {
|
|
117
|
+
return { value, next: at + 1 };
|
|
118
|
+
}
|
|
119
|
+
value += char;
|
|
120
|
+
at += 1;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function isPartialKey(token) {
|
|
125
|
+
return token.type === "word" && token.text.endsWith(".");
|
|
126
|
+
}
|
|
127
|
+
function readWord(source, start) {
|
|
128
|
+
const match = /^[^\s(),="'!<>:]+/.exec(source.slice(start));
|
|
129
|
+
if (match === null) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
return { value: match[0], next: start + match[0].length };
|
|
133
|
+
}
|
|
134
|
+
var TokenReader = class {
|
|
135
|
+
constructor(tokens) {
|
|
136
|
+
this.tokens = tokens;
|
|
137
|
+
}
|
|
138
|
+
tokens;
|
|
139
|
+
at = 0;
|
|
140
|
+
/** What stopped the last read, for the caller to report. */
|
|
141
|
+
reason = "the filter states no comparison";
|
|
142
|
+
atEnd() {
|
|
143
|
+
return this.at >= this.tokens.length;
|
|
144
|
+
}
|
|
145
|
+
rest() {
|
|
146
|
+
return this.tokens.slice(this.at).map((token) => token.text).join(" ");
|
|
147
|
+
}
|
|
148
|
+
readExpression() {
|
|
149
|
+
return this.readJunction(
|
|
150
|
+
"or",
|
|
151
|
+
"OR",
|
|
152
|
+
() => this.readJunction("and", "AND", () => this.readUnary())
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
readJunction(operator, keyword, readOperand) {
|
|
156
|
+
const first = readOperand();
|
|
157
|
+
if (first === null) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
const operands = [first];
|
|
161
|
+
while (!this.atEnd()) {
|
|
162
|
+
const joined = this.keywordAhead(keyword);
|
|
163
|
+
if (!joined && !(operator === "and" && this.termAhead())) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
if (joined) {
|
|
167
|
+
this.at += 1;
|
|
168
|
+
}
|
|
169
|
+
const next = readOperand();
|
|
170
|
+
if (next === null) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
operands.push(next);
|
|
174
|
+
}
|
|
175
|
+
return operands.length === 1 ? operands[0] : { type: "junction", operator, operands };
|
|
176
|
+
}
|
|
177
|
+
readUnary() {
|
|
178
|
+
if (this.keywordAhead("NOT")) {
|
|
179
|
+
this.at += 1;
|
|
180
|
+
const operand = this.readUnary();
|
|
181
|
+
return operand === null ? null : { type: "negation", operand };
|
|
182
|
+
}
|
|
183
|
+
return this.readPrimary();
|
|
184
|
+
}
|
|
185
|
+
readPrimary() {
|
|
186
|
+
const token = this.tokens[this.at];
|
|
187
|
+
if (token === void 0) {
|
|
188
|
+
this.reason = "the filter ends where a comparison should be";
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
if (token.type === "open") {
|
|
192
|
+
this.at += 1;
|
|
193
|
+
const inner = this.readExpression();
|
|
194
|
+
if (inner === null) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
if (this.tokens[this.at]?.type !== "close") {
|
|
198
|
+
this.reason = "a parenthesis is never closed";
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
this.at += 1;
|
|
202
|
+
return inner;
|
|
203
|
+
}
|
|
204
|
+
if (token.type === "word" && this.tokens[this.at + 1]?.type === "open") {
|
|
205
|
+
return this.readCall();
|
|
206
|
+
}
|
|
207
|
+
return this.readTerm();
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* A call where a comparison would be. Cloud Monitoring writes an SLO
|
|
211
|
+
* burn-rate condition that way, and the call says what the condition
|
|
212
|
+
* is about instead of any comparison.
|
|
213
|
+
*/
|
|
214
|
+
readCall() {
|
|
215
|
+
const name = this.tokens[this.at];
|
|
216
|
+
this.at += 2;
|
|
217
|
+
const args = [];
|
|
218
|
+
while (!this.atEnd() && this.tokens[this.at]?.type !== "close") {
|
|
219
|
+
const argument = this.tokens[this.at];
|
|
220
|
+
if (argument.type === "word" || argument.type === "string") {
|
|
221
|
+
args.push(argument.text);
|
|
222
|
+
}
|
|
223
|
+
this.at += 1;
|
|
224
|
+
}
|
|
225
|
+
if (this.atEnd()) {
|
|
226
|
+
this.reason = `the call to "${name.text}" is never closed`;
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
this.at += 1;
|
|
230
|
+
return { type: "call", name: name.text, arguments: args };
|
|
231
|
+
}
|
|
232
|
+
readTerm() {
|
|
233
|
+
const key = this.tokens[this.at];
|
|
234
|
+
const operator = this.tokens[this.at + 1];
|
|
235
|
+
const value = this.tokens[this.at + 2];
|
|
236
|
+
if (key === void 0 || key.type !== "word") {
|
|
237
|
+
this.reason = `"${key?.text ?? ""}" is not a key`;
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
if (operator === void 0 || operator.type !== "operator") {
|
|
241
|
+
this.reason = `"${key.text}" is compared to nothing`;
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
if (value === void 0 || value.type !== "word" && value.type !== "string") {
|
|
245
|
+
this.reason = `"${key.text}" is compared to no value`;
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
this.at += 3;
|
|
249
|
+
return {
|
|
250
|
+
type: "term",
|
|
251
|
+
key: key.text,
|
|
252
|
+
operator: operator.text,
|
|
253
|
+
value: value.text
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** Whether the next token is this keyword rather than a key. */
|
|
257
|
+
keywordAhead(keyword) {
|
|
258
|
+
const token = this.tokens[this.at];
|
|
259
|
+
return token !== void 0 && token.type === "word" && token.text === keyword;
|
|
260
|
+
}
|
|
261
|
+
/** Whether a new comparison starts here, with no keyword joining it. */
|
|
262
|
+
termAhead() {
|
|
263
|
+
const token = this.tokens[this.at];
|
|
264
|
+
if (token === void 0) {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
if (token.type === "open") {
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
return token.type === "word" && !KEYWORDS.has(token.text);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
// src/references.ts
|
|
275
|
+
var SUB_TOKEN = /\$\{([^}]*)\}/g;
|
|
276
|
+
var RESOURCE_ATTRIBUTE = /^([A-Za-z][\w-]*)\.([A-Za-z_][\w-]*)\.([A-Za-z_][\w-]*)$/;
|
|
277
|
+
var LOCAL_VALUE = /^local\.([A-Za-z_][\w-]*)$/;
|
|
278
|
+
var LOCALS = "local";
|
|
279
|
+
var CHAIN_LIMIT = 4;
|
|
280
|
+
function referenceScope(resources, locals = []) {
|
|
281
|
+
const scope = /* @__PURE__ */ new Map();
|
|
282
|
+
for (const [resourceType, label, body] of resources) {
|
|
283
|
+
const address = `${resourceType}.${label}`;
|
|
284
|
+
if (!scope.has(address)) {
|
|
285
|
+
scope.set(address, body);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const stated = {};
|
|
289
|
+
for (const block of locals) {
|
|
290
|
+
for (const [name, value] of Object.entries(block)) {
|
|
291
|
+
if (!(name in stated)) {
|
|
292
|
+
stated[name] = value;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
scope.set(LOCALS, stated);
|
|
297
|
+
return scope;
|
|
298
|
+
}
|
|
299
|
+
function resolveReferences(value, scope) {
|
|
300
|
+
return expand(value, scope, []) ?? value;
|
|
301
|
+
}
|
|
302
|
+
function expand(value, scope, resolving) {
|
|
303
|
+
let cycled = false;
|
|
304
|
+
const expanded = value.replace(SUB_TOKEN, (written, inner) => {
|
|
305
|
+
const reference = inner.trim();
|
|
306
|
+
if (resolving.includes(reference)) {
|
|
307
|
+
cycled = true;
|
|
308
|
+
return written;
|
|
309
|
+
}
|
|
310
|
+
const stated = statedValue(reference, scope);
|
|
311
|
+
if (stated === null || resolving.length >= CHAIN_LIMIT) {
|
|
312
|
+
return written;
|
|
313
|
+
}
|
|
314
|
+
const nested = expand(stated, scope, [...resolving, reference]);
|
|
315
|
+
if (nested === null) {
|
|
316
|
+
cycled = true;
|
|
317
|
+
return written;
|
|
318
|
+
}
|
|
319
|
+
return nested;
|
|
320
|
+
});
|
|
321
|
+
return cycled ? null : expanded;
|
|
322
|
+
}
|
|
323
|
+
function statedValue(reference, scope) {
|
|
324
|
+
const local = LOCAL_VALUE.exec(reference);
|
|
325
|
+
if (local !== null) {
|
|
326
|
+
const stated2 = scope.get(LOCALS)?.[local[1]];
|
|
327
|
+
return typeof stated2 === "string" ? stated2 : null;
|
|
328
|
+
}
|
|
329
|
+
const parsed = RESOURCE_ATTRIBUTE.exec(reference);
|
|
330
|
+
if (parsed === null) {
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
const [, resourceType, label, attribute] = parsed;
|
|
334
|
+
const stated = scope.get(`${resourceType}.${label}`)?.[attribute];
|
|
335
|
+
return typeof stated === "string" ? stated : null;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// src/index.ts
|
|
339
|
+
function terraformToSummaries(source, sourceFile, options) {
|
|
340
|
+
return summariesForFiles([{ source, sourceFile }], options);
|
|
37
341
|
}
|
|
38
342
|
function terraformFileToSummaries(target, options) {
|
|
39
343
|
const files = fs.statSync(target).isDirectory() ? fs.readdirSync(target).filter((name) => name.endsWith(".tf")).map((name) => path.join(target, name)) : [target];
|
|
40
|
-
return
|
|
41
|
-
(file) =>
|
|
344
|
+
return summariesForFiles(
|
|
345
|
+
files.map((file) => ({
|
|
346
|
+
source: fs.readFileSync(file, "utf8"),
|
|
347
|
+
sourceFile: file
|
|
348
|
+
})),
|
|
349
|
+
options
|
|
42
350
|
);
|
|
43
351
|
}
|
|
352
|
+
function summariesForFiles(files, options) {
|
|
353
|
+
const parsed = files.map((file) => parseSource(file)).filter((file) => file !== null);
|
|
354
|
+
const scope = referenceScope(
|
|
355
|
+
parsed.flatMap((file) => file.resources),
|
|
356
|
+
parsed.flatMap((file) => file.locals)
|
|
357
|
+
);
|
|
358
|
+
const summaries = [];
|
|
359
|
+
for (const file of parsed) {
|
|
360
|
+
for (const [resourceType, label, body] of file.resources) {
|
|
361
|
+
for (const pack of options.packs) {
|
|
362
|
+
const pattern = patternFor(pack, resourceType, file.constraints);
|
|
363
|
+
if (pattern === void 0) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
summaries.push(
|
|
367
|
+
...summariesFor({
|
|
368
|
+
pattern,
|
|
369
|
+
label,
|
|
370
|
+
body,
|
|
371
|
+
sourceFile: file.sourceFile,
|
|
372
|
+
resourceType,
|
|
373
|
+
scope
|
|
374
|
+
})
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return summaries;
|
|
380
|
+
}
|
|
381
|
+
function parseSource(file) {
|
|
382
|
+
let read;
|
|
383
|
+
try {
|
|
384
|
+
const [parsed] = hcl2.parseToObject(file.source);
|
|
385
|
+
read = parsed;
|
|
386
|
+
} catch {
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
const document = asRecord(read);
|
|
390
|
+
if (document === null) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
return {
|
|
394
|
+
sourceFile: file.sourceFile,
|
|
395
|
+
constraints: providerConstraints(document),
|
|
396
|
+
resources: resourcesIn(document),
|
|
397
|
+
locals: localsIn(document)
|
|
398
|
+
};
|
|
399
|
+
}
|
|
44
400
|
function providerConstraints(document) {
|
|
45
401
|
const constraints = /* @__PURE__ */ new Map();
|
|
46
402
|
for (const block of arrayOf(document.terraform)) {
|
|
@@ -79,22 +435,26 @@ function patternFor(pack, resourceType, constraints) {
|
|
|
79
435
|
}
|
|
80
436
|
});
|
|
81
437
|
}
|
|
438
|
+
var READERS = {
|
|
439
|
+
storage: (site, boundary) => {
|
|
440
|
+
const types = fieldTypes(site.body, boundary);
|
|
441
|
+
return keyedShapes(site.body, boundary).map(
|
|
442
|
+
(shape) => storageSummary({ ...site, boundary, shape, types })
|
|
443
|
+
);
|
|
444
|
+
},
|
|
445
|
+
"message-bus": (site, boundary) => [busSummary({ ...site, boundary })],
|
|
446
|
+
metric: (site, boundary) => [metricSummary({ ...site, boundary })],
|
|
447
|
+
"metric-reading": (site, boundary) => readingSummaries({ ...site, boundary })
|
|
448
|
+
};
|
|
82
449
|
function summariesFor(opts) {
|
|
83
|
-
const { pattern } = opts;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
const boundary = pattern.boundary;
|
|
88
|
-
const types = fieldTypes(opts.body, boundary);
|
|
89
|
-
return keyedShapes(opts.body, boundary).map(
|
|
90
|
-
(shape) => storageSummary({ ...opts, boundary, shape, types })
|
|
91
|
-
);
|
|
450
|
+
const { pattern, ...site } = opts;
|
|
451
|
+
const read = READERS[pattern.boundary.kind];
|
|
452
|
+
return read(site, pattern.boundary);
|
|
92
453
|
}
|
|
93
454
|
function storageSummary(opts) {
|
|
94
455
|
const { boundary, label, shape, types } = opts;
|
|
95
456
|
const address = `${opts.resourceType}.${label}`;
|
|
96
|
-
const
|
|
97
|
-
const physicalTable = declaredName === null ? null : namePatternFromSub(declaredName);
|
|
457
|
+
const physicalTable = boundary.nameAttribute === void 0 ? null : namePattern(opts.body[boundary.nameAttribute], opts.scope);
|
|
98
458
|
return {
|
|
99
459
|
kind: "library",
|
|
100
460
|
location: {
|
|
@@ -138,8 +498,7 @@ function storageSummary(opts) {
|
|
|
138
498
|
}
|
|
139
499
|
function busSummary(opts) {
|
|
140
500
|
const { boundary, label } = opts;
|
|
141
|
-
const
|
|
142
|
-
const physicalName = declaredName === null ? null : namePatternFromSub(declaredName);
|
|
501
|
+
const physicalName = boundary.nameAttribute === void 0 ? null : namePattern(opts.body[boundary.nameAttribute], opts.scope);
|
|
143
502
|
return {
|
|
144
503
|
kind: "library",
|
|
145
504
|
location: {
|
|
@@ -169,6 +528,132 @@ function busSummary(opts) {
|
|
|
169
528
|
}
|
|
170
529
|
};
|
|
171
530
|
}
|
|
531
|
+
var NAME_HOLE = "{name}";
|
|
532
|
+
function metricSummary(opts) {
|
|
533
|
+
const { boundary } = opts;
|
|
534
|
+
const declaredName = namePattern(
|
|
535
|
+
opts.body[boundary.nameAttribute],
|
|
536
|
+
opts.scope
|
|
537
|
+
);
|
|
538
|
+
const metricType = declaredName === null ? null : boundary.metricTypeTemplate.replace(NAME_HOLE, declaredName);
|
|
539
|
+
return {
|
|
540
|
+
kind: "library",
|
|
541
|
+
location: {
|
|
542
|
+
file: opts.sourceFile,
|
|
543
|
+
range: { start: 1, end: 1 },
|
|
544
|
+
exportName: null
|
|
545
|
+
},
|
|
546
|
+
identity: {
|
|
547
|
+
name: `${opts.resourceType}.${opts.label}`,
|
|
548
|
+
exportPath: null,
|
|
549
|
+
boundaryBinding: metricBinding({
|
|
550
|
+
recognition: "terraform",
|
|
551
|
+
metricSystem: boundary.metricSystem,
|
|
552
|
+
metricType
|
|
553
|
+
})
|
|
554
|
+
},
|
|
555
|
+
inputs: [],
|
|
556
|
+
transitions: [],
|
|
557
|
+
gaps: [],
|
|
558
|
+
confidence: { source: "declared", level: "high" },
|
|
559
|
+
metadata: { metricContract: metricContract(opts.body, boundary) }
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
function metricContract(body, boundary) {
|
|
563
|
+
const values = meaningOf(body, boundary.values);
|
|
564
|
+
const accumulates = meaningOf(body, boundary.accumulates);
|
|
565
|
+
return {
|
|
566
|
+
...values !== void 0 ? { values } : {},
|
|
567
|
+
...accumulates !== void 0 ? { accumulates } : {}
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
function readingSummaries(opts) {
|
|
571
|
+
const { boundary } = opts;
|
|
572
|
+
const summaries = [];
|
|
573
|
+
for (const reading of blocksAt(opts.body, boundary.readingBlocks)) {
|
|
574
|
+
const query = stringOf(valueAt(reading, boundary.queryAttribute));
|
|
575
|
+
for (const metricType of metricTypesIn(
|
|
576
|
+
query,
|
|
577
|
+
boundary.queryIdentityKey,
|
|
578
|
+
opts.scope
|
|
579
|
+
)) {
|
|
580
|
+
summaries.push({
|
|
581
|
+
kind: "consumer",
|
|
582
|
+
location: {
|
|
583
|
+
file: opts.sourceFile,
|
|
584
|
+
range: { start: 1, end: 1 },
|
|
585
|
+
exportName: null
|
|
586
|
+
},
|
|
587
|
+
identity: {
|
|
588
|
+
name: `${opts.resourceType}.${opts.label}#${summaries.length}`,
|
|
589
|
+
exportPath: null,
|
|
590
|
+
boundaryBinding: metricBinding({
|
|
591
|
+
recognition: "terraform",
|
|
592
|
+
metricSystem: boundary.metricSystem,
|
|
593
|
+
metricType
|
|
594
|
+
})
|
|
595
|
+
},
|
|
596
|
+
inputs: [],
|
|
597
|
+
transitions: [],
|
|
598
|
+
gaps: [],
|
|
599
|
+
confidence: { source: "declared", level: "high" },
|
|
600
|
+
metadata: { metricReading: metricReading(reading, boundary) }
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return summaries;
|
|
605
|
+
}
|
|
606
|
+
function metricTypesIn(query, identityKey, scope) {
|
|
607
|
+
if (query === null) {
|
|
608
|
+
return [null];
|
|
609
|
+
}
|
|
610
|
+
const parsed = parseFilterQuery(query);
|
|
611
|
+
if (!parsed.ok) {
|
|
612
|
+
return [null];
|
|
613
|
+
}
|
|
614
|
+
const found = filterValuesFor(parsed.query, identityKey).map((value) => namePattern(value, scope)).filter((value) => value !== null);
|
|
615
|
+
return found.length === 0 ? [null] : found;
|
|
616
|
+
}
|
|
617
|
+
function metricReading(reading, boundary) {
|
|
618
|
+
const compares = boundary.comparesTo;
|
|
619
|
+
const comparesTo = compares !== void 0 && valueAt(reading, compares.attribute) !== void 0 ? compares.whenSet : void 0;
|
|
620
|
+
const reduces = boundary.reducesTo;
|
|
621
|
+
const reducesTo = meaningOf(reading, reduces);
|
|
622
|
+
return {
|
|
623
|
+
...comparesTo !== void 0 ? { comparesTo } : {},
|
|
624
|
+
...reducesTo !== void 0 ? { reducesTo } : {},
|
|
625
|
+
...reduces === void 0 ? {} : { reduction: { setting: reduces.attribute, leaves: reduces.means } }
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
function meaningOf(body, spec) {
|
|
629
|
+
if (spec === void 0) {
|
|
630
|
+
return void 0;
|
|
631
|
+
}
|
|
632
|
+
const stated = stringOf(valueAt(body, spec.attribute));
|
|
633
|
+
return stated === null ? void 0 : spec.means[stated];
|
|
634
|
+
}
|
|
635
|
+
function valueAt(body, path2) {
|
|
636
|
+
const steps = path2.split(".");
|
|
637
|
+
const last = steps.pop();
|
|
638
|
+
let current = body;
|
|
639
|
+
for (const step of steps) {
|
|
640
|
+
const nested = arrayOf(current[step])[0];
|
|
641
|
+
current = asRecord(nested);
|
|
642
|
+
if (current === null) {
|
|
643
|
+
return void 0;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return current[last];
|
|
647
|
+
}
|
|
648
|
+
function blocksAt(body, blocks) {
|
|
649
|
+
let found = [body];
|
|
650
|
+
for (const block of blocks) {
|
|
651
|
+
found = found.flatMap(
|
|
652
|
+
(record) => arrayOf(record[block]).map(asRecord).filter((nested) => nested !== null)
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
return found;
|
|
656
|
+
}
|
|
172
657
|
function keyedShapes(body, boundary) {
|
|
173
658
|
if (boundary.identifies === void 0) {
|
|
174
659
|
return [{ accessPath: null, keyFields: [], serves: null }];
|
|
@@ -233,6 +718,16 @@ function fieldTypes(body, boundary) {
|
|
|
233
718
|
}
|
|
234
719
|
return types;
|
|
235
720
|
}
|
|
721
|
+
function localsIn(document) {
|
|
722
|
+
const found = [];
|
|
723
|
+
for (const block of arrayOf(document.locals)) {
|
|
724
|
+
const stated = asRecord(block);
|
|
725
|
+
if (stated !== null) {
|
|
726
|
+
found.push(stated);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return found;
|
|
730
|
+
}
|
|
236
731
|
function resourcesIn(document) {
|
|
237
732
|
const found = [];
|
|
238
733
|
for (const resource of arrayOf(document.resource)) {
|
|
@@ -259,6 +754,11 @@ function resourcesIn(document) {
|
|
|
259
754
|
}
|
|
260
755
|
return found;
|
|
261
756
|
}
|
|
757
|
+
function namePattern(value, scope) {
|
|
758
|
+
return namePatternFromSub(
|
|
759
|
+
typeof value === "string" ? resolveReferences(value, scope) : value
|
|
760
|
+
);
|
|
761
|
+
}
|
|
262
762
|
function arrayOf(value) {
|
|
263
763
|
if (value === void 0 || value === null) {
|
|
264
764
|
return [];
|
|
@@ -272,6 +772,10 @@ function stringOf(value) {
|
|
|
272
772
|
return typeof value === "string" ? value : null;
|
|
273
773
|
}
|
|
274
774
|
export {
|
|
775
|
+
filterCalls,
|
|
776
|
+
filterTerms,
|
|
777
|
+
filterValuesFor,
|
|
778
|
+
parseFilterQuery,
|
|
275
779
|
terraformFileToSummaries,
|
|
276
780
|
terraformToSummaries
|
|
277
781
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@suss/contract-terraform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Read the storage a Terraform configuration declares, for checking code against it.",
|
|
6
6
|
"keywords": [
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"typecheck": "tsc --noEmit"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@suss/behavioral-ir": "0.
|
|
48
|
+
"@suss/behavioral-ir": "0.9.0",
|
|
49
49
|
"hcl2-parser": "^1.0.3",
|
|
50
50
|
"semver": "^7.6.0"
|
|
51
51
|
},
|