19
19
import java .util .HashMap ;
20
20
import java .util .Map ;
21
21
import java .util .stream .Collectors ;
22
+ import software .amazon .awssdk .utils .StringUtils ;
22
23
23
24
/**
24
25
* Models the arbitrary data to be sent to the custom resource in response to a CloudFormation event. This object
@@ -30,12 +31,22 @@ public class Response {
30
31
private final Status status ;
31
32
private final String physicalResourceId ;
32
33
private final boolean noEcho ;
34
+ private final String reason ;
33
35
34
36
private Response (JsonNode jsonNode , Status status , String physicalResourceId , boolean noEcho ) {
35
37
this .jsonNode = jsonNode ;
36
38
this .status = status ;
37
39
this .physicalResourceId = physicalResourceId ;
38
40
this .noEcho = noEcho ;
41
+ this .reason = null ;
42
+ }
43
+
44
+ private Response (JsonNode jsonNode , Status status , String physicalResourceId , boolean noEcho , String reason ) {
45
+ this .jsonNode = jsonNode ;
46
+ this .status = status ;
47
+ this .physicalResourceId = physicalResourceId ;
48
+ this .noEcho = noEcho ;
49
+ this .reason = reason ;
39
50
}
40
51
41
52
/**
@@ -149,6 +160,15 @@ public boolean isNoEcho() {
149
160
return noEcho ;
150
161
}
151
162
163
+ /**
164
+ * The reason for the failure.
165
+ *
166
+ * @return a potentially null reason
167
+ */
168
+ public String getReason () {
169
+ return reason ;
170
+ }
171
+
152
172
/**
153
173
* Includes all Response attributes, including its value in JSON format
154
174
*
@@ -161,6 +181,7 @@ public String toString() {
161
181
attributes .put ("Status" , status );
162
182
attributes .put ("PhysicalResourceId" , physicalResourceId );
163
183
attributes .put ("NoEcho" , noEcho );
184
+ attributes .put ("Reason" , reason );
164
185
return attributes .entrySet ().stream ()
165
186
.map (entry -> entry .getKey () + " = " + entry .getValue ())
166
187
.collect (Collectors .joining ("," , "[" , "]" ));
@@ -182,6 +203,7 @@ public static class Builder {
182
203
private Status status ;
183
204
private String physicalResourceId ;
184
205
private boolean noEcho ;
206
+ private String reason ;
185
207
186
208
private Builder () {
187
209
}
@@ -263,6 +285,20 @@ public Builder noEcho(boolean noEcho) {
263
285
return this ;
264
286
}
265
287
288
+ /**
289
+ * Reason for the response.
290
+ * Reason is optional for Success responses, but required for Failed responses.
291
+ * If not provided it will be replaced with cloudwatch log stream name.
292
+ *
293
+ * @param reason if null, the default reason will be used
294
+ * @return a reference to this builder
295
+ */
296
+
297
+ public Builder reason (String reason ) {
298
+ this .reason = reason ;
299
+ return this ;
300
+ }
301
+
266
302
/**
267
303
* Builds a Response object for the value.
268
304
*
@@ -277,6 +313,9 @@ public Response build() {
277
313
node = mapper .valueToTree (value );
278
314
}
279
315
Status responseStatus = this .status != null ? this .status : Status .SUCCESS ;
316
+ if (StringUtils .isNotBlank (this .reason )) {
317
+ return new Response (node , responseStatus , physicalResourceId , noEcho , reason );
318
+ }
280
319
return new Response (node , responseStatus , physicalResourceId , noEcho );
281
320
}
282
321
}
0 commit comments