-
Notifications
You must be signed in to change notification settings - Fork 4k
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
feat(core): improved API for tags #3465
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Tagging API Change | ||
|
||
CDK support tagging and can cascade tags to all its taggable children (see [here](https://docs.aws.amazon.com/cdk/latest/guide/tagging.html)). The current CDK tagging API is shown below: | ||
|
||
``` ts | ||
myConstruct.node.applyAspect(new Tag('key', 'value')); | ||
|
||
myConstruct.node.applyAspect(new RemoveTag('key', 'value')); | ||
``` | ||
|
||
As we can see, the current tagging API is not nice and grammatically verbose for using, since there is no reason to expose `node` to users and `applyAspect` does not indicate anything towards tags, which leaves room for improvement. Also, users need to create two objects to add tag and remove tag which causes confusion to some degree. | ||
|
||
## General approach | ||
|
||
For the tagging behavior part, we propose using just one entry point `Tag` for the new tagging API: | ||
|
||
``` ts | ||
Tag.add(myConstruct, 'key', 'value'); | ||
|
||
Tag.remove(myConstruct, 'key'); | ||
``` | ||
|
||
## Code changes | ||
|
||
Given the above, we should make the following changes: | ||
1. Add two methods `add` and `remove` to `Tag` class, which calls `applyAspect`to add tags or remove tags. | ||
|
||
# Part1: Change CDK Tagging API | ||
|
||
Implementation for the new tagging API is shown below: | ||
|
||
``` ts | ||
/** | ||
* The Tag Aspect will handle adding a tag to this node and cascading tags to children | ||
*/ | ||
export class Tag extends TagBase { | ||
|
||
/** | ||
* add tags to the node of a construct and all its the taggable children | ||
*/ | ||
public static add(scope: Construct, key: string, value: string, props: TagProps = {}) { | ||
scope.node.applyAspect(new Tag(key, value, props)); | ||
} | ||
|
||
/** | ||
* remove tags to the node of a construct and all its the taggable children | ||
*/ | ||
public static remove(scope: Construct, key: string, props: TagProps = {}) { | ||
scope.node.applyAspect(new RemoveTag(key, props)); | ||
} | ||
|
||
... | ||
} | ||
``` | ||
|
||
And below is an example use case demonstrating how the adjusted tagging API works: | ||
|
||
``` ts | ||
// Create Task Definition | ||
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'TaskDef'); | ||
|
||
// Create Service | ||
const service = new ecs.Ec2Service(stack, "Service", { | ||
cluster, | ||
taskDefinition, | ||
}); | ||
|
||
Tag.add(taskDefinition, 'tfoo', 'tbar'); | ||
Tag.remove(taskDefinition, 'foo', 'bar'); | ||
|
||
Tag.add(service, 'sfoo', 'sbar'); | ||
Tag.remove(service, 'foo', 'bar'); | ||
``` |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
since tags are essentially maps, why not something like this:
This will allow people to add multiple tags like this:
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.
But tag also supports other properties like priority. See here. I would suggest Tag.add(scope, [
(tag1, 'value1', props)
(tag2, 'value2', props)
]);
but not sure if that's an over-engineer.
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.
okay, let's just leave it with
Tag.add(scope, key, value[, options)