Skip to content

Commit 98ccd95

Browse files
authored
Improving TagHelper, so it accounts for system tags in case of resource import (#400)
1 parent d4fd647 commit 98ccd95

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

python/rpdk/java/templates/init/guided_aws/TagHelper.java

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,6 @@ public static Set<Tag> convertToSet(final Map<String, String> tagMap) {
7070
.collect(Collectors.toSet());
7171
}
7272

73-
/**
74-
* generateTagsForCreate
75-
*
76-
* Generate tags to put into resource creation request.
77-
* This includes user defined tags and system tags as well.
78-
*/
79-
public final Map<String, String> generateTagsForCreate(final ResourceModel resourceModel, final ResourceHandlerRequest<ResourceModel> handlerRequest) {
80-
final Map<String, String> tagMap = new HashMap<>();
81-
82-
// merge system tags with desired resource tags if your service supports CloudFormation system tags
83-
tagMap.putAll(handlerRequest.getSystemTags());
84-
85-
if (handlerRequest.getDesiredResourceTags() != null) {
86-
tagMap.putAll(handlerRequest.getDesiredResourceTags());
87-
}
88-
89-
// TODO: get tags from resource model based on your tag property name
90-
// TODO: tagMap.putAll(convertToMap(resourceModel.getTags()));
91-
return Collections.unmodifiableMap(tagMap);
92-
}
93-
9473
/**
9574
* shouldUpdateTags
9675
*
@@ -106,32 +85,59 @@ public final boolean shouldUpdateTags(final ResourceModel resourceModel, final R
10685
* getPreviouslyAttachedTags
10786
*
10887
* If stack tags and resource tags are not merged together in Configuration class,
109-
* we will get previous attached user defined tags from both handlerRequest.getPreviousResourceTags (stack tags)
110-
* and handlerRequest.getPreviousResourceState (resource tags).
88+
* we will get previously attached system (with `aws:cloudformation` prefix) and user defined tags from
89+
* handlerRequest.getPreviousSystemTags() (system tags),
90+
* handlerRequest.getPreviousResourceTags() (stack tags),
91+
* handlerRequest.getPreviousResourceState().getTags() (resource tags).
92+
*
93+
* System tags are an optional feature. Merge them to your tags if you have enabled them for your resource.
94+
* System tags can change on resource update if the resource is imported to the stack.
11195
*/
11296
public Map<String, String> getPreviouslyAttachedTags(final ResourceHandlerRequest<ResourceModel> handlerRequest) {
97+
final Map<String, String> previousTags = new HashMap<>();
98+
99+
// TODO: get previous system tags if your service supports CloudFormation system tags
100+
// if (handlerRequest.getPreviousSystemTags() != null) {
101+
// previousTags.putAll(handlerRequest.getPreviousSystemTags());
102+
// }
103+
113104
// get previous stack level tags from handlerRequest
114-
final Map<String, String> previousTags = handlerRequest.getPreviousResourceTags() != null ?
115-
handlerRequest.getPreviousResourceTags() : Collections.emptyMap();
105+
if (handlerRequest.getPreviousResourceTags() != null) {
106+
previousTags.putAll(handlerRequest.getPreviousResourceTags());
107+
}
116108

117109
// TODO: get resource level tags from previous resource state based on your tag property name
118-
// TODO: previousTags.putAll(handlerRequest.getPreviousResourceState().getTags());
110+
// TODO: previousTags.putAll(handlerRequest.getPreviousResourceState().getTags()); // if tags are not null
119111
return previousTags;
120112
}
121113

122114
/**
123115
* getNewDesiredTags
124116
*
125117
* If stack tags and resource tags are not merged together in Configuration class,
126-
* we will get new user defined tags from both resource model and previous stack tags.
118+
* we will get new desired system (with `aws:cloudformation` prefix) and user defined tags from
119+
* handlerRequest.getSystemTags() (system tags),
120+
* handlerRequest.getDesiredResourceTags() (stack tags),
121+
* handlerRequest.getDesiredResourceState().getTags() (resource tags).
122+
*
123+
* System tags are an optional feature. Merge them to your tags if you have enabled them for your resource.
124+
* System tags can change on resource update if the resource is imported to the stack.
127125
*/
128-
public Map<String, String> getNewDesiredTags(final ResourceModel resourceModel, final ResourceHandlerRequest<ResourceModel> handlerRequest) {
129-
// get new stack level tags from handlerRequest
130-
final Map<String, String> desiredTags = handlerRequest.getDesiredResourceTags() != null ?
131-
handlerRequest.getDesiredResourceTags() : Collections.emptyMap();
126+
public Map<String, String> getNewDesiredTags(final ResourceHandlerRequest<ResourceModel> handlerRequest) {
127+
final Map<String, String> desiredTags = new HashMap<>();
128+
129+
// TODO: merge system tags with desired resource tags if your service supports CloudFormation system tags
130+
// if (handlerRequest.getSystemTags() != null) {
131+
// desiredTags.putAll(handlerRequest.getSystemTags());
132+
// }
133+
134+
// get desired stack level tags from handlerRequest
135+
if (handlerRequest.getDesiredResourceTags() != null) {
136+
desiredTags.putAll(handlerRequest.getDesiredResourceTags());
137+
}
132138

133139
// TODO: get resource level tags from resource model based on your tag property name
134-
// TODO: desiredTags.putAll(convertToMap(resourceModel.getTags()));
140+
// TODO: desiredTags.putAll(convertToMap(handlerRequest.getDesiredResourceState().getTags())); // if tags are not null
135141
return desiredTags;
136142
}
137143

0 commit comments

Comments
 (0)