|
| 1 | +import * as cdk from '@aws-cdk/core'; |
| 2 | +import { hashValues } from './private/util'; |
| 3 | +import { InputValidator } from './private/validation'; |
| 4 | +import { CfnTagOption } from './servicecatalog.generated'; |
| 5 | + |
| 6 | +// keep this import separate from other imports to reduce chance for merge conflicts with v2-main |
| 7 | +// eslint-disable-next-line no-duplicate-imports, import/order |
| 8 | +import { Construct } from 'constructs'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Properties for TagOptions. |
| 12 | + */ |
| 13 | +export interface TagOptionsProps { |
| 14 | + /** |
| 15 | + * The values that are allowed to be set for specific tags. |
| 16 | + * The keys of the map represent the tag keys, |
| 17 | + * and the values of the map are a list of allowed values for that particular tag key. |
| 18 | + */ |
| 19 | + readonly allowedValuesForTags: { [tagKey: string]: string[] }; |
| 20 | +} |
| 21 | + |
1 | 22 | /**
|
2 |
| - * Defines a Tag Option, which are similar to tags |
3 |
| - * but have multiple values per key. |
| 23 | + * Defines a set of TagOptions, which are a list of key-value pairs managed in AWS Service Catalog. |
| 24 | + * It is not an AWS tag, but serves as a template for creating an AWS tag based on the TagOption. |
| 25 | + * See https://docs.aws.amazon.com/servicecatalog/latest/adminguide/tagoptions.html |
| 26 | + * |
| 27 | + * @resource AWS::ServiceCatalog::TagOption |
4 | 28 | */
|
5 |
| -export class TagOptions { |
| 29 | +export class TagOptions extends cdk.Resource { |
6 | 30 | /**
|
7 |
| - * List of CfnTagOption |
8 |
| - */ |
9 |
| - public readonly tagOptionsMap: { [key: string]: string[] }; |
| 31 | + * List of underlying CfnTagOption resources. |
| 32 | + * |
| 33 | + * @internal |
| 34 | + */ |
| 35 | + public _cfnTagOptions: CfnTagOption[]; |
10 | 36 |
|
11 |
| - constructor(tagOptionsMap: { [key: string]: string[]} ) { |
12 |
| - this.tagOptionsMap = { ...tagOptionsMap }; |
| 37 | + constructor(scope: Construct, id: string, props: TagOptionsProps) { |
| 38 | + super(scope, id); |
| 39 | + |
| 40 | + this._cfnTagOptions = this.createUnderlyingTagOptions(props.allowedValuesForTags); |
| 41 | + } |
| 42 | + |
| 43 | + private createUnderlyingTagOptions(allowedValuesForTags: { [tagKey: string]: string[] }): CfnTagOption[] { |
| 44 | + if (Object.keys(allowedValuesForTags).length === 0) { |
| 45 | + throw new Error(`No tag option keys or values were provided for resource ${this.node.path}`); |
| 46 | + } |
| 47 | + var tagOptions: CfnTagOption[] = []; |
| 48 | + |
| 49 | + for (const [tagKey, tagValues] of Object.entries(allowedValuesForTags)) { |
| 50 | + InputValidator.validateLength(this.node.addr, 'TagOption key', 1, 128, tagKey); |
| 51 | + |
| 52 | + const uniqueTagValues = new Set(tagValues); |
| 53 | + if (uniqueTagValues.size === 0) { |
| 54 | + throw new Error(`No tag option values were provided for tag option key ${tagKey} for resource ${this.node.path}`); |
| 55 | + } |
| 56 | + uniqueTagValues.forEach((tagValue: string) => { |
| 57 | + InputValidator.validateLength(this.node.addr, 'TagOption value', 1, 256, tagValue); |
| 58 | + const tagOptionIdentifier = hashValues(tagKey, tagValue); |
| 59 | + const tagOption = new CfnTagOption(this, tagOptionIdentifier, { |
| 60 | + key: tagKey, |
| 61 | + value: tagValue, |
| 62 | + active: true, |
| 63 | + }); |
| 64 | + tagOptions.push(tagOption); |
| 65 | + }); |
| 66 | + } |
| 67 | + return tagOptions; |
13 | 68 | }
|
14 | 69 | }
|
| 70 | + |
0 commit comments