@intentius/chant 0.58.0 → 0.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/dist/audit/core.d.ts +17 -1
  2. package/dist/audit/core.d.ts.map +1 -1
  3. package/dist/audit/discover.d.ts +15 -4
  4. package/dist/audit/discover.d.ts.map +1 -1
  5. package/dist/cli/commands/audit.d.ts.map +1 -1
  6. package/dist/cli/commands/build.d.ts.map +1 -1
  7. package/dist/cli/commands/carve-bridge.d.ts.map +1 -1
  8. package/dist/cli/commands/lint.d.ts +13 -0
  9. package/dist/cli/commands/lint.d.ts.map +1 -1
  10. package/dist/cli/handlers/operator.d.ts.map +1 -1
  11. package/dist/cli/handlers/run.d.ts.map +1 -1
  12. package/dist/cli/main.d.ts +0 -16
  13. package/dist/cli/main.d.ts.map +1 -1
  14. package/dist/cli/plugins.d.ts +22 -0
  15. package/dist/cli/plugins.d.ts.map +1 -1
  16. package/dist/cli/registry.d.ts +14 -0
  17. package/dist/cli/registry.d.ts.map +1 -1
  18. package/dist/components/cli-support.d.ts +4 -1
  19. package/dist/components/cli-support.d.ts.map +1 -1
  20. package/dist/components/component.d.ts +19 -4
  21. package/dist/components/component.d.ts.map +1 -1
  22. package/dist/components/driver.d.ts +8 -2
  23. package/dist/components/driver.d.ts.map +1 -1
  24. package/dist/components/verbs/run-agent.d.ts +1 -7
  25. package/dist/components/verbs/run-agent.d.ts.map +1 -1
  26. package/dist/detectLexicon.d.ts +13 -0
  27. package/dist/detectLexicon.d.ts.map +1 -1
  28. package/dist/lexicon.d.ts +170 -3
  29. package/dist/lexicon.d.ts.map +1 -1
  30. package/dist/lifecycle/gate-ledger.d.ts +9 -1
  31. package/dist/lifecycle/gate-ledger.d.ts.map +1 -1
  32. package/dist/lint/rules/comp/comp004-gate-needs-durable-runtime.d.ts.map +1 -1
  33. package/dist/op/activities/index.d.ts +2 -2
  34. package/dist/op/activities/index.d.ts.map +1 -1
  35. package/dist/op/activities/reconcile.d.ts +62 -2
  36. package/dist/op/activities/reconcile.d.ts.map +1 -1
  37. package/dist/op/builders.d.ts +2 -2
  38. package/dist/op/builders.d.ts.map +1 -1
  39. package/dist/op/change-signal.d.ts +91 -0
  40. package/dist/op/change-signal.d.ts.map +1 -0
  41. package/dist/op/composites/apply-op.d.ts +7 -2
  42. package/dist/op/composites/apply-op.d.ts.map +1 -1
  43. package/dist/op/composites/reconcile-op.d.ts.map +1 -1
  44. package/dist/op/gate-name.d.ts +40 -0
  45. package/dist/op/gate-name.d.ts.map +1 -0
  46. package/dist/op/gate-summary.d.ts +43 -0
  47. package/dist/op/gate-summary.d.ts.map +1 -0
  48. package/dist/op/index.d.ts +6 -2
  49. package/dist/op/index.d.ts.map +1 -1
  50. package/dist/op/local-executor.d.ts.map +1 -1
  51. package/dist/op/op-ir.d.ts +2 -1
  52. package/dist/op/op-ir.d.ts.map +1 -1
  53. package/dist/op/operator.d.ts +63 -0
  54. package/dist/op/operator.d.ts.map +1 -1
  55. package/dist/op/types.d.ts +18 -3
  56. package/dist/op/types.d.ts.map +1 -1
  57. package/dist/terraform/bridge.d.ts +26 -7
  58. package/dist/terraform/bridge.d.ts.map +1 -1
  59. package/dist/terraform/carve-provider.d.ts +28 -2
  60. package/dist/terraform/carve-provider.d.ts.map +1 -1
  61. package/dist/terraform/data-source-shape.d.ts +77 -0
  62. package/dist/terraform/data-source-shape.d.ts.map +1 -0
  63. package/dist/terraform/graph.d.ts.map +1 -1
  64. package/dist/terraform/providers/kubernetes.d.ts +5 -0
  65. package/dist/terraform/providers/kubernetes.d.ts.map +1 -1
  66. package/dist/terraform/tier-map.d.ts +16 -4
  67. package/dist/terraform/tier-map.d.ts.map +1 -1
  68. package/dist/terraform/types.d.ts +8 -0
  69. package/dist/terraform/types.d.ts.map +1 -1
  70. package/package.json +1 -1
  71. package/src/audit/core.ts +25 -3
  72. package/src/audit/discover.ts +122 -14
  73. package/src/cli/commands/audit.test.ts +5 -3
  74. package/src/cli/commands/audit.ts +5 -1
  75. package/src/cli/commands/build.test.ts +39 -1
  76. package/src/cli/commands/build.ts +14 -0
  77. package/src/cli/commands/carve-bridge.test.ts +237 -14
  78. package/src/cli/commands/carve-bridge.ts +21 -17
  79. package/src/cli/commands/carve-emit-k8s.test.ts +17 -10
  80. package/src/cli/commands/lint.test.ts +146 -1
  81. package/src/cli/commands/lint.ts +82 -7
  82. package/src/cli/handlers/operator.ts +81 -0
  83. package/src/cli/handlers/run.test.ts +132 -3
  84. package/src/cli/handlers/run.ts +87 -3
  85. package/src/cli/main.test.ts +9 -11
  86. package/src/cli/main.ts +5 -27
  87. package/src/cli/plugins.test.ts +68 -2
  88. package/src/cli/plugins.ts +39 -0
  89. package/src/cli/registry.ts +14 -0
  90. package/src/components/README.md +2 -2
  91. package/src/components/__fixtures__/neo4j-fanout.json +1 -1
  92. package/src/components/cli-support.test.ts +18 -7
  93. package/src/components/cli-support.ts +8 -3
  94. package/src/components/component-schema.test.ts +18 -2
  95. package/src/components/component.schema.json +17 -4
  96. package/src/components/component.test.ts +2 -2
  97. package/src/components/component.ts +25 -5
  98. package/src/components/config-defaults.test.ts +2 -2
  99. package/src/components/driver.test.ts +20 -1
  100. package/src/components/driver.ts +12 -5
  101. package/src/components/pilots/neo4j-fanout.pilot.ts +3 -3
  102. package/src/components/verbs/run-agent.test.ts +19 -0
  103. package/src/components/verbs/run-agent.ts +1 -7
  104. package/src/detectLexicon.ts +18 -1
  105. package/src/discovery/fold-import.test.ts +1 -1
  106. package/src/fold/foldable-helpers.ts +1 -1
  107. package/src/graph-ops.test.ts +1 -1
  108. package/src/lexicon.ts +177 -3
  109. package/src/lifecycle/gate-ledger.ts +12 -1
  110. package/src/lint/pipeline-change-gate.test.ts +2 -2
  111. package/src/lint/rules/comp/comp.test.ts +26 -0
  112. package/src/lint/rules/comp/comp004-gate-needs-durable-runtime.ts +4 -2
  113. package/src/lint/rules/op/ops014-converge-rule-refusals.test.ts +1 -1
  114. package/src/op/activities/index.ts +2 -2
  115. package/src/op/activities/reconcile.test.ts +82 -2
  116. package/src/op/activities/reconcile.ts +169 -2
  117. package/src/op/builders.ts +3 -3
  118. package/src/op/change-signal.test.ts +117 -0
  119. package/src/op/change-signal.ts +169 -0
  120. package/src/op/composites/apply-op.ts +14 -4
  121. package/src/op/composites/composites.test.ts +17 -4
  122. package/src/op/composites/reconcile-op.ts +7 -4
  123. package/src/op/effect-step.test.ts +3 -3
  124. package/src/op/gate-name.test.ts +65 -0
  125. package/src/op/gate-name.ts +60 -0
  126. package/src/op/gate-summary.ts +84 -0
  127. package/src/op/index.ts +9 -2
  128. package/src/op/local-executor.test.ts +16 -1
  129. package/src/op/local-executor.ts +4 -3
  130. package/src/op/op-ir.test.ts +12 -1
  131. package/src/op/op-ir.ts +5 -3
  132. package/src/op/op-verb-class.test.ts +2 -2
  133. package/src/op/op.test.ts +2 -2
  134. package/src/op/operator.test.ts +368 -0
  135. package/src/op/operator.ts +141 -5
  136. package/src/op/runtimes/local.test.ts +1 -1
  137. package/src/op/types.ts +23 -3
  138. package/src/terraform/aws-resources.test.ts +13 -4
  139. package/src/terraform/bridge.test.ts +22 -9
  140. package/src/terraform/bridge.ts +89 -28
  141. package/src/terraform/carve-provider.ts +38 -2
  142. package/src/terraform/data-source-shape.ts +95 -0
  143. package/src/terraform/graph.ts +35 -7
  144. package/src/terraform/providers/kubernetes.ts +48 -2
  145. package/src/terraform/tier-map.ts +21 -6
  146. package/src/terraform/types.ts +8 -0
