32
32
*/
33
33
public class CloudFormationResponse {
34
34
35
- /**
36
- * Indicates if the function invoking send had successfully completed.
37
- */
38
- public enum ResponseStatus {
39
- SUCCESS , FAILED
40
- }
41
-
42
35
/**
43
36
* Internal representation of the payload to be sent to the event target URL. Retains all properties of the payload
44
37
* except for "Data". This is done so that the serialization of the non-"Data" properties and the serialization of
@@ -61,14 +54,14 @@ static class ResponseBody {
61
54
62
55
ResponseBody (CloudFormationCustomResourceEvent event ,
63
56
Context context ,
64
- ResponseStatus responseStatus ,
57
+ Response . Status responseStatus ,
65
58
String physicalResourceId ,
66
59
boolean noEcho ) {
67
60
Objects .requireNonNull (event , "CloudFormationCustomResourceEvent cannot be null" );
68
61
Objects .requireNonNull (context , "Context cannot be null" );
69
62
this .physicalResourceId = physicalResourceId != null ? physicalResourceId : context .getLogStreamName ();
70
63
this .reason = "See the details in CloudWatch Log Stream: " + context .getLogStreamName ();
71
- this .status = responseStatus == null ? ResponseStatus .SUCCESS .name () : responseStatus .name ();
64
+ this .status = responseStatus == null ? Response . Status .SUCCESS .name () : responseStatus .name ();
72
65
this .stackId = event .getStackId ();
73
66
this .requestId = event .getRequestId ();
74
67
this .logicalResourceId = event .getLogicalResourceId ();
@@ -132,95 +125,38 @@ public CloudFormationResponse(SdkHttpClient client) {
132
125
}
133
126
134
127
/**
135
- * Forwards a request containing a custom payload to the target resource specified by the event. The payload is
136
- * formed from the event and context data.
128
+ * Forwards a response containing a custom payload to the target resource specified by the event. The payload is
129
+ * formed from the event and context data. Status is assumed to be SUCCESS.
137
130
*
138
- * @param event custom CF resource event
131
+ * @param event custom CF resource event. Cannot be null.
139
132
* @param context used to specify when the function and any callbacks have completed execution, or to
140
- * access information from within the Lambda execution environment.
141
- * @param status whether the function successfully completed
133
+ * access information from within the Lambda execution environment. Cannot be null.
142
134
* @return the response object
143
- * @throws IOException when unable to generate or send the request
144
- * @throws ResponseException when unable to serialize the response payload
135
+ * @throws IOException when unable to send the request
136
+ * @throws CustomResourceResponseException when unable to synthesize or serialize the response payload
145
137
*/
146
138
public HttpExecuteResponse send (CloudFormationCustomResourceEvent event ,
147
- Context context ,
148
- ResponseStatus status ) throws IOException , ResponseException {
149
- return send (event , context , status , null );
139
+ Context context ) throws IOException , CustomResourceResponseException {
140
+ return send (event , context , null );
150
141
}
151
142
152
143
/**
153
- * Forwards a request containing a custom payload to the target resource specified by the event. The payload is
154
- * formed from the event, status , and context data.
144
+ * Forwards a response containing a custom payload to the target resource specified by the event. The payload is
145
+ * formed from the event, context , and response data.
155
146
*
156
- * @param event custom CF resource event
147
+ * @param event custom CF resource event. Cannot be null.
157
148
* @param context used to specify when the function and any callbacks have completed execution, or to
158
- * access information from within the Lambda execution environment.
159
- * @param status whether the function successfully completed
160
- * @param responseData response to send, e.g. a list of name-value pairs. May be null.
149
+ * access information from within the Lambda execution environment. Cannot be null.
150
+ * @param responseData response to send, e.g. a list of name-value pairs. If null, an empty success is assumed.
161
151
* @return the response object
162
- * @throws IOException when unable to generate or send the request
163
- * @throws ResponseException when unable to serialize the response payload
152
+ * @throws IOException when unable to generate or send the request
153
+ * @throws CustomResourceResponseException when unable to serialize the response payload
164
154
*/
165
155
public HttpExecuteResponse send (CloudFormationCustomResourceEvent event ,
166
156
Context context ,
167
- ResponseStatus status ,
168
- Response responseData ) throws IOException , ResponseException {
169
- return send (event , context , status , responseData , null );
170
- }
171
-
172
- /**
173
- * Forwards a request containing a custom payload to the target resource specified by the event. The payload is
174
- * formed from the event, status, response, and context data.
175
- *
176
- * @param event custom CF resource event
177
- * @param context used to specify when the function and any callbacks have completed execution, or to
178
- * access information from within the Lambda execution environment.
179
- * @param status whether the function successfully completed
180
- * @param responseData response to send, e.g. a list of name-value pairs. May be null.
181
- * @param physicalResourceId Optional. The unique identifier of the custom resource that invoked the function. By
182
- * default, the module uses the name of the Amazon CloudWatch Logs log stream that's
183
- * associated with the Lambda function.
184
- * @return the response object
185
- * @throws IOException when unable to send the request
186
- * @throws ResponseException when unable to serialize the response payload
187
- */
188
- public HttpExecuteResponse send (CloudFormationCustomResourceEvent event ,
189
- Context context ,
190
- ResponseStatus status ,
191
- Response responseData ,
192
- String physicalResourceId ) throws IOException , ResponseException {
193
- return send (event , context , status , responseData , physicalResourceId , false );
194
- }
195
-
196
- /**
197
- * Forwards a request containing a custom payload to the target resource specified by the event. The payload is
198
- * formed from the event, status, response, and context data.
199
- *
200
- * @param event custom CF resource event
201
- * @param context used to specify when the function and any callbacks have completed execution, or to
202
- * access information from within the Lambda execution environment.
203
- * @param status whether the function successfully completed
204
- * @param responseData response to send, e.g. a list of name-value pairs. May be null.
205
- * @param physicalResourceId Optional. The unique identifier of the custom resource that invoked the function. By
206
- * default, the module uses the name of the Amazon CloudWatch Logs log stream that's
207
- * associated with the Lambda function.
208
- * @param noEcho Optional. Indicates whether to mask the output of the custom resource when it's
209
- * retrieved by using the Fn::GetAtt function. If set to true, all returned values are
210
- * masked with asterisks (*****), except for information stored in the locations specified
211
- * below. By default, this value is false.
212
- * @return the response object
213
- * @throws IOException when unable to send the request
214
- * @throws ResponseException when unable to serialize the response payload
215
- */
216
- public HttpExecuteResponse send (CloudFormationCustomResourceEvent event ,
217
- Context context ,
218
- ResponseStatus status ,
219
- Response responseData ,
220
- String physicalResourceId ,
221
- boolean noEcho ) throws IOException , ResponseException {
157
+ Response responseData ) throws IOException , CustomResourceResponseException {
222
158
// no need to explicitly close in-memory stream
223
- StringInputStream stream = responseBodyStream (event , context , status , responseData , physicalResourceId , noEcho );
159
+ StringInputStream stream = responseBodyStream (event , context , responseData );
224
160
URI uri = URI .create (event .getResponseUrl ());
225
161
SdkHttpRequest request = SdkHttpRequest .builder ()
226
162
.uri (uri )
@@ -250,20 +186,24 @@ protected Map<String, List<String>> headers(int contentLength) {
250
186
/**
251
187
* Returns the response body as an input stream, for supplying with the HTTP request to the custom resource.
252
188
*
253
- * @throws ResponseException if unable to generate the response stream
189
+ * @throws CustomResourceResponseException if unable to generate the response stream
254
190
*/
255
- private StringInputStream responseBodyStream (CloudFormationCustomResourceEvent event ,
256
- Context context ,
257
- ResponseStatus status ,
258
- Response responseData ,
259
- String physicalResourceId ,
260
- boolean noEcho ) throws ResponseException {
191
+ StringInputStream responseBodyStream (CloudFormationCustomResourceEvent event ,
192
+ Context context ,
193
+ Response resp ) throws CustomResourceResponseException {
261
194
try {
262
- ResponseBody body = new ResponseBody (event , context , status , physicalResourceId , noEcho );
263
- ObjectNode node = body .toObjectNode (responseData == null ? null : responseData .getJsonNode ());
264
- return new StringInputStream (node .toString ());
195
+ if (resp == null ) {
196
+ ResponseBody body = new ResponseBody (event , context , Response .Status .SUCCESS , null , false );
197
+ ObjectNode node = body .toObjectNode (null );
198
+ return new StringInputStream (node .toString ());
199
+ } else {
200
+ ResponseBody body = new ResponseBody (
201
+ event , context , resp .getStatus (), resp .getPhysicalResourceId (), resp .isNoEcho ());
202
+ ObjectNode node = body .toObjectNode (resp .getJsonNode ());
203
+ return new StringInputStream (node .toString ());
204
+ }
265
205
} catch (RuntimeException e ) {
266
- throw new ResponseException ("Unable to generate response body." , e );
206
+ throw new CustomResourceResponseException ("Unable to generate response body." , e );
267
207
}
268
208
}
269
209
}
0 commit comments