@mark1russell7/stack-tofu 0.0.0-stage → 0.1.3
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/LICENSE +21 -0
- package/README.md +421 -2
- package/build/add-on.d.ts +18 -0
- package/build/add-on.d.ts.map +1 -0
- package/build/add-on.js +28 -0
- package/build/add-on.js.map +1 -0
- package/build/check-COlnfceW.d.ts +154 -0
- package/build/check-COlnfceW.d.ts.map +1 -0
- package/build/index.d.ts +171 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +297 -0
- package/build/index.js.map +1 -0
- package/build/join-1KKMeraY.js +661 -0
- package/build/join-1KKMeraY.js.map +1 -0
- package/build/layer-C9IpaDIx.js +51 -0
- package/build/layer-C9IpaDIx.js.map +1 -0
- package/build/layer-MFHgwWfO.d.ts +118 -0
- package/build/layer-MFHgwWfO.d.ts.map +1 -0
- package/build/plan-B4mHDZjK.d.ts +68 -0
- package/build/plan-B4mHDZjK.d.ts.map +1 -0
- package/build/schema.d.ts +40 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +126 -0
- package/build/schema.js.map +1 -0
- package/package.json +77 -3
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import { a as providerAddress, c as resourceProvider, i as isBuiltin, l as typeProvider, n as TOFU_SCHEMA, o as resourceAddress, s as resourceName, t as BUILTIN_PROVIDER } from "./layer-C9IpaDIx.js";
|
|
2
|
+
import { stronglyConnectedComponents } from "@mark1russell7/collections/graph";
|
|
3
|
+
import { joinRecords, joinSetting, partNeeds, partsById } from "@mark1russell7/system-stack";
|
|
4
|
+
import { app, axes, each, evaluate, lit, ref, space } from "@mark1russell7/vex";
|
|
5
|
+
import { BoolDomain, NumDomain, TextDomain } from "@mark1russell7/vex-domains";
|
|
6
|
+
// src/values.ts
|
|
7
|
+
/** The form of the type of a resource and of the type of a backend. */
|
|
8
|
+
const TYPE_FORM = /^[a-z][a-z0-9_]*$/;
|
|
9
|
+
/** The form of the local name of a provider, for example `example` or `example-beta`. */
|
|
10
|
+
const PROVIDER_FORM = /^[a-z][a-z0-9_-]*$/;
|
|
11
|
+
/** The form of a name in the files: the name of a resource, of a variable, of an alias and of an argument. */
|
|
12
|
+
const NAME_FORM = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
13
|
+
/** The form of a path of attributes, for example `network[0].address`. */
|
|
14
|
+
const PATH_FORM = /^[A-Za-z_][A-Za-z0-9_-]*(\.[A-Za-z_][A-Za-z0-9_-]*|\[[0-9]+\])*$/;
|
|
15
|
+
/** The form of the address of a provider in a registry: two or three words with `/` between them. */
|
|
16
|
+
const SOURCE_FORM = /^[a-z0-9][a-z0-9.-]*(\/[a-z0-9][a-z0-9_-]*){1,2}$/;
|
|
17
|
+
/** The form of a list of versions, for example `>= 1.8, < 2.0`. */
|
|
18
|
+
const VERSION_FORM = /^[0-9A-Za-z .,<>=~!+-]+$/;
|
|
19
|
+
/**
|
|
20
|
+
* This function gives the parts of the text of a reference, for example `example_server.web.address`.
|
|
21
|
+
* It gives `undefined` for a text with a different form.
|
|
22
|
+
*/
|
|
23
|
+
function refParts(ref) {
|
|
24
|
+
const [type, name, ...rest] = ref.split(".");
|
|
25
|
+
if (type === void 0 || name === void 0 || rest.length === 0) return void 0;
|
|
26
|
+
const attribute = rest.join(".");
|
|
27
|
+
if (!TYPE_FORM.test(type) || !NAME_FORM.test(name) || !PATH_FORM.test(attribute)) return void 0;
|
|
28
|
+
return {
|
|
29
|
+
address: `${type}.${name}`,
|
|
30
|
+
attribute
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** This function tells if a value is a list. */
|
|
34
|
+
function isList(value) {
|
|
35
|
+
return Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
/** This function tells if a value is an object of values. A list and `null` are not objects. */
|
|
38
|
+
function isObject(value) {
|
|
39
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
40
|
+
}
|
|
41
|
+
/** This function tells if an object has a key that starts with `$`: the object is a form, not a map of arguments. */
|
|
42
|
+
function isForm(value) {
|
|
43
|
+
return Object.keys(value).some((key) => key.startsWith("$"));
|
|
44
|
+
}
|
|
45
|
+
/** This function gives the one value of a form with the one key `key`, when that value is a text. */
|
|
46
|
+
function formText(value, key) {
|
|
47
|
+
const keys = Object.keys(value);
|
|
48
|
+
const text = value[key];
|
|
49
|
+
return keys.length === 1 && keys[0] === key && typeof text === "string" ? text : void 0;
|
|
50
|
+
}
|
|
51
|
+
/** This function gives the text of a correct reference form, `{ "$ref": "type.name.attribute" }`. */
|
|
52
|
+
function refOf(value) {
|
|
53
|
+
const text = formText(value, "$ref");
|
|
54
|
+
return text !== void 0 && refParts(text) !== void 0 ? text : void 0;
|
|
55
|
+
}
|
|
56
|
+
/** This function gives the name of a correct variable form, `{ "$var": "name" }`. */
|
|
57
|
+
function varOf(value) {
|
|
58
|
+
const text = formText(value, "$var");
|
|
59
|
+
return text !== void 0 && NAME_FORM.test(text) ? text : void 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* This function escapes the template sequences of a text. OpenTofu then reads the text as it is in the record.
|
|
63
|
+
* `$${` is the escape of `${`, and `%%{` is the escape of `%{`.
|
|
64
|
+
*/
|
|
65
|
+
function escapeTemplate(text) {
|
|
66
|
+
return text.replaceAll("${", () => "$${").replaceAll("%{", () => "%%{");
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* This function gives a value as OpenTofu reads it in a JSON file.
|
|
70
|
+
*
|
|
71
|
+
* - A text and each key of an object are escaped.
|
|
72
|
+
* - A reference form becomes the expression of the attribute, and a variable form the expression of the variable.
|
|
73
|
+
*
|
|
74
|
+
* A form that is not correct stays an object with an escaped key. The gate `checkTofu` refuses such a layer.
|
|
75
|
+
*/
|
|
76
|
+
function renderValue(value) {
|
|
77
|
+
if (typeof value === "string") return escapeTemplate(value);
|
|
78
|
+
if (isList(value)) return value.map(renderValue);
|
|
79
|
+
if (!isObject(value)) return value;
|
|
80
|
+
const ref = refOf(value);
|
|
81
|
+
if (ref !== void 0) return `\${${ref}}`;
|
|
82
|
+
const variable = varOf(value);
|
|
83
|
+
if (variable !== void 0) return `\${var.${variable}}`;
|
|
84
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [escapeTemplate(key), renderValue(item)]));
|
|
85
|
+
}
|
|
86
|
+
/** This function gives each form of a value and each number that is not finite, in the sequence of the value. */
|
|
87
|
+
function valueUses(value) {
|
|
88
|
+
const forms = [];
|
|
89
|
+
const numbers = [];
|
|
90
|
+
const walk = (item, path) => {
|
|
91
|
+
if (typeof item === "number") {
|
|
92
|
+
if (!Number.isFinite(item)) numbers.push({
|
|
93
|
+
path,
|
|
94
|
+
value: item
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (isList(item)) {
|
|
99
|
+
item.forEach((child, index) => {
|
|
100
|
+
walk(child, [...path, index]);
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (!isObject(item)) return;
|
|
105
|
+
if (isForm(item)) {
|
|
106
|
+
const ref = refOf(item);
|
|
107
|
+
const variable = varOf(item);
|
|
108
|
+
if (ref !== void 0) forms.push({
|
|
109
|
+
kind: "ref",
|
|
110
|
+
path,
|
|
111
|
+
text: ref
|
|
112
|
+
});
|
|
113
|
+
else if (variable !== void 0) forms.push({
|
|
114
|
+
kind: "var",
|
|
115
|
+
path,
|
|
116
|
+
text: variable
|
|
117
|
+
});
|
|
118
|
+
else forms.push({
|
|
119
|
+
kind: "bad",
|
|
120
|
+
path
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
for (const [key, child] of Object.entries(item)) walk(child, [...path, key]);
|
|
125
|
+
};
|
|
126
|
+
walk(value, []);
|
|
127
|
+
return {
|
|
128
|
+
forms,
|
|
129
|
+
numbers
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** This function gives a place in a value as a text, for example `disk.0.size`. The place of the value is `(the value)`. */
|
|
133
|
+
function pathText(path) {
|
|
134
|
+
return path.length === 0 ? "(the value)" : path.join(".");
|
|
135
|
+
}
|
|
136
|
+
// src/queries.ts
|
|
137
|
+
/** This function gives the first resource of each address of a layer, in the sequence of the records. */
|
|
138
|
+
function firstResources(layer) {
|
|
139
|
+
const seen = /* @__PURE__ */ new Set();
|
|
140
|
+
return layer.resources.filter((resource) => {
|
|
141
|
+
const address = resourceAddress(resource);
|
|
142
|
+
if (seen.has(address)) return false;
|
|
143
|
+
seen.add(address);
|
|
144
|
+
return true;
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* This function gives the addresses of the resources of each part, by the `id` of the part.
|
|
149
|
+
* The addresses of a part are sorted. With them, a host joins a plan or a state to the parts of the stack.
|
|
150
|
+
*/
|
|
151
|
+
function resourcesByPart(layer) {
|
|
152
|
+
const byPart = /* @__PURE__ */ new Map();
|
|
153
|
+
for (const resource of firstResources(layer)) {
|
|
154
|
+
const addresses = byPart.get(resource.part) ?? [];
|
|
155
|
+
addresses.push(resourceAddress(resource));
|
|
156
|
+
byPart.set(resource.part, addresses);
|
|
157
|
+
}
|
|
158
|
+
for (const addresses of byPart.values()) addresses.sort();
|
|
159
|
+
return byPart;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* This function gives the address of each resource that the arguments of a resource read, sorted.
|
|
163
|
+
* Each address comes one time. An address can name a resource that the layer does not have.
|
|
164
|
+
*/
|
|
165
|
+
function resourceRefs(resource) {
|
|
166
|
+
const addresses = /* @__PURE__ */ new Set();
|
|
167
|
+
for (const form of valueUses(resource.values ?? {}).forms) {
|
|
168
|
+
const parts = form.kind === "ref" && form.text !== void 0 && form.path.length > 0 ? refParts(form.text) : void 0;
|
|
169
|
+
if (parts !== void 0) addresses.add(parts.address);
|
|
170
|
+
}
|
|
171
|
+
return [...addresses].sort();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* This function walks the needs of a part, nearest first. It gives the `id` of each part where the walk stops or goes on.
|
|
175
|
+
* The walk does not go past a part for which `stop` gives `true`. It does not come back to the first part.
|
|
176
|
+
*/
|
|
177
|
+
function walkNeeds(parts, from, stop) {
|
|
178
|
+
const seen = /* @__PURE__ */ new Set([from]);
|
|
179
|
+
const found = [];
|
|
180
|
+
let level = partNeeds(parts.get(from) ?? {
|
|
181
|
+
id: from,
|
|
182
|
+
kind: ""
|
|
183
|
+
});
|
|
184
|
+
while (level.length > 0) {
|
|
185
|
+
const next = [];
|
|
186
|
+
for (const id of level) {
|
|
187
|
+
if (seen.has(id)) continue;
|
|
188
|
+
seen.add(id);
|
|
189
|
+
found.push(id);
|
|
190
|
+
const part = parts.get(id);
|
|
191
|
+
if (part !== void 0 && !stop(id)) next.push(...partNeeds(part));
|
|
192
|
+
}
|
|
193
|
+
level = next;
|
|
194
|
+
}
|
|
195
|
+
return found;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* This function gives the `id` of each part that a part needs, directly or through other parts.
|
|
199
|
+
* The part itself is not in the result. A need that names no part of the stack is in the result.
|
|
200
|
+
*/
|
|
201
|
+
function neededParts(stack, part) {
|
|
202
|
+
return new Set(walkNeeds(partsById(stack), part, () => false));
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* This function gives the dependencies of each resource that come from the needs of the stack, by the address of the resource.
|
|
206
|
+
*
|
|
207
|
+
* - A resource depends on the resources of each part that its part needs.
|
|
208
|
+
* - For a needed part with no resource, the walk goes on to the needs of that part. It stops at the nearest parts with resources.
|
|
209
|
+
* - An address that the arguments of the resource read is not in the list: OpenTofu knows that dependence from the arguments.
|
|
210
|
+
*
|
|
211
|
+
* The addresses of a resource are sorted. The generator writes them as `depends_on`.
|
|
212
|
+
*/
|
|
213
|
+
function dependsOn(layer, stack) {
|
|
214
|
+
const parts = partsById(stack);
|
|
215
|
+
const byPart = resourcesByPart(layer);
|
|
216
|
+
const result = /* @__PURE__ */ new Map();
|
|
217
|
+
for (const resource of firstResources(layer)) {
|
|
218
|
+
const read = new Set(resourceRefs(resource));
|
|
219
|
+
const addresses = walkNeeds(parts, resource.part, (id) => byPart.has(id)).flatMap((id) => byPart.get(id) ?? []);
|
|
220
|
+
result.set(resourceAddress(resource), addresses.filter((address) => !read.has(address)).sort());
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* This function gives the cycles of the resources of a layer. In a cycle, each resource waits for a different one, and OpenTofu can make none of them.
|
|
226
|
+
* A resource waits for each resource that its arguments read, and for each one of `dependsOn`.
|
|
227
|
+
* Each cycle is a sorted list of addresses. A resource that reads its own attribute is a cycle of one resource.
|
|
228
|
+
*/
|
|
229
|
+
function resourceCycles(layer, stack) {
|
|
230
|
+
const depends = dependsOn(layer, stack);
|
|
231
|
+
const waits = /* @__PURE__ */ new Map();
|
|
232
|
+
for (const resource of firstResources(layer)) {
|
|
233
|
+
const address = resourceAddress(resource);
|
|
234
|
+
waits.set(address, [...resourceRefs(resource), ...depends.get(address) ?? []]);
|
|
235
|
+
}
|
|
236
|
+
const nodes = [...waits.keys()].sort();
|
|
237
|
+
return stronglyConnectedComponents(nodes, (address) => waits.get(address) ?? []).filter((component) => {
|
|
238
|
+
const [only = ""] = component;
|
|
239
|
+
return component.length > 1 || (waits.get(only) ?? []).includes(only);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
// src/cost.ts
|
|
243
|
+
/** The key of the layer node: the root of the cost tree. */
|
|
244
|
+
const COST_LAYER_KEY = "layer";
|
|
245
|
+
/** This function gives the key of the node of an account: the text `account:` and the `id` of its part. */
|
|
246
|
+
function accountKey(part) {
|
|
247
|
+
return `account:${part}`;
|
|
248
|
+
}
|
|
249
|
+
/** This function gives the key of the node of a resource: the text `resource:` and its address. */
|
|
250
|
+
function resourceKey(address) {
|
|
251
|
+
return `resource:${address}`;
|
|
252
|
+
}
|
|
253
|
+
/** This function tells if a value is a finite number, 0 or more. */
|
|
254
|
+
function isZeroOrMore(value) {
|
|
255
|
+
return Number.isFinite(value) && value >= 0;
|
|
256
|
+
}
|
|
257
|
+
/** This function gives the first budget of each part, by the `id` of the part. */
|
|
258
|
+
function budgetsByPart(budgets) {
|
|
259
|
+
const byPart = /* @__PURE__ */ new Map();
|
|
260
|
+
for (const budget of budgets) if (!byPart.has(budget.part)) byPart.set(budget.part, budget);
|
|
261
|
+
return byPart;
|
|
262
|
+
}
|
|
263
|
+
/** The planned cost of each resource of an account. A resource with no known cost gives an error value. */
|
|
264
|
+
const COSTS = each(axes.children, ref("cost"));
|
|
265
|
+
/** The planned cost of an account in a month: the sum of the known costs of its resources. The program evaluates at an account node. */
|
|
266
|
+
const PLANNED = app("sum", COSTS);
|
|
267
|
+
/** This test tells if a resource node has a known cost. */
|
|
268
|
+
const HAS_COST = app("ifError", app("eq", ref("cost"), ref("cost")), lit(false));
|
|
269
|
+
/** The count of the resources of an account with no known cost. The program evaluates at an account node. */
|
|
270
|
+
const UNKNOWN_COSTS = app("count", each(axes.where(axes.children, app("not", HAS_COST)), ref("kind")));
|
|
271
|
+
/** This test tells if the planned cost of an account is more than its hard limit. An account with no hard limit gives an error value. */
|
|
272
|
+
const PLANNED_OVER = app("gt", PLANNED, ref("limit"));
|
|
273
|
+
/** The room of an account: its hard limit less its planned cost. An account with no hard limit gives an error value. */
|
|
274
|
+
const PLANNED_ROOM = app("subtract", ref("limit"), PLANNED);
|
|
275
|
+
/** The planned cost of the layer in a month: the sum of the planned costs of its accounts. The program evaluates at the layer node. */
|
|
276
|
+
const PLANNED_TOTAL = app("sum", each(axes.children, PLANNED));
|
|
277
|
+
/**
|
|
278
|
+
* This function gives the cost tree of a layer, as a space of Vex.
|
|
279
|
+
*
|
|
280
|
+
* - The layer is the root, with the key `COST_LAYER_KEY`.
|
|
281
|
+
* - An account is a child of the layer: the part of a provider configuration that has one resource or more. Its field `limit` is its hard limit.
|
|
282
|
+
* - A resource is a child of the account of its provider configuration. Its field `cost` is its planned cost.
|
|
283
|
+
*
|
|
284
|
+
* A cost or a limit that is less than 0 or not finite is not in the tree: it is not known.
|
|
285
|
+
* A resource with no provider configuration is not in the tree, because it counts against no account.
|
|
286
|
+
*/
|
|
287
|
+
function costSpace(layer, budgets = []) {
|
|
288
|
+
const limits = budgetsByPart(budgets);
|
|
289
|
+
const records = { [COST_LAYER_KEY]: { kind: "layer" } };
|
|
290
|
+
const parents = { [COST_LAYER_KEY]: null };
|
|
291
|
+
for (const resource of firstResources(layer)) {
|
|
292
|
+
const provider = isBuiltin(resource) ? void 0 : resourceProvider(layer, resource);
|
|
293
|
+
if (provider === void 0) continue;
|
|
294
|
+
const account = accountKey(provider.part);
|
|
295
|
+
if (records[account] === void 0) {
|
|
296
|
+
const limit = limits.get(provider.part)?.hardLimit;
|
|
297
|
+
records[account] = {
|
|
298
|
+
kind: "account",
|
|
299
|
+
part: provider.part,
|
|
300
|
+
...limit !== void 0 && isZeroOrMore(limit) ? { limit } : {}
|
|
301
|
+
};
|
|
302
|
+
parents[account] = COST_LAYER_KEY;
|
|
303
|
+
}
|
|
304
|
+
const key = resourceKey(resourceAddress(resource));
|
|
305
|
+
records[key] = {
|
|
306
|
+
kind: "resource",
|
|
307
|
+
...resource.cost !== void 0 && isZeroOrMore(resource.cost) ? { cost: resource.cost } : {}
|
|
308
|
+
};
|
|
309
|
+
parents[key] = account;
|
|
310
|
+
}
|
|
311
|
+
return space.tree(records, parents);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* This function evaluates a program of this module at a node of a cost tree. It gives the value, or the error value.
|
|
315
|
+
* `tree` is the result of `costSpace`, and `key` is the key of a node.
|
|
316
|
+
*/
|
|
317
|
+
function evaluateCost(program, tree, key) {
|
|
318
|
+
return evaluate(program, {
|
|
319
|
+
space: tree,
|
|
320
|
+
origin: key,
|
|
321
|
+
domains: [
|
|
322
|
+
NumDomain,
|
|
323
|
+
BoolDomain,
|
|
324
|
+
TextDomain
|
|
325
|
+
]
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
/** This function gives the number of a result, or `undefined` for an error value. */
|
|
329
|
+
function numberOf(result) {
|
|
330
|
+
return result.ok && typeof result.value === "number" ? result.value : void 0;
|
|
331
|
+
}
|
|
332
|
+
/** This function gives the state of the planned cost of each account of a layer, in the sequence of the first resource of each account. */
|
|
333
|
+
function accountStates(layer, budgets = []) {
|
|
334
|
+
const tree = costSpace(layer, budgets);
|
|
335
|
+
const accounts = /* @__PURE__ */ new Map();
|
|
336
|
+
for (const resource of firstResources(layer)) {
|
|
337
|
+
const provider = isBuiltin(resource) ? void 0 : resourceProvider(layer, resource);
|
|
338
|
+
if (provider === void 0) continue;
|
|
339
|
+
const addresses = accounts.get(provider.part) ?? [];
|
|
340
|
+
addresses.push(resourceAddress(resource));
|
|
341
|
+
accounts.set(provider.part, addresses);
|
|
342
|
+
}
|
|
343
|
+
return [...accounts].map(([part, addresses]) => {
|
|
344
|
+
const key = accountKey(part);
|
|
345
|
+
const limit = numberOf(evaluateCost(ref("limit"), tree, key));
|
|
346
|
+
const over = evaluateCost(PLANNED_OVER, tree, key);
|
|
347
|
+
const room = numberOf(evaluateCost(PLANNED_ROOM, tree, key));
|
|
348
|
+
return {
|
|
349
|
+
part,
|
|
350
|
+
resources: [...addresses].sort(),
|
|
351
|
+
planned: numberOf(evaluateCost(PLANNED, tree, key)) ?? 0,
|
|
352
|
+
unknown: numberOf(evaluateCost(UNKNOWN_COSTS, tree, key)) ?? 0,
|
|
353
|
+
...limit === void 0 ? {} : { limit },
|
|
354
|
+
...over.ok && typeof over.value === "boolean" ? { over: over.value } : {},
|
|
355
|
+
...room === void 0 ? {} : { room }
|
|
356
|
+
};
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/** This function gives the planned cost of a layer in a month: the sum of the planned costs of its accounts. */
|
|
360
|
+
function plannedTotal(layer) {
|
|
361
|
+
return numberOf(evaluateCost(PLANNED_TOTAL, costSpace(layer), "layer")) ?? 0;
|
|
362
|
+
}
|
|
363
|
+
// src/check.ts
|
|
364
|
+
/** These are the codes of the faults of an OpenTofu layer. */
|
|
365
|
+
const TOFU_CODES = [
|
|
366
|
+
"version",
|
|
367
|
+
"backend",
|
|
368
|
+
"variable-form",
|
|
369
|
+
"variable-duplicate",
|
|
370
|
+
"provider-form",
|
|
371
|
+
"provider-duplicate",
|
|
372
|
+
"provider-source",
|
|
373
|
+
"provider-part",
|
|
374
|
+
"provider-value",
|
|
375
|
+
"resource-form",
|
|
376
|
+
"resource-duplicate",
|
|
377
|
+
"resource-part",
|
|
378
|
+
"provider-none",
|
|
379
|
+
"ignore-form",
|
|
380
|
+
"value-name",
|
|
381
|
+
"value-meta",
|
|
382
|
+
"value-form",
|
|
383
|
+
"value-number",
|
|
384
|
+
"ref-none",
|
|
385
|
+
"var-none",
|
|
386
|
+
"cycle",
|
|
387
|
+
"cost-amount",
|
|
388
|
+
"cost-none",
|
|
389
|
+
"cost-builtin",
|
|
390
|
+
"budget-duplicate",
|
|
391
|
+
"budget-amount",
|
|
392
|
+
"budget-none",
|
|
393
|
+
"limit-none",
|
|
394
|
+
"cost-over",
|
|
395
|
+
"provider-need",
|
|
396
|
+
"variable-unused"
|
|
397
|
+
];
|
|
398
|
+
/** These arguments of a resource are meta-arguments. The generator writes them from the records, so the arguments of a record cannot have them. */
|
|
399
|
+
const RESOURCE_META = [
|
|
400
|
+
"provider",
|
|
401
|
+
"depends_on",
|
|
402
|
+
"lifecycle",
|
|
403
|
+
"count",
|
|
404
|
+
"for_each",
|
|
405
|
+
"provisioner",
|
|
406
|
+
"connection",
|
|
407
|
+
"dynamic"
|
|
408
|
+
];
|
|
409
|
+
/** These arguments of a provider configuration are meta-arguments. The generator writes them from the records. */
|
|
410
|
+
const PROVIDER_META = [
|
|
411
|
+
"alias",
|
|
412
|
+
"version",
|
|
413
|
+
"for_each"
|
|
414
|
+
];
|
|
415
|
+
/** This function gives a list of names for a message, for example `"a", "b"`. */
|
|
416
|
+
function names(items) {
|
|
417
|
+
return items.map((item) => `"${item}"`).join(", ");
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* This function gives each fault of an OpenTofu layer for a stack. A list with no error tells that the gate passes.
|
|
421
|
+
*
|
|
422
|
+
* The settings, the variables and the providers, as errors:
|
|
423
|
+
* - `version`: the versions of OpenTofu do not have the form of a list of versions.
|
|
424
|
+
* - `backend`: the type of the backend does not have the form of a type, or a value of the backend is not literal.
|
|
425
|
+
* - `variable-form`: the name of a variable does not have the form of a name.
|
|
426
|
+
* - `variable-duplicate`: two variables have one name.
|
|
427
|
+
* - `provider-form`: the name, the alias, the source or the versions of a provider configuration do not have their form.
|
|
428
|
+
* - `provider-duplicate`: two provider configurations have one address.
|
|
429
|
+
* - `provider-source`: two configurations of one provider have different sources or versions.
|
|
430
|
+
* - `provider-part`: a provider configuration names no part of the stack.
|
|
431
|
+
* - `provider-value`: a value of a provider configuration reads a resource.
|
|
432
|
+
*
|
|
433
|
+
* The resources, as errors:
|
|
434
|
+
* - `resource-form`: the type, the name or the import id of a resource does not have its form.
|
|
435
|
+
* - `resource-duplicate`: two resources have one address.
|
|
436
|
+
* - `resource-part`: a resource names no part of the stack.
|
|
437
|
+
* - `provider-none`: the layer does not have the provider configuration of a resource.
|
|
438
|
+
* - `ignore-form`: an attribute of `ignore` does not have the form of a path.
|
|
439
|
+
*
|
|
440
|
+
* The values, as errors:
|
|
441
|
+
* - `value-name`: the name of an argument does not have the form of a name.
|
|
442
|
+
* - `value-meta`: an argument is a meta-argument. The generator writes each meta-argument.
|
|
443
|
+
* - `value-form`: an object has a key that starts with `$`, and it is not a correct reference or variable.
|
|
444
|
+
* - `value-number`: a number is not finite. JSON cannot hold it.
|
|
445
|
+
* - `ref-none`: a reference names a resource that the layer does not have.
|
|
446
|
+
* - `var-none`: a value names a variable that the layer does not have.
|
|
447
|
+
*
|
|
448
|
+
* The queries, as errors:
|
|
449
|
+
* - `cycle`: resources wait for each other, so OpenTofu can make none of them.
|
|
450
|
+
*
|
|
451
|
+
* The costs, as errors:
|
|
452
|
+
* - `cost-amount`: the planned cost of a resource is less than 0 or not finite.
|
|
453
|
+
* - `cost-none`: a resource of a provider has no planned cost. A cost that is not known is not 0.
|
|
454
|
+
* - `cost-builtin`: a resource that is a part of OpenTofu has a planned cost above 0. It counts against no account.
|
|
455
|
+
* - `budget-duplicate`: two budgets name one part.
|
|
456
|
+
* - `budget-amount`: the hard limit of a budget is less than 0 or not finite.
|
|
457
|
+
* - `budget-none`: no budget names an account with resources. The gate has no read of its limit, so it does not pass.
|
|
458
|
+
* - `limit-none`: the resources of an account have a planned cost above 0, and the account has no hard limit.
|
|
459
|
+
* - `cost-over`: the planned cost of an account is more than its hard limit.
|
|
460
|
+
*
|
|
461
|
+
* The warnings:
|
|
462
|
+
* - `provider-need`: the part of a resource does not need the part of its provider configuration. The stack does not show that dependence.
|
|
463
|
+
* - `variable-unused`: no value reads a variable.
|
|
464
|
+
*
|
|
465
|
+
* The gate does not examine the stack: `checkStack` of the core does. The function is pure.
|
|
466
|
+
*/
|
|
467
|
+
function checkTofu(layer, stack, options = {}) {
|
|
468
|
+
const faults = [];
|
|
469
|
+
const add = (level, code, at, message, more = {}) => {
|
|
470
|
+
faults.push({
|
|
471
|
+
level,
|
|
472
|
+
code,
|
|
473
|
+
at,
|
|
474
|
+
message,
|
|
475
|
+
...more
|
|
476
|
+
});
|
|
477
|
+
};
|
|
478
|
+
const error = (code, at, message, more = {}) => {
|
|
479
|
+
add("error", code, at, message, more);
|
|
480
|
+
};
|
|
481
|
+
const money = (amount) => options.currency === void 0 ? String(amount) : `${String(amount)} ${options.currency}`;
|
|
482
|
+
const parts = partsById(stack);
|
|
483
|
+
const addresses = new Set(firstResources(layer).map(resourceAddress));
|
|
484
|
+
const variables = /* @__PURE__ */ new Set();
|
|
485
|
+
const readVariables = /* @__PURE__ */ new Set();
|
|
486
|
+
for (const variable of layer.variables ?? []) {
|
|
487
|
+
const at = `var.${variable.name}`;
|
|
488
|
+
if (variables.has(variable.name)) {
|
|
489
|
+
error("variable-duplicate", at, `Two variables have the name "${variable.name}".`);
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
variables.add(variable.name);
|
|
493
|
+
if (!NAME_FORM.test(variable.name)) error("variable-form", at, `The name "${variable.name}" of a variable does not have the form of a name.`);
|
|
494
|
+
}
|
|
495
|
+
/** This function examines the arguments of one record. `owner` is the record in words, for a message. */
|
|
496
|
+
const checkValues = (values, at, owner, kind, meta, part) => {
|
|
497
|
+
if (values === void 0) return;
|
|
498
|
+
const more = part === void 0 ? {} : { part };
|
|
499
|
+
if (isForm(values)) {
|
|
500
|
+
error("value-form", at, `The arguments of ${owner} have a key that starts with "$". The arguments of a record are not a reference and not a variable.`, more);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
for (const name of Object.keys(values)) if (meta.includes(name)) error("value-meta", at, `The argument "${name}" of ${owner} is a meta-argument. The generator writes it from the records.`, more);
|
|
504
|
+
else if (!NAME_FORM.test(name)) error("value-name", at, `The name "${name}" of an argument of ${owner} does not have the form of a name.`, more);
|
|
505
|
+
const uses = valueUses(values);
|
|
506
|
+
for (const number of uses.numbers) error("value-number", at, `The number ${String(number.value)} at "${pathText(number.path)}" of ${owner} is not finite.`, more);
|
|
507
|
+
for (const form of uses.forms) {
|
|
508
|
+
const place = pathText(form.path);
|
|
509
|
+
if (form.kind === "bad" || form.text === void 0) error("value-form", at, `The object at "${place}" of ${owner} has a key that starts with "$", and it is not a correct reference or variable.`, more);
|
|
510
|
+
else if (kind === "backend") error("backend", at, `The value at "${place}" of ${owner} is not literal. A backend reads no variable and no resource.`, more);
|
|
511
|
+
else if (form.kind === "var") {
|
|
512
|
+
if (variables.has(form.text)) readVariables.add(form.text);
|
|
513
|
+
else error("var-none", at, `The value at "${place}" of ${owner} names the variable "${form.text}", which the layer does not have.`, {
|
|
514
|
+
...more,
|
|
515
|
+
other: `var.${form.text}`
|
|
516
|
+
});
|
|
517
|
+
} else if (kind === "provider") error("provider-value", at, `The value at "${place}" of ${owner} reads the resource attribute "${form.text}". A provider configuration reads no resource.`, more);
|
|
518
|
+
else {
|
|
519
|
+
const address = refParts(form.text)?.address ?? "";
|
|
520
|
+
if (!addresses.has(address)) error("ref-none", at, `The value at "${place}" of ${owner} reads "${form.text}", and the layer has no resource "${address}".`, {
|
|
521
|
+
...more,
|
|
522
|
+
other: address
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
if (layer.version !== void 0 && !VERSION_FORM.test(layer.version)) error("version", "terraform", `The versions "${layer.version}" of OpenTofu do not have the form of a list of versions, for example ">= 1.8".`);
|
|
528
|
+
if (layer.backend !== void 0) {
|
|
529
|
+
if (!TYPE_FORM.test(layer.backend.type)) error("backend", "terraform", `The type "${layer.backend.type}" of the backend does not have the form of a type.`);
|
|
530
|
+
checkValues(layer.backend.values, "terraform", "the backend", "backend", []);
|
|
531
|
+
}
|
|
532
|
+
const providers = /* @__PURE__ */ new Set();
|
|
533
|
+
const firstOfName = /* @__PURE__ */ new Map();
|
|
534
|
+
for (const provider of layer.providers ?? []) {
|
|
535
|
+
const address = providerAddress(provider);
|
|
536
|
+
const at = `provider.${address}`;
|
|
537
|
+
const more = { part: provider.part };
|
|
538
|
+
if (providers.has(address)) {
|
|
539
|
+
error("provider-duplicate", at, `Two provider configurations have the address "${address}".`, more);
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
providers.add(address);
|
|
543
|
+
if (!PROVIDER_FORM.test(provider.name)) error("provider-form", at, `The name "${provider.name}" of a provider does not have the form of a provider name.`, more);
|
|
544
|
+
else if (provider.name === "terraform") error("provider-form", at, `The provider "${BUILTIN_PROVIDER}" is a part of OpenTofu. It has no configuration.`, more);
|
|
545
|
+
if (provider.alias !== void 0 && !NAME_FORM.test(provider.alias)) error("provider-form", at, `The alias "${provider.alias}" of the provider "${provider.name}" does not have the form of a name.`, more);
|
|
546
|
+
if (!SOURCE_FORM.test(provider.source)) error("provider-form", at, `The source "${provider.source}" of the provider "${provider.name}" does not have the form of a source, for example "example-org/example".`, more);
|
|
547
|
+
if (provider.version !== void 0 && !VERSION_FORM.test(provider.version)) error("provider-form", at, `The versions "${provider.version}" of the provider "${provider.name}" do not have the form of a list of versions, for example "~> 2.0".`, more);
|
|
548
|
+
const first = firstOfName.get(provider.name);
|
|
549
|
+
if (first === void 0) firstOfName.set(provider.name, provider);
|
|
550
|
+
else if (first.source !== provider.source || first.version !== provider.version) error("provider-source", at, `Two configurations of the provider "${provider.name}" have different sources or versions. A provider has one source and one list of versions.`, more);
|
|
551
|
+
if (!parts.has(provider.part)) error("provider-part", at, `The provider configuration "${address}" names "${provider.part}", which is no part of the stack.`, {
|
|
552
|
+
...more,
|
|
553
|
+
other: provider.part
|
|
554
|
+
});
|
|
555
|
+
checkValues(provider.values, at, `the provider configuration "${address}"`, "provider", PROVIDER_META, provider.part);
|
|
556
|
+
}
|
|
557
|
+
const seen = /* @__PURE__ */ new Set();
|
|
558
|
+
for (const resource of layer.resources) {
|
|
559
|
+
const at = resourceAddress(resource);
|
|
560
|
+
const more = { part: resource.part };
|
|
561
|
+
if (seen.has(at)) {
|
|
562
|
+
error("resource-duplicate", at, `Two resources have the address "${at}".`, more);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
seen.add(at);
|
|
566
|
+
if (!TYPE_FORM.test(resource.type)) error("resource-form", at, `The type "${resource.type}" of a resource of "${resource.part}" does not have the form of a type.`, more);
|
|
567
|
+
if (!NAME_FORM.test(resourceName(resource))) error("resource-form", at, `The name "${resourceName(resource)}" of a resource of "${resource.part}" does not have the form of a name.`, more);
|
|
568
|
+
if (resource.import === "") error("resource-form", at, `The import id of the resource "${at}" is empty.`, more);
|
|
569
|
+
if (!parts.has(resource.part)) error("resource-part", at, `The resource "${at}" names "${resource.part}", which is no part of the stack.`, {
|
|
570
|
+
...more,
|
|
571
|
+
other: resource.part
|
|
572
|
+
});
|
|
573
|
+
for (const path of resource.ignore ?? []) if (!PATH_FORM.test(path)) error("ignore-form", at, `The attribute "${path}" of the field "ignore" of the resource "${at}" does not have the form of a path.`, more);
|
|
574
|
+
checkValues(resource.values, at, `the resource "${at}"`, "resource", RESOURCE_META, resource.part);
|
|
575
|
+
const builtin = isBuiltin(resource);
|
|
576
|
+
const provider = builtin ? void 0 : resourceProvider(layer, resource);
|
|
577
|
+
if (!builtin && provider === void 0) error("provider-none", at, `The resource "${at}" uses the provider configuration "${resource.provider ?? typeProvider(resource.type)}", which the layer does not have.`, more);
|
|
578
|
+
if (resource.cost !== void 0 && !isZeroOrMore(resource.cost)) error("cost-amount", at, `The planned cost ${String(resource.cost)} of the resource "${at}" is less than 0 or not finite.`, more);
|
|
579
|
+
else if (builtin) {
|
|
580
|
+
if (resource.cost !== void 0 && resource.cost > 0) error("cost-builtin", at, `The resource "${at}" is a part of OpenTofu, and its record has a planned cost of ${money(resource.cost)}. It counts against no account.`, more);
|
|
581
|
+
} else if (resource.cost === void 0) error("cost-none", at, `The resource "${at}" has no planned cost. A cost that is not known is not 0: give the cost of a month, or 0 for a resource with no price.`, more);
|
|
582
|
+
if (provider !== void 0 && provider.part !== resource.part && parts.has(resource.part) && parts.has(provider.part) && !neededParts(stack, resource.part).has(provider.part)) add("warning", "provider-need", at, `The resource "${at}" makes the part "${resource.part}" with the provider configuration of "${provider.part}", and "${resource.part}" does not need "${provider.part}" in the stack.`, {
|
|
583
|
+
...more,
|
|
584
|
+
other: provider.part
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
for (const cycle of resourceCycles(layer, stack)) {
|
|
588
|
+
const [first = "", second] = cycle;
|
|
589
|
+
error("cycle", first, cycle.length === 1 ? `The resource "${first}" reads its own attribute, so OpenTofu cannot make it.` : `The resources ${names(cycle)} wait for each other, so OpenTofu can make none of them.`, second === void 0 ? {} : { other: second });
|
|
590
|
+
}
|
|
591
|
+
const budgets = options.budgets ?? [];
|
|
592
|
+
const budgetParts = /* @__PURE__ */ new Set();
|
|
593
|
+
const wrongBudgets = /* @__PURE__ */ new Set();
|
|
594
|
+
for (const budget of budgets) {
|
|
595
|
+
if (budgetParts.has(budget.part)) {
|
|
596
|
+
error("budget-duplicate", budget.part, `Two budgets name the part "${budget.part}".`, { part: budget.part });
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
budgetParts.add(budget.part);
|
|
600
|
+
if (budget.hardLimit !== void 0 && !isZeroOrMore(budget.hardLimit)) {
|
|
601
|
+
wrongBudgets.add(budget.part);
|
|
602
|
+
error("budget-amount", budget.part, `The hard limit ${String(budget.hardLimit)} of the budget of "${budget.part}" is less than 0 or not finite.`, { part: budget.part });
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
for (const state of accountStates(layer, budgets)) {
|
|
606
|
+
const at = state.part;
|
|
607
|
+
const more = { part: at };
|
|
608
|
+
if (!budgetParts.has(at)) error("budget-none", at, `The resources ${names(state.resources)} count against "${at}", and no budget names "${at}". The gate has no read of its limit.`, more);
|
|
609
|
+
else if (state.limit === void 0) {
|
|
610
|
+
if (state.planned > 0 && !wrongBudgets.has(at)) error("limit-none", at, `The planned cost of the resources of "${at}" is ${money(state.planned)} for a month, and "${at}" has no hard limit.`, more);
|
|
611
|
+
} else if (state.over === true) error("cost-over", at, `The planned cost of the resources of "${at}" is ${money(state.planned)} for a month. Its hard limit is ${money(state.limit)}.`, more);
|
|
612
|
+
}
|
|
613
|
+
for (const name of variables) if (!readVariables.has(name)) add("warning", "variable-unused", `var.${name}`, `No value reads the variable "${name}".`);
|
|
614
|
+
return faults;
|
|
615
|
+
}
|
|
616
|
+
// src/join.ts
|
|
617
|
+
/**
|
|
618
|
+
* This function joins the OpenTofu layers of the fragments of a stack into one layer.
|
|
619
|
+
*
|
|
620
|
+
* - The resources, the provider configurations and the variables are in the order of the fragments.
|
|
621
|
+
* - A resource address in two or more fragments is a fault, also when the resources are equal.
|
|
622
|
+
* - The same rule holds for the address of a provider configuration and for the name of a variable.
|
|
623
|
+
* - The layer has one version of OpenTofu. A fragment with a different value is a fault.
|
|
624
|
+
* - The state has one place. Thus a backend in two or more fragments is a fault, also when the backends are equal.
|
|
625
|
+
*
|
|
626
|
+
* A resource of one fragment can use a provider configuration or a variable of a different fragment.
|
|
627
|
+
* The joined layer has the field `providers` or `variables` when a fragment has it. The function is pure, and it
|
|
628
|
+
* changes no layer. The gate `checkTofu` examines the joined layer afterward.
|
|
629
|
+
*
|
|
630
|
+
* @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
|
|
631
|
+
* @returns The joined layer, or the faults of the join.
|
|
632
|
+
*/
|
|
633
|
+
function joinTofu(pieces) {
|
|
634
|
+
const version = joinSetting(pieces, (layer) => layer.version, "version of OpenTofu");
|
|
635
|
+
const backends = pieces.filter((piece) => piece.layer.backend !== void 0);
|
|
636
|
+
const providers = joinRecords(pieces, (layer) => layer.providers, providerAddress, "provider configuration");
|
|
637
|
+
const variables = joinRecords(pieces, (layer) => layer.variables, (variable) => variable.name, "variable");
|
|
638
|
+
const resources = joinRecords(pieces, (layer) => layer.resources, resourceAddress, "resource");
|
|
639
|
+
const errors = [...version.errors];
|
|
640
|
+
if (backends.length > 1) errors.push(`The fragments ${backends.map((piece) => `"${piece.from}"`).join(", ")} each have a backend. The state has one place.`);
|
|
641
|
+
errors.push(...providers.errors, ...variables.errors, ...resources.errors);
|
|
642
|
+
if (errors.length > 0) return {
|
|
643
|
+
ok: false,
|
|
644
|
+
errors
|
|
645
|
+
};
|
|
646
|
+
const backend = backends[0]?.layer.backend;
|
|
647
|
+
return {
|
|
648
|
+
ok: true,
|
|
649
|
+
layer: {
|
|
650
|
+
schema: TOFU_SCHEMA,
|
|
651
|
+
...version.value === void 0 ? {} : { version: version.value },
|
|
652
|
+
...backend === void 0 ? {} : { backend },
|
|
653
|
+
...pieces.some((piece) => piece.layer.providers !== void 0) ? { providers: providers.items } : {},
|
|
654
|
+
...pieces.some((piece) => piece.layer.variables !== void 0) ? { variables: variables.items } : {},
|
|
655
|
+
resources: resources.items
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
export { TYPE_FORM as A, valueUses as B, resourceCycles as C, PATH_FORM as D, NAME_FORM as E, isObject as F, pathText as I, refOf as L, escapeTemplate as M, isForm as N, PROVIDER_FORM as O, isList as P, refParts as R, neededParts as S, resourcesByPart as T, varOf as V, isZeroOrMore as _, checkTofu as a, dependsOn as b, PLANNED_OVER as c, UNKNOWN_COSTS as d, accountKey as f, evaluateCost as g, costSpace as h, TOFU_CODES as i, VERSION_FORM as j, SOURCE_FORM as k, PLANNED_ROOM as l, budgetsByPart as m, PROVIDER_META as n, COST_LAYER_KEY as o, accountStates as p, RESOURCE_META as r, PLANNED as s, joinTofu as t, PLANNED_TOTAL as u, plannedTotal as v, resourceRefs as w, firstResources as x, resourceKey as y, renderValue as z };
|
|
660
|
+
|
|
661
|
+
//# sourceMappingURL=join-1KKMeraY.js.map
|