@@ -1,7 +1,8 @@
1
1
  import { describe, test, expect } from "vitest";
2
2
  import { buildFixtureGraph } from "./__fixtures__/build-graph";
3
3
  import { boundaryReport } from "./carve";
4
- import { generateBridge, type CarvedIdentity } from "./bridge";
4
+ import { generateBridge, type CarvedDataSource } from "./bridge";
5
+ import { dataSourceShapeOf } from "./tier-map";
5
6
  import type { Hcl2JsonTree } from "./types";
6
7
 
7
8
  const workedExample: Hcl2JsonTree = {
@@ -35,8 +36,10 @@ const API_TF = `resource "aws_lambda_function" "api" {
35
36
  }
36
37
  `;
37
38
 
38
- const identities = new Map<string, CarvedIdentity>([
39
- ["aws_s3_bucket.assets", { attr: "bucket", value: "myapp-assets-prod" }],
39
+ // The read-back for the bucket: the shape its identity attribute implies, plus
40
+ // the literal the graph resolved out of the block.
41
+ const identities = new Map<string, CarvedDataSource>([
42
+ ["aws_s3_bucket.assets", { shape: dataSourceShapeOf("aws_s3_bucket"), values: { bucket: "myapp-assets-prod" } }],
40
43
  ]);
41
44
 
42
45
  describe("generateBridge — inbound (data-source rewrite)", () => {
@@ -51,18 +54,28 @@ describe("generateBridge — inbound (data-source rewrite)", () => {
51
54
  );
52
55
  });
53
56
 
54
- test("never renders a dotted identity attribute into the data body (#2015)", () => {
57
+ test("a type with no shape gets a TODO body, never a dotted attr assignment (#2015)", () => {
55
58
  const report = boundaryReport(buildFixtureGraph(workedExample), "aws_s3_bucket.assets")!;
56
- const dotted = new Map<string, CarvedIdentity>([
57
- ["aws_s3_bucket.assets", { attr: "manifest.metadata.name", value: "demo-config" }],
58
- ]);
59
- const plan = generateBridge(report, [{ path: "api.tf", content: API_TF }], dotted);
59
+ // A dotted identity attribute implies no shape at all — `canBridge` refuses
60
+ // such a type before the bridge runs — but an empty read-back still has to
61
+ // produce a block Terraform can parse, not `manifest.metadata.name = "x"`.
62
+ const noShape = new Map<string, CarvedDataSource>([["aws_s3_bucket.assets", {}]]);
63
+ const plan = generateBridge(report, [{ path: "api.tf", content: API_TF }], noShape);
60
64
 
61
- // `manifest.metadata.name = "demo-config"` is not valid HCL.
62
65
  expect(plan.dataSources[0].hcl).not.toContain("manifest.metadata.name =");
63
66
  expect(plan.dataSources[0].hcl).toContain("# TODO: identify the resource");
64
67
  });
65
68
 
69
+ test("a shape whose required argument has no literal falls back to the TODO (#2034)", () => {
70
+ const report = boundaryReport(buildFixtureGraph(workedExample), "aws_s3_bucket.assets")!;
71
+ const unresolved = new Map<string, CarvedDataSource>([
72
+ ["aws_s3_bucket.assets", { shape: dataSourceShapeOf("aws_s3_bucket"), values: {} }],
73
+ ]);
74
+ const plan = generateBridge(report, [{ path: "api.tf", content: API_TF }], unresolved);
75
+ // A `data "aws_s3_bucket" "assets" {}` with no `bucket` would not plan.
76
+ expect(plan.dataSources[0].hcl).toContain("# TODO: identify the resource");
77
+ });
78
+
66
79
  test("rewrites survivor references to the data source", () => {
67
80
  const report = boundaryReport(buildFixtureGraph(workedExample), "aws_s3_bucket.assets")!;
68
81
  const plan = generateBridge(report, [{ path: "api.tf", content: API_TF }], identities);
@@ -7,6 +7,9 @@
7
7
  * - inbound edge → the survivor loses its reference to the carved resource.
8
8
  * Bridge: add a `data` source for the (now chant-managed) resource and
9
9
  * rewrite the survivor's `type.name.attr` references to `data.type.name.attr`.
10
+ * The provider's data-source shape (`data-source-shape.ts`, #2034) decides
11
+ * both halves — the data type and body, and the attribute path survivors
12
+ * read through, which need not be the resource's own.
10
13
  * Required immediately, or `terraform plan` errors on the dangling ref.
11
14
  * An `output` block reading the carved resource is such a survivor (#1638):
12
15
  * same data source, same textual rewrite, applied to the output's value.
@@ -22,18 +25,32 @@
22
25
  */
23
26
 
24
27
  import { deferredParamName, type CarveReport } from "./carve";
28
+ import { rewriteReadPath, type DataSourceShape, type ShapeField } from "./data-source-shape";
25
29
  import { exciseResourceBlocks, type ExciseTarget } from "./excise";
26
30
 
27
- export interface CarvedIdentity {
28
- /** The HCL identity attribute, e.g. `bucket` or `name`. */
29
- attr?: string;
30
- /** Its literal value, e.g. `myapp-assets-prod`. */
31
- value?: string;
31
+ /** How one carved resource is read back, and the literals to render it with. */
32
+ export interface CarvedDataSource {
33
+ /**
34
+ * The type's data-source shape (`dataSourceShapeOf`): the `data` type, its
35
+ * arguments and where each comes from, and the survivor path translation.
36
+ */
37
+ shape?: DataSourceShape;
38
+ /**
39
+ * Literals resolved from the carved block, keyed by the shape field's source
40
+ * path — `{ bucket: "myapp-assets-prod" }`, or for a manifest
41
+ * `{ "manifest.apiVersion": "v1", "manifest.metadata.name": "app-config" }`.
42
+ */
43
+ values?: Record<string, string>;
32
44
  }
33
45
 
34
46
  export interface DataSourceBlock {
35
47
  /** Carved resource address the data source stands in for. */
36
48
  address: string;
49
+ /**
50
+ * The `data` type. Usually the carved resource's own type; the shape can
51
+ * name another, as `kubernetes_manifest` reads back through
52
+ * `data "kubernetes_resource"` (#2034).
53
+ */
37
54
  type: string;
38
55
  name: string;
39
56
  hcl: string;
@@ -73,22 +90,64 @@ export interface BridgePlan {
73
90
  runbook: string;
74
91
  }
75
92
 
76
- /** `aws_s3_bucket.assets` → a regex matching that reference head, not already `data.`-prefixed. */
93
+ /**
94
+ * `aws_s3_bucket.assets` → a regex matching that reference head plus the
95
+ * attribute path that follows it, not already `data.`-prefixed. The path is
96
+ * captured because the data source may expose it under a different attribute:
97
+ * a survivor reading `kubernetes_manifest.app_config.manifest.data.x` has to
98
+ * come out as `data.kubernetes_resource.app_config.object.data.x`.
99
+ */
77
100
  function referenceRegex(address: string): RegExp {
78
101
  const escaped = address.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
79
102
  // (?<!data\.) — do not double-prefix an existing data source reference.
80
- return new RegExp(`(?<!data\\.)\\b${escaped}\\b`, "g");
103
+ return new RegExp(`(?<!data\\.)\\b${escaped}\\b((?:\\.[A-Za-z_][A-Za-z0-9_-]*)*)`, "g");
81
104
  }
82
105
 
83
- function dataSourceHcl(type: string, name: string, identity: CarvedIdentity): string {
84
- // A dotted attr is a path into nested blocks; `a.b.c = v` is not valid HCL,
85
- // so it falls back to the TODO. `carve bridge` refuses such a type outright.
86
- const flat = identity.attr !== undefined && !identity.attr.includes(".");
87
- const body =
88
- flat && identity.value !== undefined
89
- ? ` ${identity.attr} = ${JSON.stringify(identity.value)}`
90
- : ` # TODO: identify the resource (its name/id was interpolated in the source)`;
91
- return `data ${JSON.stringify(type)} ${JSON.stringify(name)} {\n${body}\n}`;
106
+ const IDENTIFY_TODO = " # TODO: identify the resource (its name/id was interpolated in the source)";
107
+
108
+ /** `terraform fmt` aligns the `=` of consecutive assignments; so do we. */
109
+ function alignedAssignments(pairs: Array<[string, string]>, indent: string): string[] {
110
+ const width = Math.max(...pairs.map(([key]) => key.length));
111
+ return pairs.map(([key, value]) => `${indent}${key.padEnd(width)} = ${JSON.stringify(value)}`);
112
+ }
113
+
114
+ /** Resolved `[name, value]` pairs, or null when a required field has no literal. */
115
+ function resolveFields(fields: readonly ShapeField[], values: Record<string, string>): Array<[string, string]> | null {
116
+ const pairs: Array<[string, string]> = [];
117
+ for (const field of fields) {
118
+ const value = values[field.from];
119
+ if (value === undefined) {
120
+ if (field.required) return null;
121
+ continue;
122
+ }
123
+ pairs.push([field.name, value]);
124
+ }
125
+ return pairs;
126
+ }
127
+
128
+ /**
129
+ * The `data` block standing in for the carved resource. A shape whose required
130
+ * arguments the carved block did not supply as literals renders the TODO body
131
+ * instead of a block `terraform validate` would reject.
132
+ */
133
+ function dataSourceHcl(shape: DataSourceShape, name: string, values: Record<string, string>): string {
134
+ const body: string[] = [];
135
+ const args = resolveFields(shape.args ?? [], values);
136
+ if (args === null) return todoBlock(shape.type, name);
137
+ if (args.length) body.push(...alignedAssignments(args, " "));
138
+ for (const block of shape.blocks ?? []) {
139
+ const fields = resolveFields(block.fields, values);
140
+ if (fields === null) return todoBlock(shape.type, name);
141
+ if (!fields.length) continue;
142
+ if (body.length) body.push("");
143
+ body.push(` ${block.name} {`, ...alignedAssignments(fields, " "), " }");
144
+ }
145
+ if (!body.length) body.push(IDENTIFY_TODO);
146
+ return `data ${JSON.stringify(shape.type)} ${JSON.stringify(name)} {\n${body.join("\n")}\n}`;
147
+ }
148
+
149
+ function todoBlock(type: string, name: string): string {
150
+ return `data ${JSON.stringify(type)} ${JSON.stringify(name)} {\n${IDENTIFY_TODO}\n}`;
92
151
  }
93
152
 
94
153
  /**
@@ -96,26 +155,26 @@ function dataSourceHcl(type: string, name: string, identity: CarvedIdentity): st
96
155
  *
97
156
  * @param report the boundary report (from `boundaryReport`)
98
157
  * @param files every `.tf` file in the estate, as { path, content }
99
- * @param identities physical identity per carved address, for the data sources
158
+ * @param readBacks how each carved address is read back as a data source, and
159
+ * the literals to render it with (`dataSourceShapeOf` +
160
+ * the graph node's `dataSourceValues`)
100
161
  */
101
162
  export function generateBridge(
102
163
  report: CarveReport,
103
164
  files: Array<{ path: string; content: string }>,
104
- identities: Map<string, CarvedIdentity>,
165
+ readBacks: Map<string, CarvedDataSource>,
105
166
  ): BridgePlan {
106
167
  // Carved resources that something still depends on need a data source.
107
- const carvedWithInbound = new Map<string, { type: string; name: string }>();
168
+ const carvedWithInbound = new Map<string, { type: string; name: string; readBack: CarvedDataSource }>();
108
169
  for (const e of report.inbound) {
109
170
  const [type, ...rest] = e.carved.split(".");
110
- carvedWithInbound.set(e.carved, { type, name: rest.join(".") });
171
+ carvedWithInbound.set(e.carved, { type, name: rest.join("."), readBack: readBacks.get(e.carved) ?? {} });
111
172
  }
112
173
 
113
- const dataSources: DataSourceBlock[] = [...carvedWithInbound.entries()].map(([address, { type, name }]) => ({
114
- address,
115
- type,
116
- name,
117
- hcl: dataSourceHcl(type, name, identities.get(address) ?? {}),
118
- }));
174
+ const dataSources: DataSourceBlock[] = [...carvedWithInbound.entries()].map(([address, { type, name, readBack }]) => {
175
+ const shape = readBack.shape ?? { type };
176
+ return { address, type: shape.type, name, hcl: dataSourceHcl(shape, name, readBack.values ?? {}) };
177
+ });
119
178
 
120
179
  // Excise the carve set's own blocks (#998): after `terraform state rm`, a
121
180
  // block left behind would re-create the resource on the next apply. Then
@@ -129,8 +188,10 @@ export function generateBridge(
129
188
  const rewrites: FileRewrite[] = files.map(({ path, content }) => {
130
189
  const excision = exciseResourceBlocks(content, exciseTargets);
131
190
  let rewritten = excision.content;
132
- for (const address of carvedWithInbound.keys()) {
133
- rewritten = rewritten.replace(referenceRegex(address), `data.${address}`);
191
+ for (const [address, { type, name, readBack }] of carvedWithInbound) {
192
+ const shape = readBack.shape;
193
+ const head = `data.${shape?.type ?? type}.${name}`;
194
+ rewritten = rewritten.replace(referenceRegex(address), (_match, path: string) => head + rewriteReadPath(shape, path));
134
195
  }
135
196
  return { path, original: content, rewritten, changed: rewritten !== content, excised: excision.excised };
136
197
  });
@@ -28,6 +28,7 @@
28
28
  * not require plugin loading for the paths that run without one.
29
29
  */
30
30
 
31
+ import { identityAttrShape, type DataSourceShape } from "./data-source-shape";
31
32
  import { BUILTIN_CARVE_PROVIDERS } from "./providers";
32
33
  import type { StateResource } from "./state";
33
34
 
@@ -95,10 +96,20 @@ export interface CarveProvider {
95
96
  readonly tiers: Readonly<Record<string, TierInfo>>;
96
97
  /**
97
98
  * Terraform type → the HCL attribute carrying the physical name. A dotted
98
- * entry is a path into nested blocks (`manifest.metadata.name`); the bridge
99
- * refuses those, since a data-source body is flat `attr = value`.
99
+ * entry is a path into nested values (`manifest.metadata.name`), which a
100
+ * flat `attr = value` body cannot express, so it implies no data-source
101
+ * shape on its own — see {@link dataSourceShapes}.
100
102
  */
101
103
  readonly identityAttrs?: Readonly<Record<string, string>>;
104
+ /**
105
+ * Terraform type → how `carve bridge` reads it back as a `data` source
106
+ * (#2034): the data-source type, where each argument comes from in the
107
+ * carved body, and how a survivor's attribute path translates. Needed when
108
+ * the data source is not the resource's own type, or when its body is more
109
+ * than one flat argument. A type with a plain identity attribute and no
110
+ * entry here gets that attribute's shape ({@link identityAttrShape}).
111
+ */
112
+ readonly dataSourceShapes?: Readonly<Record<string, DataSourceShape>>;
102
113
  /**
103
114
  * Sub-resource Terraform type → parent type, for types Terraform splits out
104
115
  * of the resource the native spec keeps them in.
@@ -169,6 +180,7 @@ interface RegistryIndex {
169
180
  prefixes: Array<{ prefix: string; provider: CarveProvider }>;
170
181
  tiers: Record<string, TierInfo>;
171
182
  identityAttrs: Record<string, string>;
183
+ dataSourceShapes: Record<string, DataSourceShape>;
172
184
  foldsInto: Record<string, string>;
173
185
  /** Terraform type → the provider that emits it. */
174
186
  emitters: Map<string, CarveProvider>;
@@ -182,6 +194,7 @@ function index(): RegistryIndex {
182
194
  prefixes: [],
183
195
  tiers: {},
184
196
  identityAttrs: {},
197
+ dataSourceShapes: {},
185
198
  foldsInto: {},
186
199
  emitters: new Map(),
187
200
  };
@@ -189,6 +202,7 @@ function index(): RegistryIndex {
189
202
  for (const prefix of provider.tfTypePrefixes) built.prefixes.push({ prefix, provider });
190
203
  Object.assign(built.tiers, provider.tiers);
191
204
  if (provider.identityAttrs) Object.assign(built.identityAttrs, provider.identityAttrs);
205
+ if (provider.dataSourceShapes) Object.assign(built.dataSourceShapes, provider.dataSourceShapes);
192
206
  if (provider.foldsInto) Object.assign(built.foldsInto, provider.foldsInto);
193
207
  for (const tfType of provider.emitTypes ?? []) built.emitters.set(tfType, provider);
194
208
  }
@@ -235,3 +249,25 @@ export function carveIdentityAttr(tfType: string): string | undefined {
235
249
  export function carveFoldParent(tfType: string): string | undefined {
236
250
  return index().foldsInto[tfType];
237
251
  }
252
+
253
+ /**
254
+ * How `carve bridge` reads this type back as a `data` source (#2034), or
255
+ * undefined when it cannot be read back at all — which is what makes
256
+ * `canBridge` refuse it.
257
+ *
258
+ * Resolution order:
259
+ * 1. a shape the owning provider declared for the type;
260
+ * 2. otherwise the shape a plain identity attribute implies (same type, one
261
+ * required argument) — the path every AWS and google type takes;
262
+ * 3. otherwise undefined, when the identity attribute is a dotted path into
263
+ * nested values and no provider declared a shape for it;
264
+ * 4. and for a type with no identity attribute at all, the bare data-source
265
+ * type: the bridge still emits a block, with a TODO for the body.
266
+ */
267
+ export function carveDataSourceShape(tfType: string): DataSourceShape | undefined {
268
+ const declared = index().dataSourceShapes[tfType];
269
+ if (declared) return declared;
270
+ const attr = index().identityAttrs[tfType];
271
+ if (attr === undefined) return { type: tfType };
272
+ return identityAttrShape(tfType, attr);
273
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * How a carved Terraform type is read back as a `data` source (#2034).
3
+ *
4
+ * `carve bridge` replaces a carved resource with a data source the survivors
5
+ * read instead. Until now a carve provider said only which attribute carries
6
+ * the physical name (`identityAttrs`), and the bridge assumed the data source
7
+ * was the same type as the resource, with that one attribute in its body:
8
+ *
9
+ * data "aws_s3_bucket" "assets" { bucket = "myapp-assets-prod" }
10
+ *
11
+ * That assumption holds for AWS and google, and breaks for Kubernetes twice
12
+ * over. `kubernetes_manifest` names itself by `manifest.metadata.name`, a path
13
+ * into a nested value that a flat `attr = value` body cannot express; and the
14
+ * kubernetes provider ships no `kubernetes_manifest` data source at all
15
+ * (verified against hashicorp/kubernetes v3.2.1: 27 data sources, none a
16
+ * manifest). Its generic read is `data "kubernetes_resource"`, with a
17
+ * different type, different arguments, a nested `metadata` block, and its own
18
+ * attribute path for survivors to read through.
19
+ *
20
+ * So a provider contributes a *shape*: the data-source type, where each
21
+ * argument comes from in the carved body, and how a survivor's attribute path
22
+ * translates. An identity attribute is one instance of that shape
23
+ * ({@link identityAttrShape}), so nothing that bridges today changes.
24
+ */
25
+
26
+ /** One argument of the data-source body, and where its value comes from. */
27
+ export interface ShapeField {
28
+ /** Argument name in the emitted data source, e.g. `api_version`. */
29
+ name: string;
30
+ /**
31
+ * Dotted path into the carved resource's HCL body the literal is read from,
32
+ * e.g. `manifest.apiVersion`. Nested blocks are walked by segment.
33
+ */
34
+ from: string;
35
+ /**
36
+ * A shape that cannot resolve a required field renders the TODO body rather
37
+ * than a data source missing an argument Terraform needs.
38
+ */
39
+ required?: boolean;
40
+ }
41
+
42
+ /** A nested block in the data-source body, e.g. `metadata { name = "x" }`. */
43
+ export interface ShapeBlock {
44
+ name: string;
45
+ fields: readonly ShapeField[];
46
+ }
47
+
48
+ export interface DataSourceShape {
49
+ /** The `data` type standing in for the carved resource, e.g. `kubernetes_resource`. */
50
+ type: string;
51
+ /** Flat `name = value` arguments of the body. */
52
+ args?: readonly ShapeField[];
53
+ /** Nested `name { ... }` blocks of the body. */
54
+ blocks?: readonly ShapeBlock[];
55
+ /**
56
+ * The carved resource's top-level attribute → the data source's, for the
57
+ * survivor rewrite. `kubernetes_manifest` exposes both `manifest` (what the
58
+ * config declared) and `object` (the API server's read-back), and
59
+ * `kubernetes_resource` exposes only `object`, so both map to `object`. An
60
+ * attribute with no entry passes through unchanged, which is what every
61
+ * same-type identity-attribute shape wants.
62
+ */
63
+ readAttrs?: Readonly<Record<string, string>>;
64
+ }
65
+
66
+ /**
67
+ * The shape a plain identity attribute implies: the same type as the resource,
68
+ * one required argument, no path translation. Returns undefined for a dotted
69
+ * attribute, which is a path into nested values and needs a declared shape.
70
+ */
71
+ export function identityAttrShape(tfType: string, attr: string): DataSourceShape | undefined {
72
+ if (attr.includes(".")) return undefined;
73
+ return { type: tfType, args: [{ name: attr, from: attr, required: true }] };
74
+ }
75
+
76
+ /** Every source path the shape reads, for the graph to resolve out of the carved block. */
77
+ export function shapeSourcePaths(shape: DataSourceShape): string[] {
78
+ const paths = (shape.args ?? []).map((f) => f.from);
79
+ for (const block of shape.blocks ?? []) for (const f of block.fields) paths.push(f.from);
80
+ return [...new Set(paths)];
81
+ }
82
+
83
+ /**
84
+ * Translate a survivor's attribute path onto the data source. `path` is the
85
+ * text after the address, leading dot included (`.manifest.data.log_level`) or
86
+ * empty. Only the first segment is mapped: everything under it is the object's
87
+ * own structure, which the data source reproduces.
88
+ */
89
+ export function rewriteReadPath(shape: DataSourceShape | undefined, path: string): string {
90
+ if (!path || !shape?.readAttrs) return path;
91
+ const [head, ...rest] = path.slice(1).split(".");
92
+ const mapped = shape.readAttrs[head];
93
+ if (mapped === undefined) return path;
94
+ return `.${[mapped, ...rest].join(".")}`;
95
+ }
@@ -11,7 +11,8 @@
11
11
  * graph building stays unit-testable on hand-written fixtures.
12
12
  */
13
13
 
14
- import { identityAttrOf } from "./tier-map";
14
+ import { shapeSourcePaths } from "./data-source-shape";
15
+ import { dataSourceShapeOf, identityAttrOf } from "./tier-map";
15
16
  import type { Hcl2JsonTree, TfEdge, TfGraph, TfNode } from "./types";
16
17
 
17
18
  /**
@@ -142,16 +143,14 @@ function blockHasMeta(block: unknown, key: string): boolean {
142
143
  }
143
144
 
144
145
  /**
145
- * The resource's physical name, if its identity attribute is a plain literal
146
- * (not interpolated). A dotted identity attribute walks nested blocks —
146
+ * The literal string at a dotted path into a block, or undefined when the path
147
+ * misses or lands on an interpolation. A dotted path walks nested blocks —
147
148
  * hcl2json renders a nested block as a one-element array, so arrays step
148
149
  * through their first element (`manifest.metadata.name`).
149
150
  */
150
- function literalIdentity(block: unknown, type: string): string | undefined {
151
- const attr = identityAttrOf(type);
152
- if (!attr || !block || typeof block !== "object") return undefined;
151
+ function literalAt(block: unknown, path: string): string | undefined {
153
152
  let value: unknown = block;
154
- for (const segment of attr.split(".")) {
153
+ for (const segment of path.split(".")) {
155
154
  if (Array.isArray(value)) value = value[0];
156
155
  if (!value || typeof value !== "object") return undefined;
157
156
  value = (value as Record<string, unknown>)[segment];
@@ -160,6 +159,34 @@ function literalIdentity(block: unknown, type: string): string | undefined {
160
159
  return value;
161
160
  }
162
161
 
162
+ /**
163
+ * The resource's physical name, if its identity attribute is a plain literal
164
+ * (not interpolated).
165
+ */
166
+ function literalIdentity(block: unknown, type: string): string | undefined {
167
+ const attr = identityAttrOf(type);
168
+ if (!attr || !block || typeof block !== "object") return undefined;
169
+ return literalAt(block, attr);
170
+ }
171
+
172
+ /**
173
+ * The literals the type's data-source shape reads out of the carved block
174
+ * (#2034), keyed by the shape field's source path. `carve bridge` renders the
175
+ * data-source body from these; a required field missing here is what makes it
176
+ * write a TODO instead of a block Terraform would reject.
177
+ */
178
+ function dataSourceValues(block: unknown, type: string): Record<string, string> | undefined {
179
+ if (!block || typeof block !== "object") return undefined;
180
+ const shape = dataSourceShapeOf(type);
181
+ if (!shape) return undefined;
182
+ const values: Record<string, string> = {};
183
+ for (const path of shapeSourcePaths(shape)) {
184
+ const value = literalAt(block, path);
185
+ if (value !== undefined) values[path] = value;
186
+ }
187
+ return Object.keys(values).length ? values : undefined;
188
+ }
189
+
163
190
  /**
164
191
  * Build the dependency graph from a merged hcl2json tree.
165
192
  *
@@ -202,6 +229,7 @@ export function buildGraph(tree: Hcl2JsonTree, exprRefs: ExpressionRefs): TfGrap
202
229
  instances: 1,
203
230
  hasDynamic: dynamic || touchesData,
204
231
  identity: literalIdentity(block, type),
232
+ dataSourceValues: dataSourceValues(block, type),
205
233
  });
206
234
  }
207
235
  }
@@ -18,6 +18,11 @@
18
18
  * can only name the family, and `adopt` reads the body to learn the rest —
19
19
  * which is also why one rule here covers every CRD.
20
20
  *
21
+ * It has no `kubernetes_manifest` data source either, so it does not read back
22
+ * as its own type. `MANIFEST_READ_BACK` below is the shape `carve bridge` uses
23
+ * instead: `data "kubernetes_resource"`, built from the manifest's own
24
+ * apiVersion/kind/metadata (#2034).
25
+ *
21
26
  * It has no live adoption path. `carve emit --env` filters a live export by a
22
27
  * native type, and there is no type to filter by until the body is read; state
23
28
  * is where the body is. `liveSelectorType` therefore returns undefined and
@@ -26,6 +31,7 @@
26
31
  */
27
32
 
28
33
  import type { AdoptedSource, CarveProvider, DeferredParam, TierInfo } from "../carve-provider";
34
+ import type { DataSourceShape } from "../data-source-shape";
29
35
  import type { StateResource } from "../state";
30
36
 
31
37
  /** The chant lexicon the emitted source imports from. */
@@ -208,14 +214,54 @@ function adoptManifestFromState(resource: StateResource, params: DeferredParam[]
208
214
  };
209
215
  }
210
216
 
217
+ /**
218
+ * How `carve bridge` reads a carved `kubernetes_manifest` back (#2034).
219
+ *
220
+ * The provider ships no `kubernetes_manifest` data source — checked against
221
+ * hashicorp/kubernetes v3.2.1, whose 27 data sources include no manifest. The
222
+ * generic read is `data "kubernetes_resource"`, which asks for the GVK and the
223
+ * name rather than taking them from a body: `api_version`, `kind`, and a
224
+ * nested `metadata` block with `name` and an optional `namespace`. All three
225
+ * come out of the manifest the resource declared.
226
+ *
227
+ * Survivors read the object back through `object`, and only through `object`:
228
+ * the resource exposes both `manifest` (the declared body) and `object` (the
229
+ * API server's read-back), the data source exposes just the latter. So both
230
+ * map to `object`, and a reference to `.manifest.data.log_level` lands on
231
+ * `.object.data.log_level` rather than an attribute that does not exist.
232
+ *
233
+ * A namespace-less manifest (a ClusterRole, a CRD) simply omits `namespace`,
234
+ * which the data source allows. A manifest whose name is interpolated resolves
235
+ * no literal and falls back to the bridge's TODO body, as an AWS type with an
236
+ * interpolated bucket name does.
237
+ */
238
+ const MANIFEST_READ_BACK: DataSourceShape = {
239
+ type: "kubernetes_resource",
240
+ args: [
241
+ { name: "api_version", from: "manifest.apiVersion", required: true },
242
+ { name: "kind", from: "manifest.kind", required: true },
243
+ ],
244
+ blocks: [
245
+ {
246
+ name: "metadata",
247
+ fields: [
248
+ { name: "name", from: "manifest.metadata.name", required: true },
249
+ { name: "namespace", from: "manifest.metadata.namespace" },
250
+ ],
251
+ },
252
+ ],
253
+ readAttrs: { manifest: "object", object: "object" },
254
+ };
255
+
211
256
  export const kubernetesCarveProvider: CarveProvider = {
212
257
  name: "kubernetes",
213
258
  tfTypePrefixes: ["kubernetes_"],
214
259
  lexicon: "k8s",
215
260
  tiers: TIERS,
216
- // A dotted path into nested blocks: the graph walks it for identity, and
217
- // `carve bridge` refuses the type because a data-source body cannot express it.
261
+ // A dotted path into the manifest body: the graph walks it for identity. It
262
+ // cannot be a data-source argument on its own, so the read-back is a shape.
218
263
  identityAttrs: { kubernetes_manifest: "manifest.metadata.name" },
264
+ dataSourceShapes: { kubernetes_manifest: MANIFEST_READ_BACK },
219
265
  emitTypes: ["kubernetes_manifest"],
220
266
  adopt: (resource, params) => adoptManifestFromState(resource, params),
221
267
  // State only — the kind is in the body, so a live export has nothing to
@@ -18,14 +18,17 @@
18
18
  */
19
19
 
20
20
  import {
21
+ carveDataSourceShape,
21
22
  carveFoldParent,
22
23
  carveIdentityAttr,
23
24
  carveTierMap,
24
25
  resolveEmitProvider,
25
26
  type TierInfo,
26
27
  } from "./carve-provider";
28
+ import type { DataSourceShape } from "./data-source-shape";
27
29
 
28
30
  export { carveEmitTypes, type TierInfo } from "./carve-provider";
31
+ export type { DataSourceShape } from "./data-source-shape";
29
32
 
30
33
  /** TF resource type → native tier, over every registered provider. Absent = unsupported (score 0). */
31
34
  export function tierMap(): Readonly<Record<string, TierInfo>> {
@@ -66,12 +69,24 @@ export function canCarveEmit(tfType: string): boolean {
66
69
  }
67
70
 
68
71
  /**
69
- * Can `chant carve bridge` render a Terraform `data` source for this type? A
70
- * dotted identity attribute is a path into nested blocks, and a data source
71
- * body is flat `attr = value` — `manifest.metadata.name = "x"` is not valid
72
- * HCL. An absent entry is fine: the bridge writes a TODO comment instead.
72
+ * How `chant carve bridge` reads this type back as a `data` source: the
73
+ * data-source type, where each argument comes from in the carved body, and how
74
+ * a survivor's attribute path translates (#2034). Undefined when the type
75
+ * cannot be read back at all.
76
+ */
77
+ export function dataSourceShapeOf(tfType: string): DataSourceShape | undefined {
78
+ return carveDataSourceShape(tfType);
79
+ }
80
+
81
+ /**
82
+ * Can `chant carve bridge` render a Terraform `data` source for this type?
83
+ * True whenever its provider contributes a shape — declared, or implied by a
84
+ * plain identity attribute. False for a dotted identity attribute with no
85
+ * declared shape: that is a path into nested values, and a flat `attr = value`
86
+ * body cannot express it (`manifest.metadata.name = "x"` is not valid HCL). A
87
+ * type with no identity attribute at all still bridges; the bridge writes a
88
+ * TODO comment for the body.
73
89
  */
74
90
  export function canBridge(tfType: string): boolean {
75
- const attr = carveIdentityAttr(tfType);
76
- return attr === undefined || !attr.includes(".");
91
+ return carveDataSourceShape(tfType) !== undefined;
77
92
  }
@@ -34,6 +34,14 @@ export interface TfNode {
34
34
  * emit. Undefined when the identity attribute is absent or interpolated.
35
35
  */
36
36
  identity?: string;
37
+ /**
38
+ * The literals the type's data-source shape reads out of the block (#2034),
39
+ * keyed by the shape field's source path (`manifest.metadata.name`). This is
40
+ * what `carve bridge` renders the `data` body from — a superset of
41
+ * `identity`, since a shape can need more than one value (`api_version`,
42
+ * `kind` and `metadata.name` for a `kubernetes_manifest`).
43
+ */
44
+ dataSourceValues?: Record<string, string>;
37
45
  }
38
46
 
39
47
  /**