-
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 17 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 | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,4 +14,17 @@ export interface RefactorOptions { | |||||||||||||||||||||||||||||||||||||||||||||||
| * @default - all stacks | ||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
| stacks?: StackSelector; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||
| * A list of resources to skip during the refactor. Elements of | ||||||||||||||||||||||||||||||||||||||||||||||||
| * this list should be _destination_ locations that should be skipped, | ||||||||||||||||||||||||||||||||||||||||||||||||
| * i.e., the location to which a resource would be moved if the | ||||||||||||||||||||||||||||||||||||||||||||||||
| * refactor were to happen. | ||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||
| * The format of the locations in the file can be either: | ||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||
| * - Stack name and logical ID (e.g. `Stack1.MyQueue`) | ||||||||||||||||||||||||||||||||||||||||||||||||
| * - A construct path (e.g. `Stack1/Foo/Bar/Resource`). | ||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. But also see other conversation around destination.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| exclude?: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. file name should be exclude |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| 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 type { ResourceLocation } from './cloudformation'; | ||
|
|
||
| export interface ExcludeList { | ||
| isExcluded(location: ResourceLocation): boolean; | ||
| } | ||
|
|
||
| export class ManifestExcludeList implements ExcludeList { | ||
| private readonly skippedLocations: CfnResourceLocation[]; | ||
|
|
||
| constructor(manifest: AssemblyManifest) { | ||
| this.skippedLocations = this.getSkippedLocations(manifest); | ||
| } | ||
|
|
||
| private getSkippedLocations(asmManifest: AssemblyManifest): CfnResourceLocation[] { | ||
| // First, we need to filter the artifacts to only include CloudFormation stacks | ||
| const stackManifests = Object.entries(asmManifest.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; | ||
| } | ||
|
|
||
| isExcluded(location: ResourceLocation): boolean { | ||
| return this.skippedLocations.some( | ||
| (loc) => loc.StackName === location.stack.stackName && loc.LogicalResourceId === location.logicalResourceId, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export class InMemoryExcludeList implements ExcludeList { | ||
| private readonly skippedLocations: CfnResourceLocation[]; | ||
| private readonly skippedPaths: string[]; | ||
|
|
||
| constructor(items: string[]) { | ||
| this.skippedLocations = []; | ||
| this.skippedPaths = []; | ||
|
|
||
| if (items.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const locationRegex = /^[A-Za-z0-9]+\.[A-Za-z0-9]+$/; | ||
|
||
|
|
||
| items.forEach((item: string) => { | ||
| if (locationRegex.test(item)) { | ||
| const [stackName, logicalId] = item.split('.'); | ||
| this.skippedLocations.push({ | ||
| StackName: stackName, | ||
| LogicalResourceId: logicalId, | ||
| }); | ||
| } else { | ||
| this.skippedPaths.push(item); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| isExcluded(location: ResourceLocation): boolean { | ||
| const containsLocation = this.skippedLocations.some((loc) => { | ||
| return loc.StackName === location.stack.stackName && loc.LogicalResourceId === location.logicalResourceId; | ||
| }); | ||
|
|
||
| const containsPath = this.skippedPaths.some((path) => location.toPath() === path); | ||
| return containsLocation || containsPath; | ||
| } | ||
| } | ||
|
|
||
| export class UnionExcludeList implements ExcludeList { | ||
| constructor(private readonly skipLists: ExcludeList[]) { | ||
| } | ||
|
|
||
| isExcluded(location: ResourceLocation): boolean { | ||
| return this.skipLists.some((skipList) => skipList.isExcluded(location)); | ||
| } | ||
| } | ||
|
|
||
| export class NeverExclude implements ExcludeList { | ||
| isExcluded(_location: ResourceLocation): boolean { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export class AlwaysExclude implements ExcludeList { | ||
| isExcluded(_location: ResourceLocation): boolean { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| export function fromManifestAndExclusionList(manifest: AssemblyManifest, exclude?: string[]): ExcludeList { | ||
| return new UnionExcludeList([new ManifestExcludeList(manifest), new InMemoryExcludeList(exclude ?? [])]); | ||
| } | ||
|
|
||
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