@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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"check-COlnfceW.d.ts","names":[],"sources":["../src/cost.ts","../src/check.ts"],"mappings":";;;;;;;UAYiB;;WAEN;;WAEA;;;cAIE;;iBAGG,WAAW;;iBAKX,YAAY;;iBAKZ,aAAa;;iBAKb,cAAc,kBAAkB,WAAW,YAAY;;cAU1D,SAAS;;cAMT,eAAe;;cAGf,cAAc;;cAGd,cAAc;;cAGd,eAAe;;;;;;;;;;;iBAYZ,UAAU,OAAO,WAAW,mBAAkB,WAAgB;;;;;iBAwB9D,aAAa,SAAS,MAAM,MAAM,OAAO,cAAc;;UAUtD;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;iBAIK,cAAc,OAAO,WAAW,mBAAkB,WAAgB;;iBA4BlE,aAAa,OAAO;;cCjJvB;;KAmED,mBAAmB;;;;;;;;;;;UAYd,kBAAkB;WACxB,MAAM;;WAEN;;;UAIM;;;;;WAKN,mBAAmB;;WAEnB;;;cAIE;;cAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuDG,UAAU,OAAO,WAAW,OAAO,OAAO,UAAS,cAAmB"}
@@ -0,0 +1,171 @@
1
+ import { _ as typeProvider, a as TOFU_SCHEMA, c as TofuValue, d as VariableType, f as isBuiltin, g as resourceProvider, h as resourceName, i as Resource, l as VARIABLE_TYPES, m as resourceAddress, n as Backend, o as TofuLayer, p as providerAddress, r as Provider, s as TofuObject, t as BUILTIN_PROVIDER, u as Variable } from "./layer-MFHgwWfO.js";
2
+ import { C as resourceKey, S as plannedTotal, _ as accountStates, a as TofuFault, b as evaluateCost, c as AccountState, d as PLANNED, f as PLANNED_OVER, g as accountKey, h as UNKNOWN_COSTS, i as TofuCode, l as Budget, m as PLANNED_TOTAL, n as RESOURCE_META, o as TofuOptions, p as PLANNED_ROOM, r as TOFU_CODES, s as checkTofu, t as PROVIDER_META, u as COST_LAYER_KEY, v as budgetsByPart, x as isZeroOrMore, y as costSpace } from "./check-COlnfceW.js";
3
+ import { a as PlanOptions, i as PlanFault, n as PlanChange, o as TofuPlan, r as PlanCode, s as checkPlan, t as PLAN_CODES } from "./plan-B4mHDZjK.js";
4
+ import { Fault, LayerParse, LayerPiece, Stack } from "@mark1russell7/system-stack";
5
+ /** The form of the type of a resource and of the type of a backend. */
6
+ export declare const TYPE_FORM: RegExp;
7
+ /** The form of the local name of a provider, for example `example` or `example-beta`. */
8
+ export declare const PROVIDER_FORM: RegExp;
9
+ /** The form of a name in the files: the name of a resource, of a variable, of an alias and of an argument. */
10
+ export declare const NAME_FORM: RegExp;
11
+ /** The form of a path of attributes, for example `network[0].address`. */
12
+ export declare const PATH_FORM: RegExp;
13
+ /** The form of the address of a provider in a registry: two or three words with `/` between them. */
14
+ export declare const SOURCE_FORM: RegExp;
15
+ /** The form of a list of versions, for example `>= 1.8, < 2.0`. */
16
+ export declare const VERSION_FORM: RegExp;
17
+ /** The parts of a reference: the address of a resource, and a path of attributes of it. */
18
+ export interface RefParts {
19
+ readonly address: string;
20
+ readonly attribute: string;
21
+ }
22
+ /**
23
+ * This function gives the parts of the text of a reference, for example `example_server.web.address`.
24
+ * It gives `undefined` for a text with a different form.
25
+ */
26
+ export declare function refParts(ref: string): RefParts | undefined;
27
+ /** This function tells if a value is a list. */
28
+ export declare function isList(value: TofuValue): value is readonly TofuValue[];
29
+ /** This function tells if a value is an object of values. A list and `null` are not objects. */
30
+ export declare function isObject(value: TofuValue): value is TofuObject;
31
+ /** This function tells if an object has a key that starts with `$`: the object is a form, not a map of arguments. */
32
+ export declare function isForm(value: TofuObject): boolean;
33
+ /** This function gives the text of a correct reference form, `{ "$ref": "type.name.attribute" }`. */
34
+ export declare function refOf(value: TofuObject): string | undefined;
35
+ /** This function gives the name of a correct variable form, `{ "$var": "name" }`. */
36
+ export declare function varOf(value: TofuObject): string | undefined;
37
+ /**
38
+ * This function escapes the template sequences of a text. OpenTofu then reads the text as it is in the record.
39
+ * `$${` is the escape of `${`, and `%%{` is the escape of `%{`.
40
+ */
41
+ export declare function escapeTemplate(text: string): string;
42
+ /**
43
+ * This function gives a value as OpenTofu reads it in a JSON file.
44
+ *
45
+ * - A text and each key of an object are escaped.
46
+ * - A reference form becomes the expression of the attribute, and a variable form the expression of the variable.
47
+ *
48
+ * A form that is not correct stays an object with an escaped key. The gate `checkTofu` refuses such a layer.
49
+ */
50
+ export declare function renderValue(value: TofuValue): TofuValue;
51
+ /** A place in a value: the keys and the positions from the value to the place. */
52
+ export type ValuePath = readonly (string | number)[];
53
+ /** A form of a value, with its place. `text` is absent when the form is not correct. */
54
+ export interface FormUse {
55
+ readonly kind: "ref" | "var" | "bad";
56
+ readonly path: ValuePath;
57
+ readonly text?: string | undefined;
58
+ }
59
+ /** One number of a value that is not finite, with its place. */
60
+ export interface NumberUse {
61
+ readonly path: ValuePath;
62
+ readonly value: number;
63
+ }
64
+ /** The forms and the numbers that are not finite of a value. */
65
+ export interface ValueUses {
66
+ readonly forms: readonly FormUse[];
67
+ readonly numbers: readonly NumberUse[];
68
+ }
69
+ /** This function gives each form of a value and each number that is not finite, in the sequence of the value. */
70
+ export declare function valueUses(value: TofuValue): ValueUses;
71
+ /** This function gives a place in a value as a text, for example `disk.0.size`. The place of the value is `(the value)`. */
72
+ export declare function pathText(path: ValuePath): string;
73
+ /** This function gives the first resource of each address of a layer, in the sequence of the records. */
74
+ export declare function firstResources(layer: TofuLayer): readonly Resource[];
75
+ /**
76
+ * This function gives the addresses of the resources of each part, by the `id` of the part.
77
+ * The addresses of a part are sorted. With them, a host joins a plan or a state to the parts of the stack.
78
+ */
79
+ export declare function resourcesByPart(layer: TofuLayer): Map<string, string[]>;
80
+ /**
81
+ * This function gives the address of each resource that the arguments of a resource read, sorted.
82
+ * Each address comes one time. An address can name a resource that the layer does not have.
83
+ */
84
+ export declare function resourceRefs(resource: Resource): string[];
85
+ /**
86
+ * This function gives the `id` of each part that a part needs, directly or through other parts.
87
+ * The part itself is not in the result. A need that names no part of the stack is in the result.
88
+ */
89
+ export declare function neededParts(stack: Stack, part: string): Set<string>;
90
+ /**
91
+ * This function gives the dependencies of each resource that come from the needs of the stack, by the address of the resource.
92
+ *
93
+ * - A resource depends on the resources of each part that its part needs.
94
+ * - 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.
95
+ * - An address that the arguments of the resource read is not in the list: OpenTofu knows that dependence from the arguments.
96
+ *
97
+ * The addresses of a resource are sorted. The generator writes them as `depends_on`.
98
+ */
99
+ export declare function dependsOn(layer: TofuLayer, stack: Stack): Map<string, string[]>;
100
+ /**
101
+ * 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.
102
+ * A resource waits for each resource that its arguments read, and for each one of `dependsOn`.
103
+ * Each cycle is a sorted list of addresses. A resource that reads its own attribute is a cycle of one resource.
104
+ */
105
+ export declare function resourceCycles(layer: TofuLayer, stack: Stack): string[][];
106
+ /**
107
+ * This function joins the OpenTofu layers of the fragments of a stack into one layer.
108
+ *
109
+ * - The resources, the provider configurations and the variables are in the order of the fragments.
110
+ * - A resource address in two or more fragments is a fault, also when the resources are equal.
111
+ * - The same rule holds for the address of a provider configuration and for the name of a variable.
112
+ * - The layer has one version of OpenTofu. A fragment with a different value is a fault.
113
+ * - The state has one place. Thus a backend in two or more fragments is a fault, also when the backends are equal.
114
+ *
115
+ * A resource of one fragment can use a provider configuration or a variable of a different fragment.
116
+ * The joined layer has the field `providers` or `variables` when a fragment has it. The function is pure, and it
117
+ * changes no layer. The gate `checkTofu` examines the joined layer afterward.
118
+ *
119
+ * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
120
+ * @returns The joined layer, or the faults of the join.
121
+ */
122
+ export declare function joinTofu(pieces: readonly LayerPiece<TofuLayer>[]): LayerParse<TofuLayer>;
123
+ /** The name of the file of a document. OpenTofu reads each file with the end `.tf.json` as its JSON syntax. */
124
+ export declare const DOCUMENT_FILE = "main.tf.json";
125
+ /** The comment at the start of each document. OpenTofu does not read it. */
126
+ export declare const DOCUMENT_COMMENT = "The generator of @mark1russell7/stack-tofu made this file from the records of a stack. Do not edit it: change the records, and generate it again.";
127
+ /** A document of OpenTofu in its JSON syntax, as data. `documentText` gives the text of its file. */
128
+ export type TofuDocument = TofuObject;
129
+ /**
130
+ * The result of the generator: the document and the warnings, or the faults that stop it.
131
+ * With `ok: false`, one fault or more is an error, and no document exists.
132
+ */
133
+ export type Generated = {
134
+ readonly ok: true;
135
+ readonly document: TofuDocument;
136
+ readonly faults: readonly Fault[];
137
+ } | {
138
+ readonly ok: false;
139
+ readonly faults: readonly Fault[];
140
+ };
141
+ /**
142
+ * This function generates the OpenTofu document of a layer for a stack.
143
+ *
144
+ * - It starts with `checkStack` of the core and `checkTofu`. With one error or more, it gives the faults and no document.
145
+ * - The document has a `resource` block for each resource, and the `provider`, `variable`, `import` and `terraform` blocks of the layer.
146
+ * - `depends_on` of a resource comes from the needs of the stack, by `dependsOn`.
147
+ * - A resource with `keep` gets `prevent_destroy`, so OpenTofu refuses a plan that destroys it.
148
+ * - Each text of a record is literal in the document. A record cannot hold an expression of OpenTofu.
149
+ *
150
+ * The same records give the same document. The sequence of the records of the layer has no effect. The function is pure.
151
+ */
152
+ export declare function generateTofu(layer: TofuLayer, stack: Stack, options?: TofuOptions): Generated;
153
+ /** This function gives the text of the file of a document: JSON with an indent of 2 spaces, and a line end at its end. */
154
+ export declare function documentText(document: TofuDocument): string;
155
+ /**
156
+ * A sample OpenTofu layer for `SAMPLE_STACK` of the core. Each name and each amount is an example: the provider `example` does not exist.
157
+ * The cloud server, its disk and its firewall make the part `cloud-host`. A resource of OpenTofu starts the program on the server.
158
+ */
159
+ export declare const SAMPLE_TOFU: TofuLayer;
160
+ /**
161
+ * Sample budgets for `SAMPLE_TOFU`: the hard limit of the cloud account for a month. The amount is an example.
162
+ * The planned cost of the sample is 7, so the gate passes.
163
+ */
164
+ export declare const SAMPLE_BUDGETS: readonly Budget[];
165
+ /**
166
+ * A sample plan for `SAMPLE_TOFU`, with only the fields that the gate reads: the first plan, which makes each resource.
167
+ * The gate of a plan passes for it.
168
+ */
169
+ export declare const SAMPLE_PLAN: TofuPlan;
170
+ export { AccountState, BUILTIN_PROVIDER, Backend, Budget, COST_LAYER_KEY, PLANNED, PLANNED_OVER, PLANNED_ROOM, PLANNED_TOTAL, PLAN_CODES, PROVIDER_META, PlanChange, PlanCode, PlanFault, PlanOptions, Provider, RESOURCE_META, Resource, TOFU_CODES, TOFU_SCHEMA, TofuCode, TofuFault, TofuLayer, TofuObject, TofuOptions, TofuPlan, TofuValue, UNKNOWN_COSTS, VARIABLE_TYPES, Variable, VariableType, accountKey, accountStates, budgetsByPart, checkPlan, checkTofu, costSpace, evaluateCost, isBuiltin, isZeroOrMore, plannedTotal, providerAddress, resourceAddress, resourceKey, resourceName, resourceProvider, typeProvider };
171
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/values.ts","../src/queries.ts","../src/join.ts","../src/generate.ts","../src/sample.ts"],"mappings":";;;;;qBAMa,WAAW;;qBAGX,eAAe;;qBAGf,WAAW;;qBAGX,WAAW;;qBAGX,aAAa;;qBAGb,cAAc;;iBAGV;WACN;WACA;;;;;;wBAOK,SAAS,cAAc;;wBAUvB,OAAO,OAAO,YAAY,kBAAkB;;wBAK5C,SAAS,OAAO,YAAY,SAAS;;wBAKrC,OAAO,OAAO;;wBAYd,MAAM,OAAO;;wBAMb,MAAM,OAAO;;;;;wBASb,eAAe;;;;;;;;;wBAaf,YAAY,OAAO,YAAY;;YAYnC;;iBAGK;WACN;WACA,MAAM;WACN;;;iBAIM;WACN,MAAM;WACN;;;iBAIM;WACN,gBAAgB;WAChB,kBAAkB;;;wBAIb,UAAU,OAAO,YAAY;;wBA8B7B,SAAS,MAAM;;wBCpJf,eAAe,OAAO,qBAAqB;;;;;wBAc3C,gBAAgB,OAAO,YAAY;;;;;wBAenC,aAAa,UAAU;;;;;wBAoCvB,YAAY,OAAO,OAAO,eAAe;;;;;;;;;;wBAazC,UAAU,OAAO,WAAW,OAAO,QAAQ;;;;;;wBAoB3C,eAAe,OAAO,WAAW,OAAO;;;;;;;;;;;;;;;;;wBCtFxC,SAAS,iBAAiB,WAAW,eAAe,WAAW;;qBCXlE;;qBAGA;;YAGD,eAAe;;;;;YAMf;WAAuB;WAAmB,UAAU;WAAuB,iBAAiB;;WAAuB;WAAoB,iBAAiB;;;;;;;;;;;;;wBAqHpJ,aAAa,OAAO,WAAW,OAAO,OAAO,UAAS,cAAmB;;wBAOzE,aAAa,UAAU;;;;;qBC1I1B,aAAa;;;;;qBAwBb,yBAAyB;;;;;qBAMzB,aAAa"}
package/build/index.js ADDED
@@ -0,0 +1,297 @@
1
+ import { a as providerAddress, c as resourceProvider, i as isBuiltin, l as typeProvider, n as TOFU_SCHEMA, o as resourceAddress, r as VARIABLE_TYPES, s as resourceName, t as BUILTIN_PROVIDER } from "./layer-C9IpaDIx.js";
2
+ import { A as TYPE_FORM, B as valueUses, C as resourceCycles, D as PATH_FORM, E as NAME_FORM, F as isObject, I as pathText, L as refOf, M as escapeTemplate, N as isForm, O as PROVIDER_FORM, P as isList, R as refParts, S as neededParts, T as resourcesByPart, V as varOf, _ as isZeroOrMore, a as checkTofu, b as dependsOn, c as PLANNED_OVER, d as UNKNOWN_COSTS, f as accountKey, g as evaluateCost, h as costSpace, i as TOFU_CODES, j as VERSION_FORM, k as SOURCE_FORM, l as PLANNED_ROOM, m as budgetsByPart, n as PROVIDER_META, o as COST_LAYER_KEY, p as accountStates, r as RESOURCE_META, s as PLANNED, t as joinTofu, u as PLANNED_TOTAL, v as plannedTotal, w as resourceRefs, x as firstResources, y as resourceKey, z as renderValue } from "./join-1KKMeraY.js";
3
+ import { checkStack } from "@mark1russell7/system-stack";
4
+ // src/generate.ts
5
+ /** The name of the file of a document. OpenTofu reads each file with the end `.tf.json` as its JSON syntax. */
6
+ const DOCUMENT_FILE = "main.tf.json";
7
+ /** The comment at the start of each document. OpenTofu does not read it. */
8
+ const DOCUMENT_COMMENT = "The generator of @mark1russell7/stack-tofu made this file from the records of a stack. Do not edit it: change the records, and generate it again.";
9
+ /** This function compares two texts by their code units. The result does not change with the language of the computer. */
10
+ function byText(a, b) {
11
+ return a < b ? -1 : a > b ? 1 : 0;
12
+ }
13
+ /** This function gives an object from its entries. An entry with the key `__proto__` becomes a field, as each other entry. */
14
+ function objectOf(entries) {
15
+ return Object.fromEntries(entries);
16
+ }
17
+ /** This function gives the arguments of a record as OpenTofu reads them. */
18
+ function renderArguments(values) {
19
+ return Object.entries(values ?? {}).map(([name, value]) => [name, renderValue(value)]);
20
+ }
21
+ /** This function gives the body of the block of a resource: a comment, the arguments, then the meta-arguments. */
22
+ function renderResource(resource, depends) {
23
+ const ignore = [...new Set(resource.ignore ?? [])].sort(byText);
24
+ const lifecycle = [];
25
+ if (resource.keep === true) lifecycle.push(["prevent_destroy", true]);
26
+ if (ignore.length > 0) lifecycle.push(["ignore_changes", ignore]);
27
+ const entries = [["//", escapeTemplate(`The part "${resource.part}" of the stack.`)], ...renderArguments(resource.values)];
28
+ if (resource.provider !== void 0) entries.push(["provider", resource.provider]);
29
+ if (depends.length > 0) entries.push(["depends_on", depends]);
30
+ if (lifecycle.length > 0) entries.push(["lifecycle", objectOf(lifecycle)]);
31
+ return objectOf(entries);
32
+ }
33
+ /** This function gives the body of the block of a provider configuration: the alias, then the arguments. */
34
+ function renderProvider(provider) {
35
+ return objectOf([...provider.alias === void 0 ? [] : [["alias", provider.alias]], ...renderArguments(provider.values)]);
36
+ }
37
+ /** This function groups items by a key. The keys are sorted, and the items of a key keep their sequence. */
38
+ function groups(items, key) {
39
+ const byKey = /* @__PURE__ */ new Map();
40
+ for (const item of items) {
41
+ const group = byKey.get(key(item)) ?? [];
42
+ group.push(item);
43
+ byKey.set(key(item), group);
44
+ }
45
+ return [...byKey].sort(([a], [b]) => byText(a, b));
46
+ }
47
+ /** This function gives the document of a layer that passed the gate. */
48
+ function render(layer, stack) {
49
+ const resources = [...firstResources(layer)].sort((a, b) => byText(resourceAddress(a), resourceAddress(b)));
50
+ const providers = [...layer.providers ?? []].sort((a, b) => byText(a.name, b.name) || byText(a.alias ?? "", b.alias ?? ""));
51
+ const variables = [...layer.variables ?? []].sort((a, b) => byText(a.name, b.name));
52
+ const depends = dependsOn(layer, stack);
53
+ const document = [["//", DOCUMENT_COMMENT]];
54
+ const settings = [];
55
+ if (layer.version !== void 0) settings.push(["required_version", layer.version]);
56
+ const required = groups(providers, (provider) => provider.name).map(([name, [first]]) => {
57
+ return [name, objectOf(first === void 0 ? [] : [["source", first.source], ...first.version === void 0 ? [] : [["version", first.version]]])];
58
+ });
59
+ if (required.length > 0) settings.push(["required_providers", objectOf(required)]);
60
+ if (layer.backend !== void 0) settings.push(["backend", objectOf([[layer.backend.type, objectOf(renderArguments(layer.backend.values))]])]);
61
+ if (settings.length > 0) document.push(["terraform", objectOf(settings)]);
62
+ if (variables.length > 0) document.push(["variable", objectOf(variables.map((variable) => {
63
+ const body = [["type", variable.type ?? "string"]];
64
+ if (variable.description !== void 0) body.push(["description", escapeTemplate(variable.description)]);
65
+ if (variable.sensitive === true) body.push(["sensitive", true]);
66
+ return [variable.name, objectOf(body)];
67
+ }))]);
68
+ if (providers.length > 0) document.push(["provider", objectOf(groups(providers, (provider) => provider.name).map(([name, configurations]) => [name, configurations.map(renderProvider)]))]);
69
+ const imports = resources.filter((resource) => resource.import !== void 0);
70
+ if (imports.length > 0) document.push(["import", imports.map((resource) => objectOf([
71
+ ["to", resourceAddress(resource)],
72
+ ["id", escapeTemplate(resource.import ?? "")],
73
+ ...resource.provider === void 0 ? [] : [["provider", resource.provider]]
74
+ ]))]);
75
+ if (resources.length > 0) document.push(["resource", objectOf(groups(resources, (resource) => resource.type).map(([type, ofType]) => {
76
+ return [type, objectOf(ofType.map((resource) => [resourceName(resource), renderResource(resource, depends.get(resourceAddress(resource)) ?? [])]))];
77
+ }))]);
78
+ return objectOf(document);
79
+ }
80
+ /**
81
+ * This function generates the OpenTofu document of a layer for a stack.
82
+ *
83
+ * - It starts with `checkStack` of the core and `checkTofu`. With one error or more, it gives the faults and no document.
84
+ * - The document has a `resource` block for each resource, and the `provider`, `variable`, `import` and `terraform` blocks of the layer.
85
+ * - `depends_on` of a resource comes from the needs of the stack, by `dependsOn`.
86
+ * - A resource with `keep` gets `prevent_destroy`, so OpenTofu refuses a plan that destroys it.
87
+ * - Each text of a record is literal in the document. A record cannot hold an expression of OpenTofu.
88
+ *
89
+ * The same records give the same document. The sequence of the records of the layer has no effect. The function is pure.
90
+ */
91
+ function generateTofu(layer, stack, options = {}) {
92
+ const faults = [...checkStack(stack), ...checkTofu(layer, stack, options)];
93
+ if (faults.some((fault) => fault.level === "error")) return {
94
+ ok: false,
95
+ faults
96
+ };
97
+ return {
98
+ ok: true,
99
+ document: render(layer, stack),
100
+ faults
101
+ };
102
+ }
103
+ /** This function gives the text of the file of a document: JSON with an indent of 2 spaces, and a line end at its end. */
104
+ function documentText(document) {
105
+ return `${JSON.stringify(document, null, 2)}\n`;
106
+ }
107
+ // src/plan.ts
108
+ /** These are the codes of the faults of a plan. */
109
+ const PLAN_CODES = [
110
+ "plan-format",
111
+ "plan-errored",
112
+ "plan-action",
113
+ "plan-unknown",
114
+ "plan-missing",
115
+ "plan-destroy",
116
+ "plan-delete",
117
+ "plan-forget",
118
+ "plan-import",
119
+ "plan-change"
120
+ ];
121
+ /** The actions of a change that the gate knows. */
122
+ const ACTIONS = [
123
+ "no-op",
124
+ "create",
125
+ "read",
126
+ "update",
127
+ "delete",
128
+ "forget"
129
+ ];
130
+ /** The major version of the format of a plan that the gate reads. */
131
+ const PLAN_MAJOR = "1";
132
+ /**
133
+ * This function gives each fault of a plan for a layer. An empty list tells that the gate passes.
134
+ *
135
+ * The read of the plan:
136
+ * - `plan-format`: the format of the plan has a different major version. The gate reads nothing more.
137
+ * - `plan-errored`: the plan has an error, so it is not complete.
138
+ * - `plan-action`: a change has an action that the gate does not know, or no action.
139
+ *
140
+ * The plan and the records:
141
+ * - `plan-unknown`: the plan changes a resource that the layer does not have. No record tells its cost.
142
+ * - `plan-missing`: the plan does not have a resource of the layer. The plan is not a plan of these records.
143
+ * - `plan-destroy`: the plan deletes or replaces a resource whose record has `keep`.
144
+ * - `plan-delete`: the plan deletes a resource, and the option `removes` does not name it.
145
+ * - `plan-forget`: the plan removes a resource from the state and does not delete it. The object stays at the provider with no record.
146
+ * - `plan-import`: the import of a resource in the plan and the field `import` of its record do not agree.
147
+ * - `plan-change`: the option `noChange` is set, and the plan changes a resource.
148
+ *
149
+ * A gate that cannot read a plan does not pass. The faults are in the sequence of the changes of the plan, then of the resources of the layer.
150
+ * The function is pure.
151
+ */
152
+ function checkPlan(layer, plan, options = {}) {
153
+ const faults = [];
154
+ const add = (code, at, message, part) => {
155
+ faults.push({
156
+ level: "error",
157
+ code,
158
+ at,
159
+ message,
160
+ ...part === void 0 ? {} : { part }
161
+ });
162
+ };
163
+ if (plan.format_version.split(".")[0] !== PLAN_MAJOR) {
164
+ add("plan-format", "plan", `The format of the plan is "${plan.format_version}". The gate reads the major version ${PLAN_MAJOR}, so it has no read of this plan.`);
165
+ return faults;
166
+ }
167
+ if (plan.errored === true) add("plan-errored", "plan", "The plan has an error, so it is not complete.");
168
+ const resources = new Map(firstResources(layer).map((resource) => [resourceAddress(resource), resource]));
169
+ const removes = new Set(options.removes ?? []);
170
+ const planned = /* @__PURE__ */ new Set();
171
+ for (const entry of plan.resource_changes ?? []) {
172
+ const at = entry.address;
173
+ planned.add(at);
174
+ const resource = resources.get(at);
175
+ const part = resource?.part;
176
+ const actions = entry.change.actions;
177
+ const unknown = actions.filter((action) => !ACTIONS.includes(action));
178
+ if (actions.length === 0 || unknown.length > 0) {
179
+ add("plan-action", at, actions.length === 0 ? `The change of "${at}" has no action.` : `The change of "${at}" has the action "${unknown[0] ?? ""}", which the gate does not know.`, part);
180
+ continue;
181
+ }
182
+ const deletes = actions.includes("delete");
183
+ const creates = actions.includes("create");
184
+ const importing = entry.change.importing ?? void 0;
185
+ if (actions.includes("forget")) add("plan-forget", at, `The plan removes "${at}" from the state and does not delete it. The object stays at the provider, and no record tells about it.`, part);
186
+ if (resource === void 0) {
187
+ if (deletes && !creates) {
188
+ if (!removes.has(at)) add("plan-delete", at, `The plan deletes "${at}", and the option "removes" does not name it.`);
189
+ } else if (!actions.includes("forget")) add("plan-unknown", at, `The plan has a change (${actions.join(", ")}) of "${at}", which the layer does not have. No record tells its cost.`);
190
+ } else {
191
+ if (deletes && resource.keep === true) add("plan-destroy", at, `The plan ${creates ? "replaces" : "deletes"} "${at}". The record of the part "${resource.part}" tells to keep it.`, part);
192
+ else if (deletes && !creates && !removes.has(at)) add("plan-delete", at, `The plan deletes "${at}", and the option "removes" does not name it.`, part);
193
+ if (importing !== void 0 && resource.import === void 0) add("plan-import", at, `The plan imports an object as "${at}", and the record of the resource has no import.`, part);
194
+ else if (importing?.id !== void 0 && importing.id !== resource.import) add("plan-import", at, `The plan imports the object "${importing.id}" as "${at}". The record names the object "${resource.import ?? ""}".`, part);
195
+ else if (importing === void 0 && resource.import !== void 0 && creates) add("plan-import", at, `The plan makes a new object for "${at}". The record names the object "${resource.import}", which exists.`, part);
196
+ }
197
+ if (options.noChange === true && actions.some((action) => action !== "no-op")) add("plan-change", at, `The plan has a change (${actions.join(", ")}) of "${at}", and the plan must change nothing.`, part);
198
+ }
199
+ for (const [address, resource] of resources) if (!planned.has(address)) add("plan-missing", address, `The plan does not have the resource "${address}" of the layer. It is not a plan of these records.`, resource.part);
200
+ return faults;
201
+ }
202
+ // src/sample.ts
203
+ /**
204
+ * A sample OpenTofu layer for `SAMPLE_STACK` of the core. Each name and each amount is an example: the provider `example` does not exist.
205
+ * The cloud server, its disk and its firewall make the part `cloud-host`. A resource of OpenTofu starts the program on the server.
206
+ */
207
+ const SAMPLE_TOFU = {
208
+ schema: TOFU_SCHEMA,
209
+ version: ">= 1.8",
210
+ providers: [{
211
+ name: "example",
212
+ source: "example-org/example",
213
+ version: "~> 2.0",
214
+ part: "cloud-account",
215
+ values: { token: { $var: "example_token" } }
216
+ }],
217
+ variables: [{
218
+ name: "example_token",
219
+ sensitive: true,
220
+ description: "The token of the account of the cloud provider"
221
+ }],
222
+ resources: [
223
+ {
224
+ part: "cloud-host",
225
+ type: "example_disk",
226
+ values: { size_gb: 10 },
227
+ cost: 1,
228
+ keep: true
229
+ },
230
+ {
231
+ part: "cloud-host",
232
+ type: "example_server",
233
+ values: {
234
+ name: "alerts",
235
+ size: "small",
236
+ disk: { $ref: "example_disk.cloud-host.id" }
237
+ },
238
+ cost: 6,
239
+ keep: true,
240
+ ignore: ["labels"]
241
+ },
242
+ {
243
+ part: "cloud-host",
244
+ type: "example_firewall",
245
+ values: {
246
+ server: { $ref: "example_server.cloud-host.id" },
247
+ ports: [22, 443]
248
+ },
249
+ cost: 0
250
+ },
251
+ {
252
+ part: "program-cloud",
253
+ type: "terraform_data",
254
+ values: { input: { release: "1.4.0" } }
255
+ }
256
+ ]
257
+ };
258
+ /**
259
+ * Sample budgets for `SAMPLE_TOFU`: the hard limit of the cloud account for a month. The amount is an example.
260
+ * The planned cost of the sample is 7, so the gate passes.
261
+ */
262
+ const SAMPLE_BUDGETS = [{
263
+ part: "cloud-account",
264
+ hardLimit: 10
265
+ }];
266
+ /**
267
+ * A sample plan for `SAMPLE_TOFU`, with only the fields that the gate reads: the first plan, which makes each resource.
268
+ * The gate of a plan passes for it.
269
+ */
270
+ const SAMPLE_PLAN = {
271
+ format_version: "1.2",
272
+ resource_changes: [
273
+ {
274
+ address: "example_disk.cloud-host",
275
+ mode: "managed",
276
+ change: { actions: ["create"] }
277
+ },
278
+ {
279
+ address: "example_firewall.cloud-host",
280
+ mode: "managed",
281
+ change: { actions: ["create"] }
282
+ },
283
+ {
284
+ address: "example_server.cloud-host",
285
+ mode: "managed",
286
+ change: { actions: ["create"] }
287
+ },
288
+ {
289
+ address: "terraform_data.program-cloud",
290
+ mode: "managed",
291
+ change: { actions: ["create"] }
292
+ }
293
+ ]
294
+ };
295
+ export { BUILTIN_PROVIDER, COST_LAYER_KEY, DOCUMENT_COMMENT, DOCUMENT_FILE, NAME_FORM, PATH_FORM, PLANNED, PLANNED_OVER, PLANNED_ROOM, PLANNED_TOTAL, PLAN_CODES, PROVIDER_FORM, PROVIDER_META, RESOURCE_META, SAMPLE_BUDGETS, SAMPLE_PLAN, SAMPLE_TOFU, SOURCE_FORM, TOFU_CODES, TOFU_SCHEMA, TYPE_FORM, UNKNOWN_COSTS, VARIABLE_TYPES, VERSION_FORM, accountKey, accountStates, budgetsByPart, checkPlan, checkTofu, costSpace, dependsOn, documentText, escapeTemplate, evaluateCost, firstResources, generateTofu, isBuiltin, isForm, isList, isObject, isZeroOrMore, joinTofu, neededParts, pathText, plannedTotal, providerAddress, refOf, refParts, renderValue, resourceAddress, resourceCycles, resourceKey, resourceName, resourceProvider, resourceRefs, resourcesByPart, typeProvider, valueUses, varOf };
296
+
297
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/generate.ts","../src/plan.ts","../src/sample.ts"],"sourcesContent":["import { checkStack, type Fault, type Stack } from \"@mark1russell7/system-stack\";\nimport { checkTofu, type TofuOptions } from \"./check.ts\";\nimport { resourceAddress, resourceName, type Provider, type Resource, type TofuLayer, type TofuObject, type TofuValue } from \"./layer.ts\";\nimport { dependsOn, firstResources } from \"./queries.ts\";\nimport { escapeTemplate, renderValue } from \"./values.ts\";\n\n// The generator of the OpenTofu files. It writes the JSON syntax of OpenTofu, which a program can write with no fault of form.\n// The files exist only for records that pass the gate.\n\n/** The name of the file of a document. OpenTofu reads each file with the end `.tf.json` as its JSON syntax. */\nexport const DOCUMENT_FILE = \"main.tf.json\";\n\n/** The comment at the start of each document. OpenTofu does not read it. */\nexport const DOCUMENT_COMMENT = \"The generator of @mark1russell7/stack-tofu made this file from the records of a stack. Do not edit it: change the records, and generate it again.\";\n\n/** A document of OpenTofu in its JSON syntax, as data. `documentText` gives the text of its file. */\nexport type TofuDocument = TofuObject;\n\n/**\n * The result of the generator: the document and the warnings, or the faults that stop it.\n * With `ok: false`, one fault or more is an error, and no document exists.\n */\nexport type Generated = { readonly ok: true; readonly document: TofuDocument; readonly faults: readonly Fault[] } | { readonly ok: false; readonly faults: readonly Fault[] };\n\n/** This function compares two texts by their code units. The result does not change with the language of the computer. */\nfunction byText(a: string, b: string): number {\n return a < b ? -1 : a > b ? 1 : 0;\n}\n\n/** This function gives an object from its entries. An entry with the key `__proto__` becomes a field, as each other entry. */\nfunction objectOf(entries: readonly (readonly [string, TofuValue])[]): TofuObject {\n return Object.fromEntries(entries) as TofuObject;\n}\n\n/** This function gives the arguments of a record as OpenTofu reads them. */\nfunction renderArguments(values: TofuObject | undefined): readonly (readonly [string, TofuValue])[] {\n return Object.entries(values ?? {}).map(([name, value]) => [name, renderValue(value)] as const);\n}\n\n/** This function gives the body of the block of a resource: a comment, the arguments, then the meta-arguments. */\nfunction renderResource(resource: Resource, depends: readonly string[]): TofuObject {\n const ignore = [...new Set(resource.ignore ?? [])].sort(byText);\n const lifecycle: (readonly [string, TofuValue])[] = [];\n if (resource.keep === true) lifecycle.push([\"prevent_destroy\", true]);\n if (ignore.length > 0) lifecycle.push([\"ignore_changes\", ignore]);\n const entries: (readonly [string, TofuValue])[] = [[\"//\", escapeTemplate(`The part \"${resource.part}\" of the stack.`)], ...renderArguments(resource.values)];\n if (resource.provider !== undefined) entries.push([\"provider\", resource.provider]);\n if (depends.length > 0) entries.push([\"depends_on\", depends]);\n if (lifecycle.length > 0) entries.push([\"lifecycle\", objectOf(lifecycle)]);\n return objectOf(entries);\n}\n\n/** This function gives the body of the block of a provider configuration: the alias, then the arguments. */\nfunction renderProvider(provider: Provider): TofuObject {\n return objectOf([...(provider.alias === undefined ? [] : ([[\"alias\", provider.alias]] as const)), ...renderArguments(provider.values)]);\n}\n\n/** This function groups items by a key. The keys are sorted, and the items of a key keep their sequence. */\nfunction groups<T>(items: readonly T[], key: (item: T) => string): [string, T[]][] {\n const byKey = new Map<string, T[]>();\n for (const item of items) {\n const group = byKey.get(key(item)) ?? [];\n group.push(item);\n byKey.set(key(item), group);\n }\n return [...byKey].sort(([a], [b]) => byText(a, b));\n}\n\n/** This function gives the document of a layer that passed the gate. */\nfunction render(layer: TofuLayer, stack: Stack): TofuDocument {\n const resources = [...firstResources(layer)].sort((a, b) => byText(resourceAddress(a), resourceAddress(b)));\n const providers = [...(layer.providers ?? [])].sort((a, b) => byText(a.name, b.name) || byText(a.alias ?? \"\", b.alias ?? \"\"));\n const variables = [...(layer.variables ?? [])].sort((a, b) => byText(a.name, b.name));\n const depends = dependsOn(layer, stack);\n const document: (readonly [string, TofuValue])[] = [[\"//\", DOCUMENT_COMMENT]];\n\n const settings: (readonly [string, TofuValue])[] = [];\n if (layer.version !== undefined) settings.push([\"required_version\", layer.version]);\n const required = groups(providers, (provider) => provider.name).map(([name, [first]]) => {\n const source: (readonly [string, TofuValue])[] = first === undefined ? [] : [[\"source\", first.source], ...(first.version === undefined ? [] : ([[\"version\", first.version]] as const))];\n return [name, objectOf(source)] as const;\n });\n if (required.length > 0) settings.push([\"required_providers\", objectOf(required)]);\n if (layer.backend !== undefined) settings.push([\"backend\", objectOf([[layer.backend.type, objectOf(renderArguments(layer.backend.values))]])]);\n if (settings.length > 0) document.push([\"terraform\", objectOf(settings)]);\n\n if (variables.length > 0) {\n document.push([\n \"variable\",\n objectOf(\n variables.map((variable) => {\n const body: (readonly [string, TofuValue])[] = [[\"type\", variable.type ?? \"string\"]];\n if (variable.description !== undefined) body.push([\"description\", escapeTemplate(variable.description)]);\n if (variable.sensitive === true) body.push([\"sensitive\", true]);\n return [variable.name, objectOf(body)] as const;\n }),\n ),\n ]);\n }\n\n if (providers.length > 0) {\n document.push([\"provider\", objectOf(groups(providers, (provider) => provider.name).map(([name, configurations]) => [name, configurations.map(renderProvider)] as const))]);\n }\n\n const imports = resources.filter((resource) => resource.import !== undefined);\n if (imports.length > 0) {\n document.push([\n \"import\",\n imports.map((resource) =>\n objectOf([[\"to\", resourceAddress(resource)], [\"id\", escapeTemplate(resource.import ?? \"\")], ...(resource.provider === undefined ? [] : ([[\"provider\", resource.provider]] as const))]),\n ),\n ]);\n }\n\n if (resources.length > 0) {\n document.push([\n \"resource\",\n objectOf(\n groups(resources, (resource) => resource.type).map(([type, ofType]) => {\n const blocks = ofType.map((resource) => [resourceName(resource), renderResource(resource, depends.get(resourceAddress(resource)) ?? [])] as const);\n return [type, objectOf(blocks)] as const;\n }),\n ),\n ]);\n }\n return objectOf(document);\n}\n\n/**\n * This function generates the OpenTofu document of a layer for a stack.\n *\n * - It starts with `checkStack` of the core and `checkTofu`. With one error or more, it gives the faults and no document.\n * - The document has a `resource` block for each resource, and the `provider`, `variable`, `import` and `terraform` blocks of the layer.\n * - `depends_on` of a resource comes from the needs of the stack, by `dependsOn`.\n * - A resource with `keep` gets `prevent_destroy`, so OpenTofu refuses a plan that destroys it.\n * - Each text of a record is literal in the document. A record cannot hold an expression of OpenTofu.\n *\n * The same records give the same document. The sequence of the records of the layer has no effect. The function is pure.\n */\nexport function generateTofu(layer: TofuLayer, stack: Stack, options: TofuOptions = {}): Generated {\n const faults: Fault[] = [...checkStack(stack), ...checkTofu(layer, stack, options)];\n if (faults.some((fault) => fault.level === \"error\")) return { ok: false, faults };\n return { ok: true, document: render(layer, stack), faults };\n}\n\n/** This function gives the text of the file of a document: JSON with an indent of 2 spaces, and a line end at its end. */\nexport function documentText(document: TofuDocument): string {\n return `${JSON.stringify(document, null, 2)}\\n`;\n}\n","import type { Fault } from \"@mark1russell7/system-stack\";\nimport { resourceAddress, type TofuLayer } from \"./layer.ts\";\nimport { firstResources } from \"./queries.ts\";\n\n// The gate of a plan. OpenTofu gives a plan as JSON with the command `tofu show -json <plan file>`.\n// The gate compares the plan with the records, so a plan that the records do not tell about stops before the apply.\n\n/** A change of one resource in a plan. The names of the fields are the names of the JSON of OpenTofu. */\nexport interface PlanChange {\n /** The address of the resource, for example `example_server.web`. */\n readonly address: string;\n /** `managed` for a resource, or `data` for a data source. */\n readonly mode?: string | undefined;\n readonly change: {\n /** The actions of the change: `no-op`, `create`, `read`, `update`, `delete` or `forget`. A replacement has `delete` and `create`. */\n readonly actions: readonly string[];\n /** The import of an object that exists at the provider, with its id. A change with no import has none. */\n readonly importing?: { readonly id?: string | undefined } | null | undefined;\n };\n}\n\n/** The JSON of a plan of OpenTofu. This type has only the fields that the gate reads. */\nexport interface TofuPlan {\n /** The version of the format, for example `1.2`. A reader ignores a field that it does not know. */\n readonly format_version: string;\n /** `true`: the plan has an error, and it is not complete. */\n readonly errored?: boolean | undefined;\n readonly resource_changes?: readonly PlanChange[] | undefined;\n}\n\n/** These are the codes of the faults of a plan. */\nexport const PLAN_CODES: readonly [\"plan-format\", \"plan-errored\", \"plan-action\", \"plan-unknown\", \"plan-missing\", \"plan-destroy\", \"plan-delete\", \"plan-forget\", \"plan-import\", \"plan-change\"] = [\n \"plan-format\",\n \"plan-errored\",\n \"plan-action\",\n \"plan-unknown\",\n \"plan-missing\",\n \"plan-destroy\",\n \"plan-delete\",\n \"plan-forget\",\n \"plan-import\",\n \"plan-change\",\n];\n\n/** A code of a fault of a plan. */\nexport type PlanCode = (typeof PLAN_CODES)[number];\n\n/** A fault of a plan. Its field `at` is the address of the resource, or `plan` for the plan as a whole. */\nexport interface PlanFault extends Fault {\n readonly code: PlanCode;\n /** The `id` of the part of the stack of the resource. A resource that the layer does not have has none. */\n readonly part?: string | undefined;\n}\n\n/** The options of the gate of a plan. */\nexport interface PlanOptions {\n /**\n * `true`: the plan must change nothing. Use it for the capture of resources that exist.\n * After their import, a plan with no change tells that the records and the provider agree.\n */\n readonly noChange?: boolean | undefined;\n /** The addresses of the resources that the plan can delete. A delete of a different resource stops the gate. */\n readonly removes?: readonly string[] | undefined;\n}\n\n/** The actions of a change that the gate knows. */\nconst ACTIONS: readonly string[] = [\"no-op\", \"create\", \"read\", \"update\", \"delete\", \"forget\"];\n\n/** The major version of the format of a plan that the gate reads. */\nconst PLAN_MAJOR = \"1\";\n\n/**\n * This function gives each fault of a plan for a layer. An empty list tells that the gate passes.\n *\n * The read of the plan:\n * - `plan-format`: the format of the plan has a different major version. The gate reads nothing more.\n * - `plan-errored`: the plan has an error, so it is not complete.\n * - `plan-action`: a change has an action that the gate does not know, or no action.\n *\n * The plan and the records:\n * - `plan-unknown`: the plan changes a resource that the layer does not have. No record tells its cost.\n * - `plan-missing`: the plan does not have a resource of the layer. The plan is not a plan of these records.\n * - `plan-destroy`: the plan deletes or replaces a resource whose record has `keep`.\n * - `plan-delete`: the plan deletes a resource, and the option `removes` does not name it.\n * - `plan-forget`: the plan removes a resource from the state and does not delete it. The object stays at the provider with no record.\n * - `plan-import`: the import of a resource in the plan and the field `import` of its record do not agree.\n * - `plan-change`: the option `noChange` is set, and the plan changes a resource.\n *\n * A gate that cannot read a plan does not pass. The faults are in the sequence of the changes of the plan, then of the resources of the layer.\n * The function is pure.\n */\nexport function checkPlan(layer: TofuLayer, plan: TofuPlan, options: PlanOptions = {}): PlanFault[] {\n const faults: PlanFault[] = [];\n const add = (code: PlanCode, at: string, message: string, part?: string): void => {\n faults.push({ level: \"error\", code, at, message, ...(part === undefined ? {} : { part }) });\n };\n if (plan.format_version.split(\".\")[0] !== PLAN_MAJOR) {\n add(\"plan-format\", \"plan\", `The format of the plan is \"${plan.format_version}\". The gate reads the major version ${PLAN_MAJOR}, so it has no read of this plan.`);\n return faults;\n }\n if (plan.errored === true) add(\"plan-errored\", \"plan\", \"The plan has an error, so it is not complete.\");\n const resources = new Map(firstResources(layer).map((resource) => [resourceAddress(resource), resource]));\n const removes = new Set(options.removes ?? []);\n const planned = new Set<string>();\n for (const entry of plan.resource_changes ?? []) {\n const at = entry.address;\n planned.add(at);\n const resource = resources.get(at);\n const part = resource?.part;\n const actions = entry.change.actions;\n const unknown = actions.filter((action) => !ACTIONS.includes(action));\n if (actions.length === 0 || unknown.length > 0) {\n add(\"plan-action\", at, actions.length === 0 ? `The change of \"${at}\" has no action.` : `The change of \"${at}\" has the action \"${unknown[0] ?? \"\"}\", which the gate does not know.`, part);\n continue;\n }\n const deletes = actions.includes(\"delete\");\n const creates = actions.includes(\"create\");\n const importing = entry.change.importing ?? undefined;\n if (actions.includes(\"forget\")) {\n add(\"plan-forget\", at, `The plan removes \"${at}\" from the state and does not delete it. The object stays at the provider, and no record tells about it.`, part);\n }\n if (resource === undefined) {\n if (deletes && !creates) {\n if (!removes.has(at)) add(\"plan-delete\", at, `The plan deletes \"${at}\", and the option \"removes\" does not name it.`);\n } else if (!actions.includes(\"forget\")) {\n add(\"plan-unknown\", at, `The plan has a change (${actions.join(\", \")}) of \"${at}\", which the layer does not have. No record tells its cost.`);\n }\n } else {\n if (deletes && resource.keep === true) {\n add(\"plan-destroy\", at, `The plan ${creates ? \"replaces\" : \"deletes\"} \"${at}\". The record of the part \"${resource.part}\" tells to keep it.`, part);\n } else if (deletes && !creates && !removes.has(at)) {\n add(\"plan-delete\", at, `The plan deletes \"${at}\", and the option \"removes\" does not name it.`, part);\n }\n if (importing !== undefined && resource.import === undefined) {\n add(\"plan-import\", at, `The plan imports an object as \"${at}\", and the record of the resource has no import.`, part);\n } else if (importing?.id !== undefined && importing.id !== resource.import) {\n add(\"plan-import\", at, `The plan imports the object \"${importing.id}\" as \"${at}\". The record names the object \"${resource.import ?? \"\"}\".`, part);\n } else if (importing === undefined && resource.import !== undefined && creates) {\n add(\"plan-import\", at, `The plan makes a new object for \"${at}\". The record names the object \"${resource.import}\", which exists.`, part);\n }\n }\n if (options.noChange === true && actions.some((action) => action !== \"no-op\")) {\n add(\"plan-change\", at, `The plan has a change (${actions.join(\", \")}) of \"${at}\", and the plan must change nothing.`, part);\n }\n }\n for (const [address, resource] of resources) {\n if (!planned.has(address)) add(\"plan-missing\", address, `The plan does not have the resource \"${address}\" of the layer. It is not a plan of these records.`, resource.part);\n }\n return faults;\n}\n","import type { Budget } from \"./cost.ts\";\nimport { TOFU_SCHEMA, type TofuLayer } from \"./layer.ts\";\nimport type { TofuPlan } from \"./plan.ts\";\n\n/**\n * A sample OpenTofu layer for `SAMPLE_STACK` of the core. Each name and each amount is an example: the provider `example` does not exist.\n * The cloud server, its disk and its firewall make the part `cloud-host`. A resource of OpenTofu starts the program on the server.\n */\nexport const SAMPLE_TOFU: TofuLayer = {\n schema: TOFU_SCHEMA,\n version: \">= 1.8\",\n providers: [{ name: \"example\", source: \"example-org/example\", version: \"~> 2.0\", part: \"cloud-account\", values: { token: { $var: \"example_token\" } } }],\n variables: [{ name: \"example_token\", sensitive: true, description: \"The token of the account of the cloud provider\" }],\n resources: [\n { part: \"cloud-host\", type: \"example_disk\", values: { size_gb: 10 }, cost: 1, keep: true },\n {\n part: \"cloud-host\",\n type: \"example_server\",\n values: { name: \"alerts\", size: \"small\", disk: { $ref: \"example_disk.cloud-host.id\" } },\n cost: 6,\n keep: true,\n ignore: [\"labels\"],\n },\n { part: \"cloud-host\", type: \"example_firewall\", values: { server: { $ref: \"example_server.cloud-host.id\" }, ports: [22, 443] }, cost: 0 },\n { part: \"program-cloud\", type: \"terraform_data\", values: { input: { release: \"1.4.0\" } } },\n ],\n};\n\n/**\n * Sample budgets for `SAMPLE_TOFU`: the hard limit of the cloud account for a month. The amount is an example.\n * The planned cost of the sample is 7, so the gate passes.\n */\nexport const SAMPLE_BUDGETS: readonly Budget[] = [{ part: \"cloud-account\", hardLimit: 10 }];\n\n/**\n * A sample plan for `SAMPLE_TOFU`, with only the fields that the gate reads: the first plan, which makes each resource.\n * The gate of a plan passes for it.\n */\nexport const SAMPLE_PLAN: TofuPlan = {\n format_version: \"1.2\",\n resource_changes: [\n { address: \"example_disk.cloud-host\", mode: \"managed\", change: { actions: [\"create\"] } },\n { address: \"example_firewall.cloud-host\", mode: \"managed\", change: { actions: [\"create\"] } },\n { address: \"example_server.cloud-host\", mode: \"managed\", change: { actions: [\"create\"] } },\n { address: \"terraform_data.program-cloud\", mode: \"managed\", change: { actions: [\"create\"] } },\n ],\n};\n"],"mappings":";;;;;AAUA,MAAa,gBAAgB;;AAG7B,MAAa,mBAAmB;;AAYhC,SAAS,OAAO,GAAW,GAAmB;CAC5C,OAAO,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;AAClC;;AAGA,SAAS,SAAS,SAAgE;CAChF,OAAO,OAAO,YAAY,OAAO;AACnC;;AAGA,SAAS,gBAAgB,QAA2E;CAClG,OAAO,OAAO,QAAQ,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,WAAW,CAAC,MAAM,YAAY,KAAK,CAAC,CAAU;AAChG;;AAGA,SAAS,eAAe,UAAoB,SAAwC;CAClF,MAAM,SAAS,CAAC,GAAG,IAAI,IAAI,SAAS,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM;CAC9D,MAAM,YAA8C,CAAC;CACrD,IAAI,SAAS,SAAS,MAAM,UAAU,KAAK,CAAC,mBAAmB,IAAI,CAAC;CACpE,IAAI,OAAO,SAAS,GAAG,UAAU,KAAK,CAAC,kBAAkB,MAAM,CAAC;CAChE,MAAM,UAA4C,CAAC,CAAC,MAAM,eAAe,aAAa,SAAS,KAAK,gBAAgB,CAAC,GAAG,GAAG,gBAAgB,SAAS,MAAM,CAAC;CAC3J,IAAI,SAAS,aAAa,KAAA,GAAW,QAAQ,KAAK,CAAC,YAAY,SAAS,QAAQ,CAAC;CACjF,IAAI,QAAQ,SAAS,GAAG,QAAQ,KAAK,CAAC,cAAc,OAAO,CAAC;CAC5D,IAAI,UAAU,SAAS,GAAG,QAAQ,KAAK,CAAC,aAAa,SAAS,SAAS,CAAC,CAAC;CACzE,OAAO,SAAS,OAAO;AACzB;;AAGA,SAAS,eAAe,UAAgC;CACtD,OAAO,SAAS,CAAC,GAA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