Skip to content

feat: refactor (dry-run mode only) #342

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

Merged
merged 16 commits into from
Apr 11, 2025
Merged

Conversation

otaviomacedo
Copy link
Contributor

Add a refactor command. For now, it only works in dry run mode (-dry-run in the command line, or dryRun: true to the toolkit). It computes the mappings based on the difference between the deployed stacks and the cloud assembly stacks, and shows them in a table. Example:

$ cdk refactor --dry-run --unstable=refactor

The following resources were moved or renamed:
┌───────────────────────┬────────────────────────────────────────┬───────────────────────────────────────┐
│ Resource Type         │ Old Construct Path                     │ New Construct Path                    │
├───────────────────────┼────────────────────────────────────────┼───────────────────────────────────────┤
│ AWS::IAM::Role        │ Consumer/Function/ServiceRole/Resource │ Consumer/NewName/ServiceRole/Resource │
├───────────────────────┼────────────────────────────────────────┼───────────────────────────────────────┤
│ AWS::Lambda::Function │ Consumer/Function/Resource             │ Consumer/NewName/Resource             │
└───────────────────────┴────────────────────────────────────────┴───────────────────────────────────────┘

Note that we are launching this feature under unstable mode, which requires the user to acknowledge that by passing the --unstable=refactor flag.

Closes #132, #133, #141, #134 and #135.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

# Conflicts:
#	packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts
@github-actions github-actions bot added the p2 label Apr 8, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team April 8, 2025 13:10
Comment on lines +353 to +364
// 8. Refactor (8xxx)
CDK_TOOLKIT_I8900: make.result<RefactorResult>({
code: 'CDK_TOOLKIT_I8900',
description: 'Refactor result',
interface: 'RefactorResult',
}),

CDK_TOOLKIT_W8010: make.warn({
code: 'CDK_TOOLKIT_W8010',
description: 'Refactor execution not yet supported',
}),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I got the pattern right, here. Someone please check me on this.

@@ -0,0 +1,23 @@
import { Writable } from 'node:stream';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved from the diff-formatter.ts file.

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surface level stuff, still need to look at the meat & potatoes

/**
* Refactor Action. Moves resources from one location (stack + logical ID) to another.
*/
public async refactor(cx: ICloudAssemblySource, options: RefactorOptions = {}): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor should probably return some useful structure as well, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. I'm confused as to what should get notified to the IoHost, and what should be returned.


const strategy = options.stacks?.strategy ?? StackSelectionStrategy.ALL_STACKS;
if (strategy !== StackSelectionStrategy.ALL_STACKS) {
await ioHelper.notify(IO.CDK_TOOLKIT_W8010.msg(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this just be an error?

If you want to convert user input into a warning and proceeding anyway, then do the conversion in the CLI?


const stacks = await this.selectStacksForList([]);
try {
const mappings = await detectRefactorMappings(stacks.stackArtifacts, this.props.sdkProvider);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I thought the CLI would use the toolkit code here, but I see we haven't gotten that far yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, the idea is that both the CLI and the toolkit library share common code, but the CLI doesn't call the toolkit library. Not yet, at least.

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some comments that I'll leave for you to decide what to do with, but I don't want to stand in the way of this, especially since you're going to iterate on it over time anyway.

Comment on lines 303 to 304
// 3. Topological sort
const inDegree = Object.keys(graph).reduce((acc, k) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure already have a couple of flavors of toposort lying around the code base already you should be able to reuse 😉

}

// 2. Detect dependencies by searching for Ref/Fn::GetAtt
const findDependencies = (value: any): string[] => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget that dependencies can also come from DependsOn at the top level, which contains logical IDs.

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing: in calculating the digest, should we normalize property order and dependency order?

A reordering of the properties in the bag will affect the hash right now, but it probably shouldn't.

@otaviomacedo otaviomacedo added this pull request to the merge queue Apr 11, 2025
Merged via the queue into main with commit 24b23e4 Apr 11, 2025
19 of 20 checks passed
@otaviomacedo otaviomacedo deleted the otaviom/refactor-dry-run-2 branch April 11, 2025 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement basic resource equivalence
2 participants