From 7d5870eff12e2ca9c011fb41b6748d0c3ec5f1d9 Mon Sep 17 00:00:00 2001 From: "Paul B. Beskow" Date: Tue, 9 Feb 2021 08:11:49 +0100 Subject: [PATCH] fix(template): field Export on type Output should be pointer (#299) the field Export of th type Output should be a pointer, otherwise it would produce invalid output, by adding an empty struct to the export field: Outputs: VpcId: Export: {} This empty struct will cause a template validation error: "Template format error: Output VpcId is malformed. The Name field of every Export member is required." Closes #294 --- cloudformation/template.go | 2 +- goformation_test.go | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/cloudformation/template.go b/cloudformation/template.go index 03b91f06f6..32fc3aaba6 100644 --- a/cloudformation/template.go +++ b/cloudformation/template.go @@ -40,7 +40,7 @@ type Parameter struct { type Output struct { Value interface{} `json:"Value"` Description string `json:"Description,omitempty"` - Export Export `json:"Export,omitempty"` + Export *Export `json:"Export,omitempty"` } type Export struct { diff --git a/goformation_test.go b/goformation_test.go index d40e94919c..d8b1afa78c 100644 --- a/goformation_test.go +++ b/goformation_test.go @@ -9,6 +9,7 @@ import ( "github.com/awslabs/goformation/v4" "github.com/awslabs/goformation/v4/cloudformation" + "github.com/awslabs/goformation/v4/cloudformation/ec2" "github.com/awslabs/goformation/v4/cloudformation/lambda" "github.com/awslabs/goformation/v4/cloudformation/policies" "github.com/awslabs/goformation/v4/cloudformation/route53" @@ -1252,4 +1253,47 @@ var _ = Describe("Goformation", func() { }) + Context("with a template that contains outputs with no exports", func() { + + Context("described as Go structs", func() { + + template := cloudformation.NewTemplate() + + template.Resources["Vpc"] = &ec2.VPC{ + CidrBlock: "192.168.0.0/20", + } + + template.Outputs["VpcId"] = cloudformation.Output{ + Value: cloudformation.Ref("Vpc"), + } + + expected := `{ + "AWSTemplateFormatVersion": "2010-09-09", + "Outputs": { + "VpcId": { + "Value": { + "Ref": "Vpc" + } + } + }, + "Resources": { + "Vpc": { + "Properties": { + "CidrBlock": "192.168.0.0/20" + }, + "Type": "AWS::EC2::VPC" + } + } +}` + + got, err := template.JSON() + It("should marshal template successfully", func() { + Expect(err).To(BeNil()) + }) + + It("should be equal to expected output", func() { + Expect(string(got)).To(Equal(expected)) + }) + }) + }) })