-
Notifications
You must be signed in to change notification settings - Fork 69
refactor(toolkit): resource metadata #243
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { ArtifactMetadataEntryType, type MetadataEntry } from '@aws-cdk/cloud-assembly-schema'; | ||
| import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api'; | ||
|
|
||
| /** | ||
| * Metadata entry for a resource within a CloudFormation stack | ||
| */ | ||
| export interface ResourceMetadata { | ||
| /** | ||
| * The resource's metadata as declared in the cloud assembly | ||
| */ | ||
| readonly entry: MetadataEntry; | ||
| /** | ||
| * The construct path of the resource | ||
| */ | ||
| readonly constructPath: string; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to read metadata for resources from a CloudFormation stack artifact | ||
| * | ||
| * @param stack The CloudFormation stack to read from | ||
| * @param logicalId The logical ID of the resource to read | ||
| * | ||
| * @returns The resource metadata, or undefined if the resource was not found | ||
| */ | ||
| export function resourceMetadata(stack: CloudFormationStackArtifact, logicalId: string): ResourceMetadata | undefined { | ||
| const metadata = stack.manifest?.metadata; | ||
| if (!metadata) { | ||
| return undefined; | ||
| } | ||
|
|
||
| for (const path of Object.keys(metadata)) { | ||
| const entry = metadata[path] | ||
| .filter((e) => e.type === ArtifactMetadataEntryType.LOGICAL_ID) | ||
| .find((e) => e.data === logicalId); | ||
| if (entry) { | ||
| return { | ||
| entry, | ||
| constructPath: simplifyConstructPath(path, stack.stackName), | ||
| }; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function simplifyConstructPath(path: string, stackName: string) { | ||
| path = path.replace(/\/Resource$/, ''); | ||
| path = path.replace(/^\//, ''); // remove "/" prefix | ||
|
|
||
| // remove "<stack-name>/" prefix | ||
| if (stackName) { | ||
| if (path.startsWith(stackName + '/')) { | ||
| path = path.slice(stackName.length + 1); | ||
| } | ||
| } | ||
| return path; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
|
|
||
| import * as util from 'util'; | ||
| import { ArtifactMetadataEntryType } from '@aws-cdk/cloud-assembly-schema'; | ||
| import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api'; | ||
| import type { ResourceMetadata, StackActivity, StackMonitoringControlEvent } from '@aws-cdk/tmp-toolkit-helpers'; | ||
| import type { StackActivity, StackMonitoringControlEvent } from '@aws-cdk/tmp-toolkit-helpers'; | ||
| import * as uuid from 'uuid'; | ||
| import { StackEventPoller } from './stack-event-poller'; | ||
| import { resourceMetadata } from '../../../../@aws-cdk/tmp-toolkit-helpers/src/api/resource-metadata/resource-metadata'; | ||
| import { debug, error, info } from '../../cli/messages'; | ||
| import { stackEventHasErrorMessage } from '../../util'; | ||
| import type { ICloudFormationClient } from '../aws-auth'; | ||
|
|
@@ -181,23 +181,12 @@ export class StackActivityMonitor { | |
| this.scheduleNextTick(); | ||
| } | ||
|
|
||
| private findMetadataFor(logicalId: string | undefined): ResourceMetadata | undefined { | ||
| private findMetadataFor(logicalId: string | undefined) { | ||
|
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. why delete this? seems like we are still returning the same type |
||
| const metadata = this.stack.manifest?.metadata; | ||
| if (!logicalId || !metadata) { | ||
| return undefined; | ||
| } | ||
| for (const path of Object.keys(metadata)) { | ||
| const entry = metadata[path] | ||
| .filter((e) => e.type === ArtifactMetadataEntryType.LOGICAL_ID) | ||
| .find((e) => e.data === logicalId); | ||
| if (entry) { | ||
| return { | ||
| entry, | ||
| constructPath: this.simplifyConstructPath(path), | ||
| }; | ||
| } | ||
| } | ||
| return undefined; | ||
| return resourceMetadata(this.stack, logicalId); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -278,15 +267,4 @@ export class StackActivityMonitor { | |
| } | ||
| } | ||
| } | ||
|
|
||
| private simplifyConstructPath(path: string) { | ||
| path = path.replace(/\/Resource$/, ''); | ||
| path = path.replace(/^\//, ''); // remove "/" prefix | ||
|
|
||
| // remove "<stack-name>/" prefix | ||
| if (path.startsWith(this.stackName + '/')) { | ||
| path = path.slice(this.stackName.length + 1); | ||
| } | ||
| return path; | ||
| } | ||
| } | ||
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.
i assume by the way you are "enforcing" this that these are not meant to be strongly enforced at all, and will rely on our attention to detail to ensure we follow.
i feel like we are getting to the point where these rules may be hard to get right when we eventually introduce new message codes.