-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assertions): added getResourceId method to Template (#33521)
### Reason for this change Sometimes you want to correlate how cloudformation resources correlate to each other. CDK synthesizes the template expectedly with `Ref` and `Fn:GetAtt`. Currently you'll have to do something like this to verify that a bucketpolicy is attached to the correct bucket: ```ts const resources = template.findResources('AWS::S3::Bucket', { Properties: { BucketName: 'my-bucket', } }) const keys = Object.keys(resources) if (keys.length === 0) { throw new Error('Resource not found.') } if (keys.length !== 1) { throw new Error('Resource is not unique.') } const bucket = keys[0] template.hasResourceProperties('AWS::S3::BucketPolicy', { Bucket: { Ref: bucket, }, // .... }) ``` ### Description of changes Added method `getResourceId` on `Template` to retrieve a distinct match's resource id. ```ts // throws AssertionError on none or multiple matches const bucket = template.getResourceId('AWS::S3::Bucket', { Properties: { BucketName: 'my-bucket', } }) template.hasResourceProperties('AWS::S3::BucketPolicy', { Bucket: { Ref: bucket, }, // .... }) ``` ### Description of how you validated changes Unit tests. Integration tests not applicable. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
4 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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