Skip to content

Commit 94cce1e

Browse files
authored
refactor: filter stacks before getting their templates (#960)
The `refactor` command only operates on the stacks that are relevant for the target CDK application. However, it gets all the templates of the deployed stacks before filtering them, which is wasteful. Invert the order, so that the filtering happens before getting the templates. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 1ec3310 commit 94cce1e

File tree

1 file changed

+36
-21
lines changed
  • packages/@aws-cdk/toolkit-lib/lib/api/refactoring

1 file changed

+36
-21
lines changed

packages/@aws-cdk/toolkit-lib/lib/api/refactoring/index.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export async function usePrescribedMappings(
4242

4343
const stackGroups: MappingGroupWithStacks[] = [];
4444
for (const group of mappingGroups) {
45+
const summaries = await listStacks(sdkProvider, environmentOf(group));
46+
const stacks = await getDeployedStacksByNames(sdkProvider, environmentOf(group), summaries.map(s => s.StackName!));
4547
stackGroups.push({
4648
...group,
47-
stacks: await getDeployedStacks(sdkProvider, environmentOf(group)),
49+
stacks,
4850
});
4951
}
5052

@@ -119,36 +121,44 @@ export async function usePrescribedMappings(
119121
}
120122
}
121123

122-
export async function getDeployedStacks(
124+
export async function getDeployedStacksByNames(
123125
sdkProvider: SdkProvider,
124126
environment: cxapi.Environment,
127+
stackNames: string[],
125128
): Promise<CloudFormationStack[]> {
126129
const cfn = (await sdkProvider.forEnvironment(environment, Mode.ForReading)).sdk.cloudFormation();
127130

128-
const summaries = await cfn.paginatedListStacks({
129-
StackStatusFilter: [
130-
'CREATE_COMPLETE',
131-
'UPDATE_COMPLETE',
132-
'UPDATE_ROLLBACK_COMPLETE',
133-
'IMPORT_COMPLETE',
134-
'ROLLBACK_COMPLETE',
135-
],
136-
});
137-
138-
const normalize = async (summary: StackSummary) => {
139-
const templateCommandOutput = await cfn.getTemplate({ StackName: summary.StackName! });
131+
const normalize = async (stackName: string) => {
132+
const templateCommandOutput = await cfn.getTemplate({ StackName: stackName });
140133
const template = deserializeStructure(templateCommandOutput.TemplateBody ?? '{}');
141134
return {
142135
environment,
143-
stackName: summary.StackName!,
136+
stackName: stackName,
144137
template,
145138
};
146139
};
147140

148141
const limit = pLimit(20);
149142

150143
// eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism
151-
return Promise.all(summaries.map(s => limit(() => normalize(s))));
144+
return Promise.all(stackNames.map(s => limit(() => normalize(s))));
145+
}
146+
147+
async function listStacks(
148+
sdkProvider: SdkProvider,
149+
environment: cxapi.Environment,
150+
): Promise<StackSummary[]> {
151+
const cfn = (await sdkProvider.forEnvironment(environment, Mode.ForReading)).sdk.cloudFormation();
152+
153+
return cfn.paginatedListStacks({
154+
StackStatusFilter: [
155+
'CREATE_COMPLETE',
156+
'UPDATE_COMPLETE',
157+
'UPDATE_ROLLBACK_COMPLETE',
158+
'IMPORT_COMPLETE',
159+
'ROLLBACK_COMPLETE',
160+
],
161+
});
152162
}
153163

154164
export function formatEnvironmentSectionHeader(environment: cxapi.Environment) {
@@ -188,15 +198,20 @@ export async function groupStacks(sdkProvider: SdkProvider, localStacks: CloudFo
188198

189199
const groups: StackGroup[] = [];
190200
for (let key of localByEnvironment.keys()) {
191-
const environment = environments.get(key)!;
192-
const allDeployedStacks = await getDeployedStacks(sdkProvider, environment);
193201
const local = localByEnvironment.get(key)!;
194-
const hasLocalCounterpart = (s: CloudFormationStack) => local.some((l) => l.stackName === s.stackName);
195-
const wasExplicitlyProvided = (s: CloudFormationStack) => additionalStackNames.includes(s.stackName);
202+
const hasLocalCounterpart = (stackName: string) => local.some((l) => l.stackName === stackName);
203+
const wasExplicitlyProvided = (stackName: string) => additionalStackNames.includes(stackName);
204+
205+
const environment = environments.get(key)!;
206+
const stackSummaries = await listStacks(sdkProvider, environment);
207+
const stackNames = stackSummaries
208+
.map(s => s.StackName!)
209+
.filter(s => hasLocalCounterpart(s) || wasExplicitlyProvided(s));
210+
const deployedStacks = await getDeployedStacksByNames(sdkProvider, environment, stackNames);
196211

197212
groups.push({
198213
environment,
199-
deployedStacks: allDeployedStacks.filter(s => hasLocalCounterpart(s) || wasExplicitlyProvided(s)),
214+
deployedStacks,
200215
localStacks: local,
201216
});
202217
}

0 commit comments

Comments
 (0)