@intentius/chant-lexicon-terraform 0.124.77 → 0.124.79

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.
Files changed (43) hide show
  1. package/dist/change-set.d.ts +1 -0
  2. package/dist/change-set.d.ts.map +1 -1
  3. package/dist/change-set.js +19 -0
  4. package/dist/components/capability-plugin.d.ts +9 -1
  5. package/dist/components/capability-plugin.d.ts.map +1 -1
  6. package/dist/components/capability-plugin.js +44 -22
  7. package/dist/drift-record.d.ts +24 -0
  8. package/dist/drift-record.d.ts.map +1 -1
  9. package/dist/drift-record.js +52 -0
  10. package/dist/drift-rows.d.ts +49 -4
  11. package/dist/drift-rows.d.ts.map +1 -1
  12. package/dist/drift-rows.js +87 -19
  13. package/dist/integrity.json +2 -2
  14. package/dist/manifest.json +1 -1
  15. package/dist/op/activities/terraform.d.ts +11 -0
  16. package/dist/op/activities/terraform.d.ts.map +1 -1
  17. package/dist/op/activities/terraform.js +9 -1
  18. package/dist/terragrunt/capability.d.ts +129 -0
  19. package/dist/terragrunt/capability.d.ts.map +1 -0
  20. package/dist/terragrunt/capability.js +347 -0
  21. package/dist/terragrunt/index.d.ts +3 -1
  22. package/dist/terragrunt/index.d.ts.map +1 -1
  23. package/dist/terragrunt/index.js +3 -1
  24. package/dist/terragrunt/units.d.ts +9 -0
  25. package/dist/terragrunt/units.d.ts.map +1 -1
  26. package/dist/terragrunt/units.js +25 -0
  27. package/package.json +2 -2
  28. package/src/__fixtures__/drift-rows/refresh-plan-module.json +48 -0
  29. package/src/change-set.test.ts +27 -1
  30. package/src/change-set.ts +19 -0
  31. package/src/components/capability-plugin.test.ts +61 -6
  32. package/src/components/capability-plugin.ts +48 -23
  33. package/src/components/compose-op-wave.test.ts +134 -0
  34. package/src/drift-record.test.ts +62 -0
  35. package/src/drift-record.ts +58 -0
  36. package/src/drift-rows.test.ts +90 -1
  37. package/src/drift-rows.ts +89 -18
  38. package/src/op/activities/terraform.test.ts +21 -0
  39. package/src/op/activities/terraform.ts +20 -1
  40. package/src/terragrunt/capability.test.ts +327 -0
  41. package/src/terragrunt/capability.ts +431 -0
  42. package/src/terragrunt/index.ts +3 -1
  43. package/src/terragrunt/units.ts +25 -0
