-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(servicecatalog): Create TagOptions Construct (#18314)
Fixes: [#17753](#17753) Previously TagOptions were defined via an interface and we only created the underlying resources upon an association. This broke CX if tagoptions were mangaged centrally. We move to make the TagOptions class a wrapper around aggregate individual TagOptions. BREAKING CHANGE: `TagOptions` now have `scope` and `props` argument in constructor, and data is now passed via a `allowedValueForTags` field in props ---- *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
12 changed files
with
351 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,70 @@ | ||
import * as cdk from '@aws-cdk/core'; | ||
import { hashValues } from './private/util'; | ||
import { InputValidator } from './private/validation'; | ||
import { CfnTagOption } from './servicecatalog.generated'; | ||
|
||
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main | ||
// eslint-disable-next-line no-duplicate-imports, import/order | ||
import { Construct } from 'constructs'; | ||
|
||
/** | ||
* Properties for TagOptions. | ||
*/ | ||
export interface TagOptionsProps { | ||
/** | ||
* The values that are allowed to be set for specific tags. | ||
* The keys of the map represent the tag keys, | ||
* and the values of the map are a list of allowed values for that particular tag key. | ||
*/ | ||
readonly allowedValuesForTags: { [tagKey: string]: string[] }; | ||
} | ||
|
||
/** | ||
* Defines a Tag Option, which are similar to tags | ||
* but have multiple values per key. | ||
* Defines a set of TagOptions, which are a list of key-value pairs managed in AWS Service Catalog. | ||
* It is not an AWS tag, but serves as a template for creating an AWS tag based on the TagOption. | ||
* See https://docs.aws.amazon.com/servicecatalog/latest/adminguide/tagoptions.html | ||
* | ||
* @resource AWS::ServiceCatalog::TagOption | ||
*/ | ||
export class TagOptions { | ||
export class TagOptions extends cdk.Resource { | ||
/** | ||
* List of CfnTagOption | ||
*/ | ||
public readonly tagOptionsMap: { [key: string]: string[] }; | ||
* List of underlying CfnTagOption resources. | ||
* | ||
* @internal | ||
*/ | ||
public _cfnTagOptions: CfnTagOption[]; | ||
|
||
constructor(tagOptionsMap: { [key: string]: string[]} ) { | ||
this.tagOptionsMap = { ...tagOptionsMap }; | ||
constructor(scope: Construct, id: string, props: TagOptionsProps) { | ||
super(scope, id); | ||
|
||
this._cfnTagOptions = this.createUnderlyingTagOptions(props.allowedValuesForTags); | ||
} | ||
|
||
private createUnderlyingTagOptions(allowedValuesForTags: { [tagKey: string]: string[] }): CfnTagOption[] { | ||
if (Object.keys(allowedValuesForTags).length === 0) { | ||
throw new Error(`No tag option keys or values were provided for resource ${this.node.path}`); | ||
} | ||
var tagOptions: CfnTagOption[] = []; | ||
|
||
for (const [tagKey, tagValues] of Object.entries(allowedValuesForTags)) { | ||
InputValidator.validateLength(this.node.addr, 'TagOption key', 1, 128, tagKey); | ||
|
||
const uniqueTagValues = new Set(tagValues); | ||
if (uniqueTagValues.size === 0) { | ||
throw new Error(`No tag option values were provided for tag option key ${tagKey} for resource ${this.node.path}`); | ||
} | ||
uniqueTagValues.forEach((tagValue: string) => { | ||
InputValidator.validateLength(this.node.addr, 'TagOption value', 1, 256, tagValue); | ||
const tagOptionIdentifier = hashValues(tagKey, tagValue); | ||
const tagOption = new CfnTagOption(this, tagOptionIdentifier, { | ||
key: tagKey, | ||
value: tagValue, | ||
active: true, | ||
}); | ||
tagOptions.push(tagOption); | ||
}); | ||
} | ||
return tagOptions; | ||
} | ||
} | ||
|
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
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
Oops, something went wrong.