16
16
import software .amazon .awssdk .awscore .AwsResponse ;
17
17
import software .amazon .awssdk .core .SdkClient ;
18
18
// TODO: Critical! Please replace the CloudFormation Tag model below with your service's own SDK Tag model
19
- import software .amazon .awssdk .services .cloudformation .model .Tag ;
20
19
import software .amazon .cloudformation .proxy .AmazonWebServicesClientProxy ;
21
20
import software .amazon .cloudformation .proxy .Logger ;
22
21
import software .amazon .cloudformation .proxy .ProgressEvent ;
@@ -40,11 +39,11 @@ public static Map<String, String> convertToMap(final Collection<Tag> tags) {
40
39
return Collections .emptyMap ();
41
40
}
42
41
return tags .stream ()
43
- .filter (tag -> tag .value () != null )
44
- .collect (Collectors .toMap (
45
- Tag ::key ,
46
- Tag ::value ,
47
- (oldValue , newValue ) -> newValue ));
42
+ .filter (tag -> tag .getValue () != null )
43
+ .collect (Collectors .toMap (
44
+ Tag ::getKey ,
45
+ Tag ::getValue ,
46
+ (oldValue , newValue ) -> newValue ));
48
47
}
49
48
50
49
/**
@@ -57,17 +56,17 @@ public static Map<String, String> convertToMap(final Collection<Tag> tags) {
57
56
* @param tagMap Map of tags to convert
58
57
* @return Set of Tag objects
59
58
*/
60
- public static Set <Tag > convertToSet (final Map <String , String > tagMap ) {
59
+ public static Set <Tag > convertToTagSet (final Map <String , String > tagMap ) {
61
60
if (MapUtils .isEmpty (tagMap )) {
62
61
return Collections .emptySet ();
63
62
}
64
63
return tagMap .entrySet ().stream ()
65
- .filter (tag -> tag .getValue () != null )
66
- .map (tag -> Tag .builder ()
67
- .key (tag .getKey ())
68
- .value (tag .getValue ())
69
- .build ())
70
- .collect (Collectors .toSet ());
64
+ .filter (tag -> tag .getValue () != null )
65
+ .map (tag -> Tag .builder ()
66
+ .key (tag .getKey ())
67
+ .value (tag .getValue ())
68
+ .build ())
69
+ .collect (Collectors .toSet ());
71
70
}
72
71
73
72
/**
@@ -96,23 +95,45 @@ public final Map<String, String> generateTagsForCreate(final ResourceModel resou
96
95
*
97
96
* Determines whether user defined tags have been changed during update.
98
97
*/
99
- public final boolean shouldUpdateTags (final ResourceModel resourceModel , final ResourceHandlerRequest <ResourceModel > handlerRequest ) {
98
+ public static boolean shouldUpdateTags (final ResourceHandlerRequest <ResourceModel > handlerRequest ) {
100
99
final Map <String , String > previousTags = getPreviouslyAttachedTags (handlerRequest );
101
- final Map <String , String > desiredTags = getNewDesiredTags (resourceModel , handlerRequest );
100
+ final Map <String , String > desiredTags = getNewDesiredTags (handlerRequest );
102
101
return ObjectUtils .notEqual (previousTags , desiredTags );
103
102
}
104
103
104
+ /**
105
+ * If stack tags and resource tags are not merged together in Configuration class,
106
+ * we will get new user defined tags from both resource model and previous stack tags.
107
+ */
108
+ public static Map <String , String > mergeTags (
109
+ ResourceHandlerRequest <ResourceModel > request ,
110
+ Map <String , String > resourceTags ,
111
+ Map <String , String > systemTags ,
112
+ Map <String , String > stackTags ) {
113
+ final Map <String , String > tagMap = new HashMap <>();
114
+ if (resourceTags != null ) {
115
+ tagMap .putAll (resourceTags );
116
+ }
117
+ if (systemTags != null ) {
118
+ tagMap .putAll (systemTags );
119
+ }
120
+ if (stackTags != null ) {
121
+ tagMap .putAll (stackTags );
122
+ }
123
+ return tagMap ;
124
+ }
125
+
105
126
/**
106
127
* getPreviouslyAttachedTags
107
128
*
108
129
* If stack tags and resource tags are not merged together in Configuration class,
109
130
* we will get previous attached user defined tags from both handlerRequest.getPreviousResourceTags (stack tags)
110
131
* and handlerRequest.getPreviousResourceState (resource tags).
111
132
*/
112
- public Map <String , String > getPreviouslyAttachedTags (final ResourceHandlerRequest <ResourceModel > handlerRequest ) {
133
+ public static Map <String , String > getPreviouslyAttachedTags (final ResourceHandlerRequest <ResourceModel > handlerRequest ) {
113
134
// get previous stack level tags from handlerRequest
114
135
final Map <String , String > previousTags = handlerRequest .getPreviousResourceTags () != null ?
115
- handlerRequest .getPreviousResourceTags () : Collections .emptyMap ();
136
+ handlerRequest .getPreviousResourceTags () : Collections .emptyMap ();
116
137
117
138
// TODO: get resource level tags from previous resource state based on your tag property name
118
139
// TODO: previousTags.putAll(handlerRequest.getPreviousResourceState().getTags());
@@ -125,10 +146,10 @@ public Map<String, String> getPreviouslyAttachedTags(final ResourceHandlerReques
125
146
* If stack tags and resource tags are not merged together in Configuration class,
126
147
* we will get new user defined tags from both resource model and previous stack tags.
127
148
*/
128
- public Map <String , String > getNewDesiredTags (final ResourceModel resourceModel , final ResourceHandlerRequest <ResourceModel > handlerRequest ) {
149
+ public static Map <String , String > getNewDesiredTags (final ResourceHandlerRequest <ResourceModel > handlerRequest ) {
129
150
// get new stack level tags from handlerRequest
130
151
final Map <String , String > desiredTags = handlerRequest .getDesiredResourceTags () != null ?
131
- handlerRequest .getDesiredResourceTags () : Collections .emptyMap ();
152
+ handlerRequest .getDesiredResourceTags () : Collections .emptyMap ();
132
153
133
154
// TODO: get resource level tags from resource model based on your tag property name
134
155
// TODO: desiredTags.putAll(convertToMap(resourceModel.getTags()));
@@ -140,25 +161,25 @@ public Map<String, String> getNewDesiredTags(final ResourceModel resourceModel,
140
161
*
141
162
* Determines the tags the customer desired to define or redefine.
142
163
*/
143
- public Map <String , String > generateTagsToAdd (final Map <String , String > previousTags , final Map <String , String > desiredTags ) {
164
+ public static Map <String , String > generateTagsToAdd (final Map <String , String > previousTags , final Map <String , String > desiredTags ) {
144
165
return desiredTags .entrySet ().stream ()
145
- .filter (e -> !previousTags .containsKey (e .getKey ()) || !Objects .equals (previousTags .get (e .getKey ()), e .getValue ()))
146
- .collect (Collectors .toMap (
147
- Map .Entry ::getKey ,
148
- Map .Entry ::getValue ));
166
+ .filter (e -> !previousTags .containsKey (e .getKey ()) || !Objects .equals (previousTags .get (e .getKey ()), e .getValue ()))
167
+ .collect (Collectors .toMap (
168
+ Map .Entry ::getKey ,
169
+ Map .Entry ::getValue ));
149
170
}
150
171
151
172
/**
152
173
* getTagsToRemove
153
174
*
154
175
* Determines the tags the customer desired to remove from the function.
155
176
*/
156
- public Set <String > generateTagsToRemove (final Map <String , String > previousTags , final Map <String , String > desiredTags ) {
177
+ public static Set <String > generateTagsToRemove (final Map <String , String > previousTags , final Map <String , String > desiredTags ) {
157
178
final Set <String > desiredTagNames = desiredTags .keySet ();
158
179
159
180
return previousTags .keySet ().stream ()
160
- .filter (tagName -> !desiredTagNames .contains (tagName ))
161
- .collect (Collectors .toSet ());
181
+ .filter (tagName -> !desiredTagNames .contains (tagName ))
182
+ .collect (Collectors .toSet ());
162
183
}
163
184
164
185
/**
@@ -178,54 +199,5 @@ public Set<Tag> generateTagsToAdd(final Set<Tag> previousTags, final Set<Tag> de
178
199
public Set <Tag > generateTagsToRemove (final Set <Tag > previousTags , final Set <Tag > desiredTags ) {
179
200
return Sets .difference (new HashSet <>(previousTags ), new HashSet <>(desiredTags ));
180
201
}
181
-
182
-
183
- /**
184
- * tagResource during update
185
- *
186
- * Calls the service:TagResource API.
187
- */
188
- private ProgressEvent <ResourceModel , CallbackContext >
189
- tagResource (final AmazonWebServicesClientProxy proxy , final ProxyClient <SdkClient > serviceClient , final ResourceModel resourceModel ,
190
- final ResourceHandlerRequest <ResourceModel > handlerRequest , final CallbackContext callbackContext , final Map <String , String > addedTags , final Logger logger ) {
191
- // TODO: add log for adding tags to resources during update
192
- // e.g. logger.log(String.format("[UPDATE][IN PROGRESS] Going to add tags for ... resource: %s with AccountId: %s",
193
- // resourceModel.getResourceName(), handlerRequest.getAwsAccountId()));
194
-
195
- // TODO: change untagResource in the method to your service API according to your SDK
196
- return proxy .initiate ("AWS-RedshiftServerless-Namespace::TagOps" , serviceClient , resourceModel , callbackContext )
197
- .translateToServiceRequest (model ->
198
- Translator .tagResourceRequest (model , addedTags ))
199
- .makeServiceCall ((request , client ) -> {
200
- return (AwsResponse ) null ;
201
- // TODO: replace the return null with your invoke log to call tagResource API to add tags
202
- // e.g. proxy.injectCredentialsAndInvokeV2(request, client.client()::tagResource))
203
- })
204
- .progress ();
205
- }
206
-
207
- /**
208
- * untagResource during update
209
- *
210
- * Calls the service:UntagResource API.
211
- */
212
- private ProgressEvent <ResourceModel , CallbackContext >
213
- untagResource (final AmazonWebServicesClientProxy proxy , final ProxyClient <SdkClient > serviceClient , final ResourceModel resourceModel ,
214
- final ResourceHandlerRequest <ResourceModel > handlerRequest , final CallbackContext callbackContext , final Set <String > removedTags , final Logger logger ) {
215
- // TODO: add log for removing tags from resources during update
216
- // e.g. logger.log(String.format("[UPDATE][IN PROGRESS] Going to remove tags for ... resource: %s with AccountId: %s",
217
- // resourceModel.getResourceName(), handlerRequest.getAwsAccountId()));
218
-
219
- // TODO: change untagResource in the method to your service API according to your SDK
220
- return proxy .initiate ("AWS-RedshiftServerless-Namespace::TagOps" , serviceClient , resourceModel , callbackContext )
221
- .translateToServiceRequest (model ->
222
- Translator .untagResourceRequest (model , removedTags ))
223
- .makeServiceCall ((request , client ) -> {
224
- return (AwsResponse ) null ;
225
- // TODO: replace the return null with your invoke log to call untag API to remove tags
226
- // e.g. proxy.injectCredentialsAndInvokeV2(request, client.client()::untagResource)
227
- })
228
- .progress ();
229
- }
230
-
231
202
}
203
+
0 commit comments