From d89866dd74044a1ac19f515a4795972a1369ebb0 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 8 Jan 2024 14:03:31 -0800 Subject: [PATCH] reset point, in case I break it all... --- .../cloudformation-diff/lib/diff-template.ts | 17 ++++++++++++++--- .../cloudformation-diff/lib/diff/types.ts | 2 +- .../aws-cdk/lib/api/nested-stack-helpers.ts | 9 +-------- packages/aws-cdk/lib/cdk-toolkit.ts | 1 - 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts b/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts index 055bc3be8248b..98cbb25408705 100644 --- a/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts +++ b/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts @@ -11,6 +11,14 @@ export * from './diff/types'; type DiffHandler = (diff: types.ITemplateDiff, oldValue: any, newValue: any) => void; type HandlerRegistry = { [section: string]: DiffHandler }; +export type NestedChangeSet = CloudFormation.DescribeChangeSetOutput & { + Changes?: CloudFormation.Changes & { + ResourceChange?: { + NestedChanges?: CloudFormation.DescribeChangeSetOutput | NestedChangeSet, + }, + }[], +}; + const DIFF_HANDLERS: HandlerRegistry = { AWSTemplateFormatVersion: (diff, oldValue, newValue) => diff.awsTemplateFormatVersion = impl.diffAttribute(oldValue, newValue), @@ -209,7 +217,7 @@ function deepCopy(x: any): any { return x; } -function filterFalsePositivies(diff: types.TemplateDiff, changeSet: CloudFormation.DescribeChangeSetOutput) { +function filterFalsePositivies(diff: types.TemplateDiff, changeSet: CloudFormation.DescribeChangeSetOutput | NestedChangeSet) { const replacements = findResourceReplacements(changeSet); diff.resources.forEachDifference((logicalId: string, change: types.ResourceDifference) => { change.forEachDifference((type: 'Property' | 'Other', name: string, value: types.Difference | types.PropertyDifference) => { @@ -246,10 +254,13 @@ function filterFalsePositivies(diff: types.TemplateDiff, changeSet: CloudFormati }); } -function findResourceReplacements(changeSet: CloudFormation.DescribeChangeSetOutput): types.ResourceReplacements { - const replacements: types.ResourceReplacements = {}; +function findResourceReplacements(changeSet: NestedChangeSet, replacements: types.ResourceReplacements = {}): types.ResourceReplacements { for (const resourceChange of changeSet.Changes ?? []) { const propertiesReplaced: { [propName: string]: types.ChangeSetReplacement } = {}; + if (resourceChange.ResourceChange?.NestedChanges) { + // eslint-disable-next-line max-len + findResourceReplacements(resourceChange.ResourceChange.NestedChanges, replacements[resourceChange.ResourceChange!.LogicalResourceId!] as types.ResourceReplacements); + } for (const propertyChange of resourceChange.ResourceChange?.Details ?? []) { if (propertyChange.Target?.Attribute === 'Properties') { const requiresReplacement = propertyChange.Target.RequiresRecreation === 'Always'; diff --git a/packages/@aws-cdk/cloudformation-diff/lib/diff/types.ts b/packages/@aws-cdk/cloudformation-diff/lib/diff/types.ts index e85265bf99c1f..134c107583148 100644 --- a/packages/@aws-cdk/cloudformation-diff/lib/diff/types.ts +++ b/packages/@aws-cdk/cloudformation-diff/lib/diff/types.ts @@ -6,7 +6,7 @@ import { SecurityGroupChanges } from '../network/security-group-changes'; export type PropertyMap = {[key: string]: any }; -export type ResourceReplacements = { [logicalId: string]: ResourceReplacement }; +export type ResourceReplacements = { [logicalId: string]: ResourceReplacement | ResourceReplacements }; export interface ResourceReplacement { resourceReplaced: boolean, diff --git a/packages/aws-cdk/lib/api/nested-stack-helpers.ts b/packages/aws-cdk/lib/api/nested-stack-helpers.ts index 26d0086d859c9..a2b01d9cd067a 100644 --- a/packages/aws-cdk/lib/api/nested-stack-helpers.ts +++ b/packages/aws-cdk/lib/api/nested-stack-helpers.ts @@ -1,4 +1,5 @@ import * as path from 'path'; +import { NestedChangeSet } from '@aws-cdk/cloudformation-diff'; import * as cxapi from '@aws-cdk/cx-api'; import { CloudFormation } from 'aws-sdk'; import * as fs from 'fs-extra'; @@ -21,14 +22,6 @@ export interface TemplateWithNestedStackCount { readonly nestedStackCount: number; } -export type NestedChangeSet = CloudFormation.DescribeChangeSetOutput & { - Changes?: CloudFormation.Changes & { - ResourceChange?: { - NestedChanges?: CloudFormation.DescribeChangeSetOutput | NestedChangeSet, - }, - }[], -}; - /** * Reads the currently deployed template from CloudFormation and adds a * property, `NestedTemplate`, to any nested stacks that appear in either diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 6ae81e0f3c3d8..f336658c6a4e2 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -203,7 +203,6 @@ export class CdkToolkit { const templateWithNames = await this.props.deployments.readCurrentTemplateWithNestedStacks( stack, options.compareAgainstProcessedTemplate, ); - const currentTemplate = templateWithNames.deployedTemplate; const nestedStackCount = templateWithNames.nestedStackCount;