Skip to content

fix(ir): implement support for ResourceValue::Objects containing a nested TypeReference::Unions in deserialized templates #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ir/constructor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{parser::parameters::Parameter, Hasher};
use indexmap::IndexMap;
use voca_rs::case::camel_case;

#[derive(Debug)]
pub struct Constructor {
pub inputs: Vec<ConstructorParameter>,
}
Expand Down Expand Up @@ -42,6 +43,7 @@ impl Constructor {
}
}

#[derive(Debug)]
pub struct ConstructorParameter {
pub name: String,
pub description: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions src/ir/mappings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::ir::mappings::OutputType::{Complex, Consistent};
use crate::parser::lookup_table::{MappingInnerValue, MappingTable};
use crate::Hasher;

#[derive(Debug)]
pub struct MappingInstruction {
pub name: String,
pub map: IndexMap<String, IndexMap<String, MappingInnerValue, Hasher>, Hasher>,
Expand Down
1 change: 1 addition & 0 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod reference;
pub mod resources;
pub mod sub;

#[derive(Debug)]
pub struct CloudformationProgramIr {
pub description: Option<String>,
pub transforms: Vec<String>,
Expand Down
14 changes: 14 additions & 0 deletions src/ir/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ impl<'a, 'b> ResourceTranslator<'a, 'b> {
Some(TypeReference::Primitive(Primitive::Json)) => {
Box::new(MapOf(&TypeReference::Primitive(Primitive::Json)))
}
Some(TypeReference::Union(type_union)) => {
Box::new(UnionOf(type_union))
}
other => unimplemented!("{other:?}"),
};

Expand Down Expand Up @@ -359,6 +362,17 @@ impl PropertyBag for MapOf<'_> {
}
}

struct UnionOf<'a>(&'a TypeUnion);
impl PropertyBag for UnionOf<'_> {
fn property(&self, key: &str) -> Option<Property> {
Some(Property {
name: voca_rs::case::camel_case(key).into(),
required: false,
value_type: TypeReference::Union(self.0.clone()),
})
}
}

// ResourceInstruction is all the information needed to output a resource assignment.
#[derive(Clone, Debug, PartialEq)]
pub struct ResourceInstruction {
Expand Down
1 change: 1 addition & 0 deletions tests/end-to-end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ test_case!(batch, "BatchStack", &["golang", "java"]); //java fails cdk synth bc
test_case!(cloudwatch, "CloudwatchStack", &["golang"]);
test_case!(ecs, "EcsStack", &["java", "golang"]);
test_case!(ec2, "Ec2Stack", &["java", "golang"]);
test_case!(sam_serverless, "SamServerlessStack", &["java", "csharp", "golang"]);

// Add new test cases here

Expand Down
57 changes: 57 additions & 0 deletions tests/end-to-end/sam_serverless/python/Stack.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/./tests/end-to-end/sam_serverless/template.json b/tests/end-to-end/sam_serverless-python-working-dir/cdk.out/Stack.template.json
index ef393b7..612089e 100644
--- a/./tests/end-to-end/sam_serverless/template.json
+++ b/tests/end-to-end/sam_serverless-python-working-dir/cdk.out/Stack.template.json
@@ -1,28 +1,38 @@
{
- "AWSTemplateFormatVersion": "2010-09-09",
- "Transform": "AWS::Serverless-2016-10-31",
+ "Transform": [
+ "AWS::Serverless-2016-10-31"
+ ],
"Resources": {
"ClientFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
+ "CodeUri": "./pricing-client/",
+ "Events": {
+ "ApiEvent": {
+ "Properties": {
+ "Method": "get",
+ "Path": "/path"
+ },
+ "Type": "Api"
+ }
+ },
"FunctionName": "pricing-client",
"Handler": "client_lambda.lambda_handler",
"MemorySize": 512,
- "Timeout": 300,
- "CodeUri": "./pricing-client/",
"PermissionsBoundary": {
- "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:policy/GithubActionsIamResourcePermissionsBoundary"
+ "Fn::Join": [
+ "",
+ [
+ "arn:aws:iam::",
+ {
+ "Ref": "AWS::AccountId"
+ },
+ ":policy/GithubActionsIamResourcePermissionsBoundary"
+ ]
+ ]
},
"Runtime": "python3.11",
- "Events": {
- "ApiEvent": {
- "Type": "Api",
- "Properties": {
- "Path": "/path",
- "Method": "get"
- }
- }
- }
+ "Timeout": 300
}
}
}
39 changes: 39 additions & 0 deletions tests/end-to-end/sam_serverless/python/Stack.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"Transform": [
"AWS::Serverless-2016-10-31"
],
"Resources": {
"ClientFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"CodeUri": "./pricing-client/",
"Events": {
"ApiEvent": {
"Properties": {
"Method": "get",
"Path": "/path"
},
"Type": "Api"
}
},
"FunctionName": "pricing-client",
"Handler": "client_lambda.lambda_handler",
"MemorySize": 512,
"PermissionsBoundary": {
"Fn::Join": [
"",
[
"arn:aws:iam::",
{
"Ref": "AWS::AccountId"
},
":policy/GithubActionsIamResourcePermissionsBoundary"
]
]
},
"Runtime": "python3.11",
"Timeout": 300
}
}
}
}
11 changes: 11 additions & 0 deletions tests/end-to-end/sam_serverless/python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# autogenerated
import aws_cdk as cdk
from stack import SamServerlessStack
app = cdk.App(
default_stack_synthesizer=cdk.DefaultStackSynthesizer(
generate_bootstrap_version_rule=False
)
)

