1
1
import { TagType } from './cfn-resource' ;
2
2
import { CfnTag } from './cfn-tag' ;
3
+ import { Lazy } from './lazy' ;
4
+ import { IResolvable } from './resolvable' ;
3
5
4
6
interface Tag {
5
7
key : string ;
@@ -228,7 +230,32 @@ export interface TagManagerOptions {
228
230
}
229
231
230
232
/**
231
- * TagManager facilitates a common implementation of tagging for Constructs.
233
+ * TagManager facilitates a common implementation of tagging for Constructs
234
+ *
235
+ * Normally, you do not need to use this class, as the CloudFormation specification
236
+ * will indicate which resources are taggable. However, sometimes you will need this
237
+ * to make custom resources taggable. Used `tagManager.renderedTags` to obtain a
238
+ * value that will resolve to the tags at synthesis time.
239
+ *
240
+ * @example
241
+ * import * as cdk from '@aws-cdk/core';
242
+ *
243
+ * class MyConstruct extends cdk.Resource implements cdk.ITaggable {
244
+ * public readonly tags = new cdk.TagManager(cdk.TagType.KEY_VALUE, 'Whatever::The::Type');
245
+ *
246
+ * constructor(scope: cdk.Construct, id: string) {
247
+ * super(scope, id);
248
+ *
249
+ * new cdk.CfnResource(this, 'Resource', {
250
+ * type: 'Whatever::The::Type',
251
+ * properties: {
252
+ * // ...
253
+ * Tags: this.tags.renderedTags,
254
+ * },
255
+ * });
256
+ * }
257
+ * }
258
+ *
232
259
*/
233
260
export class TagManager {
234
261
@@ -247,6 +274,14 @@ export class TagManager {
247
274
*/
248
275
public readonly tagPropertyName : string ;
249
276
277
+ /**
278
+ * A lazy value that represents the rendered tags at synthesis time
279
+ *
280
+ * If you need to make a custom construct taggable, use the value of this
281
+ * property to pass to the `tags` property of the underlying construct.
282
+ */
283
+ public readonly renderedTags : IResolvable ;
284
+
250
285
private readonly tags = new Map < string , Tag > ( ) ;
251
286
private readonly priorities = new Map < string , number > ( ) ;
252
287
private readonly tagFormatter : ITagFormatter ;
@@ -260,6 +295,8 @@ export class TagManager {
260
295
this . _setTag ( ...this . tagFormatter . parseTags ( tagStructure , this . initialTagPriority ) ) ;
261
296
}
262
297
this . tagPropertyName = options . tagPropertyName || 'tags' ;
298
+
299
+ this . renderedTags = Lazy . any ( { produce : ( ) => this . renderTags ( ) } ) ;
263
300
}
264
301
265
302
/**
@@ -287,6 +324,11 @@ export class TagManager {
287
324
288
325
/**
289
326
* Renders tags into the proper format based on TagType
327
+ *
328
+ * This method will eagerly render the tags currently applied. In
329
+ * most cases, you should be using `tagManager.renderedTags` instead,
330
+ * which will return a `Lazy` value that will resolve to the correct
331
+ * tags at synthesis time.
290
332
*/
291
333
public renderTags ( ) : any {
292
334
return this . tagFormatter . formatTags ( this . sortedTags ) ;
0 commit comments