@intentius/chant-lexicon-aws 0.124.52 → 0.124.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/credential-chain.d.ts +3 -3
- package/dist/api/credential-chain.d.ts.map +1 -1
- package/dist/api/credential-chain.js +2 -2
- package/dist/api/read-client.d.ts +2 -2
- package/dist/api/read-client.d.ts.map +1 -1
- package/dist/api/read-client.js +2 -2
- package/dist/api/{sigv4.d.ts → signing.d.ts} +14 -34
- package/dist/api/signing.d.ts.map +1 -0
- package/dist/api/signing.js +95 -0
- package/dist/codegen/generate-lexicon.d.ts +2 -0
- package/dist/codegen/generate-lexicon.d.ts.map +1 -1
- package/dist/codegen/generate-lexicon.js +1 -0
- package/dist/components/__tests__/mock-cloud-executor.d.ts +4 -0
- package/dist/components/__tests__/mock-cloud-executor.d.ts.map +1 -1
- package/dist/components/__tests__/mock-cloud-executor.js +18 -1
- package/dist/components/apply.d.ts +46 -2
- package/dist/components/apply.d.ts.map +1 -1
- package/dist/components/apply.js +266 -30
- package/dist/components/cfn-adopt-run.d.ts +37 -0
- package/dist/components/cfn-adopt-run.d.ts.map +1 -0
- package/dist/components/cfn-adopt-run.js +104 -0
- package/dist/components/cfn-adopt.d.ts +99 -0
- package/dist/components/cfn-adopt.d.ts.map +1 -0
- package/dist/components/cfn-adopt.js +278 -0
- package/dist/components/cfn-imports.d.ts +84 -0
- package/dist/components/cfn-imports.d.ts.map +1 -0
- package/dist/components/cfn-imports.js +239 -0
- package/dist/components/cloud-executor.d.ts +38 -2
- package/dist/components/cloud-executor.d.ts.map +1 -1
- package/dist/components/cloud-executor.js +84 -2
- package/dist/health.js +1 -1
- package/dist/importable.d.ts +30 -0
- package/dist/importable.d.ts.map +1 -0
- package/dist/importable.js +72 -0
- package/dist/index-identity.d.ts +19 -0
- package/dist/index-identity.d.ts.map +1 -0
- package/dist/index-identity.js +79 -0
- package/dist/index-import.d.ts +25 -0
- package/dist/index-import.d.ts.map +1 -0
- package/dist/index-import.js +132 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/integrity.json +4 -4
- package/dist/listed-types.d.ts +11 -0
- package/dist/listed-types.d.ts.map +1 -0
- package/dist/listed-types.js +52 -0
- package/dist/manifest.json +1 -1
- package/dist/meta.json +13206 -1237
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +12 -3
- package/dist/reference-catalog.d.ts.map +1 -1
- package/dist/reference-catalog.js +62 -20
- package/dist/skills/chant-aws-carve-terraform.md +21 -14
- package/dist/spec/fetch.d.ts +5 -0
- package/dist/spec/fetch.d.ts.map +1 -1
- package/dist/spec/parse.d.ts +8 -0
- package/dist/spec/parse.d.ts.map +1 -1
- package/dist/spec/parse.js +3 -0
- package/package.json +2 -2
- package/src/api/credential-chain.ts +3 -3
- package/src/api/read-client.test.ts +2 -2
- package/src/api/read-client.ts +3 -3
- package/src/api/{sigv4.test.ts → signing.test.ts} +84 -96
- package/src/api/signing.ts +130 -0
- package/src/codegen/generate-lexicon.ts +3 -0
- package/src/components/__tests__/mock-cloud-executor.ts +20 -1
- package/src/components/apply.ts +288 -30
- package/src/components/cfn-adopt-run.test.ts +227 -0
- package/src/components/cfn-adopt-run.ts +141 -0
- package/src/components/cfn-adopt.test.ts +284 -0
- package/src/components/cfn-adopt.ts +340 -0
- package/src/components/cfn-exports-in-use.test.ts +146 -0
- package/src/components/cfn-imports-preview.test.ts +178 -0
- package/src/components/cfn-imports-three-stacks.test.ts +107 -0
- package/src/components/cfn-imports.test.ts +213 -0
- package/src/components/cfn-imports.ts +246 -0
- package/src/components/cfn-plan-shape-conformance.test.ts +83 -0
- package/src/components/cloud-executor.ts +127 -5
- package/src/components/demo-composed-stacks-floci.e2e.test.ts +235 -0
- package/src/derived-edges.test.ts +142 -0
- package/src/generated/lexicon-aws.json +13206 -1237
- package/src/health.ts +1 -1
- package/src/importable.test.ts +86 -0
- package/src/importable.ts +83 -0
- package/src/index-identity.test.ts +134 -0
- package/src/index-identity.ts +98 -0
- package/src/index-import.test.ts +257 -0
- package/src/index-import.ts +158 -0
- package/src/index-search.test.ts +87 -0
- package/src/index.ts +1 -1
- package/src/listed-types.test.ts +97 -0
- package/src/listed-types.ts +56 -0
- package/src/plugin.ts +13 -3
- package/src/reference-catalog.ts +63 -20
- package/src/skills/chant-aws-carve-terraform.md +21 -14
- package/src/spec/fetch.ts +5 -0
- package/src/spec/parse.test.ts +15 -0
- package/src/spec/parse.ts +12 -0
- package/src/testdata/indexed/identity.listing.json +65 -0
- package/src/testdata/indexed/import.listing.json +126 -0
- package/src/testdata/indexed/listing.json +69 -0
- package/src/testdata/indexed/topology.listing.json +350 -0
- package/src/testdata/three-cloud/dns.graph.json +336 -0
- package/src/three-cloud-parity.test.ts +49 -0
- package/dist/api/sigv4.d.ts.map +0 -1
- package/dist/api/sigv4.js +0 -185
- package/src/api/sigv4.ts +0 -243
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adopting indexed resources into a CloudFormation stack (#4536; design and
|
|
3
|
+
* ruling on INTENTIUS/choudoufu#2284, 2026-10-26).
|
|
4
|
+
*
|
|
5
|
+
* The aws lexicon deploys CREATE and UPDATE change sets. A CREATE of a
|
|
6
|
+
* template that declares an existing bucket fails, so adoption takes two
|
|
7
|
+
* change sets:
|
|
8
|
+
*
|
|
9
|
+
* 1. IMPORT, with the template's markers left out. CloudFormation import
|
|
10
|
+
* brings a resource under the stack without applying the template's
|
|
11
|
+
* property values, so markers in the import template would never be
|
|
12
|
+
* written, and the next UPDATE would then see no change and not write them
|
|
13
|
+
* either. The import template also carries no stack tags.
|
|
14
|
+
* 2. UPDATE, with the template as built: it adds chant's marker tags to each
|
|
15
|
+
* imported resource and the stack's own tags. It must be tags only: a
|
|
16
|
+
* change to anything but the tag property, a replace, an add or a remove
|
|
17
|
+
* refuses the run before it executes.
|
|
18
|
+
*
|
|
19
|
+
* The gate is chant's change-set review (Q3): each change set is shown before
|
|
20
|
+
* it is executed, through the caller's `review`, and the marker review lists
|
|
21
|
+
* each marker write as tags only. Nothing here asks a question of its own.
|
|
22
|
+
*
|
|
23
|
+
* Refused before any change set is made ({@link planCfnAdoption}): an
|
|
24
|
+
* adoption file that does not match its index version, a node the template
|
|
25
|
+
* does not declare under the file's `as`, a type CloudFormation cannot import
|
|
26
|
+
* or whose tags can only be set at create (../importable.ts), a resource with
|
|
27
|
+
* no DeletionPolicy (import needs one; `chant import --index` writes Retain,
|
|
28
|
+
* Q4), a resource already in the stack, and a template that would also create
|
|
29
|
+
* a resource. The index proposes; the live read decides: CloudFormation reads
|
|
30
|
+
* each object again when it imports it, by the identifier given here.
|
|
31
|
+
*/
|
|
32
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
33
|
+
import { tmpdir } from "node:os";
|
|
34
|
+
import { join } from "node:path";
|
|
35
|
+
import { adoptionFileProblemsAgainst, type AdoptionFile } from "@intentius/chant/lifecycle/adoption-file";
|
|
36
|
+
import type { IndexDocument } from "@intentius/chant/lifecycle/index-document";
|
|
37
|
+
import { adoptionRefusal, importabilityManifest, type ImportabilityManifestEntry } from "../importable";
|
|
38
|
+
import { AWS_TAG_OWNERSHIP_KEYS, OWNERSHIP_METADATA_KEY } from "../ownership";
|
|
39
|
+
import { isNoopChangeSet } from "./apply";
|
|
40
|
+
import type { CfnChange, CfnChangeSet, CfnResourceToImport, CloudFormationClient } from "./cloud-executor";
|
|
41
|
+
|
|
42
|
+
/** A CloudFormation template as chant builds it. */
|
|
43
|
+
export interface CfnTemplate {
|
|
44
|
+
Metadata?: Record<string, unknown>;
|
|
45
|
+
Resources: Record<string, { Type: string; Properties?: Record<string, unknown>; DeletionPolicy?: string; UpdateReplacePolicy?: string; [key: string]: unknown }>;
|
|
46
|
+
[key: string]: unknown;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** One resource the adoption brings under the stack. */
|
|
50
|
+
export interface CfnAdoptedResource {
|
|
51
|
+
node: string;
|
|
52
|
+
logicalId: string;
|
|
53
|
+
type: string;
|
|
54
|
+
identifier: Record<string, string>;
|
|
55
|
+
deletionPolicy: string;
|
|
56
|
+
/** The property the markers are written to, when the type has one chant can stamp. */
|
|
57
|
+
tagProperty?: string;
|
|
58
|
+
/** The marker tags the UPDATE writes. Empty for a type chant cannot tag. */
|
|
59
|
+
markers: Record<string, string>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface CfnAdoptionPlan {
|
|
63
|
+
/** Why the adoption cannot run, each by name. Empty when it can. */
|
|
64
|
+
refusals: string[];
|
|
65
|
+
resources: CfnAdoptedResource[];
|
|
66
|
+
/** The IMPORT change set's template: the built one without markers or stack tags. */
|
|
67
|
+
importTemplate: CfnTemplate;
|
|
68
|
+
/** The UPDATE change set's template: the built one. */
|
|
69
|
+
markerTemplate: CfnTemplate;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface PlanCfnAdoptionInput {
|
|
73
|
+
/** The built template of the stack the resources join, from the source `chant import --index` wrote. */
|
|
74
|
+
template: CfnTemplate;
|
|
75
|
+
adoption: AdoptionFile;
|
|
76
|
+
/** The index version the adoption file names. */
|
|
77
|
+
index: IndexDocument;
|
|
78
|
+
/** Logical ids already in the stack (empty for a new stack). */
|
|
79
|
+
existing?: ReadonlySet<string>;
|
|
80
|
+
manifest?: ReadonlyMap<string, ImportabilityManifestEntry>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const MARKER_KEYS: ReadonlySet<string> = new Set(Object.values(AWS_TAG_OWNERSHIP_KEYS));
|
|
84
|
+
|
|
85
|
+
/** The marker tags in a tag property's value, and the value without them. */
|
|
86
|
+
function splitMarkers(tags: unknown, shape: "list" | "map" | undefined): { markers: Record<string, string>; rest: unknown } {
|
|
87
|
+
const markers: Record<string, string> = {};
|
|
88
|
+
if (Array.isArray(tags) && shape !== "map") {
|
|
89
|
+
const rest = tags.filter((t) => {
|
|
90
|
+
const key = (t as { Key?: unknown })?.Key;
|
|
91
|
+
const value = (t as { Value?: unknown })?.Value;
|
|
92
|
+
if (typeof key === "string" && MARKER_KEYS.has(key)) {
|
|
93
|
+
if (typeof value === "string") markers[key] = value;
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
});
|
|
98
|
+
return { markers, rest };
|
|
99
|
+
}
|
|
100
|
+
if (tags && typeof tags === "object" && !Array.isArray(tags)) {
|
|
101
|
+
const rest: Record<string, unknown> = {};
|
|
102
|
+
for (const [k, v] of Object.entries(tags as Record<string, unknown>)) {
|
|
103
|
+
if (MARKER_KEYS.has(k)) {
|
|
104
|
+
if (typeof v === "string") markers[k] = v;
|
|
105
|
+
} else rest[k] = v;
|
|
106
|
+
}
|
|
107
|
+
return { markers, rest };
|
|
108
|
+
}
|
|
109
|
+
return { markers, rest: tags };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const isEmpty = (v: unknown): boolean =>
|
|
113
|
+
v === undefined || (Array.isArray(v) && v.length === 0) || (typeof v === "object" && v !== null && !Array.isArray(v) && Object.keys(v).length === 0);
|
|
114
|
+
|
|
115
|
+
/** The import identifier: the type's primary identifier properties zipped with the native id split on `|`. */
|
|
116
|
+
export function cfnResourceIdentifier(primaryIdentifier: readonly string[], nativeId: string): Record<string, string> | undefined {
|
|
117
|
+
const parts = nativeId.split("|");
|
|
118
|
+
if (primaryIdentifier.length === 0 || parts.length !== primaryIdentifier.length) return undefined;
|
|
119
|
+
return Object.fromEntries(primaryIdentifier.map((p, i) => [p, parts[i]!]));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Plan an adoption. Pure: reads nothing from the cloud. */
|
|
123
|
+
export function planCfnAdoption(input: PlanCfnAdoptionInput): CfnAdoptionPlan {
|
|
124
|
+
const manifest = input.manifest ?? importabilityManifest();
|
|
125
|
+
const existing = input.existing ?? new Set<string>();
|
|
126
|
+
const refusals = adoptionFileProblemsAgainst(input.adoption, input.index);
|
|
127
|
+
const nodes = new Map(input.index.graph.nodes.map((n) => [n.id, n]));
|
|
128
|
+
const importTemplate = JSON.parse(JSON.stringify(input.template)) as CfnTemplate;
|
|
129
|
+
const markerTemplate = JSON.parse(JSON.stringify(input.template)) as CfnTemplate;
|
|
130
|
+
const resources: CfnAdoptedResource[] = [];
|
|
131
|
+
const adopted = new Set<string>();
|
|
132
|
+
|
|
133
|
+
for (const sel of input.adoption.select ?? []) {
|
|
134
|
+
const node = nodes.get(sel.node);
|
|
135
|
+
if (!node) continue; // adoptionFileProblemsAgainst names it
|
|
136
|
+
const logicalId = sel.as;
|
|
137
|
+
adopted.add(logicalId);
|
|
138
|
+
const resource = input.template.Resources?.[logicalId];
|
|
139
|
+
if (!resource) {
|
|
140
|
+
refusals.push(`${sel.node}: the template declares no resource ${logicalId}`);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
const type = resource.Type;
|
|
144
|
+
if (type !== node.kind) refusals.push(`${logicalId}: the template declares ${type}, but ${sel.node} is ${node.kind}`);
|
|
145
|
+
if (sel.type !== undefined && sel.type !== type) refusals.push(`${logicalId}: the adoption file names type ${sel.type}, but the template declares ${type}`);
|
|
146
|
+
const refusal = adoptionRefusal(type, manifest);
|
|
147
|
+
if (refusal) refusals.push(`${logicalId}: ${refusal}`);
|
|
148
|
+
if (existing.has(logicalId)) refusals.push(`${logicalId} is already in the stack; nothing to import`);
|
|
149
|
+
const deletionPolicy = resource.DeletionPolicy;
|
|
150
|
+
if (typeof deletionPolicy !== "string") {
|
|
151
|
+
refusals.push(`${logicalId} declares no DeletionPolicy; CloudFormation import needs one (chant import --index writes Retain)`);
|
|
152
|
+
}
|
|
153
|
+
const entry = manifest.get(type);
|
|
154
|
+
const identifier = cfnResourceIdentifier(entry?.primaryIdentifier ?? [], node.nativeId ?? "");
|
|
155
|
+
if (!identifier) {
|
|
156
|
+
refusals.push(
|
|
157
|
+
`${logicalId}: native id "${node.nativeId ?? ""}" does not split into ${type}'s primary identifier (${(entry?.primaryIdentifier ?? []).join(", ") || "none"})`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const tagProperty = entry?.tagging?.taggable ? entry.tagging.tagProperty : undefined;
|
|
161
|
+
let markers: Record<string, string> = {};
|
|
162
|
+
if (tagProperty) {
|
|
163
|
+
const props = importTemplate.Resources[logicalId]!.Properties ?? {};
|
|
164
|
+
const split = splitMarkers(props[tagProperty], entry?.tagging?.tagShape);
|
|
165
|
+
markers = split.markers;
|
|
166
|
+
if (isEmpty(split.rest)) delete props[tagProperty];
|
|
167
|
+
else props[tagProperty] = split.rest;
|
|
168
|
+
importTemplate.Resources[logicalId]!.Properties = props;
|
|
169
|
+
}
|
|
170
|
+
resources.push({
|
|
171
|
+
node: sel.node,
|
|
172
|
+
logicalId,
|
|
173
|
+
type,
|
|
174
|
+
identifier: identifier ?? {},
|
|
175
|
+
deletionPolicy: typeof deletionPolicy === "string" ? deletionPolicy : "",
|
|
176
|
+
...(tagProperty ? { tagProperty } : {}),
|
|
177
|
+
markers,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
for (const id of Object.keys(input.template.Resources ?? {}).sort()) {
|
|
182
|
+
if (!adopted.has(id) && !existing.has(id)) {
|
|
183
|
+
refusals.push(`the template also creates ${id}; adopt first, then add it in a later deploy (the marker UPDATE must be tags only)`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// No stack tags on the IMPORT: the UPDATE writes them with the resource markers.
|
|
188
|
+
if (importTemplate.Metadata && OWNERSHIP_METADATA_KEY in importTemplate.Metadata) {
|
|
189
|
+
delete importTemplate.Metadata[OWNERSHIP_METADATA_KEY];
|
|
190
|
+
if (Object.keys(importTemplate.Metadata).length === 0) delete importTemplate.Metadata;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
resources.sort((a, b) => (a.logicalId < b.logicalId ? -1 : a.logicalId > b.logicalId ? 1 : 0));
|
|
194
|
+
return { refusals, resources, importTemplate, markerTemplate };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** What the IMPORT change set's review shows, one line per resource. */
|
|
198
|
+
export function cfnImportReview(plan: CfnAdoptionPlan): string[] {
|
|
199
|
+
return plan.resources.map(
|
|
200
|
+
(r) =>
|
|
201
|
+
`import ${r.logicalId} (${r.type}) ${Object.entries(r.identifier).map(([k, v]) => `${k}=${v}`).join(" ")}, DeletionPolicy ${r.deletionPolicy}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** What the marker UPDATE's review shows: each marker write, as tags only. */
|
|
206
|
+
export function cfnMarkerReview(plan: CfnAdoptionPlan): string[] {
|
|
207
|
+
return plan.resources.map((r) =>
|
|
208
|
+
Object.keys(r.markers).length > 0 && r.tagProperty
|
|
209
|
+
? `marker write, tags only: ${r.logicalId} (${r.type}) ${r.tagProperty} += ${Object.entries(r.markers)
|
|
210
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
211
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
212
|
+
.join(", ")}`
|
|
213
|
+
: r.tagProperty
|
|
214
|
+
? `no marker: ${r.logicalId} (${r.type}) carries no chant marker in the template; the stack owns it on its template alone`
|
|
215
|
+
: `no marker: ${r.logicalId} (${r.type}) has no tags chant can stamp; the stack owns it on its template alone`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Why an IMPORT change set is not the one planned, or empty. */
|
|
220
|
+
export function importChangeSetProblems(changeSet: CfnChangeSet, plan: CfnAdoptionPlan): string[] {
|
|
221
|
+
const problems: string[] = [];
|
|
222
|
+
if (changeSet.status === "FAILED") problems.push(`the IMPORT change set failed: ${changeSet.statusReason ?? "no reason given"}`);
|
|
223
|
+
const want = new Set(plan.resources.map((r) => r.logicalId));
|
|
224
|
+
const seen = new Set<string>();
|
|
225
|
+
for (const c of changeSet.changes) {
|
|
226
|
+
if (c.action !== "Import" || !want.has(c.logicalResourceId)) {
|
|
227
|
+
problems.push(`the IMPORT change set would ${c.action.toLowerCase()} ${c.logicalResourceId} (${c.resourceType}), which the adoption does not import`);
|
|
228
|
+
}
|
|
229
|
+
seen.add(c.logicalResourceId);
|
|
230
|
+
}
|
|
231
|
+
for (const id of want) if (!seen.has(id) && changeSet.status !== "FAILED") problems.push(`the IMPORT change set does not import ${id}`);
|
|
232
|
+
return problems;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function changeReplaces(c: CfnChange): boolean {
|
|
236
|
+
return c.replacement || c.replacementMode === "True" || c.replacementMode === "Conditional";
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Why the marker UPDATE is not tags only on the imported resources, or empty. */
|
|
240
|
+
export function markerChangeSetProblems(changeSet: CfnChangeSet, plan: CfnAdoptionPlan): string[] {
|
|
241
|
+
const problems: string[] = [];
|
|
242
|
+
if (changeSet.status === "FAILED" && !isNoopChangeSet(changeSet)) {
|
|
243
|
+
problems.push(`the marker change set failed: ${changeSet.statusReason ?? "no reason given"}`);
|
|
244
|
+
}
|
|
245
|
+
const byId = new Map(plan.resources.map((r) => [r.logicalId, r]));
|
|
246
|
+
for (const c of changeSet.changes) {
|
|
247
|
+
const r = byId.get(c.logicalResourceId);
|
|
248
|
+
if (c.action !== "Modify" || !r) {
|
|
249
|
+
problems.push(`the marker change set would ${c.action.toLowerCase()} ${c.logicalResourceId} (${c.resourceType}); it may only add tags to the imported resources`);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (changeReplaces(c)) problems.push(`the marker change set would replace ${c.logicalResourceId} (${c.resourceType})`);
|
|
253
|
+
const other = (c.details ?? []).filter((d) => d !== r.tagProperty);
|
|
254
|
+
if (other.length > 0) problems.push(`the marker change set changes ${c.logicalResourceId}'s ${other.join(", ")}, not only its tags`);
|
|
255
|
+
}
|
|
256
|
+
return problems;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** One change-set review: the step, the change set and what it writes. Approving executes it. */
|
|
260
|
+
export type CfnAdoptionReview = (step: "import" | "markers", changeSet: CfnChangeSet, lines: string[]) => Promise<boolean>;
|
|
261
|
+
|
|
262
|
+
export type CfnAdoptionOutcome =
|
|
263
|
+
| { status: "adopted"; resources: CfnAdoptedResource[]; importChangeSet: string; markerChangeSet?: string }
|
|
264
|
+
| { status: "refused"; step: "plan" | "import" | "markers"; refusals: string[] }
|
|
265
|
+
| { status: "declined"; step: "import" | "markers" }
|
|
266
|
+
| { status: "failed"; step: "import" | "markers"; stackStatus: string };
|
|
267
|
+
|
|
268
|
+
export interface AdoptIntoStackOptions {
|
|
269
|
+
cfn: CloudFormationClient;
|
|
270
|
+
stackName: string;
|
|
271
|
+
plan: CfnAdoptionPlan;
|
|
272
|
+
review: CfnAdoptionReview;
|
|
273
|
+
parameters?: Record<string, string>;
|
|
274
|
+
waitOptions?: { intervalMs?: number; timeoutMs?: number };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Run a planned adoption: the IMPORT change set, its review and execution,
|
|
279
|
+
* then the marker UPDATE, its tags-only check, review and execution. A
|
|
280
|
+
* refusal or a declined review deletes the change set it made and stops. A
|
|
281
|
+
* marker step that stops leaves the resources imported with Retain and
|
|
282
|
+
* without markers; running the adoption's UPDATE again (a plain deploy of
|
|
283
|
+
* the same template) writes them.
|
|
284
|
+
*/
|
|
285
|
+
export async function adoptIntoStack(opts: AdoptIntoStackOptions): Promise<CfnAdoptionOutcome> {
|
|
286
|
+
const { cfn, stackName, plan, review } = opts;
|
|
287
|
+
if (plan.refusals.length > 0) return { status: "refused", step: "plan", refusals: plan.refusals };
|
|
288
|
+
if (plan.resources.length === 0) return { status: "refused", step: "plan", refusals: ["the adoption names no resource"] };
|
|
289
|
+
if (!cfn.createImportChangeSet) {
|
|
290
|
+
return { status: "refused", step: "plan", refusals: ["this CloudFormation client cannot create an IMPORT change set"] };
|
|
291
|
+
}
|
|
292
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-cfn-adopt-"));
|
|
293
|
+
try {
|
|
294
|
+
const importPath = join(dir, "import.template.json");
|
|
295
|
+
writeFileSync(importPath, JSON.stringify(plan.importTemplate, null, 2));
|
|
296
|
+
const resourcesToImport: CfnResourceToImport[] = plan.resources.map((r) => ({
|
|
297
|
+
resourceType: r.type,
|
|
298
|
+
logicalResourceId: r.logicalId,
|
|
299
|
+
resourceIdentifier: r.identifier,
|
|
300
|
+
}));
|
|
301
|
+
const imported = await cfn.createImportChangeSet({ stackName, templatePath: importPath, resourcesToImport, ...(opts.parameters ? { parameters: opts.parameters } : {}) });
|
|
302
|
+
const ref = { stackName, changeSetName: imported.changeSetName };
|
|
303
|
+
const importProblems = importChangeSetProblems(imported, plan);
|
|
304
|
+
if (importProblems.length > 0) {
|
|
305
|
+
await cfn.deleteChangeSet(ref);
|
|
306
|
+
return { status: "refused", step: "import", refusals: importProblems };
|
|
307
|
+
}
|
|
308
|
+
if (!(await review("import", imported, cfnImportReview(plan)))) {
|
|
309
|
+
await cfn.deleteChangeSet(ref);
|
|
310
|
+
return { status: "declined", step: "import" };
|
|
311
|
+
}
|
|
312
|
+
await cfn.executeChangeSet(ref);
|
|
313
|
+
const afterImport = await cfn.waitForStack(stackName, opts.waitOptions);
|
|
314
|
+
if (afterImport.stackStatus !== "IMPORT_COMPLETE") return { status: "failed", step: "import", stackStatus: afterImport.stackStatus };
|
|
315
|
+
|
|
316
|
+
const markerPath = join(dir, "markers.template.json");
|
|
317
|
+
writeFileSync(markerPath, JSON.stringify(plan.markerTemplate, null, 2));
|
|
318
|
+
const marked = await cfn.createChangeSet({ stackName, templatePath: markerPath, ...(opts.parameters ? { parameters: opts.parameters } : {}) });
|
|
319
|
+
const markerRef = { stackName, changeSetName: marked.changeSetName };
|
|
320
|
+
if (isNoopChangeSet(marked)) {
|
|
321
|
+
await cfn.deleteChangeSet(markerRef);
|
|
322
|
+
return { status: "adopted", resources: plan.resources, importChangeSet: imported.changeSetName };
|
|
323
|
+
}
|
|
324
|
+
const markerProblems = markerChangeSetProblems(marked, plan);
|
|
325
|
+
if (markerProblems.length > 0) {
|
|
326
|
+
await cfn.deleteChangeSet(markerRef);
|
|
327
|
+
return { status: "refused", step: "markers", refusals: markerProblems };
|
|
328
|
+
}
|
|
329
|
+
if (!(await review("markers", marked, cfnMarkerReview(plan)))) {
|
|
330
|
+
await cfn.deleteChangeSet(markerRef);
|
|
331
|
+
return { status: "declined", step: "markers" };
|
|
332
|
+
}
|
|
333
|
+
await cfn.executeChangeSet(markerRef);
|
|
334
|
+
const afterMarkers = await cfn.waitForStack(stackName, opts.waitOptions);
|
|
335
|
+
if (afterMarkers.stackStatus !== "UPDATE_COMPLETE") return { status: "failed", step: "markers", stackStatus: afterMarkers.stackStatus };
|
|
336
|
+
return { status: "adopted", resources: plan.resources, importChangeSet: imported.changeSetName, markerChangeSet: marked.changeSetName };
|
|
337
|
+
} finally {
|
|
338
|
+
rmSync(dir, { recursive: true, force: true });
|
|
339
|
+
}
|
|
340
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An export another stack still imports (#4466c): CloudFormation refuses to
|
|
3
|
+
* change or remove it at apply, so a plan that would is refused at plan
|
|
4
|
+
* time, naming both stacks. A change the plan cannot say about is a hole.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
8
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { CapabilityRegistry } from "@intentius/chant/components/capability";
|
|
12
|
+
import type { DriverComponent } from "@intentius/chant/components/driver";
|
|
13
|
+
import { planFanOut } from "@intentius/chant/components/fan-out";
|
|
14
|
+
import { planPrSet } from "@intentius/chant/components/pr-run";
|
|
15
|
+
import { CfnExportInUseError, createCfnDeployCapability } from "./apply";
|
|
16
|
+
import { createMockCloudExecutor, type FakeStackConfig } from "./__tests__/mock-cloud-executor";
|
|
17
|
+
|
|
18
|
+
let dir: string;
|
|
19
|
+
let template: string;
|
|
20
|
+
const write = (t: unknown) => writeFileSync(template, JSON.stringify(t));
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
dir = mkdtempSync(join(tmpdir(), "cfn-exports-"));
|
|
24
|
+
template = join(dir, "network.json");
|
|
25
|
+
});
|
|
26
|
+
afterEach(() => rmSync(dir, { recursive: true, force: true }));
|
|
27
|
+
|
|
28
|
+
const exported = (value: unknown = { Ref: "Vpc" }) => ({
|
|
29
|
+
Resources: { Vpc: { Type: "AWS::EC2::VPC" }, Topic: { Type: "AWS::SNS::Topic" } },
|
|
30
|
+
Outputs: { VpcId: { Value: value, Export: { Name: { "Fn::Sub": "${AWS::StackName}-VpcId" } } } },
|
|
31
|
+
});
|
|
32
|
+
const topicChange = { action: "Modify" as const, logicalResourceId: "Topic", resourceType: "AWS::SNS::Topic", replacement: false, replacementMode: "False" as const };
|
|
33
|
+
const network = (extra: Partial<FakeStackConfig> = {}, importers: string[] = ["app"]): FakeStackConfig => ({
|
|
34
|
+
outputs: { VpcId: "vpc-1" },
|
|
35
|
+
exports: { "network-VpcId": "VpcId" },
|
|
36
|
+
importedBy: { "network-VpcId": importers },
|
|
37
|
+
changes: [topicChange],
|
|
38
|
+
...extra,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
async function plan(config: FakeStackConfig, extra: Record<string, unknown> = {}) {
|
|
42
|
+
const mock = createMockCloudExecutor({ stacks: { network: config } });
|
|
43
|
+
const result = await createCfnDeployCapability(mock.executor)
|
|
44
|
+
.plan!({ env: "local", component: "network" }, { stack: "network", template, ...extra })
|
|
45
|
+
.catch((e: unknown) => e);
|
|
46
|
+
return { mock, result };
|
|
47
|
+
}
|
|
48
|
+
const methods = (mock: ReturnType<typeof createMockCloudExecutor>) => mock.calls.map((c) => c.method);
|
|
49
|
+
|
|
50
|
+
describe("a plan that changes or removes an export in use (#4466c)", () => {
|
|
51
|
+
it("removing an export app imports is refused at plan time, naming both stacks, and nothing is executed", async () => {
|
|
52
|
+
write({ Resources: { Vpc: { Type: "AWS::EC2::VPC" }, Topic: { Type: "AWS::SNS::Topic" } } });
|
|
53
|
+
const { mock, result } = await plan(network());
|
|
54
|
+
expect(result).toBeInstanceOf(CfnExportInUseError);
|
|
55
|
+
expect((result as CfnExportInUseError).uses).toEqual([{ exportName: "network-VpcId", change: "removes {export}", importers: ["app"] }]);
|
|
56
|
+
expect((result as Error).message).toBe(
|
|
57
|
+
'cfn-deploy "network": refusing the plan: it removes export network-VpcId, which stack app still imports. ' +
|
|
58
|
+
"CloudFormation refuses to change or remove an export another stack imports. Change app to stop importing it first, then change network.",
|
|
59
|
+
);
|
|
60
|
+
expect(methods(mock)).toContain("deleteChangeSet");
|
|
61
|
+
expect(methods(mock)).not.toContain("executeChangeSet");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("changing an export's value is refused, with the value it has and the one it would get", async () => {
|
|
65
|
+
write(exported("vpc-2"));
|
|
66
|
+
const { result } = await plan(network());
|
|
67
|
+
expect(result).toBeInstanceOf(CfnExportInUseError);
|
|
68
|
+
expect((result as Error).message).toContain("it changes export network-VpcId from vpc-1 to vpc-2, which stack app still imports");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("renaming an export is removing it", async () => {
|
|
72
|
+
write({ ...exported(), Outputs: { VpcId: { Value: { Ref: "Vpc" }, Export: { Name: "renamed-VpcId" } } } });
|
|
73
|
+
const { result } = await plan(network());
|
|
74
|
+
expect((result as CfnExportInUseError).uses?.[0]).toMatchObject({ exportName: "network-VpcId", change: "removes {export}" });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("every importer is named", async () => {
|
|
78
|
+
write({ Resources: { Topic: { Type: "AWS::SNS::Topic" } } });
|
|
79
|
+
const { result } = await plan(network({}, ["edge", "app"]));
|
|
80
|
+
expect((result as Error).message).toContain("which stacks app, edge still import");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("an export whose value stays plans as before, and only that export's importers are asked for", async () => {
|
|
84
|
+
write(exported());
|
|
85
|
+
const { mock, result } = await plan(network());
|
|
86
|
+
expect(result).not.toBeInstanceOf(Error);
|
|
87
|
+
expect((result as { changeSet: { member: { holes: unknown[] } } }).changeSet.member.holes).toEqual([]);
|
|
88
|
+
expect(methods(mock)).not.toContain("listImports");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("an export nobody imports can change", async () => {
|
|
92
|
+
write(exported("vpc-2"));
|
|
93
|
+
const { result } = await plan(network({}, []));
|
|
94
|
+
expect(result).not.toBeInstanceOf(Error);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("an export whose new value is known only after apply is a hole naming its importers, not a refusal", async () => {
|
|
98
|
+
write(exported());
|
|
99
|
+
const { result } = await plan(
|
|
100
|
+
network({ changes: [{ action: "Modify", logicalResourceId: "Vpc", resourceType: "AWS::EC2::VPC", replacement: true, replacementMode: "True" }] }),
|
|
101
|
+
{ onReplace: "allow" },
|
|
102
|
+
);
|
|
103
|
+
expect(result).not.toBeInstanceOf(Error);
|
|
104
|
+
expect((result as { changeSet: { member: { holes: unknown[] } } }).changeSet.member.holes).toEqual([
|
|
105
|
+
{
|
|
106
|
+
address: "Outputs.VpcId",
|
|
107
|
+
reason:
|
|
108
|
+
"the plan may change export network-VpcId: the value of output VpcId is known only after apply, and app still imports it; CloudFormation refuses the apply if it changes",
|
|
109
|
+
},
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("in a pull request's plan (#4466c)", () => {
|
|
115
|
+
it("the exporter's member fails to plan with the refusal, and the set is failed", async () => {
|
|
116
|
+
write({ Resources: { Topic: { Type: "AWS::SNS::Topic" } } });
|
|
117
|
+
const mock = createMockCloudExecutor({ stacks: { network: network() } });
|
|
118
|
+
const registry = new CapabilityRegistry();
|
|
119
|
+
registry.register(createCfnDeployCapability(mock.executor));
|
|
120
|
+
const components: DriverComponent[] = [
|
|
121
|
+
{ name: "network", dependsOn: [], deploy: [{ phase: "Apply", steps: [{ kind: "cfn-deploy", stack: "network", template }] }] },
|
|
122
|
+
];
|
|
123
|
+
const set = await planPrSet({ components, plan: planFanOut({ components, changed: ["network"] }), registry, env: "local" });
|
|
124
|
+
expect(set.failed).toBe(true);
|
|
125
|
+
const member = set.members.find((m) => m.member === "network")!;
|
|
126
|
+
expect(member.status).toBe("plan-failed");
|
|
127
|
+
expect(member.error).toContain("it removes export network-VpcId, which stack app still imports");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("a backend that cannot list imports (#4466d)", () => {
|
|
132
|
+
it("is a hole on the export at stake, not a failed plan", async () => {
|
|
133
|
+
write({ Resources: { Topic: { Type: "AWS::SNS::Topic" } } });
|
|
134
|
+
const mock = createMockCloudExecutor({ stacks: { network: network() } });
|
|
135
|
+
mock.executor.cloudformation.listImports = async () => {
|
|
136
|
+
throw new Error("UnknownAction: ListImports");
|
|
137
|
+
};
|
|
138
|
+
const result = await createCfnDeployCapability(mock.executor).plan!({ env: "local", component: "network" }, { stack: "network", template });
|
|
139
|
+
expect(result.changeSet!.member.holes).toEqual([
|
|
140
|
+
{
|
|
141
|
+
address: "Outputs.VpcId",
|
|
142
|
+
reason: "the plan removes export network-VpcId, and the stacks that import it could not be listed: UnknownAction: ListImports",
|
|
143
|
+
},
|
|
144
|
+
]);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The preview of an importer whose export is not known yet (#4466b): its
|
|
3
|
+
* change set is made from a copy of its template in which each such import
|
|
4
|
+
* is a stand-in parameter (`cfnPlaceholder`), with entries and a PlanShape
|
|
5
|
+
* as #4423 gives a member whose parameters are unknown. Once the exporter
|
|
6
|
+
* applies, the real change set is checked with `planShapeDifferences`
|
|
7
|
+
* before it executes.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
11
|
+
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { CapabilityRegistry } from "@intentius/chant/components/capability";
|
|
15
|
+
import type { DriverComponent } from "@intentius/chant/components/driver";
|
|
16
|
+
import { withDeclaredReads } from "@intentius/chant/components/declared-reads";
|
|
17
|
+
import { planFanOut } from "@intentius/chant/components/fan-out";
|
|
18
|
+
import { applyPrSet, planPrSet } from "@intentius/chant/components/pr-run";
|
|
19
|
+
import { CFN_UNKNOWN_MARK, createCfnDeployCapability } from "./apply";
|
|
20
|
+
import { cfnStandInParameter, cfnTemplateWithStandIns } from "./cfn-imports";
|
|
21
|
+
import { createMockCloudExecutor, type FakeStackConfig } from "./__tests__/mock-cloud-executor";
|
|
22
|
+
|
|
23
|
+
let dir: string;
|
|
24
|
+
const write = (name: string, template: unknown): string => {
|
|
25
|
+
const path = join(dir, name);
|
|
26
|
+
writeFileSync(path, JSON.stringify(template));
|
|
27
|
+
return path;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
dir = mkdtempSync(join(tmpdir(), "cfn-imports-preview-"));
|
|
32
|
+
});
|
|
33
|
+
afterEach(() => rmSync(dir, { recursive: true, force: true }));
|
|
34
|
+
|
|
35
|
+
const NETWORK = {
|
|
36
|
+
Resources: { Vpc: { Type: "AWS::EC2::VPC" } },
|
|
37
|
+
Outputs: { VpcId: { Value: { Ref: "Vpc" }, Export: { Name: { "Fn::Sub": "${AWS::StackName}-VpcId" } } } },
|
|
38
|
+
};
|
|
39
|
+
const APP = {
|
|
40
|
+
Resources: {
|
|
41
|
+
Group: { Type: "AWS::EC2::SecurityGroup", Properties: { GroupDescription: "app", VpcId: { "Fn::ImportValue": "network-VpcId" } } },
|
|
42
|
+
},
|
|
43
|
+
Outputs: { Vpc: { Value: { "Fn::ImportValue": "network-VpcId" } } },
|
|
44
|
+
};
|
|
45
|
+
const STAND_IN = cfnStandInParameter("network-VpcId");
|
|
46
|
+
|
|
47
|
+
describe("the template copy (#4466b)", () => {
|
|
48
|
+
it("each import of a waiting name is a Ref to a String stand-in parameter; other imports stay", () => {
|
|
49
|
+
const template = { ...APP, Resources: { ...APP.Resources, Other: { Type: "AWS::SNS::Topic", Properties: { TopicName: { "Fn::ImportValue": "elsewhere" } } } } };
|
|
50
|
+
const { template: copy, standIns } = cfnTemplateWithStandIns(template, ["network-VpcId"], {});
|
|
51
|
+
expect(standIns).toEqual({ [STAND_IN]: "network-VpcId" });
|
|
52
|
+
expect(copy.Resources).toEqual({
|
|
53
|
+
Group: { Type: "AWS::EC2::SecurityGroup", Properties: { GroupDescription: "app", VpcId: { Ref: STAND_IN } } },
|
|
54
|
+
Other: { Type: "AWS::SNS::Topic", Properties: { TopicName: { "Fn::ImportValue": "elsewhere" } } },
|
|
55
|
+
});
|
|
56
|
+
expect(copy.Outputs).toEqual({ Vpc: { Value: { Ref: STAND_IN } } });
|
|
57
|
+
expect((copy.Parameters as Record<string, { Type: string }>)[STAND_IN]!.Type).toBe("String");
|
|
58
|
+
// The original is untouched.
|
|
59
|
+
expect(template.Resources.Group.Properties.VpcId).toEqual({ "Fn::ImportValue": "network-VpcId" });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("a stand-in name keeps only letters and digits, and two names never share one", () => {
|
|
63
|
+
expect(cfnStandInParameter("a-b")).toMatch(/^ChantImportab[0-9a-f]{8}$/);
|
|
64
|
+
expect(cfnStandInParameter("a-b")).not.toBe(cfnStandInParameter("a:b"));
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("cfn-deploy previews an importer whose export is not known yet (#4466b)", () => {
|
|
69
|
+
it("creates its change set from the copy with a stand-in value, marks what the import drives, and removes the copy", async () => {
|
|
70
|
+
const template = write("app.json", APP);
|
|
71
|
+
const mock = createMockCloudExecutor({
|
|
72
|
+
stacks: { app: { changes: [{ action: "Add", logicalResourceId: "Group", resourceType: "AWS::EC2::SecurityGroup", replacement: false }] } },
|
|
73
|
+
});
|
|
74
|
+
const planned = await createCfnDeployCapability(mock.executor).plan!(
|
|
75
|
+
{ env: "local", component: "app", unresolvedInputs: ["network"] },
|
|
76
|
+
{ stack: "app", template, imports: { "network-VpcId": undefined } },
|
|
77
|
+
);
|
|
78
|
+
const created = mock.calls.find((c) => c.method === "createChangeSet")!.args as { templatePath: string; parameters: Record<string, string> };
|
|
79
|
+
expect(created.templatePath).not.toBe(template);
|
|
80
|
+
expect(created.templatePath).toMatch(/app\.preview\.template\.json$/);
|
|
81
|
+
expect(created.parameters).toEqual({ [STAND_IN]: CFN_UNKNOWN_MARK });
|
|
82
|
+
expect(existsSync(created.templatePath)).toBe(false);
|
|
83
|
+
expect(planned.artifact).toMatchObject({ kind: "cfn-deploy-shape", member: "app" });
|
|
84
|
+
expect(planned.shape).toMatchObject({ member: "app", unknownInputs: ["network"] });
|
|
85
|
+
expect(planned.shape!.entries).toEqual([
|
|
86
|
+
expect.objectContaining({ logicalId: "Group", action: "create", unknown: ["VpcId"] }),
|
|
87
|
+
]);
|
|
88
|
+
expect(planned.changeSet!.member.holes).toEqual([]);
|
|
89
|
+
expect(planned.changeSet!.entries).toHaveLength(1);
|
|
90
|
+
expect(mock.calls.map((c) => c.method)).not.toContain("executeChangeSet");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("the preview's shape enters the digest, so a different preview is a different plan", async () => {
|
|
94
|
+
const template = write("app.json", APP);
|
|
95
|
+
const plan = async (changes: FakeStackConfig["changes"]) =>
|
|
96
|
+
createCfnDeployCapability(createMockCloudExecutor({ stacks: { app: { changes } } }).executor).plan!(
|
|
97
|
+
{ env: "local", component: "app", unresolvedInputs: ["network"] },
|
|
98
|
+
{ stack: "app", template, imports: { "network-VpcId": undefined } },
|
|
99
|
+
);
|
|
100
|
+
const one = await plan([{ action: "Add", logicalResourceId: "Group", resourceType: "AWS::EC2::SecurityGroup", replacement: false }]);
|
|
101
|
+
const two = await plan([
|
|
102
|
+
{ action: "Add", logicalResourceId: "Group", resourceType: "AWS::EC2::SecurityGroup", replacement: false },
|
|
103
|
+
{ action: "Add", logicalResourceId: "Extra", resourceType: "AWS::SNS::Topic", replacement: false },
|
|
104
|
+
]);
|
|
105
|
+
expect(one.planDigest).not.toBe(two.planDigest);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("pr-apply over two stacks wired only by an import (#4466b)", () => {
|
|
110
|
+
const approvedFirst = { status: "approved" as const, approvedBy: ["github:rev"] };
|
|
111
|
+
const groupAdd = { action: "Add" as const, logicalResourceId: "Group", resourceType: "AWS::EC2::SecurityGroup", replacement: false };
|
|
112
|
+
const extraAdd = { action: "Add" as const, logicalResourceId: "Extra", resourceType: "AWS::SNS::Topic", replacement: false };
|
|
113
|
+
const stacks = (): Record<string, FakeStackConfig> => ({
|
|
114
|
+
network: { outputs: { VpcId: "vpc-1" }, changes: [{ action: "Add", logicalResourceId: "Vpc", resourceType: "AWS::EC2::VPC", replacement: false }] },
|
|
115
|
+
app: { changes: [groupAdd] },
|
|
116
|
+
});
|
|
117
|
+
const executed = (mock: ReturnType<typeof createMockCloudExecutor>) =>
|
|
118
|
+
mock.calls.filter((c) => c.method === "executeChangeSet").map((c) => (c.args as { stackName: string }).stackName);
|
|
119
|
+
|
|
120
|
+
async function planned(config: Record<string, FakeStackConfig>) {
|
|
121
|
+
const declared: DriverComponent[] = [
|
|
122
|
+
{ name: "network", dependsOn: [], deploy: [{ phase: "Apply", steps: [{ kind: "cfn-deploy", stack: "network", template: write("network.json", NETWORK) }] }] },
|
|
123
|
+
{ name: "app", dependsOn: [], deploy: [{ phase: "Apply", steps: [{ kind: "cfn-deploy", stack: "app", template: write("app.json", APP) }] }] },
|
|
124
|
+
];
|
|
125
|
+
const mock = createMockCloudExecutor({ stacks: config });
|
|
126
|
+
const registry = new CapabilityRegistry();
|
|
127
|
+
registry.register(createCfnDeployCapability(mock.executor));
|
|
128
|
+
const components = await withDeclaredReads(declared, registry, dir);
|
|
129
|
+
const plan = planFanOut({ components, changed: ["network", "app"] });
|
|
130
|
+
const set = await planPrSet({ components, plan, registry, env: "local" });
|
|
131
|
+
return { mock, registry, components, plan, set };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
it("the plan previews app with entries and a shape that names the import as unknown", async () => {
|
|
135
|
+
const { set } = await planned(stacks());
|
|
136
|
+
expect(set.shapes?.app).toMatchObject({ member: "app", unknownInputs: ["network"] });
|
|
137
|
+
expect(set.plans.app?.app).toMatchObject({ kind: "cfn-deploy-shape" });
|
|
138
|
+
expect(set.doc.entries.filter((e) => e.member === "app").map((e) => e.address)).toEqual(["Group"]);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("the real change set has the previewed shape: one approval covers the round, and both stacks execute in order", async () => {
|
|
142
|
+
const { mock, registry, components, plan, set } = await planned(stacks());
|
|
143
|
+
let asked = 0;
|
|
144
|
+
const applied = await applyPrSet({
|
|
145
|
+
components,
|
|
146
|
+
plan,
|
|
147
|
+
registry,
|
|
148
|
+
env: "local",
|
|
149
|
+
set,
|
|
150
|
+
approval: approvedFirst,
|
|
151
|
+
decideRound: async () => {
|
|
152
|
+
asked++;
|
|
153
|
+
return { satisfied: false, approval: { status: "pending" } };
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
expect(asked).toBe(0);
|
|
157
|
+
expect(applied.status).toBe("applied");
|
|
158
|
+
expect(executed(mock)).toEqual(["network", "app"]);
|
|
159
|
+
expect(applied.rounds?.[0]).toMatchObject({ round: 2, status: "applied", covered: { digest: set.doc.digest, filledInputs: ["network.VpcId"] } });
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("a real change set the preview did not show is not executed: app waits at the next round", async () => {
|
|
163
|
+
const { mock, registry, components, plan, set } = await planned(stacks());
|
|
164
|
+
mock.setStack("app", { changes: [groupAdd, extraAdd] });
|
|
165
|
+
const applied = await applyPrSet({
|
|
166
|
+
components,
|
|
167
|
+
plan,
|
|
168
|
+
registry,
|
|
169
|
+
env: "local",
|
|
170
|
+
set,
|
|
171
|
+
approval: approvedFirst,
|
|
172
|
+
decideRound: async () => ({ satisfied: false, approval: { status: "pending" } }),
|
|
173
|
+
});
|
|
174
|
+
expect(applied.status).toBe("waiting");
|
|
175
|
+
expect(executed(mock)).toEqual(["network"]);
|
|
176
|
+
expect(applied.rounds?.[0]).toMatchObject({ round: 2, status: "waiting", notCovered: "app now plans to create Extra, which the first plan did not" });
|
|
177
|
+
});
|
|
178
|
+
});
|