@huaweidevtools/aishell-dsh-deploy 0.0.15 → 0.0.16

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 (54) hide show
  1. package/README.md +25 -9
  2. package/cordis.patch.yml +12 -3
  3. package/lib/data/permission-registry.json +1 -1
  4. package/lib/index.js +5271 -1402
  5. package/lib/types/auth-http.d.ts +36 -4
  6. package/lib/types/auth-obs-signer.d.ts +8 -5
  7. package/lib/types/auth-signer.d.ts +6 -1
  8. package/lib/types/bridge.d.ts +4 -4
  9. package/lib/types/bss.d.ts +159 -0
  10. package/lib/types/catalog.d.ts +118 -0
  11. package/lib/types/credentials.d.ts +64 -18
  12. package/lib/types/d2-render.d.ts +74 -0
  13. package/lib/types/data-paths.d.ts +10 -0
  14. package/lib/types/deployment-state.d.ts +90 -13
  15. package/lib/types/existing.d.ts +79 -0
  16. package/lib/types/gates/cost_gate.d.ts +38 -16
  17. package/lib/types/gates/credential_gate.d.ts +29 -3
  18. package/lib/types/gates/design_gate.d.ts +72 -0
  19. package/lib/types/gates/mirror.d.ts +1 -1
  20. package/lib/types/gates/plan_gate.d.ts +116 -0
  21. package/lib/types/gates/provider_installed.d.ts +32 -0
  22. package/lib/types/gates/terraform_env.d.ts +15 -2
  23. package/lib/types/hcl.d.ts +176 -0
  24. package/lib/types/index.d.ts +18 -4
  25. package/lib/types/network-checks.d.ts +67 -0
  26. package/lib/types/permission/gate.d.ts +6 -6
  27. package/lib/types/permission/registry.d.ts +11 -0
  28. package/lib/types/plan.d.ts +68 -0
  29. package/lib/types/prompt.d.ts +5 -4
  30. package/lib/types/rating-map.d.ts +132 -0
  31. package/lib/types/security-groups.d.ts +16 -23
  32. package/lib/types/terraform-run.d.ts +32 -0
  33. package/lib/types/tools/annotations.d.ts +1 -1
  34. package/lib/types/tools/balance_summary.d.ts +1 -1
  35. package/lib/types/tools/deployment.d.ts +3 -3
  36. package/lib/types/tools/design.d.ts +54 -0
  37. package/lib/types/tools/errors.d.ts +22 -4
  38. package/lib/types/tools/examples.d.ts +1 -1
  39. package/lib/types/tools/existing_resources.d.ts +1 -1
  40. package/lib/types/tools/index.d.ts +26 -12
  41. package/lib/types/tools/openapi.d.ts +19 -9
  42. package/lib/types/tools/pricing.d.ts +213 -0
  43. package/lib/types/tools/provider_schema.d.ts +1 -1
  44. package/lib/types/tools/specs.d.ts +41 -0
  45. package/lib/types/tools/terraform.d.ts +27 -25
  46. package/lib/types/topology.d.ts +232 -0
  47. package/lib/types/types.d.ts +1 -1
  48. package/lib/types/utils/hash.d.ts +15 -5
  49. package/package.json +4 -2
  50. package/lib/types/gates/networking_gate.d.ts +0 -9
  51. package/lib/types/schemas/cost.d.ts +0 -108
  52. package/lib/types/schemas/networking.d.ts +0 -115
  53. package/lib/types/tools/cost.d.ts +0 -16
  54. package/lib/types/tools/networking.d.ts +0 -13
@@ -1,12 +1,14 @@
1
1
  /**
2
- * Deployment workspace state (§4.3 .deployment.json).
2
+ * Deployment workspace state: the `.deployment.json` snapshot.
3
3
  *
4
4
  * A deployment directory is identified by the presence of
5
5
  * .deployment.json. The file holds a status snapshot — not an operation
6
- * log. The tools update it on each state change (update_networking /
7
- * update_cost / terraform_apply / terraform_destroy). The model never writes
6
+ * log. The tools update it on each state change (validate_design /
7
+ * price_resources / terraform_plan / terraform_apply / terraform_destroy). The model never writes
8
8
  * this file directly.
9
9
  */
10
+ import type { DesignCheck } from "./gates/design_gate.js";
11
+ import type { DestroyRecord, PlanRecord } from "./gates/plan_gate.js";
10
12
  export declare const DEPLOYMENT_STATE_FILE = ".deployment.json";