@@ -0,0 +1,134 @@
1
+ /**
2
+ * A composed run as an op wave (#4842): a wave marked `together` on
3
+ * `terraform-apply` plans its roots as one composed run through the group
4
+ * methods (`./compose-group.ts`, chant's `ComposedRun`) and applies it in
5
+ * one call, as terragucci's `waves.compose` does. The run is a fake
6
+ * `ComposedRunner`, so nothing here runs a binary; the composition is real,
7
+ * read from roots written to a temporary project.
8
+ */
9
+
10
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
11
+ import { tmpdir } from "node:os";
12
+ import { dirname, join } from "node:path";
13
+ import { afterAll, describe, expect, test } from "vitest";
14
+ import { CapabilityRegistry, type Capability } from "@intentius/chant/components/capability";
15
+ import { memoryGateLedgerPort, runOpWave, type OpWavesSpec } from "@intentius/chant/op";
16
+ import type { ChangeSetPart } from "@intentius/chant/change-set";
17
+ import type { TerraformConfig } from "../config";
18
+ import type { ComposedRunApply, ComposedRunner, ComposedRunPlan } from "../compose/estate";
19
+ import { composeGroup, type GroupRunnerOptions } from "./compose-group";
20
+ import { terraformApplyCapability } from "./capability-plugin";
21
+
22
+ const made: string[] = [];
23
+ afterAll(() => {
24
+ for (const d of made) rmSync(d, { recursive: true, force: true });
25
+ });
26
+
27
+ function project(): string {
28
+ const dir = mkdtempSync(join(tmpdir(), "chant-compose-op-wave-"));
29
+ made.push(dir);
30
+ const roots: Record<string, string> = {
31
+ "roots/a/main.tf": 'resource "terraform_data" "x" {\n input = "a"\n}\n\noutput "id" {\n value = terraform_data.x.output\n}\n',
32
+ "roots/b/main.tf": 'variable "a_id" {\n type = string\n}\n\nresource "terraform_data" "x" {\n input = var.a_id\n}\n',
33
+ };
34
+ for (const [path, text] of Object.entries(roots)) {
35
+ mkdirSync(dirname(join(dir, path)), { recursive: true });
36
+ writeFileSync(join(dir, path), text);
37
+ }
38
+ return dir;
39
+ }
40
+
41
+ const terraform = { binary: "tofu", roots: { a: { dir: "roots/a" }, b: { dir: "roots/b" } }, compose: { enabled: true } } as unknown as TerraformConfig;
42
+
43
+ const part = (root: string): ChangeSetPart => ({
44
+ member: { member: root, lexicon: "terraform", planner: "tofu", status: "planned", planDigest: `jcs1-sha256:${root}`, holes: [] },
45
+ entries: [{ member: root, address: "terraform_data.x", type: "terraform_data", action: "create" }] as never,
46
+ });
47
+
48
+ function composedPlan(digest: string): ComposedRunPlan {
49
+ const member = (root: string) => ({
50
+ member: root,
51
+ planDigest: `jcs1-sha256:${root}`,
52
+ changeSet: part(root),
53
+ outputs: { known: {}, unknown: ["id"] },
54
+ shape: { member: root, entries: [{ logicalId: "terraform_data.x", action: "create" }], unknownInputs: [] },
55
+ slice: {},
56
+ counts: { add: 1, change: 0, destroy: 0, changes: 1, destroys: 0 },
57
+ reads: [],
58
+ });
59
+ return { members: { a: member("a"), b: member("b") }, digest, planFile: "/w/composed.tfplan", text: "", texts: {}, generated: [], counts: { add: 2, change: 0, destroy: 0, changes: 2, destroys: 0 }, summary: "Plan: 2 to add, 0 to change, 0 to destroy." } as unknown as ComposedRunPlan;
60
+ }
61
+
62
+ const appliedBoth: ComposedRunApply = {
63
+ ok: true,
64
+ members: { a: { status: "applied", planned: 1, done: 1, outputs: { id: "a-1" } }, b: { status: "applied", planned: 1, done: 1, outputs: {} } },
65
+ } as unknown as ComposedRunApply;
66
+
67
+ /** `terraform-apply` with its group methods on a fake run: each plan answers the next of `digests`. */
68
+ function capability(dir: string, digests: string[]): { registry: CapabilityRegistry; calls: string[]; built: GroupRunnerOptions[] } {
69
+ const calls: string[] = [];
70
+ const built: GroupRunnerOptions[] = [];
71
+ const g = composeGroup({
72
+ project: async () => ({ projectRoot: dir, terraform }),
73
+ runner: (o): ComposedRunner => {
74
+ built.push(o);
75
+ return {
76
+ plan: async () => {
77
+ calls.push(`plan lock=${o.lock}`);
78
+ return composedPlan(digests.shift() ?? "sha256:composed-1");
79
+ },
80
+ apply: async () => {
81
+ calls.push("apply");
82
+ return appliedBoth;
83
+ },
84
+ release: async () => void calls.push("release"),
85
+ } as ComposedRunner;
86
+ },
87
+ identity: async () => undefined,
88
+ env: {},
89
+ log: () => {},
90
+ });
91
+ const cap = { ...terraformApplyCapability, planGroup: g.planGroup, runGroup: g.runGroup } as unknown as Capability<never, unknown>;
92
+ return { registry: new CapabilityRegistry().register(cap), calls, built };
93
+ }
94
+
95
+ const spec: OpWavesSpec = {
96
+ name: "tf-apply",
97
+ op: "tf-apply",
98
+ gateName: "wave-{k}",
99
+ step: { kind: "terraform-apply", root: "{target}" },
100
+ waves: [
101
+ {
102
+ name: "wave-1",
103
+ together: true,
104
+ gate: "never",
105
+ runs: [{ target: "a" }, { target: "b", dependsOn: ["a"], input: { vars: { a_id: { stackOutput: { stack: "a", name: "id" } } } } }],
106
+ },
107
+ ],
108
+ };
109
+
110
+ const wave = (cwd: string, registry: CapabilityRegistry) =>
111
+ runOpWave({ spec, specFile: "waves.json", wave: 1, cwd, show: () => ({ sha: "b".repeat(40), text: JSON.stringify(spec) }), gates: memoryGateLedgerPort(), capabilities: registry, log: () => {} });
112
+
113
+ describe("a composed run as a together op wave (#4842)", () => {
114
+ test("plans the roots as one composed run without locks, and applies that same plan under them in one call", async () => {
115
+ const dir = project();
116
+ const { registry, calls, built } = capability(dir, ["sha256:composed-1", "sha256:composed-1"]);
117
+ const result = await wave(dir, registry);
118
+ expect(result.exitCode).toBe(0);
119
+ expect(calls).toEqual(["plan lock=false", "release", "plan lock=true", "apply", "release"]);
120
+ expect(built[0]!.composition.members).toEqual(["a", "b"]);
121
+ expect(built[0]!.vars).toEqual({ a: undefined, b: { a_id: { stackOutput: { stack: "a", name: "id" } } } });
122
+ expect(result.applied).toEqual(["a", "b"]);
123
+ expect(result.decision?.members.map((m) => m.member)).toEqual(["a", "b"]);
124
+ });
125
+
126
+ test("a composed plan that moved since the wave was decided applies nothing, and the wave exits 4", async () => {
127
+ const dir = project();
128
+ const { registry, calls } = capability(dir, ["sha256:composed-1", "sha256:composed-2"]);
129
+ const result = await wave(dir, registry);
130
+ expect(result.exitCode).toBe(4);
131
+ expect(result.moved).toEqual(["a", "b"]);
132
+ expect(calls).not.toContain("apply");
133
+ });
134
+ });
@@ -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
+ });
@@ -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"];
@@ -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 { terraformPlanDriftCount, terraformPlanDriftNames, terraformPlanDriftRows } from "./drift-rows";
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 rows, carries
6
- * no names and stays as it is.
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 equals the
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
- /** A refresh-only plan's `resource_drift`, one row per entry. */
89
- function stockRows(doc: Json): TerraformDriftRow[] {
90
- const list = Array.isArray(doc.resource_drift) ? doc.resource_drift : [];
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
- /** A live root's full plan, its changes read as drift. */
99
- function liveRows(doc: Json): TerraformDriftRow[] {
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: TerraformDriftRow[] = [];
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 = isObject(ch.before) ? ch.before : null;
109
- let code = isObject(ch.after) ? ch.after : null;
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 && live) code = { ...code, ...Object.fromEntries(Object.keys(unknown).filter((k) => unknown[k] === true && k in live).map((k) => [k, live[k]])) };
113
- out.push(rowOf(entry, action, [live, code]));
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
- if (!isObject(plan)) return [];
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`. */
@@ -397,6 +397,27 @@ describe("root resolution against terraform.roots (#2086)", () => {
397
397
  await terraformInit({ root: "app", cwd: dir });
398
398
  expect(execCalls[0].cmd.startsWith("tofu init")).toBe(true);
399
399
  });
400
+
401
+ test("a root with no entry plans the dir it is given, with the namespace's binary (#4842)", async () => {
402
+ const dir = project({ binary: "tofu", roots: { app: { dir: "./infra" } } });
403
+ mkdirSync(join(dir, "live", "net"), { recursive: true });
404
+ const result = await terraformInit({ root: "live/net", dir: "live/net", cwd: dir });
405
+ expect(result.dir).toBe(resolve(dir, "live/net"));
406
+ expect(execCalls[0].cmd).toBe("tofu init -input=false");
407
+ expect(execCalls[0].opts.cwd).toBe(resolve(dir, "live/net"));
408
+ // Without a dir, an unknown name still fails.
409
+ await expect(terraformInit({ root: "live/net", cwd: dir })).rejects.toThrow(/no root named "live\/net"/);
410
+ });
411
+
412
+ test("env is laid over the job's for every command of the root (#4842)", async () => {
413
+ const dir = project({ roots: { app: { dir: "./infra", workspace: "blue" } } });
414
+ await terraformInit({ root: "app", cwd: dir, env: { AWS_ROLE_ARN: "arn:aws:iam::1:role/app", TF_WORKSPACE: "green" } });
415
+ expect(execCalls[0].opts.env).toMatchObject({ TF_IN_AUTOMATION: "1", AWS_ROLE_ARN: "arn:aws:iam::1:role/app", TF_WORKSPACE: "green" });
416
+ await terraformApply({ root: "app", cwd: dir, planFile: "chant.tfplan", env: { AWS_ROLE_ARN: "arn:aws:iam::1:role/app" } });
417
+ expect(execCalls[1].opts.env).toMatchObject({ AWS_ROLE_ARN: "arn:aws:iam::1:role/app", TF_WORKSPACE: "blue" });
418
+ await terraformInit({ root: "app", cwd: dir });
419
+ expect(execCalls[2].opts.env?.AWS_ROLE_ARN).toBe(process.env.AWS_ROLE_ARN);
420
+ });
400
421
  });
401
422
 
402
423
  // ── terraformInit ───────────────────────────────────────────────────────────
@@ -203,8 +203,19 @@ export interface TerraformRootArgs {
203
203
  * under a release sets it to the release's recorded copy of the root; the
204
204
  * entry's workspace, var files, backend config and estate still apply, and
205
205
  * its var files and backend-config files resolve against this directory.
206
+ *
207
+ * With no `terraform.roots` entry named `root`, a `dir` names the root's
208
+ * directory itself (chant#4842): the root plans there with the namespace's
209
+ * binary and no workspace, var files or backend config of an entry.
206
210
  */
207
211
  dir?: string;
212
+ /**
213
+ * Environment variables every command for the root runs with, over the
214
+ * job's and the root's own (`TF_WORKSPACE`), such as a role's credentials
215
+ * (chant#4842). An op wave's run hook gives them per run through
216
+ * `DeployContext.processEnv`.
217
+ */
218
+ env?: Record<string, string>;
208
219
  }
209
220
 
210
221
  export interface TerraformInitArgs extends TerraformRootArgs {
@@ -608,9 +619,14 @@ export function terraformEnvironment(root?: Pick<TerraformRootConfig, "workspace
608
619
  return {
609
620
  TF_IN_AUTOMATION: "1",
610
621
  ...(root?.workspace ? { TF_WORKSPACE: root.workspace } : {}),
622
+ // chant#4842: the run's own environment, which resolveRoot ties to the root it resolved.
623
+ ...(root ? rootProcessEnv.get(root) : undefined),
611
624
  };
612
625
  }
613
626
 
627
+ /** The `env` a root was resolved with (chant#4842), by the resolved root's entry, so every command run for it gets it. */
628
+ const rootProcessEnv = new WeakMap<object, Record<string, string>>();
629
+
614
630
  /**
615
631
  * `terraform init`, with the root's `backendConfigFiles` as
616
632
  * `-backend-config=<path>` flags and then its `backendConfig` as
@@ -958,7 +974,10 @@ async function resolveRoot(args: TerraformRootArgs, signal?: AbortSignal): Promi
958
974
  const projectRoot = configPath ? dirname(configPath) : start;
959
975
  const namespace = (config as { terraform?: TerraformConfig }).terraform;
960
976
  const roots = namespace?.roots ?? {};
961
- const root = roots[args.root];
977
+ // chant#4842: a root with no entry and a dir of its own plans that directory.
978
+ const declared = roots[args.root] ?? (args.dir !== undefined ? { dir: args.dir } : undefined);
979
+ const root = declared && args.env ? { ...declared } : declared;
980
+ if (root && args.env) rootProcessEnv.set(root, args.env);
962
981
  if (!root) {
963
982
  const known = Object.keys(roots).sort();
964
983
  throw new Error(