@intentius/chant-lexicon-terraform 0.124.77 → 0.124.78
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/dist/drift-record.d.ts +24 -0
- package/dist/drift-record.d.ts.map +1 -1
- package/dist/drift-record.js +52 -0
- package/dist/drift-rows.d.ts +49 -4
- package/dist/drift-rows.d.ts.map +1 -1
- package/dist/drift-rows.js +87 -19
- package/dist/integrity.json +2 -2
- package/dist/manifest.json +1 -1
- package/package.json +2 -2
- package/src/__fixtures__/drift-rows/refresh-plan-module.json +48 -0
- package/src/drift-record.test.ts +62 -0
- package/src/drift-record.ts +58 -0
- package/src/drift-rows.test.ts +90 -1
- package/src/drift-rows.ts +89 -18
package/dist/drift-record.d.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* | `live-plan -json` | `ownership` | `bound[]`: in-sync, owned by its `source`. `unowned[]` and `adoptable[]`: unclaimed. `ABSENT`: pending. Any other omission: not-observed. `foreign[]`: undeclared, foreign. `controller_held[]`: runtime |
|
|
12
12
|
* | `live-ls -json` | `ownership` | `declared: false`: undeclared. A gap no live-plan row answers: not-observed |
|
|
13
13
|
* | live-plan and a plan together | `plan` | bound with a planned update or replace: drifted with paths, the live-plan ownership kept |
|
|
14
|
+
* | `show -json` of a live root's full plan | `plan` | planned create: missing. Planned delete or forget: undeclared. Update or any pair of actions: drifted with paths, declared then live. No-op: in-sync |
|
|
14
15
|
*
|
|
15
16
|
* A state read never says in-sync or drifted: `driftRecord` refuses it.
|
|
16
17
|
*
|
|
@@ -77,6 +78,29 @@ export interface TerraformPlanDriftInput extends DriftAdapterBase {
|
|
|
77
78
|
* configuration is the change set's to show and not drift.
|
|
78
79
|
*/
|
|
79
80
|
export declare function terraformPlanDriftRecords(input: TerraformPlanDriftInput): DriftRecord[];
|
|
81
|
+
export interface TerraformLivePlanDriftInput extends DriftAdapterBase {
|
|
82
|
+
/** The engine that planned the root. Default `choudoufu`, which plans a live root. */
|
|
83
|
+
engine?: "choudoufu" | "terraform" | "tofu";
|
|
84
|
+
/** `show -json <planfile>` of the root's full plan, parsed. */
|
|
85
|
+
plan: unknown;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A live root's records from its full plan, read as drift (#4812). A root
|
|
89
|
+
* under live resource markers takes no refresh-only plan, and its plan reads
|
|
90
|
+
* the live system, so what the plan would change is what moved outside the
|
|
91
|
+
* code: one record per managed instance in `resource_changes`, a planned
|
|
92
|
+
* create missing (the object no longer exists), a planned delete or forget
|
|
93
|
+
* undeclared (it exists outside the code), an update or any pair of actions
|
|
94
|
+
* drifted with the plan's paths, declared before and live after, and a
|
|
95
|
+
* no-op in-sync. A path whose declared value is known only after the apply
|
|
96
|
+
* is not a difference and is left out. `resource_drift` is not read.
|
|
97
|
+
*
|
|
98
|
+
* The rules are drift-rows' `live: true` read (`./drift-rows.ts`), so the
|
|
99
|
+
* records that are not in-sync are its rows. `choudoufuLivePlusPlanDriftRecords`
|
|
100
|
+
* is the read for a root whose `live-plan -json` was taken too: it keeps
|
|
101
|
+
* live-plan's ownership, which a plan alone does not give.
|
|
102
|
+
*/
|
|
103
|
+
export declare function terraformLivePlanDriftRecords(input: TerraformLivePlanDriftInput): DriftRecord[];
|
|
80
104
|
export interface ChoudoufuLivePlanDriftInput extends DriftAdapterBase {
|
|
81
105
|
/** `choudoufu live-plan -json`, parsed. */
|
|
82
106
|
livePlan: unknown;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drift-record.d.ts","sourceRoot":"","sources":["../src/drift-record.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"drift-record.d.ts","sourceRoot":"","sources":["../src/drift-record.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAGL,KAAK,gBAAgB,EAKrB,KAAK,WAAW,EACjB,MAAM,+BAA+B,CAAC;AAiEvC,MAAM,WAAW,wBAAyB,SAAQ,gBAAgB;IAChE,MAAM,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC;IAC9B,yFAAyF;IACzF,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,8EAA8E;IAC9E,MAAM,CAAC,EAAE;QAAE,MAAM,EAAE,aAAa,GAAG,gBAAgB,GAAG,YAAY,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACvF;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,wBAAwB,GAAG,WAAW,EAAE,CA0BzF;AAID,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D,MAAM,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC;IAC9B,iDAAiD;IACjD,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,uBAAuB,GAAG,WAAW,EAAE,CA0BvF;AAED,MAAM,WAAW,2BAA4B,SAAQ,gBAAgB;IACnE,sFAAsF;IACtF,MAAM,CAAC,EAAE,WAAW,GAAG,WAAW,GAAG,MAAM,CAAC;IAC5C,+DAA+D;IAC/D,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,2BAA2B,GAAG,WAAW,EAAE,CAgC/F;AAMD,MAAM,WAAW,2BAA4B,SAAQ,gBAAgB;IACnE,2CAA2C;IAC3C,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,2BAA2B,GAAG,WAAW,EAAE,CA2F/F;AAID,MAAM,WAAW,yBAA0B,SAAQ,gBAAgB;IACjE,yCAAyC;IACzC,MAAM,EAAE,OAAO,CAAC;IAChB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;CAC7B;AAED;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,yBAAyB,GAAG,WAAW,EAAE,CAmD3F;AAED,kGAAkG;AAClG,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,gBAAgB,GAAG;IAAE,QAAQ,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,WAAW,EAAE,CAM1H;AAID,MAAM,WAAW,+BAAgC,SAAQ,gBAAgB;IACvE,2CAA2C;IAC3C,QAAQ,EAAE,OAAO,CAAC;IAClB,kEAAkE;IAClE,IAAI,EAAE,OAAO,CAAC;IACd,+DAA+D;IAC/D,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAAC,KAAK,EAAE,+BAA+B,GAAG,WAAW,EAAE,CAqBvG"}
|
package/dist/drift-record.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* | `live-plan -json` | `ownership` | `bound[]`: in-sync, owned by its `source`. `unowned[]` and `adoptable[]`: unclaimed. `ABSENT`: pending. Any other omission: not-observed. `foreign[]`: undeclared, foreign. `controller_held[]`: runtime |
|
|
12
12
|
* | `live-ls -json` | `ownership` | `declared: false`: undeclared. A gap no live-plan row answers: not-observed |
|
|
13
13
|
* | live-plan and a plan together | `plan` | bound with a planned update or replace: drifted with paths, the live-plan ownership kept |
|
|
14
|
+
* | `show -json` of a live root's full plan | `plan` | planned create: missing. Planned delete or forget: undeclared. Update or any pair of actions: drifted with paths, declared then live. No-op: in-sync |
|
|
14
15
|
*
|
|
15
16
|
* A state read never says in-sync or drifted: `driftRecord` refuses it.
|
|
16
17
|
*
|
|
@@ -170,6 +171,57 @@ export function terraformPlanDriftRecords(input) {
|
|
|
170
171
|
return driftRecord({ ...at, status: "in-sync" });
|
|
171
172
|
});
|
|
172
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* A live root's records from its full plan, read as drift (#4812). A root
|
|
176
|
+
* under live resource markers takes no refresh-only plan, and its plan reads
|
|
177
|
+
* the live system, so what the plan would change is what moved outside the
|
|
178
|
+
* code: one record per managed instance in `resource_changes`, a planned
|
|
179
|
+
* create missing (the object no longer exists), a planned delete or forget
|
|
180
|
+
* undeclared (it exists outside the code), an update or any pair of actions
|
|
181
|
+
* drifted with the plan's paths, declared before and live after, and a
|
|
182
|
+
* no-op in-sync. A path whose declared value is known only after the apply
|
|
183
|
+
* is not a difference and is left out. `resource_drift` is not read.
|
|
184
|
+
*
|
|
185
|
+
* The rules are drift-rows' `live: true` read (`./drift-rows.ts`), so the
|
|
186
|
+
* records that are not in-sync are its rows. `choudoufuLivePlusPlanDriftRecords`
|
|
187
|
+
* is the read for a root whose `live-plan -json` was taken too: it keeps
|
|
188
|
+
* live-plan's ownership, which a plan alone does not give.
|
|
189
|
+
*/
|
|
190
|
+
export function terraformLivePlanDriftRecords(input) {
|
|
191
|
+
const engine = input.engine ?? "choudoufu";
|
|
192
|
+
const doc = isObject(input.plan) ? input.plan : {};
|
|
193
|
+
const version = input.version ?? asString(doc.terraform_version);
|
|
194
|
+
const evidence = { tool: engine, ...(version ? { version } : {}), command: `${engine} show -json` };
|
|
195
|
+
const index = indexPlan(input.plan);
|
|
196
|
+
const types = new Map();
|
|
197
|
+
for (const entry of asArray(doc.resource_changes)) {
|
|
198
|
+
if (isObject(entry) && asString(entry.address) && asString(entry.type))
|
|
199
|
+
types.set(entry.address, entry.type);
|
|
200
|
+
}
|
|
201
|
+
const out = [];
|
|
202
|
+
for (const [address, change] of index.changes) {
|
|
203
|
+
if (isDataAddress(address))
|
|
204
|
+
continue;
|
|
205
|
+
const type = types.get(address);
|
|
206
|
+
const at = { ...common(input, engine, "plan", address), ...(type ? { resourceType: type } : {}), evidence };
|
|
207
|
+
const actions = change.actions.filter((a) => a !== "no-op" && a !== "read");
|
|
208
|
+
if (actions.length === 0) {
|
|
209
|
+
out.push(driftRecord({ ...at, status: "in-sync" }));
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (change.actions.length === 1 && actions[0] === "create") {
|
|
213
|
+
out.push(driftRecord({ ...at, status: "missing" }));
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (change.actions.length === 1 && (actions[0] === "delete" || actions[0] === "forget")) {
|
|
217
|
+
out.push(driftRecord({ ...at, status: "undeclared" }));
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const attributes = change.changes.filter((c) => c.after !== KNOWN_AFTER_APPLY).map((c) => attributeOf(c, true));
|
|
221
|
+
out.push(driftRecord({ ...at, status: "drifted", ...(attributes.length > 0 ? { attributes } : {}) }));
|
|
222
|
+
}
|
|
223
|
+
return out.sort(byAddress);
|
|
224
|
+
}
|
|
173
225
|
// ── choudoufu live-plan ────────────────────────────────────────────────────────
|
|
174
226
|
const BOUND_SOURCES = ["marker", "record", "derived", "cache"];
|
|
175
227
|
/**
|
package/dist/drift-rows.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Drift rows (#4580): one row per object a plan found drifted, by address,
|
|
3
3
|
* with its action and the object's real name, counted the way a product's
|
|
4
4
|
* drift report counts. `./drift-record.ts` answers a different question (the
|
|
5
|
-
* status of each instance the plan names), so it skips create
|
|
6
|
-
*
|
|
5
|
+
* status of each instance the plan names, redacted), so it skips create
|
|
6
|
+
* rows and carries no names.
|
|
7
7
|
*
|
|
8
8
|
* A stock root's refresh-only plan reports drift as `resource_drift`, and
|
|
9
9
|
* every entry there is one row, data blocks and creates included. A full
|
|
@@ -14,8 +14,15 @@
|
|
|
14
14
|
* Data blocks, no-op and read entries are not drift.
|
|
15
15
|
*
|
|
16
16
|
* These are terragucci's rules (packages/terragucci/src/report/drift.ts:
|
|
17
|
-
* `liveDrift`, `driftCount`, `driftNames`), so a row count here
|
|
18
|
-
* count its report gives for the same plan.
|
|
17
|
+
* `driftPlan`, `liveDrift`, `driftCount`, `driftNames`), so a row count here
|
|
18
|
+
* equals the count its report gives for the same plan.
|
|
19
|
+
*
|
|
20
|
+
* Behind every row is a whole change (#4812): the plan's entry for the
|
|
21
|
+
* object, shaped as a `resource_changes[]` entry with its actions, full
|
|
22
|
+
* before and after and their masks (`terraformPlanDriftChanges`). A drift
|
|
23
|
+
* report is a plan report over those changes: `terraformDriftPlan` puts a
|
|
24
|
+
* refresh-only plan's drift in place of its changes, and
|
|
25
|
+
* `terraformLivePlanDrift` reads a live root's full plan as drift.
|
|
19
26
|
*
|
|
20
27
|
* A row carries no attribute values. Its `name` is the first non-empty
|
|
21
28
|
* string among `name`, `id`, `bucket`, `function_name` and `arn`, read from
|
|
@@ -49,6 +56,44 @@ export interface TerraformPlanDriftRowsOptions {
|
|
|
49
56
|
*/
|
|
50
57
|
live?: boolean;
|
|
51
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* The drift a plan (`show -json`, parsed) found as whole changes (#4812),
|
|
61
|
+
* one per drifted object in the plan's order, each shaped as a
|
|
62
|
+
* `resource_changes[]` entry: `address`, `mode`, `type`, `name`,
|
|
63
|
+
* `module_address`, `index`, `deposed` and `provider_name` as the plan has
|
|
64
|
+
* them, and `change` with its actions, its full before and after, and their
|
|
65
|
+
* masks. A stock root's are its `resource_drift` entries unchanged. A live
|
|
66
|
+
* root's are its full plan's changes seen from the other side: a create
|
|
67
|
+
* there is a `delete` (the object no longer exists), a delete a `create`
|
|
68
|
+
* (an object exists outside the code), any pair of actions an `update`, the
|
|
69
|
+
* code's value before and the live one after, the sensitive masks swapped
|
|
70
|
+
* with them and an unknown value replaced by the live one.
|
|
71
|
+
*
|
|
72
|
+
* Values are not redacted: a change carries what the plan carries, masks
|
|
73
|
+
* included, for a report that renders the plan. A drift record
|
|
74
|
+
* (`./drift-record.ts`) is the redacted read.
|
|
75
|
+
*
|
|
76
|
+
* Its length is the drift count, and `terraformPlanDriftRows` is one row
|
|
77
|
+
* per entry. A malformed `resource_drift` entry that is not an object is
|
|
78
|
+
* passed on as the plan has it, and its row has the address `""`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function terraformPlanDriftChanges(plan: unknown, options?: TerraformPlanDriftRowsOptions): Record<string, unknown>[];
|
|
81
|
+
/**
|
|
82
|
+
* A refresh-only plan as its drift report reads it: the drift found in place
|
|
83
|
+
* of the changes proposed, so the report is built from the same JSON as a
|
|
84
|
+
* plan's and code waiting on the default branch never shows in it.
|
|
85
|
+
* `resource_changes` becomes `terraformPlanDriftChanges(plan)`; the rest of
|
|
86
|
+
* the document is kept. Anything not an object is returned as it is.
|
|
87
|
+
*/
|
|
88
|
+
export declare function terraformDriftPlan(plan: unknown): unknown;
|
|
89
|
+
/**
|
|
90
|
+
* A full plan of a root under live resource markers, as drift: its
|
|
91
|
+
* `resource_drift` becomes `terraformPlanDriftChanges(plan, { live: true })`
|
|
92
|
+
* and the rest of the document is kept, so it reads as a refresh-only plan
|
|
93
|
+
* would (`terraformDriftPlan`, and every function here without `live`).
|
|
94
|
+
* Anything not an object is returned as it is.
|
|
95
|
+
*/
|
|
96
|
+
export declare function terraformLivePlanDrift(plan: unknown): unknown;
|
|
52
97
|
/**
|
|
53
98
|
* The drift a plan (`show -json`, parsed) found, one row per drifted object
|
|
54
99
|
* in the plan's order. Its length is the drift count.
|
package/dist/drift-rows.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drift-rows.d.ts","sourceRoot":"","sources":["../src/drift-rows.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"drift-rows.d.ts","sourceRoot":"","sources":["../src/drift-rows.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAMH,0BAA0B;AAC1B,MAAM,WAAW,iBAAiB;IAChC,iEAAiE;IACjE,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;OAKG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mEAAmE;IACnE,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,6BAA6B;IAC5C;;;;OAIG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AA4ED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,6BAAkC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAG/H;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAGzD;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAG7D;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,6BAAkC,GAAG,iBAAiB,EAAE,CAEtH;AAED,6FAA6F;AAC7F,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,6BAAkC,GAAG,MAAM,CAE1G;AAED,8EAA8E;AAC9E,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,6BAAkC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAIvH"}
|
package/dist/drift-rows.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Drift rows (#4580): one row per object a plan found drifted, by address,
|
|
3
3
|
* with its action and the object's real name, counted the way a product's
|
|
4
4
|
* drift report counts. `./drift-record.ts` answers a different question (the
|
|
5
|
-
* status of each instance the plan names), so it skips create
|
|
6
|
-
*
|
|
5
|
+
* status of each instance the plan names, redacted), so it skips create
|
|
6
|
+
* rows and carries no names.
|
|
7
7
|
*
|
|
8
8
|
* A stock root's refresh-only plan reports drift as `resource_drift`, and
|
|
9
9
|
* every entry there is one row, data blocks and creates included. A full
|
|
@@ -14,8 +14,15 @@
|
|
|
14
14
|
* Data blocks, no-op and read entries are not drift.
|
|
15
15
|
*
|
|
16
16
|
* These are terragucci's rules (packages/terragucci/src/report/drift.ts:
|
|
17
|
-
* `liveDrift`, `driftCount`, `driftNames`), so a row count here
|
|
18
|
-
* count its report gives for the same plan.
|
|
17
|
+
* `driftPlan`, `liveDrift`, `driftCount`, `driftNames`), so a row count here
|
|
18
|
+
* equals the count its report gives for the same plan.
|
|
19
|
+
*
|
|
20
|
+
* Behind every row is a whole change (#4812): the plan's entry for the
|
|
21
|
+
* object, shaped as a `resource_changes[]` entry with its actions, full
|
|
22
|
+
* before and after and their masks (`terraformPlanDriftChanges`). A drift
|
|
23
|
+
* report is a plan report over those changes: `terraformDriftPlan` puts a
|
|
24
|
+
* refresh-only plan's drift in place of its changes, and
|
|
25
|
+
* `terraformLivePlanDrift` reads a live root's full plan as drift.
|
|
19
26
|
*
|
|
20
27
|
* A row carries no attribute values. Its `name` is the first non-empty
|
|
21
28
|
* string among `name`, `id`, `bucket`, `function_name` and `arn`, read from
|
|
@@ -54,17 +61,22 @@ function stockAction(actions) {
|
|
|
54
61
|
return "replace";
|
|
55
62
|
return "update";
|
|
56
63
|
}
|
|
57
|
-
/**
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
return list.map((entry) => {
|
|
64
|
+
/** One row per whole change. */
|
|
65
|
+
function rowsOf(changes) {
|
|
66
|
+
return changes.map((entry) => {
|
|
61
67
|
const ch = isObject(entry) && isObject(entry.change) ? entry.change : {};
|
|
62
68
|
const actions = Array.isArray(ch.actions) ? ch.actions.map(String) : [];
|
|
63
69
|
return rowOf(entry, stockAction(actions), [ch.after, ch.before]);
|
|
64
70
|
});
|
|
65
71
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
72
|
+
/**
|
|
73
|
+
* A live root's full plan, its changes read as drift: each one the plan
|
|
74
|
+
* would make, seen from the other side, as terragucci's `liveDrift` builds
|
|
75
|
+
* it. The code's value is before and the live value after, the masks swap
|
|
76
|
+
* with them, and a value the plan knows only after the apply takes the live
|
|
77
|
+
* one, since it is not a difference.
|
|
78
|
+
*/
|
|
79
|
+
function liveChanges(doc) {
|
|
68
80
|
const list = Array.isArray(doc.resource_changes) ? doc.resource_changes : [];
|
|
69
81
|
const out = [];
|
|
70
82
|
for (const entry of list) {
|
|
@@ -75,24 +87,80 @@ function liveRows(doc) {
|
|
|
75
87
|
if (actions.length === 0 || actions.every((a) => a === "no-op" || a === "read"))
|
|
76
88
|
continue;
|
|
77
89
|
const action = actions.length === 1 ? (FLIP[actions[0]] ?? actions[0]) : "update";
|
|
78
|
-
const live =
|
|
79
|
-
let code =
|
|
80
|
-
// A value the plan knows only after the apply is not a difference: the live one stands for it.
|
|
90
|
+
const live = (ch.before ?? null);
|
|
91
|
+
let code = (ch.after ?? null);
|
|
81
92
|
const unknown = isObject(ch.after_unknown) ? ch.after_unknown : {};
|
|
82
|
-
if (code && live)
|
|
83
|
-
|
|
84
|
-
|
|
93
|
+
if (isObject(code) && isObject(live)) {
|
|
94
|
+
const known = live;
|
|
95
|
+
code = { ...code, ...Object.fromEntries(Object.keys(unknown).filter((k) => unknown[k] === true && k in known).map((k) => [k, known[k]])) };
|
|
96
|
+
}
|
|
97
|
+
out.push({
|
|
98
|
+
...entry,
|
|
99
|
+
change: { actions: [action], before: code, after: live, before_sensitive: ch.after_sensitive ?? false, after_sensitive: ch.before_sensitive ?? false },
|
|
100
|
+
});
|
|
85
101
|
}
|
|
86
102
|
return out;
|
|
87
103
|
}
|
|
104
|
+
/** A refresh-only plan's `resource_drift`, each entry as the plan has it. */
|
|
105
|
+
function stockChanges(doc) {
|
|
106
|
+
return (Array.isArray(doc.resource_drift) ? [...doc.resource_drift] : []);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The drift a plan (`show -json`, parsed) found as whole changes (#4812),
|
|
110
|
+
* one per drifted object in the plan's order, each shaped as a
|
|
111
|
+
* `resource_changes[]` entry: `address`, `mode`, `type`, `name`,
|
|
112
|
+
* `module_address`, `index`, `deposed` and `provider_name` as the plan has
|
|
113
|
+
* them, and `change` with its actions, its full before and after, and their
|
|
114
|
+
* masks. A stock root's are its `resource_drift` entries unchanged. A live
|
|
115
|
+
* root's are its full plan's changes seen from the other side: a create
|
|
116
|
+
* there is a `delete` (the object no longer exists), a delete a `create`
|
|
117
|
+
* (an object exists outside the code), any pair of actions an `update`, the
|
|
118
|
+
* code's value before and the live one after, the sensitive masks swapped
|
|
119
|
+
* with them and an unknown value replaced by the live one.
|
|
120
|
+
*
|
|
121
|
+
* Values are not redacted: a change carries what the plan carries, masks
|
|
122
|
+
* included, for a report that renders the plan. A drift record
|
|
123
|
+
* (`./drift-record.ts`) is the redacted read.
|
|
124
|
+
*
|
|
125
|
+
* Its length is the drift count, and `terraformPlanDriftRows` is one row
|
|
126
|
+
* per entry. A malformed `resource_drift` entry that is not an object is
|
|
127
|
+
* passed on as the plan has it, and its row has the address `""`.
|
|
128
|
+
*/
|
|
129
|
+
export function terraformPlanDriftChanges(plan, options = {}) {
|
|
130
|
+
if (!isObject(plan))
|
|
131
|
+
return [];
|
|
132
|
+
return options.live ? liveChanges(plan) : stockChanges(plan);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A refresh-only plan as its drift report reads it: the drift found in place
|
|
136
|
+
* of the changes proposed, so the report is built from the same JSON as a
|
|
137
|
+
* plan's and code waiting on the default branch never shows in it.
|
|
138
|
+
* `resource_changes` becomes `terraformPlanDriftChanges(plan)`; the rest of
|
|
139
|
+
* the document is kept. Anything not an object is returned as it is.
|
|
140
|
+
*/
|
|
141
|
+
export function terraformDriftPlan(plan) {
|
|
142
|
+
if (!isObject(plan))
|
|
143
|
+
return plan;
|
|
144
|
+
return { ...plan, resource_changes: terraformPlanDriftChanges(plan) };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A full plan of a root under live resource markers, as drift: its
|
|
148
|
+
* `resource_drift` becomes `terraformPlanDriftChanges(plan, { live: true })`
|
|
149
|
+
* and the rest of the document is kept, so it reads as a refresh-only plan
|
|
150
|
+
* would (`terraformDriftPlan`, and every function here without `live`).
|
|
151
|
+
* Anything not an object is returned as it is.
|
|
152
|
+
*/
|
|
153
|
+
export function terraformLivePlanDrift(plan) {
|
|
154
|
+
if (!isObject(plan))
|
|
155
|
+
return plan;
|
|
156
|
+
return { ...plan, resource_drift: terraformPlanDriftChanges(plan, { live: true }) };
|
|
157
|
+
}
|
|
88
158
|
/**
|
|
89
159
|
* The drift a plan (`show -json`, parsed) found, one row per drifted object
|
|
90
160
|
* in the plan's order. Its length is the drift count.
|
|
91
161
|
*/
|
|
92
162
|
export function terraformPlanDriftRows(plan, options = {}) {
|
|
93
|
-
|
|
94
|
-
return [];
|
|
95
|
-
return options.live ? liveRows(plan) : stockRows(plan);
|
|
163
|
+
return rowsOf(terraformPlanDriftChanges(plan, options));
|
|
96
164
|
}
|
|
97
165
|
/** How many objects a plan found drifted: `terraformPlanDriftRows(plan, options).length`. */
|
|
98
166
|
export function terraformPlanDriftCount(plan, options = {}) {
|
package/dist/integrity.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"algorithm": "sha256",
|
|
3
3
|
"artifacts": {
|
|
4
|
-
"manifest.json": "
|
|
4
|
+
"manifest.json": "8ad4ee999ef6cc002b43068ba53aed3520d22ee0544e0ece887f9777542f7df7",
|
|
5
5
|
"meta.json": "ca3d163bab055381827226140568f3bef7eaac187cebd76878e0b63e9e442356",
|
|
6
6
|
"types/index.d.ts": "022927e6a7b351cc4cf52d5f973f60ccd140e5810e17b32fb8868965aa1db161",
|
|
7
7
|
"rules/plan-before-apply.ts": "7c10245b2c3335253b48ee7d8b075d89f43116bde20313a5d1031b343fef1282",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"rules/tf047.ts": "bfa86f85c4c5efff297280fc2f15104d6319fa96dfc444a31829fb7353224092",
|
|
60
60
|
"skills/chant-terraform.md": "53dd11c7b289652f92fdaf2c03766abda46d8964e31fe709e8b142ced4f19c50"
|
|
61
61
|
},
|
|
62
|
-
"composite": "
|
|
62
|
+
"composite": "dc15705fd3b8efb31bb5e9de4d327c106926808c0f59f7ffb0ce1c160e5d0ede"
|
|
63
63
|
}
|
package/dist/manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intentius/chant-lexicon-terraform",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.78",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"src/",
|
|
@@ -93,7 +93,7 @@
|
|
|
93
93
|
"access": "public"
|
|
94
94
|
},
|
|
95
95
|
"peerDependencies": {
|
|
96
|
-
"@intentius/chant": "^0.124.
|
|
96
|
+
"@intentius/chant": "^0.124.78",
|
|
97
97
|
"zod": "^4.3.6"
|
|
98
98
|
},
|
|
99
99
|
"description": "terraform lexicon for chant, existing Terraform root modules as declarable entities",
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"format_version": "1.2",
|
|
3
|
+
"terraform_version": "1.9.8",
|
|
4
|
+
"resource_drift": [
|
|
5
|
+
{
|
|
6
|
+
"address": "module.service.aws_sqs_queue.jobs[\"orders\"]",
|
|
7
|
+
"module_address": "module.service",
|
|
8
|
+
"mode": "managed",
|
|
9
|
+
"type": "aws_sqs_queue",
|
|
10
|
+
"name": "jobs",
|
|
11
|
+
"index": "orders",
|
|
12
|
+
"provider_name": "registry.terraform.io/hashicorp/aws",
|
|
13
|
+
"change": {
|
|
14
|
+
"actions": ["update"],
|
|
15
|
+
"before": { "name": "shop-orders-jobs", "visibility_timeout_seconds": 30, "tags": { "team": "a" }, "kms_data_key": "hunter2" },
|
|
16
|
+
"after": { "name": "shop-orders-jobs", "visibility_timeout_seconds": 45, "tags": { "team": "b" }, "kms_data_key": "hunter2" },
|
|
17
|
+
"after_unknown": {},
|
|
18
|
+
"before_sensitive": { "kms_data_key": true, "tags": {} },
|
|
19
|
+
"after_sensitive": { "kms_data_key": true, "tags": {} }
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"address": "aws_instance.web",
|
|
24
|
+
"mode": "managed",
|
|
25
|
+
"type": "aws_instance",
|
|
26
|
+
"name": "web",
|
|
27
|
+
"deposed": "00000001",
|
|
28
|
+
"provider_name": "registry.terraform.io/hashicorp/aws",
|
|
29
|
+
"change": {
|
|
30
|
+
"actions": ["delete"],
|
|
31
|
+
"before": { "id": "i-0001", "instance_type": "t3.micro" },
|
|
32
|
+
"after": null,
|
|
33
|
+
"after_unknown": {},
|
|
34
|
+
"before_sensitive": {},
|
|
35
|
+
"after_sensitive": false
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
"resource_changes": [
|
|
40
|
+
{
|
|
41
|
+
"address": "aws_s3_bucket.code_only",
|
|
42
|
+
"mode": "managed",
|
|
43
|
+
"type": "aws_s3_bucket",
|
|
44
|
+
"name": "code_only",
|
|
45
|
+
"change": { "actions": ["create"], "before": null, "after": { "bucket": "waiting-on-main" } }
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
package/src/drift-record.test.ts
CHANGED
|
@@ -14,9 +14,11 @@ import {
|
|
|
14
14
|
choudoufuLiveLsDriftRecords,
|
|
15
15
|
choudoufuLivePlanDriftRecords,
|
|
16
16
|
choudoufuLivePlusPlanDriftRecords,
|
|
17
|
+
terraformLivePlanDriftRecords,
|
|
17
18
|
terraformPlanDriftRecords,
|
|
18
19
|
terraformStateDriftRecords,
|
|
19
20
|
} from "./drift-record";
|
|
21
|
+
import { terraformPlanDriftRows } from "./drift-rows";
|
|
20
22
|
|
|
21
23
|
const FIX = join(import.meta.dirname, "__fixtures__");
|
|
22
24
|
const read = (...path: string[]): unknown => JSON.parse(readFileSync(join(FIX, ...path), "utf-8"));
|
|
@@ -221,3 +223,63 @@ describe("choudoufu live+plan", () => {
|
|
|
221
223
|
roundTrip(records);
|
|
222
224
|
});
|
|
223
225
|
});
|
|
226
|
+
|
|
227
|
+
describe("a live root's full plan, read as drift (#4812)", () => {
|
|
228
|
+
const records = terraformLivePlanDriftRecords({ member: "estate", observedAt: AT, plan: read("live-plan-depth", "show-plan.json") });
|
|
229
|
+
|
|
230
|
+
test("a planned create is missing, an update drifted declared then live, a no-op in-sync, all read plan", () => {
|
|
231
|
+
expect(statusOf(records)).toEqual({
|
|
232
|
+
"aws_cloudwatch_log_group.adoptable": "missing",
|
|
233
|
+
"aws_cloudwatch_log_group.app": "drifted",
|
|
234
|
+
"aws_cloudwatch_log_group.held_elsewhere": "missing",
|
|
235
|
+
"aws_cloudwatch_log_group.never_applied": "missing",
|
|
236
|
+
"aws_eip.pool[0]": "in-sync",
|
|
237
|
+
"aws_eip.pool[1]": "in-sync",
|
|
238
|
+
"aws_security_group.main": "in-sync",
|
|
239
|
+
"aws_security_group_rule.https": "missing",
|
|
240
|
+
"aws_subnet.app": "in-sync",
|
|
241
|
+
"aws_vpc.adoptable": "missing",
|
|
242
|
+
"aws_vpc.main": "drifted",
|
|
243
|
+
});
|
|
244
|
+
const app = at(records, "aws_cloudwatch_log_group.app");
|
|
245
|
+
expect(app).toMatchObject({ engine: "choudoufu", read: "plan", resourceType: "aws_cloudwatch_log_group", evidence: { tool: "choudoufu", command: "choudoufu show -json" } });
|
|
246
|
+
expect(app.attributes).toContainEqual({ path: "retention_in_days", before: 1, after: 3 });
|
|
247
|
+
expect(at(records, "aws_eip.pool[0]").instanceOf).toBe(at(records, "aws_eip.pool[1]").instanceOf);
|
|
248
|
+
roundTrip(records);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("the records that are not in-sync are drift-rows' live rows", () => {
|
|
252
|
+
for (const plan of [read("live-plan-depth", "show-plan.json"), read("drift-rows", "live-root-plan.json")]) {
|
|
253
|
+
const drifted = terraformLivePlanDriftRecords({ member: "estate", observedAt: AT, plan }).filter((r) => r.status !== "in-sync");
|
|
254
|
+
expect(drifted.map((r) => r.address)).toEqual(terraformPlanDriftRows(plan, { live: true }).map((r) => r.address).sort());
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("a delete is undeclared, a replacement drifted, and a value known only after the apply is no difference", () => {
|
|
259
|
+
const live = terraformLivePlanDriftRecords({ member: "estate", observedAt: AT, engine: "tofu", plan: read("drift-rows", "live-root-plan.json") });
|
|
260
|
+
expect(statusOf(live)).toEqual({
|
|
261
|
+
"aws_iam_role.deployer": "drifted",
|
|
262
|
+
"aws_lambda_function.api": "drifted",
|
|
263
|
+
"aws_s3_bucket.assets": "missing",
|
|
264
|
+
"aws_s3_bucket.steady": "in-sync",
|
|
265
|
+
"aws_security_group.edge": "drifted",
|
|
266
|
+
"aws_sqs_queue.stray": "undeclared",
|
|
267
|
+
});
|
|
268
|
+
expect(at(live, "aws_lambda_function.api").attributes).toEqual([{ path: "memory_size", before: 512, after: 256 }]);
|
|
269
|
+
expect(at(live, "aws_lambda_function.api").evidence).toEqual({ tool: "tofu", version: "1.9.8", command: "tofu show -json" });
|
|
270
|
+
roundTrip(live);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("a sensitive value never leaves, on either side", () => {
|
|
274
|
+
const sensitive = terraformLivePlanDriftRecords({ member: "estate", observedAt: AT, plan: read("live-plan-depth", "show-plan-sensitive.json") });
|
|
275
|
+
expect(at(sensitive, "aws_vpc.main").attributes).toContainEqual({ path: "tags.token", sensitive: true });
|
|
276
|
+
const text = JSON.stringify(driftDocument(sensitive));
|
|
277
|
+
expect(text).not.toContain("hunter2");
|
|
278
|
+
expect(text).not.toContain("hunter3");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("a stock root's plan reader is unchanged: the same plan's changes read as in-sync or pending, not drift", () => {
|
|
282
|
+
const stock = terraformPlanDriftRecords({ member: "estate", observedAt: AT, plan: read("live-plan-depth", "show-plan.json") });
|
|
283
|
+
expect(stock.some((r) => r.status === "drifted" || r.status === "missing")).toBe(false);
|
|
284
|
+
});
|
|
285
|
+
});
|
package/src/drift-record.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* | `live-plan -json` | `ownership` | `bound[]`: in-sync, owned by its `source`. `unowned[]` and `adoptable[]`: unclaimed. `ABSENT`: pending. Any other omission: not-observed. `foreign[]`: undeclared, foreign. `controller_held[]`: runtime |
|
|
12
12
|
* | `live-ls -json` | `ownership` | `declared: false`: undeclared. A gap no live-plan row answers: not-observed |
|
|
13
13
|
* | live-plan and a plan together | `plan` | bound with a planned update or replace: drifted with paths, the live-plan ownership kept |
|
|
14
|
+
* | `show -json` of a live root's full plan | `plan` | planned create: missing. Planned delete or forget: undeclared. Update or any pair of actions: drifted with paths, declared then live. No-op: in-sync |
|
|
14
15
|
*
|
|
15
16
|
* A state read never says in-sync or drifted: `driftRecord` refuses it.
|
|
16
17
|
*
|
|
@@ -206,6 +207,63 @@ export function terraformPlanDriftRecords(input: TerraformPlanDriftInput): Drift
|
|
|
206
207
|
});
|
|
207
208
|
}
|
|
208
209
|
|
|
210
|
+
export interface TerraformLivePlanDriftInput extends DriftAdapterBase {
|
|
211
|
+
/** The engine that planned the root. Default `choudoufu`, which plans a live root. */
|
|
212
|
+
engine?: "choudoufu" | "terraform" | "tofu";
|
|
213
|
+
/** `show -json <planfile>` of the root's full plan, parsed. */
|
|
214
|
+
plan: unknown;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* A live root's records from its full plan, read as drift (#4812). A root
|
|
219
|
+
* under live resource markers takes no refresh-only plan, and its plan reads
|
|
220
|
+
* the live system, so what the plan would change is what moved outside the
|
|
221
|
+
* code: one record per managed instance in `resource_changes`, a planned
|
|
222
|
+
* create missing (the object no longer exists), a planned delete or forget
|
|
223
|
+
* undeclared (it exists outside the code), an update or any pair of actions
|
|
224
|
+
* drifted with the plan's paths, declared before and live after, and a
|
|
225
|
+
* no-op in-sync. A path whose declared value is known only after the apply
|
|
226
|
+
* is not a difference and is left out. `resource_drift` is not read.
|
|
227
|
+
*
|
|
228
|
+
* The rules are drift-rows' `live: true` read (`./drift-rows.ts`), so the
|
|
229
|
+
* records that are not in-sync are its rows. `choudoufuLivePlusPlanDriftRecords`
|
|
230
|
+
* is the read for a root whose `live-plan -json` was taken too: it keeps
|
|
231
|
+
* live-plan's ownership, which a plan alone does not give.
|
|
232
|
+
*/
|
|
233
|
+
export function terraformLivePlanDriftRecords(input: TerraformLivePlanDriftInput): DriftRecord[] {
|
|
234
|
+
const engine = input.engine ?? "choudoufu";
|
|
235
|
+
const doc = isObject(input.plan) ? input.plan : {};
|
|
236
|
+
const version = input.version ?? asString(doc.terraform_version);
|
|
237
|
+
const evidence: DriftEvidence = { tool: engine, ...(version ? { version } : {}), command: `${engine} show -json` };
|
|
238
|
+
const index = indexPlan(input.plan);
|
|
239
|
+
const types = new Map<string, string>();
|
|
240
|
+
for (const entry of asArray(doc.resource_changes)) {
|
|
241
|
+
if (isObject(entry) && asString(entry.address) && asString(entry.type)) types.set(entry.address as string, entry.type as string);
|
|
242
|
+
}
|
|
243
|
+
const out: DriftRecord[] = [];
|
|
244
|
+
for (const [address, change] of index.changes) {
|
|
245
|
+
if (isDataAddress(address)) continue;
|
|
246
|
+
const type = types.get(address);
|
|
247
|
+
const at = { ...common(input, engine, "plan", address), ...(type ? { resourceType: type } : {}), evidence };
|
|
248
|
+
const actions = change.actions.filter((a) => a !== "no-op" && a !== "read");
|
|
249
|
+
if (actions.length === 0) {
|
|
250
|
+
out.push(driftRecord({ ...at, status: "in-sync" }));
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (change.actions.length === 1 && actions[0] === "create") {
|
|
254
|
+
out.push(driftRecord({ ...at, status: "missing" }));
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (change.actions.length === 1 && (actions[0] === "delete" || actions[0] === "forget")) {
|
|
258
|
+
out.push(driftRecord({ ...at, status: "undeclared" }));
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const attributes = change.changes.filter((c) => c.after !== KNOWN_AFTER_APPLY).map((c) => attributeOf(c, true));
|
|
262
|
+
out.push(driftRecord({ ...at, status: "drifted", ...(attributes.length > 0 ? { attributes } : {}) }));
|
|
263
|
+
}
|
|
264
|
+
return out.sort(byAddress);
|
|
265
|
+
}
|
|
266
|
+
|
|
209
267
|
// ── choudoufu live-plan ────────────────────────────────────────────────────────
|
|
210
268
|
|
|
211
269
|
const BOUND_SOURCES: readonly DriftOwnershipBy[] = ["marker", "record", "derived", "cache"];
|
package/src/drift-rows.test.ts
CHANGED
|
@@ -10,16 +10,30 @@
|
|
|
10
10
|
import { readFileSync } from "node:fs";
|
|
11
11
|
import { join } from "node:path";
|
|
12
12
|
import { describe, expect, test } from "vitest";
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
terraformDriftPlan,
|
|
15
|
+
terraformLivePlanDrift,
|
|
16
|
+
terraformPlanDriftChanges,
|
|
17
|
+
terraformPlanDriftCount,
|
|
18
|
+
terraformPlanDriftNames,
|
|
19
|
+
terraformPlanDriftRows,
|
|
20
|
+
} from "./drift-rows";
|
|
14
21
|
|
|
15
22
|
const FIX = join(import.meta.dirname, "__fixtures__", "drift-rows");
|
|
16
23
|
const read = (name: string): unknown => JSON.parse(readFileSync(join(FIX, name), "utf-8"));
|
|
24
|
+
const readAt = (...path: string[]): unknown => JSON.parse(readFileSync(join(import.meta.dirname, "__fixtures__", ...path), "utf-8"));
|
|
17
25
|
|
|
18
26
|
// ── terragucci's rules, copied as they are, for the parity checks ─────────────
|
|
19
27
|
|
|
20
28
|
type Json = Record<string, unknown>;
|
|
21
29
|
const FLIP: Record<string, string> = { create: "delete", delete: "create" };
|
|
22
30
|
|
|
31
|
+
function driftPlan(plan: unknown): unknown {
|
|
32
|
+
if (plan === null || typeof plan !== "object" || Array.isArray(plan)) return plan;
|
|
33
|
+
const p = plan as Json;
|
|
34
|
+
return { ...p, resource_changes: Array.isArray(p.resource_drift) ? p.resource_drift : [] };
|
|
35
|
+
}
|
|
36
|
+
|
|
23
37
|
function liveDrift(plan: unknown): unknown {
|
|
24
38
|
if (plan === null || typeof plan !== "object" || Array.isArray(plan)) return plan;
|
|
25
39
|
const p = plan as Json;
|
|
@@ -133,3 +147,78 @@ describe("documents with nothing to read", () => {
|
|
|
133
147
|
}
|
|
134
148
|
});
|
|
135
149
|
});
|
|
150
|
+
|
|
151
|
+
// ── the whole changes (#4812) ─────────────────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
describe("whole changes", () => {
|
|
154
|
+
const stock = ["refresh-plan.json", "refresh-plan-module.json"].map(read);
|
|
155
|
+
const live = [read("live-root-plan.json"), readAt("live-plan-depth", "show-plan.json"), readAt("live-plan-depth", "show-plan-sensitive.json")];
|
|
156
|
+
|
|
157
|
+
test("a refresh-only plan's drift report plan is byte-identical to terragucci's driftPlan", () => {
|
|
158
|
+
for (const plan of stock) expect(JSON.stringify(terraformDriftPlan(plan))).toBe(JSON.stringify(driftPlan(plan)));
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("a live root's full plan read as drift is byte-identical to terragucci's liveDrift", () => {
|
|
162
|
+
for (const plan of live) expect(JSON.stringify(terraformLivePlanDrift(plan))).toBe(JSON.stringify(liveDrift(plan)));
|
|
163
|
+
// And then as a drift report's plan, as terragucci's stage reads a live root.
|
|
164
|
+
for (const plan of live) expect(JSON.stringify(terraformDriftPlan(terraformLivePlanDrift(plan)))).toBe(JSON.stringify(driftPlan(liveDrift(plan))));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("a stock change keeps module, index, deposed, the provider and the masks as the plan has them", () => {
|
|
168
|
+
const plan = read("refresh-plan-module.json") as { resource_drift: unknown[] };
|
|
169
|
+
const changes = terraformPlanDriftChanges(plan);
|
|
170
|
+
expect(changes).toEqual(plan.resource_drift);
|
|
171
|
+
expect(changes[0]).toMatchObject({ module_address: "module.service", index: "orders", provider_name: "registry.terraform.io/hashicorp/aws" });
|
|
172
|
+
expect(changes[0]!.change).toMatchObject({ before_sensitive: { kms_data_key: true }, after_sensitive: { kms_data_key: true }, after_unknown: {} });
|
|
173
|
+
expect(changes[1]).toMatchObject({ address: "aws_instance.web", deposed: "00000001", change: { actions: ["delete"], after: null } });
|
|
174
|
+
// Code waiting on main is not drift.
|
|
175
|
+
expect((terraformDriftPlan(plan) as { resource_changes: { address: string }[] }).resource_changes.map((c) => c.address)).toEqual([
|
|
176
|
+
'module.service.aws_sqs_queue.jobs["orders"]',
|
|
177
|
+
"aws_instance.web",
|
|
178
|
+
]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a live change is seen from the other side: code before, live after, masks swapped, an unknown taking the live value", () => {
|
|
182
|
+
const changes = terraformPlanDriftChanges(read("live-root-plan.json"), { live: true });
|
|
183
|
+
expect(changes.map((c) => [c.address, (c.change as { actions: string[] }).actions])).toEqual([
|
|
184
|
+
["aws_s3_bucket.assets", ["delete"]],
|
|
185
|
+
["aws_sqs_queue.stray", ["create"]],
|
|
186
|
+
["aws_lambda_function.api", ["update"]],
|
|
187
|
+
["aws_iam_role.deployer", ["update"]],
|
|
188
|
+
["aws_security_group.edge", ["update"]],
|
|
189
|
+
]);
|
|
190
|
+
expect(changes[2]!.change).toEqual({
|
|
191
|
+
actions: ["update"],
|
|
192
|
+
before: { function_name: "acme-api", memory_size: 512, version: "7" },
|
|
193
|
+
after: { function_name: "acme-api", memory_size: 256, version: "7" },
|
|
194
|
+
before_sensitive: false,
|
|
195
|
+
after_sensitive: false,
|
|
196
|
+
});
|
|
197
|
+
const sensitive = terraformPlanDriftChanges(readAt("live-plan-depth", "show-plan-sensitive.json"), { live: true });
|
|
198
|
+
const vpc = sensitive.find((c) => c.address === "aws_vpc.main")!.change as Json;
|
|
199
|
+
const source = (readAt("live-plan-depth", "show-plan-sensitive.json") as { resource_changes: { address: string; change: Json }[] }).resource_changes.find((c) => c.address === "aws_vpc.main")!.change;
|
|
200
|
+
expect(vpc.before_sensitive).toEqual(source.after_sensitive);
|
|
201
|
+
expect(vpc.after_sensitive).toEqual(source.before_sensitive);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("one row per whole change, the same count either way", () => {
|
|
205
|
+
for (const plan of stock) expect(terraformPlanDriftRows(plan).map((r) => r.address)).toEqual(terraformPlanDriftChanges(plan).map((c) => c.address));
|
|
206
|
+
for (const plan of live) {
|
|
207
|
+
expect(terraformPlanDriftRows(plan, { live: true }).map((r) => r.address)).toEqual(terraformPlanDriftChanges(plan, { live: true }).map((c) => c.address));
|
|
208
|
+
expect(terraformPlanDriftCount(terraformLivePlanDrift(plan))).toBe(terraformPlanDriftCount(plan, { live: true }));
|
|
209
|
+
expect(terraformPlanDriftNames(terraformLivePlanDrift(plan))).toEqual(terraformPlanDriftNames(plan, { live: true }));
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test("a missing or malformed plan is returned as it is, with no changes", () => {
|
|
214
|
+
for (const plan of [undefined, null, "plan", []]) {
|
|
215
|
+
expect(terraformDriftPlan(plan)).toBe(plan);
|
|
216
|
+
expect(terraformLivePlanDrift(plan)).toBe(plan);
|
|
217
|
+
expect(terraformPlanDriftChanges(plan)).toEqual([]);
|
|
218
|
+
}
|
|
219
|
+
for (const plan of [{}, { resource_drift: "x" }]) {
|
|
220
|
+
expect(terraformDriftPlan(plan)).toEqual(driftPlan(plan));
|
|
221
|
+
expect(terraformLivePlanDrift(plan)).toEqual(liveDrift(plan));
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
package/src/drift-rows.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Drift rows (#4580): one row per object a plan found drifted, by address,
|
|
3
3
|
* with its action and the object's real name, counted the way a product's
|
|
4
4
|
* drift report counts. `./drift-record.ts` answers a different question (the
|
|
5
|
-
* status of each instance the plan names), so it skips create
|
|
6
|
-
*
|
|
5
|
+
* status of each instance the plan names, redacted), so it skips create
|
|
6
|
+
* rows and carries no names.
|
|
7
7
|
*
|
|
8
8
|
* A stock root's refresh-only plan reports drift as `resource_drift`, and
|
|
9
9
|
* every entry there is one row, data blocks and creates included. A full
|
|
@@ -14,8 +14,15 @@
|
|
|
14
14
|
* Data blocks, no-op and read entries are not drift.
|
|
15
15
|
*
|
|
16
16
|
* These are terragucci's rules (packages/terragucci/src/report/drift.ts:
|
|
17
|
-
* `liveDrift`, `driftCount`, `driftNames`), so a row count here
|
|
18
|
-
* count its report gives for the same plan.
|
|
17
|
+
* `driftPlan`, `liveDrift`, `driftCount`, `driftNames`), so a row count here
|
|
18
|
+
* equals the count its report gives for the same plan.
|
|
19
|
+
*
|
|
20
|
+
* Behind every row is a whole change (#4812): the plan's entry for the
|
|
21
|
+
* object, shaped as a `resource_changes[]` entry with its actions, full
|
|
22
|
+
* before and after and their masks (`terraformPlanDriftChanges`). A drift
|
|
23
|
+
* report is a plan report over those changes: `terraformDriftPlan` puts a
|
|
24
|
+
* refresh-only plan's drift in place of its changes, and
|
|
25
|
+
* `terraformLivePlanDrift` reads a live root's full plan as drift.
|
|
19
26
|
*
|
|
20
27
|
* A row carries no attribute values. Its `name` is the first non-empty
|
|
21
28
|
* string among `name`, `id`, `bucket`, `function_name` and `arn`, read from
|
|
@@ -85,43 +92,107 @@ function stockAction(actions: readonly string[]): string {
|
|
|
85
92
|
return "update";
|
|
86
93
|
}
|
|
87
94
|
|
|
88
|
-
/**
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
return list.map((entry) => {
|
|
95
|
+
/** One row per whole change. */
|
|
96
|
+
function rowsOf(changes: readonly unknown[]): TerraformDriftRow[] {
|
|
97
|
+
return changes.map((entry) => {
|
|
92
98
|
const ch = isObject(entry) && isObject(entry.change) ? entry.change : {};
|
|
93
99
|
const actions = Array.isArray(ch.actions) ? ch.actions.map(String) : [];
|
|
94
100
|
return rowOf(entry, stockAction(actions), [ch.after, ch.before]);
|
|
95
101
|
});
|
|
96
102
|
}
|
|
97
103
|
|
|
98
|
-
/**
|
|
99
|
-
|
|
104
|
+
/**
|
|
105
|
+
* A live root's full plan, its changes read as drift: each one the plan
|
|
106
|
+
* would make, seen from the other side, as terragucci's `liveDrift` builds
|
|
107
|
+
* it. The code's value is before and the live value after, the masks swap
|
|
108
|
+
* with them, and a value the plan knows only after the apply takes the live
|
|
109
|
+
* one, since it is not a difference.
|
|
110
|
+
*/
|
|
111
|
+
function liveChanges(doc: Json): Json[] {
|
|
100
112
|
const list = Array.isArray(doc.resource_changes) ? doc.resource_changes : [];
|
|
101
|
-
const out:
|
|
113
|
+
const out: Json[] = [];
|
|
102
114
|
for (const entry of list) {
|
|
103
115
|
if (!isObject(entry) || entry.mode === "data") continue;
|
|
104
116
|
const ch = isObject(entry.change) ? entry.change : {};
|
|
105
117
|
const actions = Array.isArray(ch.actions) ? ch.actions.map(String) : [];
|
|
106
118
|
if (actions.length === 0 || actions.every((a) => a === "no-op" || a === "read")) continue;
|
|
107
119
|
const action = actions.length === 1 ? (FLIP[actions[0]!] ?? actions[0]!) : "update";
|
|
108
|
-
const live =
|
|
109
|
-
let code =
|
|
110
|
-
// A value the plan knows only after the apply is not a difference: the live one stands for it.
|
|
120
|
+
const live = (ch.before ?? null) as unknown;
|
|
121
|
+
let code = (ch.after ?? null) as unknown;
|
|
111
122
|
const unknown = isObject(ch.after_unknown) ? ch.after_unknown : {};
|
|
112
|
-
if (code
|
|
113
|
-
|
|
123
|
+
if (isObject(code) && isObject(live)) {
|
|
124
|
+
const known = live;
|
|
125
|
+
code = { ...code, ...Object.fromEntries(Object.keys(unknown).filter((k) => unknown[k] === true && k in known).map((k) => [k, known[k]])) };
|
|
126
|
+
}
|
|
127
|
+
out.push({
|
|
128
|
+
...entry,
|
|
129
|
+
change: { actions: [action], before: code, after: live, before_sensitive: ch.after_sensitive ?? false, after_sensitive: ch.before_sensitive ?? false },
|
|
130
|
+
});
|
|
114
131
|
}
|
|
115
132
|
return out;
|
|
116
133
|
}
|
|
117
134
|
|
|
135
|
+
/** A refresh-only plan's `resource_drift`, each entry as the plan has it. */
|
|
136
|
+
function stockChanges(doc: Json): Json[] {
|
|
137
|
+
return (Array.isArray(doc.resource_drift) ? [...doc.resource_drift] : []) as Json[];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The drift a plan (`show -json`, parsed) found as whole changes (#4812),
|
|
142
|
+
* one per drifted object in the plan's order, each shaped as a
|
|
143
|
+
* `resource_changes[]` entry: `address`, `mode`, `type`, `name`,
|
|
144
|
+
* `module_address`, `index`, `deposed` and `provider_name` as the plan has
|
|
145
|
+
* them, and `change` with its actions, its full before and after, and their
|
|
146
|
+
* masks. A stock root's are its `resource_drift` entries unchanged. A live
|
|
147
|
+
* root's are its full plan's changes seen from the other side: a create
|
|
148
|
+
* there is a `delete` (the object no longer exists), a delete a `create`
|
|
149
|
+
* (an object exists outside the code), any pair of actions an `update`, the
|
|
150
|
+
* code's value before and the live one after, the sensitive masks swapped
|
|
151
|
+
* with them and an unknown value replaced by the live one.
|
|
152
|
+
*
|
|
153
|
+
* Values are not redacted: a change carries what the plan carries, masks
|
|
154
|
+
* included, for a report that renders the plan. A drift record
|
|
155
|
+
* (`./drift-record.ts`) is the redacted read.
|
|
156
|
+
*
|
|
157
|
+
* Its length is the drift count, and `terraformPlanDriftRows` is one row
|
|
158
|
+
* per entry. A malformed `resource_drift` entry that is not an object is
|
|
159
|
+
* passed on as the plan has it, and its row has the address `""`.
|
|
160
|
+
*/
|
|
161
|
+
export function terraformPlanDriftChanges(plan: unknown, options: TerraformPlanDriftRowsOptions = {}): Record<string, unknown>[] {
|
|
162
|
+
if (!isObject(plan)) return [];
|
|
163
|
+
return options.live ? liveChanges(plan) : stockChanges(plan);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* A refresh-only plan as its drift report reads it: the drift found in place
|
|
168
|
+
* of the changes proposed, so the report is built from the same JSON as a
|
|
169
|
+
* plan's and code waiting on the default branch never shows in it.
|
|
170
|
+
* `resource_changes` becomes `terraformPlanDriftChanges(plan)`; the rest of
|
|
171
|
+
* the document is kept. Anything not an object is returned as it is.
|
|
172
|
+
*/
|
|
173
|
+
export function terraformDriftPlan(plan: unknown): unknown {
|
|
174
|
+
if (!isObject(plan)) return plan;
|
|
175
|
+
return { ...plan, resource_changes: terraformPlanDriftChanges(plan) };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* A full plan of a root under live resource markers, as drift: its
|
|
180
|
+
* `resource_drift` becomes `terraformPlanDriftChanges(plan, { live: true })`
|
|
181
|
+
* and the rest of the document is kept, so it reads as a refresh-only plan
|
|
182
|
+
* would (`terraformDriftPlan`, and every function here without `live`).
|
|
183
|
+
* Anything not an object is returned as it is.
|
|
184
|
+
*/
|
|
185
|
+
export function terraformLivePlanDrift(plan: unknown): unknown {
|
|
186
|
+
if (!isObject(plan)) return plan;
|
|
187
|
+
return { ...plan, resource_drift: terraformPlanDriftChanges(plan, { live: true }) };
|
|
188
|
+
}
|
|
189
|
+
|
|
118
190
|
/**
|
|
119
191
|
* The drift a plan (`show -json`, parsed) found, one row per drifted object
|
|
120
192
|
* in the plan's order. Its length is the drift count.
|
|
121
193
|
*/
|
|
122
194
|
export function terraformPlanDriftRows(plan: unknown, options: TerraformPlanDriftRowsOptions = {}): TerraformDriftRow[] {
|
|
123
|
-
|
|
124
|
-
return options.live ? liveRows(plan) : stockRows(plan);
|
|
195
|
+
return rowsOf(terraformPlanDriftChanges(plan, options));
|
|
125
196
|
}
|
|
126
197
|
|
|
127
198
|
/** How many objects a plan found drifted: `terraformPlanDriftRows(plan, options).length`. */
|