11
13
  export interface DeploymentSnapshot {
12
14
  created_at: string;
@@ -17,7 +19,27 @@ export interface DeploymentSnapshot {
17
19
  account_id: string;
18
20
  };
19
21
  status: {
20
- networking_written: boolean;
22
+ /**
23
+ * The last `validate_design` run: what it checked, and whether it passed.
24
+ *
25
+ * A digest rather than a boolean, because the question the apply gate asks
26
+ * is "was THIS configuration checked" — and a design edited after it was
27
+ * checked is not a checked one. Absent until the first run.
28
+ */
29
+ design_check?: DesignCheck;
30
+ /**
31
+ * The last `terraform_plan`: the digest of the configuration it planned,
32
+ * and what it found. `terraform_apply` runs THAT plan file and refuses when
33
+ * this digest no longer matches — terraform itself will apply a saved plan
34
+ * whose configuration has changed, so the record is what ties the two.
35
+ */
36
+ plan_check?: PlanRecord;
37
+ /**
38
+ * The last `terraform_plan({ destroy: true })`: the targets it previewed and
39
+ * the configuration it computed them from. `terraform_destroy` applies that
40
+ * saved plan and refuses when this record does not name the same targets.
41
+ */
42
+ destroy_check?: DestroyRecord;
21
43
  cost_written: boolean;
22
44
  applied: boolean;
23
45
  destroyed: boolean;
@@ -27,6 +49,20 @@ export interface DeploymentSnapshot {
27
49
  }
28
50
  /** Check whether a directory is a deployment (has .deployment.json). */
29
51
  export declare function isDeploymentDir(dir: string): Promise<boolean>;
52
+ /**
53
+ * Read the deployment snapshot, when the directory has one.
54
+ *
55
+ * `undefined` rather than a throw: a directory that is not a deployment
56
+ * workspace is a normal thing to point at (the user named one directly, or
57
+ * `create_deployment` was skipped), and the tools that write into the
58
+ * workspace work there too. Each caller decides whether a missing snapshot is
59
+ * interesting — the design gate treats it as "never checked", `updateSnapshot`
60
+ * as "nothing to update".
61
+ *
62
+ * @param dir - the directory that may hold a deployment.
63
+ * @returns the snapshot, or `undefined`.
64
+ */
65
+ export declare function readSnapshot(dir: string): Promise<DeploymentSnapshot | undefined>;
30
66
  /** Create a deployment directory + initial .deployment.json. Refuse if non-empty. */
31
67
  export declare function ensureDeployment(dir: string): Promise<DeploymentSnapshot>;
32
68
  /** Update the status snapshot (called by other modules after state changes). */
@@ -35,8 +71,8 @@ export declare function updateSnapshot(dir: string, patch: Partial<DeploymentSna
35
71
  }): Promise<void>;
36
72
  /**
37
73
  * Backfill the account field in .deployment.json if it's empty and the
38
- * server is now authenticated (§4.3: "后续 auth 后 server 操作该 deployment
39
- * 时补写").
74
+ * server is now authenticated, so a workspace created before the first
75
+ * sign-in still ends up naming the account it belongs to.
40
76
  *
41
77
  * Called by runTerraform after resolveTerraformEnv succeeds. The account
42
78
  * identity was resolved at auth time (GET /v3/projects → domain_id + name)
@@ -55,15 +91,56 @@ export declare function backfillAccount(dir: string, account: {
55
91
  account_id: string;
56
92
  }): Promise<void>;
57
93
  /**
58
- * Count resources recorded in terraform.tfstate (0 = clean / never applied).
94
+ * Count the resources this deployment manages, by asking terraform.
95
+ *
96
+ * ## Why not read `terraform.tfstate`
97
+ *
98
+ * A deployment may configure a backend (`backend "obs"`, `backend "s3"`), in
99
+ * which case terraform writes state to the bucket and leaves no
100
+ * `terraform.tfstate` in the workspace. Reading that file as a proxy for "does
101
+ * this deployment own anything" then answers 0 for a deployment that owns
102
+ * everything — and both callers of this function act on the answer:
103
+ * `terraform_destroy` refuses ("nothing to destroy"), and `delete_deployment`
104
+ * proceeds to delete the workspace while the cloud resources stay up and
105
+ * unmanaged.
106
+ *
107
+ * So the count is asked of the backend that is actually configured. The
108
+ * subprocess is injected rather than imported: this module is the snapshot
109
+ * store, and reaching into the terraform runner would make the import graph
110
+ * cyclic — the runner already reads this module to backfill the account.
111
+ *
112
+ * @param dir - the deployment workspace directory.
113
+ * @param listState - runs `terraform state list` and returns its stdout and
114
+ * exit code; injected so this module stays testable without a binary.
115
+ * @returns how many resources the configured backend holds.
116
+ */
117
+ export declare function stateRecordedResourceCount(dir: string, listState: (dir: string, args: string[]) => Promise<{
118
+ exitCode: number;
119
+ stdout: string;
120
+ }>): Promise<number>;
121
+ /**
122
+ * Forget what was planned and previewed, after the run those records authorised.
123
+ *
124
+ * ## Why this is not housekeeping
125
+ *
126
+ * A saved plan is a one-shot artifact. terraform refuses to apply the same plan
127
+ * file twice — "the given plan file can no longer be applied because the state
128
+ * was changed by another operation after the plan was created" (verified) —
129
+ * while every record this module keeps still matches, because a successful
130
+ * apply does not change the `.tf`. So a second `terraform_apply` would pass all
131
+ * the gates, run, and fail on terraform's own error, which names neither the
132
+ * gate nor the remedy.
133
+ *
134
+ * Clearing the records moves that case onto the gate that has the remedy: with
135
+ * no `plan_check`, `assertPlanApproved` says "call terraform_plan first", which
136
+ * is exactly what the next deployment change needs anyway.
137
+ *
138
+ * The destroy record goes with it for the same reason: a teardown previewed
139
+ * before an apply describes state that no longer exists.
59
140
  *
60
- * This is the count RECORDED in the state file, NOT the live count on the
61
- * cloud — tfstate may be stale if resources were changed outside terraform.
62
- * For the live count, run terraform_refresh first. Used by delete_deployment
63
- * and terraform_destroy to guard against destroying/emptying a deployment
64
- * that still has recorded resources.
141
+ * @param dir - the deployment workspace directory.
65
142
  */
66
- export declare function stateRecordedResourceCount(dir: string): Promise<number>;
143
+ export declare function clearPlanRecords(dir: string): Promise<void>;
67
144
  /** Remove the deployment directory. */
68
145
  export declare function removeDeploymentDir(dir: string): Promise<void>;
69
146
  //# sourceMappingURL=deployment-state.d.ts.map
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Cloud resources that already exist, as the diagram shows them.
3
+ *
4
+ * ## Why these arrive as a parameter
5
+ *
6
+ * The design stage answers "what should the network be". In an account that
7
+ * already has a VPC, that question is partly "what can be reused", and a
8
+ * diagram drawn only from the `.tf` answers a different question: it shows what
9
+ * will be created and says nothing about what is already there.
10
+ *
11
+ * The caller supplies them because it is the only party that knows which of the
12
+ * account's resources the user is designing AROUND. `list_existing_resources`
13
+ * returns everything in the account — usually far more than the design is
14
+ * about — and picking the relevant subset is a judgement, not a query. An
15
+ * automatic "match by name or CIDR" rule would be a guess, and a diagram that
16
+ * guesses is worse than one that omits.
17
+ *
18
+ * ## Why they are not given terraform addresses
19
+ *
20
+ * These are not managed by this deployment. They have a cloud id and a name,
21
+ * and no `type.name` address — that identity belongs to whoever's terraform
22
+ * manages them, not to this one. They are marked instead, so a reader can tell
23
+ * at a glance which boxes this deployment will create and which it will use.
24
+ *
25
+ * @module @huaweidevtools/aishell-dsh-deploy/existing
26
+ */
27
+ import { z } from 'zod';
28
+ /**
29
+ * The shape a caller sends for one existing resource.
30
+ *
31
+ * `type` is accepted in either spelling — the terraform type
32
+ * (`huaweicloud_vpc`) or RMS's short name (`vpc`) — because both are what a
33
+ * caller has to hand. RMS is the likelier source: it is the tool that lists
34
+ * what exists, and it answers with short names.
35
+ *
36
+ * `parent` is the containing resource's ID, which is how a caller says "this
37
+ * ECS is in that subnet". It is optional: a caller that does not know the
38
+ * placement still gets the box, at the root.
39
+ */
40
+ export declare const ExistingResourceSchema: z.ZodObject<{
41
+ id: z.ZodString;
42
+ type: z.ZodString;
43
+ name: z.ZodOptional<z.ZodString>;
44
+ parent: z.ZodOptional<z.ZodString>;
45
+ cidr: z.ZodOptional<z.ZodString>;
46
+ region: z.ZodOptional<z.ZodString>;
47
+ flavor: z.ZodOptional<z.ZodString>;
48
+ address: z.ZodOptional<z.ZodString>;
49
+ }, z.core.$strip>;
50
+ /** One existing resource, as the caller described it. */
51
+ export type ExistingResource = z.infer<typeof ExistingResourceSchema>;
52
+ /** The node kind an existing resource takes, from either type spelling. */
53
+ export type ExistingKind = 'vpc' | 'subnet' | 'security-group' | 'security-group-rule' | 'gateway' | 'load-balancer' | 'compute' | 'database';
54
+ /**
55
+ * What a resource is, as far as the topology is concerned — or nothing.
56
+ *
57
+ * The seven kinds that can appear are the vocabulary a network design is
58
+ * written in. Everything else is not "an unknown kind of box": it is a
59
+ * resource the picture is not about, and drawing it would put a key pair or an
60
+ * alarm rule next to a subnet as if the reader should compare them.
61
+ *
62
+ * So the lookup answers `undefined` for those, and the caller reports them
63
+ * instead of inventing a shape. That is the difference between "this diagram
64
+ * shows the network" and "this diagram shows whatever the file contained".
65
+ */
66
+ export type TopologyKind = ExistingKind;
67
+ /**
68
+ * The kind one resource takes, when the topology has one for it.
69
+ *
70
+ * The terraform spelling is normalised by dropping the `huaweicloud_` prefix,
71
+ * and a few types carry a `_v2`/`_v1` suffix that is the same resource. That
72
+ * leaves one table rather than two that can disagree.
73
+ *
74
+ * @param type - either spelling the caller sent.
75
+ * @returns the node kind, or `undefined` when this resource is not part of the
76
+ * picture — see the note on the table.
77
+ */
78
+ export declare function kindOfExisting(type: string): ExistingKind | undefined;
79
+ //# sourceMappingURL=existing.d.ts.map
@@ -1,24 +1,46 @@
1
1
  /**
2
- * cost_gate.ts — cost.json existence + tf_hash state machine (§6.3 门 2+3).
2
+ * The gate `terraform_apply` runs on the cost record.
3
3
  *
4
- * The tf_hash logic:
5
- * - cost.json written BEFORE .tf, so tf_hash is null on write
6
- * - null → allow (first time / last apply failed); fill hash after success
7
- * - non-null → recompute current .tf SHA256, compare; mismatch rejects
4
+ * ## Two questions, both about the record's honesty
8
5
  *
9
- * "Fill after success, not before" (§4.1): apply failure doesn't fill,
10
- * so retry after a bug fix doesn't demand a pointless cost recompute.
6
+ * **Was anything priced?** `cost.json` exists and carries either a quote or a
7
+ * statement that the plan holds nothing to charge for. A deployment with
8
+ * neither has no approved cost, and applying it spends money nobody agreed to.
9
+ *
10
+ * An empty quote list alone is not an answer: a network of VPCs, subnets and
11
+ * security groups has no items and costs nothing, while a deployment of
12
+ * unpriced databases has no items and costs something nobody established. The
13
+ * record's `nothing_billable` flag is what separates them.
14
+ *
15
+ * **Is the record current?** The record stores the digest of the configuration
16
+ * it was priced against. This gate recomputes that digest from the files on
17
+ * disk and refuses when they differ. A price is a property of the
18
+ * configuration: edit a flavour, an image or a resource count and the old total
19
+ * describes something that will not be built.
20
+ *
21
+ * ## What the record is now
22
+ *
23
+ * `cost.json` is written by `price_resources` from the BSS rating API, so every
24
+ * figure in it came from an inquiry. It is not a form the model fills in — the
25
+ * tool that writes it is the only writer, which is why there is no separate
26
+ * "did you cost it" step to enforce. This gate guards the two ways a correct
27
+ * record can still be wrong at apply time: absent, and stale.
28
+ *
29
+ * ## Why it does not re-price
30
+ *
31
+ * Re-pricing here would make apply depend on a live billing call — a rate limit
32
+ * or a catalogue hiccup would block a deployment that was fully reviewed five
33
+ * minutes earlier. The digest answers "was this configuration priced", which is
34
+ * the question that matters; a price that has moved since is a difference the
35
+ * user accepted when they approved that number.
36
+ *
37
+ * @module @huaweidevtools/aishell-dsh-deploy/gates/cost_gate
11
38
  */
12
- import type { Cost } from "../schemas/cost.js";
13
- export declare function loadCost(dir: string): Promise<Cost>;
14
- export declare function costJsonExists(dir: string): Promise<boolean>;
15
39
  /**
16
- * Run the cost gate. Returns the cost.json (with its tf_hash) so the
17
- * caller can fill it after a successful apply.
40
+ * Refuse an apply whose cost record is absent, empty, or stale.
18
41
  *
19
- * @throws Error if cost.json missing or tf_hash mismatch
42
+ * @param dir - the deployment workspace directory.
43
+ * @throws when the record is unusable.
20
44
  */
21
- export declare function runCostGate(dir: string): Promise<Cost>;
22
- /** Fill tf_hash in cost.json after a successful apply (§6.3). */
23
- export declare function fillTfHash(dir: string): Promise<void>;
45
+ export declare function runCostGate(dir: string, hashConfiguration?: (directory: string) => Promise<string>): Promise<void>;
24
46
  //# sourceMappingURL=cost_gate.d.ts.map
@@ -1,5 +1,5 @@
1
1
  /**
2
- * credential_gate.ts — plaintext credential scanner (§6.2).
2
+ * Plaintext credential scanner for configuration files.
3
3
  *
4
4
  * Deterministic grep for access_key/secret_key/security_token/password
5
5
  * assignments in .tf files. Catches the most severe leak: credentials
@@ -7,14 +7,40 @@
7
7
  * not plaintext values, though instructions still say don't define cred
8
8
  * variables — the gate only catches the plaintext case).
9
9
  *
10
- * Mounted at terraform_init (early exposure) and terraform_apply (§6.3 门 4).
10
+ * Runs at terraform_init — early, before a provider is fetched — and again at
11
+ * terraform_apply, where it is the last gate before resources are created.
11
12
  */
12
13
  export interface CredentialFinding {
13
14
  file: string;
14
15
  line: number;
15
16
  pattern: string;
16
17
  }
17
- /** Scan all .tf files (recursively, including modules/) for plaintext credentials. */
18
+ /**
19
+ * Scan every configuration file this deployment owns for plaintext credentials.
20
+ *
21
+ * The file set comes from {@link configurationFiles}, so it is the same one the
22
+ * design tool reads and the digest hashes. That alignment is the point: a
23
+ * module's `.tf` is compiled into the plan, so a credential written there is
24
+ * exactly as exposed as one in the workspace root — and a scan that skipped it
25
+ * while the digest counted it would be two readers disagreeing about what the
26
+ * deployment is.
27
+ *
28
+ * ## Why the two syntaxes are scanned differently
29
+ *
30
+ * HCL writes an assignment as `key = "value"`, so a line pattern finds it. JSON
31
+ * writes `"key": "value"`, where the same pattern matches nothing — the
32
+ * separator and the quoting both differ, so a leak written in JSON is invisible
33
+ * to a scan built for HCL. Verified: a line scan over a `.tf.json` holding an
34
+ * `access_key` returned no findings.
35
+ *
36
+ * So a `.tf.json` is parsed and its string leaves inspected, comparing the KEY
37
+ * rather than the line. That comparison is exact, which is also why it does not
38
+ * need the HCL scan's `var.` exemption: a JSON value is either a string or a
39
+ * reference object, never an expression that reads like one.
40
+ *
41
+ * @param dir - the deployment workspace directory.
42
+ * @returns the findings, empty when the configuration is clean.
43
+ */
18
44
  export declare function scanTfCredentials(dir: string): Promise<CredentialFinding[]>;
19
45
  /** Throw if any plaintext credential is found. */
20
46
  export declare function assertNoCredentials(dir: string): Promise<void>;
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The gate `terraform_apply` runs on the network design.
3
+ *
4
+ * ## What it checks, and what it deliberately does not
5
+ *
6
+ * The design IS the `.tf`: there is no separate description of the network, so
7
+ * "has the design been checked" can only be asked of the configuration itself.
8
+ * This gate asks two questions, both answerable without a cloud credential:
9
+ *
10
+ * 1. Do the `.tf` files declare any network resources at all? A deployment
11
+ * that plans to create compute in a subnet it never wrote is a plan that
12
+ * will fail on an unresolved reference — the error terraform gives points
13
+ * at the compute block, several steps from the omission that caused it.
14
+ *
15
+ * 2. Did `validate_design` run over THIS configuration?
16
+ *
17
+ * The second is a hash, not a timestamp or a boolean. `validate_design`
18
+ * records the digest of the configuration it checked; this gate recomputes the
19
+ * digest from the files on disk and refuses when they differ. A design edited
20
+ * after it was checked is not a checked design — and a boolean would have said
21
+ * it was.
22
+ *
23
+ * ## Why the evidence is a digest of the configuration
24
+ *
25
+ * The design IS the `.tf`. Any separate statement of the network — a file the
26
+ * model fills in beside it — is a second description that nothing reconciles
27
+ * with the first, so passing a gate on it would prove only that somebody wrote
28
+ * something down. A digest of the configuration is evidence about the thing
29
+ * that will be built, which is the only thing worth approving.
30
+ *
31
+ * ## What it still does not do
32
+ *
33
+ * It does not re-run `terraform validate`. That is `validate_design`'s job, and
34
+ * it is the check whose result is recorded — a design that failed validation
35
+ * is recorded as failed and this gate refuses it.
36
+ *
37
+ * @module @huaweidevtools/aishell-dsh-deploy/gates/design_gate
38
+ */
39
+ import type { DeploymentSnapshot } from '../deployment-state.ts';
40
+ import { computeTfHash } from '../utils/hash.ts';
41
+ /**
42
+ * Whether one resource type is part of the network design.
43
+ *
44
+ * @param type - the resource type.
45
+ * @returns whether it counts as network.
46
+ */
47
+ export declare function isNetworkType(type: string): boolean;
48
+ /**
49
+ * The state a deployment records about its design check.
50
+ *
51
+ * Written by `validate_design`. `tf_hash` is the digest of the configuration
52
+ * that was checked; `ok` is whether it passed, so a recorded failure cannot be
53
+ * read as a pass.
54
+ */
55
+ export interface DesignCheck {
56
+ readonly tf_hash: string;
57
+ readonly ok: boolean;
58
+ readonly checked_at: string;
59
+ }
60
+ /**
61
+ * Refuse an apply whose network design was never checked, or changed since.
62
+ *
63
+ * @param directory - the deployment workspace directory.
64
+ * @param readState - reader for the deployment snapshot, injected so the check
65
+ * is testable without a filesystem.
66
+ * @param hashConfiguration - digest of the configuration on disk.
67
+ * @throws when the design is absent, unchecked, failed, or stale.
68
+ */
69
+ export declare function assertDesignChecked(directory: string, readState: (dir: string) => Promise<DeploymentSnapshot | undefined>, hashConfiguration: (dir: string) => Promise<string>): Promise<void>;
70
+ /** The digest function the gate uses; re-exported so a caller cannot pass a different one by accident. */
71
+ export { computeTfHash };
72
+ //# sourceMappingURL=design_gate.d.ts.map
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Provider mirror config generator (§6.6 / mirror.go 思路).
2
+ * Provider mirror config generator.
3
3
  *
4
4
  * Generates <deployment>/.terraformrc with network_mirror / filesystem_mirror
5
5
  * entries from a list of mirror URLs or paths. Terraform tries mirrors in
@@ -0,0 +1,116 @@
1
+ /**
2
+ * The gate that makes `terraform_apply` run the plan the user approved.
3
+ *
4
+ * ## The gap it closes
5
+ *
6
+ * `terraform apply` re-plans by default. That means the changes it makes are
7
+ * computed at apply time, from whatever the configuration says THEN — not the
8
+ * ones the user looked at in `terraform_plan`. Verified: `terraform apply
9
+ * <saved-plan>` executes the saved plan and does NOT check that the `.tf` still
10
+ * matches it, so terraform itself offers no protection either way.
11
+ *
12
+ * So the approval step has to be enforced here, and it is enforcement of two
13
+ * facts:
14
+ *
15
+ * 1. A plan exists, produced by `terraform_plan` for THIS configuration
16
+ * (its digest matches the `.tf` on disk).
17
+ * 2. Apply runs THAT plan file, not a fresh computation.
18
+ *
19
+ * ## Why the digest and not the plan file's timestamp
20
+ *
21
+ * A plan file has no intrinsic link to the configuration it came from —
22
+ * terraform will happily apply one whose `.tf` has since changed, as the test
23
+ * above shows. So the record of "which configuration this plan describes" has
24
+ * to be kept beside it, and the digest is that record. A timestamp would only
25
+ * say when someone ran the command.
26
+ *
27
+ * ## What this gate does NOT check
28
+ *
29
+ * Whether the user actually looked. That is not observable from here, and the
30
+ * prompt says to show the plan and get agreement. What IS observable is that a
31
+ * specific set of changes was computed and approved as a unit, and that apply
32
+ * executes exactly it — which is the property that matters when the answer to
33
+ * "was this reviewed" is "reviewed for a different plan".
34
+ *
35
+ * @module @huaweidevtools/aishell-dsh-deploy/gates/plan_gate
36
+ */
37
+ import { computeTfHash } from '../utils/hash.ts';
38
+ import type { DeploymentSnapshot } from '../deployment-state.ts';
39
+ /** Where `terraform_plan` writes the plan it computes. */
40
+ export declare const PLAN_FILE = "plan.tfplan";
41
+ /**
42
+ * The record `terraform_plan` writes about the plan it produced.
43
+ *
44
+ * Stored in the deployment snapshot rather than beside the plan file: the file
45
+ * is a terraform artifact and a second file next to it would be one more thing
46
+ * to keep in step. The snapshot is already the place this plugin records what
47
+ * it has done.
48
+ */
49
+ export interface PlanRecord {
50
+ /** Digest of the configuration the plan was computed from. */
51
+ readonly tf_hash: string;
52
+ /** When the plan ran. */
53
+ readonly planned_at: string;
54
+ /**
55
+ * What the plan would do, as counts — absent when the plan produced no
56
+ * readable change list, which is different from "changes nothing".
57
+ */
58
+ readonly actions?: {
59
+ readonly create: number;
60
+ readonly change: number;
61
+ readonly destroy: number;
62
+ };
63
+ }
64
+ /**
65
+ * Refuse an apply that has no approved plan, or whose plan is for a
66
+ * configuration that has since changed.
67
+ *
68
+ * @param directory - the deployment workspace directory.
69
+ * @param readState - reader for the deployment snapshot.
70
+ * @param hashConfiguration - digest of the configuration on disk.
71
+ * @throws when there is no plan, no record of it, or the record is stale.
72
+ */
73
+ export declare function assertPlanApproved(directory: string, readState: (dir: string) => Promise<DeploymentSnapshot | undefined>, hashConfiguration: (dir: string) => Promise<string>): Promise<void>;
74
+ /** The digest function the gate uses, re-exported so a caller cannot pass another by accident. */
75
+ export { computeTfHash };
76
+ /** Where `terraform_plan({ destroy: true })` writes the destruction it previewed. */
77
+ export declare const DESTROY_PLAN_FILE = "destroy.plan.tfplan";
78
+ /**
79
+ * The record `terraform_plan({ destroy: true })` writes about what it previewed.
80
+ *
81
+ * `targets` is empty for a full teardown and the addresses otherwise. It is
82
+ * stored because the preview and the destruction must name the same resources:
83
+ * applying a whole-deployment teardown because someone asked to remove one
84
+ * instance is the worst outcome available here, and the two lists are the only
85
+ * thing that can tell them apart.
86
+ */
87
+ export interface DestroyRecord {
88
+ /** Digest of the configuration the preview was computed from. */
89
+ readonly tf_hash: string;
90
+ readonly previewed_at: string;
91
+ /** The addresses previewed, or empty for the whole deployment. */
92
+ readonly targets: readonly string[];
93
+ }
94
+ /**
95
+ * Refuse a destroy with no preview, or one that passes for a different teardown.
96
+ *
97
+ * `terraform destroy -auto-approve` computes its own plan, so the list the user
98
+ * approved and the resources that disappear are two separate computations —
99
+ * and anything can change between them: a refresh, a console edit, a failed
100
+ * apply. Applying the previewed plan file instead makes the destruction
101
+ * exactly what was shown, and terraform refuses a plan whose state has moved
102
+ * on since. Verified: `plan -destroy -target=A -out=f` followed by `apply f`
103
+ * removes A and leaves B.
104
+ *
105
+ * Three questions, each one a way the preview and the request can disagree:
106
+ * does a preview exist, does it cover the same targets, and was it computed
107
+ * from the configuration still on disk.
108
+ *
109
+ * @param directory - the deployment workspace directory.
110
+ * @param readState - reader for the deployment snapshot.
111
+ * @param hashConfiguration - digest of the configuration on disk.
112
+ * @param targets - the addresses this call intends to destroy; empty for all.
113
+ * @throws when there is no preview, it covers a different set, or it is stale.
114
+ */
115
+ export declare function assertDestroyPreviewed(directory: string, readState: (dir: string) => Promise<DeploymentSnapshot | undefined>, hashConfiguration: (dir: string) => Promise<string>, targets: readonly string[]): Promise<void>;
116
+ //# sourceMappingURL=plan_gate.d.ts.map
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Whether this deployment has the huaweicloud provider installed.
3
+ *
4
+ * ## Why this is a question about files, not about terraform
5
+ *
6
+ * `terraform validate` refuses a configuration whose provider is absent — it
7
+ * answers "requires provider registry.terraform.io/huaweicloud/huaweicloud,
8
+ * but that provider isn't available" (verified against terraform 1.x with no
9
+ * `.terraform` directory). That message describes the install state, not the
10
+ * configuration, but a caller that only reads terraform's exit code cannot
11
+ * tell those apart.
12
+ *
13
+ * So the install state is decided here, from the directory terraform itself
14
+ * writes: `terraform init` unpacks providers into
15
+ * `<deployment>/.terraform/providers/<registry-address>/`. The directory is the
16
+ * signal, and it needs no subprocess, no credentials, and no network.
17
+ *
18
+ * `validate` also needs `.terraform.lock.hcl` to agree. A lock file naming the
19
+ * provider without the matching directory is the state `init` did not finish,
20
+ * and validate refuses it as an inconsistent lock — a different message for
21
+ * the same cause, so both halves are required here.
22
+ *
23
+ * @module @huaweidevtools/aishell-dsh-deploy/gates/provider_installed
24
+ */
25
+ /**
26
+ * Whether the huaweicloud provider is installed and locked in this deployment.
27
+ *
28
+ * @param deployment - the deployment workspace directory.
29
+ * @returns `true` when `terraform validate` can run against the provider.
30
+ */
31
+ export declare function providerInstalled(deployment: string): Promise<boolean>;
32
+ //# sourceMappingURL=provider_installed.d.ts.map
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Stateless terraform execution environment resolver (§6.6).
2
+ * Stateless terraform execution environment resolver.
3
3
  *
