-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): warn the user of resource renaming #33257
Draft
otaviomacedo
wants to merge
10
commits into
main
Choose a base branch
from
otaviom/renaming-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
74595f1
feat(cli): warn the user of resource renaming
otaviomacedo 05445a7
Merge branch 'main' into otaviom/renaming-warning
otaviomacedo 93c47b2
Integ test
otaviomacedo 26171e3
Fix message format
otaviomacedo 3c3b391
Move formatCorrespondence to refactoring.ts
otaviomacedo ce62091
toString unit test, and general refactoring
otaviomacedo d52a25b
README
otaviomacedo 7e3e263
Merge branch 'main' into otaviom/renaming-warning
otaviomacedo 93bee12
ioHost
otaviomacedo e3f0a52
Fenced code blocks should have a language specified
otaviomacedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/cdk.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"app": "node refactoring.js", | ||
"versionReporting": false, | ||
"context": { | ||
"aws-cdk:enableDiffNoFail": "true" | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/@aws-cdk-testing/cli-integ/resources/cdk-apps/refactoring/refactoring.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const cdk = require('aws-cdk-lib'); | ||
const sqs = require('aws-cdk-lib/aws-sqs'); | ||
|
||
const stackPrefix = process.env.STACK_NAME_PREFIX; | ||
|
||
const app = new cdk.App(); | ||
|
||
class NoticesStackRefactored extends cdk.Stack { | ||
constructor(parent, id, props) { | ||
super(parent, id, props); | ||
new sqs.Queue(this, 'newQueueName'); | ||
} | ||
} | ||
|
||
new NoticesStackRefactored(app, `${stackPrefix}-notices`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { createHash } from 'crypto'; | ||
|
||
/** | ||
* The Resources section of a CFN template | ||
*/ | ||
export type ResourceMap = Map<string, any> | ||
|
||
/** | ||
* Map of resource hashes to sets of logical IDs | ||
*/ | ||
export type ResourceIndex = Map<string, Set<string>> | ||
|
||
/** | ||
* A pair of sets of logical IDs that are equivalent, | ||
* that is, that refer to the same resource. | ||
*/ | ||
export type RenamingPair = [Set<string>, Set<string>] | ||
|
||
export class ResourceCorrespondence { | ||
constructor( | ||
public readonly pairs: RenamingPair[], | ||
private readonly oldResources: Record<string, any>, | ||
private readonly newResources: Record<string, any>, | ||
) { | ||
} | ||
|
||
toString(): string { | ||
function toMetadataUsing(set: Set<string>, resources: Record<string, any>): Set<string> { | ||
return new Set([...set].map(s => resources[s].Metadata?.['aws:cdk:path'] as string ?? s)); | ||
} | ||
|
||
let text = this.pairs | ||
.map(([before, after]) => ([ | ||
toMetadataUsing(before, this.oldResources), | ||
toMetadataUsing(after, this.newResources), | ||
])) | ||
.map(([before, after]) => { | ||
if (before.size === 1 && after.size === 1) { | ||
return `${[...before][0]} -> ${[...after][0]}`; | ||
} else { | ||
return `{${[...before].join(', ')}} -> {${[...after].join(', ')}}`; | ||
} | ||
}) | ||
.map(x => ` - ${x}`) | ||
.join('\n'); | ||
|
||
return `\n${text}\n`; | ||
} | ||
|
||
isEmpty(): boolean { | ||
return this.pairs.length === 0; | ||
} | ||
} | ||
|
||
/** | ||
* Given two records of (logical ID -> object), finds a list of objects that | ||
* are present in both records but have different logical IDs. For each of | ||
* these objects, this function returns a pair of sets of logical IDs. The | ||
* first set coming from the first record, and the second set, from the | ||
* second object. For example: | ||
* | ||
* const corr = findResourceCorrespondence({a: {x: 0}}, {b: {x: 0}}); | ||
* | ||
* `corr` should be `[new Set('a'), new Set('b')]` | ||
* | ||
*/ | ||
export function findResourceCorrespondence(m1: Record<string, any>, m2: Record<string, any>): ResourceCorrespondence { | ||
const pairs = renamingPairs( | ||
index(new Map(Object.entries(m1))), | ||
index(new Map(Object.entries(m2))), | ||
); | ||
|
||
return new ResourceCorrespondence(pairs, m1, m2); | ||
} | ||
|
||
function index(resourceMap: ResourceMap): ResourceIndex { | ||
const result: ResourceIndex = new Map(); | ||
resourceMap.forEach((resource, logicalId) => { | ||
const h = hashObject(resource); | ||
if (!result.has(h)) { | ||
result.set(h, new Set()); | ||
} | ||
result.get(h)?.add(logicalId); | ||
}); | ||
return result; | ||
} | ||
|
||
function renamingPairs(index1: ResourceIndex, index2: ResourceIndex): RenamingPair[] { | ||
const result: RenamingPair[] = []; | ||
|
||
const keyUnion = new Set([...index1.keys(), ...index2.keys()]); | ||
keyUnion.forEach((key) => { | ||
if (index1.has(key) && index2.has(key) && index1.get(key) !== index2.get(key)) { | ||
result.push(partitionedSymmetricDifference(index1.get(key)!, index2.get(key)!)); | ||
} | ||
}); | ||
|
||
return result.filter(c => c[0].size > 0 && c[1].size > 0); | ||
} | ||
|
||
/** | ||
* Returns a pair [s0 \ s1, s1 \ s0]. | ||
*/ | ||
function partitionedSymmetricDifference(s0: Set<string>, s1: Set<string>): [Set<string>, Set<string>] { | ||
return [difference(s0, s1), difference(s1, s0)]; | ||
} | ||
|
||
function hashObject(obj: any): string { | ||
const hash = createHash('sha256'); | ||
|
||
function addToHash(value: any) { | ||
if (typeof value === 'object') { | ||
if (value == null) { | ||
addToHash('null'); | ||
} else if (Array.isArray(value)) { | ||
value.forEach(addToHash); | ||
} else { | ||
Object.keys(value).sort().forEach(key => { | ||
hash.update(key); | ||
addToHash(value[key]); | ||
}); | ||
} | ||
} else { | ||
hash.update(typeof value + value.toString()); | ||
} | ||
} | ||
|
||
const { Metadata, ...rest } = obj; | ||
addToHash(rest); | ||
|
||
return hash.digest('hex'); | ||
} | ||
|
||
function difference<A>(xs: Set<A>, ys: Set<A>): Set<A> { | ||
return new Set([...xs].filter(x => !ys.has(x))); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to be added to the new Toolkit as well (yes I now it's annoying).