SamServerlessStack(app, 'Stack')
app.synth()
33 changes: 33 additions & 0 deletions tests/end-to-end/sam_serverless/python/stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from aws_cdk import Stack
import aws_cdk as cdk
import aws_cdk.aws_sam as sam
from constructs import Construct

class SamServerlessStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

# Transforms
Stack.add_transform(self, 'AWS::Serverless-2016-10-31')

# Resources
clientFunction = sam.CfnFunction(self, 'ClientFunction',
function_name = 'pricing-client',
handler = 'client_lambda.lambda_handler',
memory_size = 512,
timeout = 300,
code_uri = './pricing-client/',
permissions_boundary = f"""arn:aws:iam::{self.account}:policy/GithubActionsIamResourcePermissionsBoundary""",
runtime = 'python3.11',
events = {
'ApiEvent': {
'type': 'Api',
'properties': {
'path': '/path',
'method': 'get',
},
},
},
)


29 changes: 29 additions & 0 deletions tests/end-to-end/sam_serverless/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Transform": "AWS::Serverless-2016-10-31",
"Resources": {
"ClientFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"FunctionName": "pricing-client",
"Handler": "client_lambda.lambda_handler",
"MemorySize": 512,
"Timeout": 300,
"CodeUri": "./pricing-client/",
"PermissionsBoundary": {
"Fn::Sub": "arn:aws:iam::${AWS::AccountId}:policy/GithubActionsIamResourcePermissionsBoundary"
},
"Runtime": "python3.11",
"Events": {
"ApiEvent": {
"Type": "Api",
"Properties": {
"Path": "/path",
"Method": "get"
}
}
}
}
}
}
}
57 changes: 57 additions & 0 deletions tests/end-to-end/sam_serverless/typescript/Stack.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/./tests/end-to-end/sam_serverless/template.json b/tests/end-to-end/sam_serverless-typescript-working-dir/cdk.out/Stack.template.json
index ef393b7..612089e 100644
--- a/./tests/end-to-end/sam_serverless/template.json
+++ b/tests/end-to-end/sam_serverless-typescript-working-dir/cdk.out/Stack.template.json
@@ -1,28 +1,38 @@
{
- "AWSTemplateFormatVersion": "2010-09-09",
- "Transform": "AWS::Serverless-2016-10-31",
+ "Transform": [
+ "AWS::Serverless-2016-10-31"
+ ],
"Resources": {
"ClientFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
+ "CodeUri": "./pricing-client/",
+ "Events": {
+ "ApiEvent": {
+ "Properties": {
+ "Method": "get",
+ "Path": "/path"
+ },
+ "Type": "Api"
+ }
+ },
"FunctionName": "pricing-client",
"Handler": "client_lambda.lambda_handler",
"MemorySize": 512,
- "Timeout": 300,
- "CodeUri": "./pricing-client/",
"PermissionsBoundary": {
- "Fn::Sub": "arn:aws:iam::${AWS::AccountId}:policy/GithubActionsIamResourcePermissionsBoundary"
+ "Fn::Join": [
+ "",
+ [
+ "arn:aws:iam::",
+ {
+ "Ref": "AWS::AccountId"
+ },
+ ":policy/GithubActionsIamResourcePermissionsBoundary"
+ ]
+ ]
},
"Runtime": "python3.11",
- "Events": {
- "ApiEvent": {
- "Type": "Api",
- "Properties": {
- "Path": "/path",
- "Method": "get"
- }
- }
- }
+ "Timeout": 300
}
}
}
39 changes: 39 additions & 0 deletions tests/end-to-end/sam_serverless/typescript/Stack.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"Transform": [
"AWS::Serverless-2016-10-31"
],
"Resources": {
"ClientFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"CodeUri": "./pricing-client/",
"Events": {
"ApiEvent": {
"Properties": {
"Method": "get",
"Path": "/path"
},
"Type": "Api"
}
},
"FunctionName": "pricing-client",
"Handler": "client_lambda.lambda_handler",
"MemorySize": 512,
"PermissionsBoundary": {
"Fn::Join": [
"",
[
"arn:aws:iam::",
{
"Ref": "AWS::AccountId"
},
":policy/GithubActionsIamResourcePermissionsBoundary"
]
]
},
"Runtime": "python3.11",
"Timeout": 300
}
}
}
}
10 changes: 10 additions & 0 deletions tests/end-to-end/sam_serverless/typescript/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// auto-generated! a human should update this!
import * as cdk from "aws-cdk-lib";
import { SamServerlessStack } from "./stack";
const app = new cdk.App({
defaultStackSynthesizer: new cdk.DefaultStackSynthesizer({
generateBootstrapVersionRule: false,
}),
});
new SamServerlessStack(app, "Stack");
app.synth();
34 changes: 34 additions & 0 deletions tests/end-to-end/sam_serverless/typescript/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as cdk from 'aws-cdk-lib';
import * as sam from 'aws-cdk-lib/aws-sam';

export interface SamServerlessStackProps extends cdk.StackProps {
}

export class SamServerlessStack extends cdk.Stack {
public constructor(scope: cdk.App, id: string, props: SamServerlessStackProps = {}) {
super(scope, id, props);

// Transforms
this.addTransform('AWS::Serverless-2016-10-31');

// Resources
const clientFunction = new sam.CfnFunction(this, 'ClientFunction', {
functionName: 'pricing-client',
handler: 'client_lambda.lambda_handler',
memorySize: 512,
timeout: 300,
codeUri: './pricing-client/',
permissionsBoundary: `arn:aws:iam::${this.account}:policy/GithubActionsIamResourcePermissionsBoundary`,
runtime: 'python3.11',
events: {
ApiEvent: {
type: 'Api',
properties: {
path: '/path',
method: 'get',
},
},
},
});
}
}