4
4
  * Each tool call is independent — nothing here remembers what terraform_install
5
5
  * or terraform_init did. But the terraform subprocess needs to know: binary
@@ -9,7 +9,7 @@
9
9
  * before spawning. It re-derives from filesystem state — a few stat calls,
10
10
  * no cross-call memory.
11
11
  *
12
- * Credential env is injected from the keychain (§5.5) — NEVER passed via
12
+ * Credential env is injected from the keychain — NEVER passed via
13
13
  * tool parameters. The tool-param-controlled env is `extraEnv` — an array
14
14
  * of KEY=VALUE strings merged onto the subprocess env (e.g. TF_LOG=DEBUG,
15
15
  * TF_LOG_PROVIDER=OFF, TF_CLI_ARGS_plan=-refresh=false).
@@ -23,4 +23,17 @@ export interface ResolvedEnv {
23
23
  * Throws if no binary can be found.
24
24
  */
25
25
  export declare function resolveTerraformEnv(deployment: string, extraEnv?: string[]): Promise<ResolvedEnv>;
26
+ /**
27
+ * Record the binary path `terraform_install` was given.
28
+ *
29
+ * Without this, the path is validated, reported in the tool result, and
30
+ * forgotten — and the very next `terraform_plan` fails with "terraform not
31
+ * found", which is the least useful answer to a call that just succeeded.
32
+ *
33
+ * A failure to write is swallowed by the caller: the install itself worked, and
34
+ * losing the record only means the next call falls back to the search order.
35
+ *
36
+ * @param path - an absolute path to a terraform binary the user supplied.
37
+ */
38
+ export declare function recordBinaryPath(path: string): Promise<void>;
26
39
  //# sourceMappingURL=terraform_env.d.ts.map