generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bc4db2a
feat(refactor): support for stack filtering
otaviomacedo f0f5107
feat(refactor): support for stack filtering
otaviomacedo f9969b8
Merge remote-tracking branch 'origin/otaviom/refactor-stack-filtering…
otaviomacedo a50d6fc
feat(refactor): ability to skip refactoring for selected resources
otaviomacedo 60cfd8d
Resource locations instead of just resource IDs
otaviomacedo 3dee873
Support construct paths in the skip file
otaviomacedo 72c06de
Merge branch 'main' into otaviom/refactor-skip
otaviomacedo b6b2783
regex
otaviomacedo ba46c6b
regex
otaviomacedo 11d0158
Merge branch 'main' into otaviom/refactor-skip
otaviomacedo c39e769
Fixes after merge
otaviomacedo 6df0690
Fix import
otaviomacedo 8cc3278
Reuse toolkit library
otaviomacedo 3caad77
Merge branch 'main' into otaviom/refactor-skip
otaviomacedo 5d75613
Fix imports
otaviomacedo 8832ada
README
otaviomacedo 807cd60
Addressed comments
otaviomacedo f8f2213
SKIP_REFACTOR -> DO_NOT_REFACTOR
otaviomacedo d4c6dc8
More skip -> exclude
otaviomacedo 45ddacc
More skip -> exclude
otaviomacedo d750e5c
Merge branch 'main' into otaviom/refactor-skip
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 hidden or 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 hidden or 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 hidden or 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
112 changes: 112 additions & 0 deletions
112
packages/@aws-cdk/toolkit-lib/lib/api/refactoring/exclude.ts
This file contains hidden or 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,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 excludedLocations: CfnResourceLocation[]; | ||
|
|
||
| constructor(manifest: AssemblyManifest) { | ||
| this.excludedLocations = this.getExcludedLocations(manifest); | ||
| } | ||
|
|
||
| private getExcludedLocations(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 DO_NOT_REFACTOR | ||
| .filter((entries) => | ||
| entries.some((entry) => entry.type === ArtifactMetadataEntryType.DO_NOT_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.excludedLocations.some( | ||
| (loc) => loc.StackName === location.stack.stackName && loc.LogicalResourceId === location.logicalResourceId, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export class InMemoryExcludeList implements ExcludeList { | ||
| private readonly excludedLocations: CfnResourceLocation[]; | ||
| private readonly excludedPaths: string[]; | ||
|
|
||
| constructor(items: string[]) { | ||
| this.excludedLocations = []; | ||
| this.excludedPaths = []; | ||
|
|
||
| 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.excludedLocations.push({ | ||
| StackName: stackName, | ||
| LogicalResourceId: logicalId, | ||
| }); | ||
| } else { | ||
| this.excludedPaths.push(item); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| isExcluded(location: ResourceLocation): boolean { | ||
| const containsLocation = this.excludedLocations.some((loc) => { | ||
| return loc.StackName === location.stack.stackName && loc.LogicalResourceId === location.logicalResourceId; | ||
| }); | ||
|
|
||
| const containsPath = this.excludedPaths.some((path) => location.toPath() === path); | ||
| return containsLocation || containsPath; | ||
| } | ||
| } | ||
|
|
||
| export class UnionExcludeList implements ExcludeList { | ||
| constructor(private readonly excludeLists: ExcludeList[]) { | ||
| } | ||
|
|
||
| isExcluded(location: ResourceLocation): boolean { | ||
| return this.excludeLists.some((excludeList) => excludeList.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 ?? [])]); | ||
| } | ||
|
|
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.