From 7c6287bddc8b0880a88eb573779e95c8a15c19ce Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 12 Dec 2023 16:07:35 -0800 Subject: [PATCH] refactor --- .../aws-cdk/lib/api/util/cloudformation.ts | 39 ++++++++++++++-- packages/aws-cdk/lib/cdk-toolkit.ts | 46 ++++--------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/packages/aws-cdk/lib/api/util/cloudformation.ts b/packages/aws-cdk/lib/api/util/cloudformation.ts index 0c687b4865658..1fbee6a41778c 100644 --- a/packages/aws-cdk/lib/api/util/cloudformation.ts +++ b/packages/aws-cdk/lib/api/util/cloudformation.ts @@ -4,7 +4,8 @@ import { CloudFormation } from 'aws-sdk'; import { StackStatus } from './cloudformation/stack-status'; import { debug } from '../../logging'; import { deserializeStructure } from '../../serialize'; -import { TemplateBodyParameter } from '../deploy-stack'; +import { TemplateBodyParameter, makeBodyParameterAndUpload } from '../deploy-stack'; +import { Deployments } from '../deployments'; export type Template = { Parameters?: Record; @@ -282,8 +283,16 @@ export async function waitForChangeSet( return ret; } +export type PrepareChangeSetOptions = { + stack: cxapi.CloudFormationStackArtifact, + deployments: Deployments, + uuid: string, + resourcesToImport?: ResourcesToImport, + willExecute: boolean, +} + export type CreateChangeSetOptions = { - cfn: CloudFormation + cfn: CloudFormation, changeSetName: string, resourcesToImport?: ResourcesToImport, willExecute: boolean, @@ -293,7 +302,31 @@ export type CreateChangeSetOptions = { bodyParameter: TemplateBodyParameter, } -export async function createChangeSet(options: CreateChangeSetOptions): Promise { +export async function prepareAndCreateChangeSet(options: PrepareChangeSetOptions) { + const preparedSdk = (await options.deployments.prepareSdkWithDeployRole(options.stack)); + const bodyParameter = await makeBodyParameterAndUpload( + options.stack, + preparedSdk.resolvedEnvironment, + undefined as any, + undefined as any, + preparedSdk.stackSdk, + ); + const cfn = preparedSdk.stackSdk.cloudFormation(); + const exists = (await CloudFormationStack.lookup(cfn, options.stack.stackName, false)).exists; + + return createChangeSet({ + cfn, + changeSetName: 'some-name', + resourcesToImport: options.resourcesToImport, + stack: options.stack, + exists, + uuid: options.uuid, + willExecute: options.willExecute, + bodyParameter, + }); +} + +async function createChangeSet(options: CreateChangeSetOptions): Promise { await cleanupOldChangeset(options.exists, options.changeSetName, options.stack.stackName, options.cfn); //debug(`Attempting to create ChangeSet with name ${options.changeSetName} to ${verb} stack ${options.stack.stackName}`); diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index d1510fb5153c7..72ec51f4c0fda 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -6,7 +6,7 @@ import * as chokidar from 'chokidar'; import * as fs from 'fs-extra'; import * as promptly from 'promptly'; import * as uuid from 'uuid'; -import { DeploymentMethod, makeBodyParameterAndUpload } from './api'; +import { DeploymentMethod } from './api'; import { SdkProvider } from './api/aws-auth'; import { Bootstrapper, BootstrapEnvironmentOptions } from './api/bootstrap'; import { CloudAssembly, DefaultSelection, ExtendedStackSelection, StackCollection, StackSelector } from './api/cxapp/cloud-assembly'; @@ -15,7 +15,7 @@ import { Deployments } from './api/deployments'; import { HotswapMode } from './api/hotswap/common'; import { findCloudWatchLogGroups } from './api/logs/find-cloudwatch-logs'; import { CloudWatchLogEventMonitor } from './api/logs/logs-monitor'; -import { CloudFormationStack, ResourcesToImport, createChangeSet } from './api/util/cloudformation'; +import { ResourcesToImport, prepareAndCreateChangeSet } from './api/util/cloudformation'; import { StackActivityProgress } from './api/util/cloudformation/stack-activity-monitor'; import { generateCdkApp, generateStack, readFromPath, readFromStack, setEnvironment, validateSourceOptions } from './commands/migrate'; import { printSecurityDiff, printStackDiff, RequireApproval } from './diff'; @@ -134,27 +134,12 @@ export class CdkToolkit { } /////////////////////////////// - const preparedSdk = (await this.props.deployments.prepareSdkWithDeployRole(stacks.firstStack)); - const bodyParameter = await makeBodyParameterAndUpload( - stacks.firstStack, - preparedSdk.resolvedEnvironment, - undefined as any, - undefined as any, - preparedSdk.stackSdk, - ); - const cfn = preparedSdk.stackSdk.cloudFormation(); - const exists = (await CloudFormationStack.lookup(cfn, stacks.firstStack.stackName, false)).exists; - const diffChangeSetUuid = uuid.v4(); - - const changeSet = await createChangeSet({ - cfn, - changeSetName: 'some-name', + const changeSet = await prepareAndCreateChangeSet({ resourcesToImport: options.resourcesToImport, stack: stacks.firstStack, - exists, - uuid: diffChangeSetUuid, + uuid: uuid.v4(), willExecute: false, - bodyParameter, + deployments: this.props.deployments, }); /////////////////////////////// @@ -175,27 +160,12 @@ export class CdkToolkit { const currentTemplate = templateWithNames.deployedTemplate; const nestedStackCount = templateWithNames.nestedStackCount; - const preparedSdk = (await this.props.deployments.prepareSdkWithDeployRole(stack)); - const bodyParameter = await makeBodyParameterAndUpload( - stack, - preparedSdk.resolvedEnvironment, - undefined as any, - undefined as any, - preparedSdk.stackSdk, - ); - const cfn = preparedSdk.stackSdk.cloudFormation(); - const exists = (await CloudFormationStack.lookup(cfn, stack.stackName, false)).exists; - const diffChangeSetUuid = uuid.v4(); - - const changeSet = await createChangeSet({ - cfn, - changeSetName: 'some-name', + const changeSet = await prepareAndCreateChangeSet({ resourcesToImport: options.resourcesToImport, stack, - exists, - uuid: diffChangeSetUuid, + uuid: uuid.v4(), + deployments: this.props.deployments, willExecute: false, - bodyParameter, }); const stackCount =