Skip to content

Commit

Permalink
Add events for CDK custom resource provider framework. (#846)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Wallace <[email protected]>
  • Loading branch information
mawallace and Michael Wallace authored Mar 26, 2024
1 parent 731d201 commit ddd22a3
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lambda-events/src/event/cloudformation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

pub mod provider;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "RequestType")]
pub enum CloudFormationCustomResourceRequest<P1 = Value, P2 = Value>
Expand Down
159 changes: 159 additions & 0 deletions lambda-events/src/event/cloudformation/provider.rs
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);
}
}
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" }
}
}
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" }
}
}
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"
}
}
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" }
}
}

0 comments on commit ddd22a3

Please sign in to comment.