-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add events for CDK custom resource provider framework. (#846)
Co-authored-by: Michael Wallace <[email protected]>
- Loading branch information
Showing
6 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
//! These events are to be used with the CDK custom resource provider framework. | ||
//! | ||
//! Note that they are similar (but not the same) as the events in the `super` module. | ||
//! | ||
//! See https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.custom_resources-readme.html for details. | ||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
use serde_json::Value; | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
#[serde(tag = "RequestType")] | ||
pub enum CloudFormationCustomResourceRequest<P1 = Value, P2 = Value> | ||
where | ||
P1: DeserializeOwned + Serialize, | ||
P2: DeserializeOwned + Serialize, | ||
{ | ||
#[serde(bound = "")] | ||
Create(CreateRequest<P2>), | ||
#[serde(bound = "")] | ||
Update(UpdateRequest<P1, P2>), | ||
#[serde(bound = "")] | ||
Delete(DeleteRequest<P2>), | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct CreateRequest<P2 = Value> | ||
where | ||
P2: DeserializeOwned + Serialize, | ||
{ | ||
#[serde(flatten, bound = "")] | ||
pub common: CommonRequestParams<P2>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct UpdateRequest<P1 = Value, P2 = Value> | ||
where | ||
P1: DeserializeOwned + Serialize, | ||
P2: DeserializeOwned + Serialize, | ||
{ | ||
pub physical_resource_id: String, | ||
|
||
#[serde(bound = "")] | ||
pub old_resource_properties: P1, | ||
|
||
#[serde(flatten, bound = "")] | ||
pub common: CommonRequestParams<P2>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct DeleteRequest<P2 = Value> | ||
where | ||
P2: DeserializeOwned + Serialize, | ||
{ | ||
pub physical_resource_id: String, | ||
|
||
#[serde(flatten, bound = "")] | ||
pub common: CommonRequestParams<P2>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct CommonRequestParams<P2 = Value> | ||
where | ||
P2: DeserializeOwned + Serialize, | ||
{ | ||
pub logical_resource_id: String, | ||
#[serde(bound = "")] | ||
pub resource_properties: P2, | ||
pub resource_type: String, | ||
pub request_id: String, | ||
pub stack_id: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Default)] | ||
#[serde(rename_all = "PascalCase")] | ||
pub struct CloudFormationCustomResourceResponse<D = Value> | ||
where | ||
D: DeserializeOwned + Serialize, | ||
{ | ||
pub physical_resource_id: Option<String>, | ||
#[serde(bound = "")] | ||
pub data: D, | ||
pub no_echo: bool, | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::collections::HashMap; | ||
|
||
use super::CloudFormationCustomResourceRequest::*; | ||
use super::*; | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
#[serde(rename_all = "PascalCase")] | ||
struct TestProperties { | ||
key_1: String, | ||
key_2: Vec<String>, | ||
key_3: HashMap<String, String>, | ||
} | ||
|
||
type TestRequest = CloudFormationCustomResourceRequest<TestProperties, TestProperties>; | ||
|
||
#[test] | ||
fn example_create_request() { | ||
let data = include_bytes!("../../fixtures/example-cloudformation-custom-resource-provider-create-request.json"); | ||
let parsed: TestRequest = serde_json::from_slice(data).unwrap(); | ||
|
||
match parsed { | ||
Create(_) => (), | ||
_ => panic!("expected Create request"), | ||
} | ||
|
||
let output: String = serde_json::to_string(&parsed).unwrap(); | ||
let reparsed: TestRequest = serde_json::from_slice(output.as_bytes()).unwrap(); | ||
assert_eq!(parsed, reparsed); | ||
} | ||
|
||
#[test] | ||
fn example_update_request() { | ||
let data = include_bytes!("../../fixtures/example-cloudformation-custom-resource-provider-update-request.json"); | ||
let parsed: TestRequest = serde_json::from_slice(data).unwrap(); | ||
|
||
match parsed { | ||
Update(_) => (), | ||
_ => panic!("expected Update request"), | ||
} | ||
|
||
let output: String = serde_json::to_string(&parsed).unwrap(); | ||
let reparsed: TestRequest = serde_json::from_slice(output.as_bytes()).unwrap(); | ||
assert_eq!(parsed, reparsed); | ||
} | ||
|
||
#[test] | ||
fn example_delete_request() { | ||
let data = include_bytes!("../../fixtures/example-cloudformation-custom-resource-provider-delete-request.json"); | ||
let parsed: TestRequest = serde_json::from_slice(data).unwrap(); | ||
|
||
match parsed { | ||
Delete(_) => (), | ||
_ => panic!("expected Delete request"), | ||
} | ||
|
||
let output: String = serde_json::to_string(&parsed).unwrap(); | ||
let reparsed: TestRequest = serde_json::from_slice(output.as_bytes()).unwrap(); | ||
assert_eq!(parsed, reparsed); | ||
} | ||
|
||
#[test] | ||
fn example_response() { | ||
let data = include_bytes!("../../fixtures/example-cloudformation-custom-resource-provider-response.json"); | ||
let parsed: CloudFormationCustomResourceResponse = serde_json::from_slice(data).unwrap(); | ||
let output: String = serde_json::to_string(&parsed).unwrap(); | ||
let reparsed: CloudFormationCustomResourceResponse = serde_json::from_slice(output.as_bytes()).unwrap(); | ||
assert_eq!(parsed, reparsed); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...a-events/src/fixtures/example-cloudformation-custom-resource-provider-create-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"RequestType" : "Create", | ||
"RequestId" : "82304eb2-bdda-469f-a33b-a3f1406d0a52", | ||
"StackId" : "arn:aws:cloudformation:us-east-1:123456789012:stack/stack-name/16580499-7622-4a9c-b32f-4eba35da93da", | ||
"ResourceType" : "Custom::MyCustomResourceType", | ||
"LogicalResourceId" : "CustomResource", | ||
"ResourceProperties" : { | ||
"Key1" : "string", | ||
"Key2" : [ "list" ], | ||
"Key3" : { "Key4" : "map" } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...a-events/src/fixtures/example-cloudformation-custom-resource-provider-delete-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"RequestType" : "Delete", | ||
"RequestId" : "ef70561d-d4ba-42a4-801b-33ad88dafc37", | ||
"StackId" : "arn:aws:cloudformation:us-east-1:123456789012:stack/stack-name/16580499-7622-4a9c-b32f-4eba35da93da", | ||
"ResourceType" : "Custom::MyCustomResourceType", | ||
"LogicalResourceId" : "CustomResource", | ||
"PhysicalResourceId" : "custom-resource-f4bd5382-3de3-4caf-b7ad-1be06b899647", | ||
"ResourceProperties" : { | ||
"Key1" : "string", | ||
"Key2" : [ "list" ], | ||
"Key3" : { "Key4" : "map" } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
lambda-events/src/fixtures/example-cloudformation-custom-resource-provider-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"PhysicalResourceId": "custom-resource-f4bd5382-3de3-4caf-b7ad-1be06b899647", | ||
"NoEcho": false, | ||
"Data": { | ||
"Key1": "a", | ||
"Key2": "b", | ||
"Key3": "c" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...a-events/src/fixtures/example-cloudformation-custom-resource-provider-update-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"RequestType" : "Update", | ||
"RequestId" : "49347ca5-c603-44e5-a34b-10cf1854a887", | ||
"StackId" : "arn:aws:cloudformation:us-east-1:123456789012:stack/stack-name/16580499-7622-4a9c-b32f-4eba35da93da", | ||
"ResourceType" : "Custom::MyCustomResourceType", | ||
"LogicalResourceId" : "CustomResource", | ||
"PhysicalResourceId" : "custom-resource-f4bd5382-3de3-4caf-b7ad-1be06b899647", | ||
"ResourceProperties" : { | ||
"Key1" : "new-string", | ||
"Key2" : [ "new-list" ], | ||
"Key3" : { "Key4" : "new-map" } | ||
}, | ||
"OldResourceProperties" : { | ||
"Key1" : "string", | ||
"Key2" : [ "list" ], | ||
"Key3" : { "Key4" : "map" } | ||
} | ||
} |