Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
comcalvi committed Dec 13, 2023
1 parent 03a79f2 commit 7c6287b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
39 changes: 36 additions & 3 deletions packages/aws-cdk/lib/api/util/cloudformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, TemplateParameter>;
Expand Down Expand Up @@ -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,
Expand All @@ -293,7 +302,31 @@ export type CreateChangeSetOptions = {
bodyParameter: TemplateBodyParameter,
}

export async function createChangeSet(options: CreateChangeSetOptions): Promise<CloudFormation.DescribeChangeSetOutput> {
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<CloudFormation.DescribeChangeSetOutput> {
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}`);
Expand Down
46 changes: 8 additions & 38 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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,
});
///////////////////////////////

Expand All @@ -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 =
Expand Down

0 comments on commit 7c6287b

Please sign in to comment.