-
Notifications
You must be signed in to change notification settings - Fork 74
feat(refactor): ability to exclude selected resources from refactoring #421
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
Changes from 5 commits
bc4db2a
f0f5107
f9969b8
a50d6fc
60cfd8d
3dee873
72c06de
b6b2783
ba46c6b
11d0158
c39e769
6df0690
8cc3278
3caad77
5d75613
8832ada
807cd60
f8f2213
d4c6dc8
45ddacc
d750e5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import * as fs from 'node:fs'; | ||
| import type { AssemblyManifest } from '@aws-cdk/cloud-assembly-schema'; | ||
| import { ArtifactMetadataEntryType, ArtifactType } from '@aws-cdk/cloud-assembly-schema'; | ||
| import type { ResourceLocation as CfnResourceLocation } from '@aws-sdk/client-cloudformation'; | ||
| import { ToolkitError } from '../toolkit-error'; | ||
|
|
||
| export interface SkipList { | ||
| resourceLocations: CfnResourceLocation[]; | ||
| } | ||
|
|
||
| export class ManifestSkipList implements SkipList { | ||
| constructor(private readonly manifest: AssemblyManifest) { | ||
| } | ||
|
|
||
| get resourceLocations(): CfnResourceLocation[] { | ||
| // First, we need to filter the artifacts to only include CloudFormation stacks | ||
| const stackManifests = Object.entries(this.manifest.artifacts ?? {}).filter( | ||
| ([_, manifest]) => manifest.type === ArtifactType.AWS_CLOUDFORMATION_STACK, | ||
| ); | ||
|
|
||
| const result: CfnResourceLocation[] = []; | ||
| for (let [stackName, manifest] of stackManifests) { | ||
| const locations = Object.values(manifest.metadata ?? {}) | ||
| // Then pick only the resources in each stack marked with SKIP_REFACTOR | ||
| .filter((entries) => | ||
| entries.some((entry) => entry.type === ArtifactMetadataEntryType.SKIP_REFACTOR && entry.data === true), | ||
| ) | ||
| // Finally, get the logical ID of each resource | ||
| .map((entries) => { | ||
| const logicalIdEntry = entries.find((entry) => entry.type === ArtifactMetadataEntryType.LOGICAL_ID); | ||
| const location: CfnResourceLocation = { | ||
| StackName: stackName, | ||
| LogicalResourceId: logicalIdEntry!.data! as string, | ||
| }; | ||
| return location; | ||
| }); | ||
| result.push(...locations); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| export class SkipFile implements SkipList { | ||
| constructor(private readonly filePath?: string) { | ||
| } | ||
|
|
||
| get resourceLocations(): CfnResourceLocation[] { | ||
| if (!this.filePath) { | ||
| return []; | ||
| } | ||
| const parsedData = JSON.parse(fs.readFileSync(this.filePath, 'utf-8')); | ||
| if (!isValidSkipFileContent(parsedData)) { | ||
| throw new ToolkitError('The content of a skip file must be a JSON array of strings'); | ||
| } | ||
|
|
||
| const result: CfnResourceLocation[] = []; | ||
| parsedData.forEach((item: string) => { | ||
| const parts = item.split('.'); | ||
| if (parts.length !== 2) { | ||
| throw new ToolkitError(`Invalid resource location format: ${item}. Expected format: stackName.logicalId`); | ||
| } | ||
| const [stackName, logicalId] = parts; | ||
| result.push({ | ||
| StackName: stackName, | ||
| LogicalResourceId: logicalId, | ||
| }); | ||
| }); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| function isValidSkipFileContent(data: any): data is string[] { | ||
| return Array.isArray(data) && data.every((item: any) => typeof item === 'string'); | ||
| } | ||
|
|
||
| export class UnionSkipList implements SkipList { | ||
| constructor(private readonly skipLists: SkipList[]) { | ||
| } | ||
|
|
||
| get resourceLocations(): CfnResourceLocation[] { | ||
| return this.skipLists.flatMap((skipList) => skipList.resourceLocations); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,4 +14,13 @@ export interface RefactorOptions { | |
| * @default - all stacks | ||
| */ | ||
| stacks?: StackSelector; | ||
|
|
||
| /** | ||
| * The absolute path to a file that contains a list of | ||
| * resources to skip during the refactor. The file should | ||
| * be in JSON format and contain an array of _destination_ | ||
| * logical IDs, that is, the logical IDs of the resources | ||
| * as they would be after the refactor. | ||
| */ | ||
| skipFile?: string; | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import * as s3 from 'aws-cdk-lib/aws-s3'; | ||
| import * as core from 'aws-cdk-lib/core'; | ||
|
|
||
| export default async () => { | ||
| const app = new core.App({ autoSynth: false }); | ||
| const stack = new core.Stack(app, 'Stack1'); | ||
| const bucket = new s3.Bucket(stack, 'MyBucket'); | ||
| bucket.node.defaultChild?.node.addMetadata('aws:cdk:skip-refactor', true); | ||
| return app.synth(); | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.
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.
Should this be called exclude now? Or
or