Skip to content

Commit

Permalink
fix(csharp): cdk stacks with nested ResourceIr objects fail to synt…
Browse files Browse the repository at this point in the history
…hesize due to implicit type conversion errors (#578)

* initial commit for batch template end-to-end testing

Signed-off-by: Francis <colifran@amazon.com>

* python typescript

Signed-off-by: Francis <colifran@amazon.com>

* golang and java

Signed-off-by: Francis <colifran@amazon.com>

* bug fix and snaps

Signed-off-by: Francis <colifran@amazon.com>

* re-ran with update snapshots set

Signed-off-by: Francis <colifran@amazon.com>

* bug fix

Signed-off-by: Francis <colifran@amazon.com>

---------

Signed-off-by: Francis <colifran@amazon.com>
  • Loading branch information
colifran authored Feb 26, 2024
1 parent e0167d6 commit 65fe906
Show file tree
Hide file tree
Showing 20 changed files with 3,126 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/synthesizer/csharp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,7 @@ impl ResourceIr {
TypeReference::Primitive(_) => format!("{{ \"{name}\", "),
other => unimplemented!("{other:?}"),
});
match val {
ResourceIr::Bool(_) | ResourceIr::Number(_) | ResourceIr::Double(_) => {
object_block.text("\"");
val.emit_csharp(&object_block, schema);
object_block.text("\"");
}
_ => val.emit_csharp(&object_block, schema),
}
val.emit_csharp(&object_block, schema);
object_block.text(match structure {
TypeReference::Named(_) => ",",
TypeReference::Primitive(_) => "},",
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 @@ -79,6 +79,7 @@ test_case!(vpc, "VpcStack");
test_case!(sam_nodejs_lambda, "SAMNodeJSLambda");
// These stack should be identical to the ones above
test_case!(sam_nodejs_lambda_arr_transform, "SAMNodeJSLambda", ALL);
test_case!(batch, "BatchStack", &["golang"]);

// Add new test cases here

Expand Down
17 changes: 17 additions & 0 deletions tests/end-to-end/batch/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//Auto-generated
using Amazon.CDK;
sealed class Program
{
public static void Main(string[] args)
{
var app = new App(new AppProps
{
DefaultStackSynthesizer = new DefaultStackSynthesizer(new DefaultStackSynthesizerProps
{
GenerateBootstrapVersionRule = false,
}),
});
new BatchStack.BatchStack(app, "Stack");
app.Synth();
}
}
189 changes: 189 additions & 0 deletions tests/end-to-end/batch/csharp/Stack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using Amazon.CDK;
using Amazon.CDK.AWS.Batch;
using Amazon.CDK.AWS.EC2;
using Amazon.CDK.AWS.IAM;
using Constructs;
using System.Collections.Generic;

namespace BatchStack
{
public class BatchStackProps : StackProps
{
}

/// <summary>
/// AWS CloudFormation Sample Template Managed Single Batch Job Queue: This template demonstrates the usage of simple Job Queue and EC2 style Compute Environment. **WARNING** You will be billed for the AWS resources used if you create a stack from this template.
/// </summary>
public class BatchStack : Stack
{
public object ComputeEnvironmentArn { get; }

public object JobQueueArn { get; }

public object JobDefinitionArn { get; }

public BatchStack(Construct scope, string id, BatchStackProps props = null) : base(scope, id, props)
{

// Resources
var batchServiceRole = new CfnRole(this, "BatchServiceRole", new CfnRoleProps
{
AssumeRolePolicyDocument = new Dictionary<string, object>
{
{ "Version", "2012-10-17"},
{ "Statement", new []
{
new Dictionary<string, object>
{
{ "Effect", "Allow"},
{ "Principal", new Dictionary<string, object>
{
{ "Service", "batch.amazonaws.com"},
}},
{ "Action", "sts:AssumeRole"},
},
}},
},
ManagedPolicyArns = new []
{
"arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole",
},
});
var ecsInstanceRole = new CfnRole(this, "EcsInstanceRole", new CfnRoleProps
{
AssumeRolePolicyDocument = new Dictionary<string, object>
{
{ "Version", "2008-10-17"},
{ "Statement", new []
{
new Dictionary<string, object>
{
{ "Sid", ""},
{ "Effect", "Allow"},
{ "Principal", new Dictionary<string, object>
{
{ "Service", "ec2.amazonaws.com"},
}},
{ "Action", "sts:AssumeRole"},
},
}},
},
ManagedPolicyArns = new []
{
"arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role",
},
});
var internetGateway = new CfnInternetGateway(this, "InternetGateway", new CfnInternetGatewayProps
{
});
var jobDefinition = new CfnJobDefinition(this, "JobDefinition", new CfnJobDefinitionProps
{
Type = "container",
ContainerProperties = new CfnJobDefinition.ContainerPropertiesProperty
{
Image = string.Join("", new []
{
"137112412989.dkr.ecr.",
Region,
".amazonaws.com/amazonlinux:latest",
}),
Vcpus = 2,
Memory = 2000,
Command = new []
{
"echo",
"Hello world",
},
},
RetryStrategy = new CfnJobDefinition.RetryStrategyProperty
{
Attempts = 1,
},
});
var vpc = new CfnVPC(this, "VPC", new CfnVPCProps
{
CidrBlock = "10.0.0.0/16",
});
var iamInstanceProfile = new CfnInstanceProfile(this, "IamInstanceProfile", new CfnInstanceProfileProps
{
Roles = new []
{
ecsInstanceRole.Ref,
},
});
var routeTable = new CfnRouteTable(this, "RouteTable", new CfnRouteTableProps
{
VpcId = vpc.Ref,
});
var securityGroup = new CfnSecurityGroup(this, "SecurityGroup", new CfnSecurityGroupProps
{
GroupDescription = "EC2 Security Group for instances launched in the VPC by Batch",
VpcId = vpc.Ref,
});
var subnet = new CfnSubnet(this, "Subnet", new CfnSubnetProps
{
CidrBlock = "10.0.0.0/24",
VpcId = vpc.Ref,
MapPublicIpOnLaunch = true,
});
var vpcGatewayAttachment = new CfnVPCGatewayAttachment(this, "VPCGatewayAttachment", new CfnVPCGatewayAttachmentProps
{
VpcId = vpc.Ref,
InternetGatewayId = internetGateway.Ref,
});
var computeEnvironment = new CfnComputeEnvironment(this, "ComputeEnvironment", new CfnComputeEnvironmentProps
{
Type = "MANAGED",
ComputeResources = new CfnComputeEnvironment.ComputeResourcesProperty
{
Type = "EC2",
MinvCpus = 0,
DesiredvCpus = 0,
MaxvCpus = 64,
InstanceTypes = new []
{
"optimal",
},
Subnets = new []
{
subnet.Ref,
},
SecurityGroupIds = new []
{
securityGroup.Ref,
},
InstanceRole = iamInstanceProfile.Ref,
},
ServiceRole = batchServiceRole.Ref,
});
var route = new CfnRoute(this, "Route", new CfnRouteProps
{
RouteTableId = routeTable.Ref,
DestinationCidrBlock = "0.0.0.0/0",
GatewayId = internetGateway.Ref,
});
var subnetRouteTableAssociation = new CfnSubnetRouteTableAssociation(this, "SubnetRouteTableAssociation", new CfnSubnetRouteTableAssociationProps
{
RouteTableId = routeTable.Ref,
SubnetId = subnet.Ref,
});
var jobQueue = new CfnJobQueue(this, "JobQueue", new CfnJobQueueProps
{
Priority = 1,
ComputeEnvironmentOrder = new []
{
new CfnJobQueue.ComputeEnvironmentOrderProperty
{
Order = 1,
ComputeEnvironment = computeEnvironment.Ref,
},
},
});

// Outputs
ComputeEnvironmentArn = computeEnvironment.Ref;
JobQueueArn = jobQueue.Ref;
JobDefinitionArn = jobDefinition.Ref;
}
}
}
Loading

0 comments on commit 65fe906

Please sign in to comment.