Skip to content

Commit 682214d

Browse files
authored
added some pre-actionable checks for update and read handler to align… (#294)
1 parent a4bf268 commit 682214d

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
5656
// STEP 1.3 [TODO: handle exception]
5757
.handleError((awsRequest, exception, client, model, context) -> {
5858
// TODO: uncomment when ready to implement
59-
// if (exception instanceof CfnNotFoundException)
59+
// if (exception instanceof ResourceNotFoundException)
6060
// return ProgressEvent.success(model, context);
6161
// throw exception;
6262
return ProgressEvent.progress(model, context);
@@ -105,6 +105,10 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
105105
logger.log(String.format("%s [%s] deletion has stabilized: %s", ResourceModel.TYPE_NAME, model.getPrimaryIdentifier(), stabilized));
106106
return stabilized;
107107
})
108-
.success());
108+
.progress()
109+
)
110+
111+
// STEP 3 [TODO: return the successful progress event without resource model]
112+
.then(progress -> ProgressEvent.defaultSuccessHandler(null));
109113
}
110114
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ public void handleRequest_SimpleSuccess() {
6060
assertThat(response).isNotNull();
6161
assertThat(response.getStatus()).isEqualTo(OperationStatus.SUCCESS);
6262
assertThat(response.getCallbackDelaySeconds()).isEqualTo(0);
63+
{% if operation == "Delete" %}
64+
assertThat(response.getResourceModel()).isNull();
65+
{% else %}
6366
assertThat(response.getResourceModel()).isEqualTo(request.getDesiredResourceState());
67+
{% endif %}
6468
assertThat(response.getResourceModels()).isNull();
6569
assertThat(response.getMessage()).isNull();
6670
assertThat(response.getErrorCode()).isNull();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
4242
try {
4343

4444
// TODO: add custom read resource logic
45+
// If describe request does not return ResourceNotFoundException, you must throw ResourceNotFoundException based on
46+
// awsResponse values
4547

4648
} catch (final AwsServiceException e) { // ResourceNotFoundException
4749
/*

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,45 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
2929
// https://github.com/aws-cloudformation/cloudformation-cli-java-plugin/blob/master/src/main/java/software/amazon/cloudformation/proxy/CallChain.java
3030

3131
return ProgressEvent.progress(request.getDesiredResourceState(), callbackContext)
32-
// STEP 1 [first update/stabilize progress chain - required for resource update]
32+
33+
// STEP 1 [check if resource already exists]
34+
// for more information -> https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/resource-type-test-contract.html
35+
// if target API does not support 'ResourceNotFoundException' then following check is required
3336
.then(progress ->
3437
// STEP 1.0 [initialize a proxy context]
38+
// If your service API does not return ResourceNotFoundException on update requests against some identifier (e.g; resource Name)
39+
// and instead returns a 200 even though a resource does not exist, you must first check if the resource exists here
40+
// NOTE: If your service API throws 'ResourceNotFoundException' for update requests this method is not necessary
41+
proxy.initiate("{{ call_graph }}::{{ operation }}::PreUpdateCheck", proxyClient, progress.getResourceModel(), progress.getCallbackContext())
42+
43+
// STEP 1.1 [initialize a proxy context]
44+
.translateToServiceRequest(Translator::translateToReadRequest)
45+
46+
// STEP 1.2 [TODO: make an api call]
47+
.makeServiceCall((awsRequest, client) -> {
48+
AwsResponse awsResponse = null;
49+
50+
// TODO: add custom read resource logic
51+
// If describe request does not return ResourceNotFoundException, you must throw ResourceNotFoundException based on
52+
// awsResponse values
53+
54+
logger.log(String.format("%s has successfully been read.", ResourceModel.TYPE_NAME));
55+
return awsResponse;
56+
})
57+
.progress()
58+
)
59+
60+
// STEP 2 [first update/stabilize progress chain - required for resource update]
61+
.then(progress ->
62+
// STEP 2.0 [initialize a proxy context]
3563
// Implement client invocation of the update request through the proxyClient, which is already initialised with
3664
// caller credentials, correct region and retry settings
3765
proxy.initiate("{{ call_graph }}::{{ operation }}::first", proxyClient, progress.getResourceModel(), progress.getCallbackContext())
3866

39-
// STEP 1.1 [TODO: construct a body of a request]
67+
// STEP 2.1 [TODO: construct a body of a request]
4068
.translateToServiceRequest(Translator::translateToFirstUpdateRequest)
4169

42-
// STEP 1.2 [TODO: make an api call]
70+
// STEP 2.2 [TODO: make an api call]
4371
.makeServiceCall((awsRequest, client) -> {
4472
AwsResponse awsResponse = null;
4573
try {
@@ -60,7 +88,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
6088
return awsResponse;
6189
})
6290

63-
// STEP 1.3 [TODO: stabilize step is not necessarily required but typically involves describing the resource until it is in a certain status, though it can take many forms]
91+
// STEP 2.3 [TODO: stabilize step is not necessarily required but typically involves describing the resource until it is in a certain status, though it can take many forms]
6492
// stabilization step may or may not be needed after each API call
6593
// for more information -> https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/resource-type-test-contract.html
6694
.stabilize((awsRequest, awsResponse, client, model, context) -> {
@@ -74,17 +102,17 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
74102
.progress())
75103

76104
// If your resource is provisioned through multiple API calls, then the following pattern is required (and might take as many postUpdate callbacks as necessary)
77-
// STEP 2 [second update/stabilize progress chain]
105+
// STEP 3 [second update/stabilize progress chain]
78106
.then(progress ->
79-
// STEP 2.0 [initialize a proxy context]
107+
// STEP 3.0 [initialize a proxy context]
80108
// If your resource is provisioned through multiple API calls, you will need to apply each subsequent update
81109
// step in a discrete call/stabilize chain to ensure the entire resource is provisioned as intended.
82110
proxy.initiate("{{ call_graph }}::{{ operation }}::second", proxyClient, progress.getResourceModel(), progress.getCallbackContext())
83111

84-
// STEP 2.1 [TODO: construct a body of a request]
112+
// STEP 3.1 [TODO: construct a body of a request]
85113
.translateToServiceRequest(Translator::translateToSecondUpdateRequest)
86114

87-
// STEP 2.2 [TODO: make an api call]
115+
// STEP 3.2 [TODO: make an api call]
88116
.makeServiceCall((awsRequest, client) -> {
89117
AwsResponse awsResponse = null;
90118
try {
@@ -106,7 +134,7 @@ protected ProgressEvent<ResourceModel, CallbackContext> handleRequest(
106134
})
107135
.progress())
108136

109-
// STEP 3 [TODO: describe call/chain to return the resource model]
137+
// STEP 4 [TODO: describe call/chain to return the resource model]
110138
.then(progress -> new ReadHandler().handleRequest(proxy, request, callbackContext, proxyClient, logger));
111139
}
112140
}

0 commit comments

Comments
 (0)