From 9d3038aacf00a1ca56c9d712c64ce964b3bcf91f Mon Sep 17 00:00:00 2001 From: graham jenson Date: Fri, 25 Jan 2019 11:00:04 +0000 Subject: [PATCH] [Refactor] moving generated resources and policies into their own packages --- README.md | 27 ++++++----- cloudformation/{ => policies}/policies.go | 2 +- cloudformation/policies_test.go | 36 +++++++------- example/go-to-yaml/main.go | 5 +- generate/generate.go | 4 +- generate/property_test.go | 24 +++++----- generate/resource_test.go | 33 +++++++------ generate/templates/all.template | 13 ++--- .../templates/polymorphic-property.template | 2 +- generate/templates/resource.template | 23 +++++---- goformation_test.go | 48 ++++++++++--------- 11 files changed, 113 insertions(+), 104 deletions(-) rename cloudformation/{ => policies}/policies.go (99%) diff --git a/README.md b/README.md index 0c89fa99bd..7a7dfca9ad 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # AWS GoFormation -[![Build Status](https://travis-ci.org/awslabs/goformation.svg?branch=0.1.0)](https://travis-ci.org/awslabs/goformation) [![GoDoc Reference](https://godoc.org/gopkg.in/awslabs/goformation.v1?status.svg)](http://godoc.org/github.com/awslabs/goformation) ![Apache-2.0](https://img.shields.io/badge/Licence-Apache%202.0-blue.svg) +[![Build Status](https://travis-ci.org/awslabs/goformation.svg?branch=0.1.0)](https://travis-ci.org/awslabs/goformation) [![GoDoc Reference](https://godoc.org/gopkg.in/awslabs/goformation.v1?status.svg)](http://godoc.org/github.com/awslabs/goformation) ![Apache-2.0](https://img.shields.io/badge/Licence-Apache%202.0-blue.svg) -`GoFormation` is a Go library for working with AWS CloudFormation / AWS Serverless Application Model (SAM) templates. +`GoFormation` is a Go library for working with AWS CloudFormation / AWS Serverless Application Model (SAM) templates. - [Main features](#main-features) - [Installation](#installation) - [Usage](#usage) @@ -45,6 +45,7 @@ import ( "time" "github.com/awslabs/goformation/cloudformation" + "github.com/awslabs/goformation/cloudformation/resources" ) func main() { @@ -53,12 +54,12 @@ func main() { template := cloudformation.NewTemplate() // Create an Amazon SNS topic, with a unique name based off the current timestamp - template.Resources["MyTopic"] = &cloudformation.AWSSNSTopic{ + template.Resources["MyTopic"] = &resources.AWSSNSTopic{ TopicName: "my-topic-" + strconv.FormatInt(time.Now().Unix(), 10), } // Create a subscription, connected to our topic, that forwards notifications to an email address - template.Resources["MyTopicSubscription"] = &cloudformation.AWSSNSSubscription{ + template.Resources["MyTopicSubscription"] = &resources.AWSSNSSubscription{ TopicArn: cloudformation.Ref("MyTopic"), Protocol: "email", Endpoint: "some.email@example.com", @@ -146,7 +147,7 @@ When creating templates, you can use the following convenience functions to use - `Not(conditions []string)` - `Or(conditions []string)` -### Unmarshalling CloudFormation YAML/JSON into Go structs +### Unmarshalling CloudFormation YAML/JSON into Go structs GoFormation also works the other way - parsing JSON/YAML CloudFormation/SAM templates into Go structs. @@ -191,7 +192,7 @@ func main() { ``` ## Updating CloudFormation / SAM Resources in GoFormation - + AWS GoFormation contains automatically generated Go structs for every CloudFormation/SAM resource, located in the [cloudformation/](cloudformation/) directory. These can be generated, from the latest [AWS CloudFormation Resource Specification](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-resource-specification.html) published for `us-east-1` by just running `go generate`: ``` @@ -216,13 +217,13 @@ The following [AWS CloudFormation Intrinsic Functions](http://docs.aws.amazon.co - [x] [Fn::Select](intrinsics/fnselect.go) - [x] [Fn::Split](intrinsics/fnsplit.go) - [x] [Fn::Sub](intrinsics/fnsub.go) -- [x] [Ref](intrinsics/ref.go) -- [x] [Fn::And](intrinsics/fnand.go) -- [x] [Fn::Equals](intrinsics/fnequals.go) -- [x] [Fn::If](intrinsics/fnif.go) -- [x] [Fn::Not](intrinsics/fnnot.go) -- [x] [Fn::Or](intrinsics/fnor.go) -- [ ] Fn::GetAtt +- [x] [Ref](intrinsics/ref.go) +- [x] [Fn::And](intrinsics/fnand.go) +- [x] [Fn::Equals](intrinsics/fnequals.go) +- [x] [Fn::If](intrinsics/fnif.go) +- [x] [Fn::Not](intrinsics/fnnot.go) +- [x] [Fn::Or](intrinsics/fnor.go) +- [ ] Fn::GetAtt - [x] [Fn::GetAZs](intrinsics/fngetazs.go) - [ ] Fn::ImportValue diff --git a/cloudformation/policies.go b/cloudformation/policies/policies.go similarity index 99% rename from cloudformation/policies.go rename to cloudformation/policies/policies.go index d1c4d60d55..d70ed0a08d 100644 --- a/cloudformation/policies.go +++ b/cloudformation/policies/policies.go @@ -1,4 +1,4 @@ -package cloudformation +package policies // CreationPolicy prevents a resource status from reaching create complete until AWS CloudFormation receives a specified number of success signals or the timeout period is exceeded. To signal a resource, you can use the cfn-signal helper script or SignalResource API. AWS CloudFormation publishes valid signals to the stack events so that you track the number of signals sent. type CreationPolicy struct { diff --git a/cloudformation/policies_test.go b/cloudformation/policies_test.go index f3619b662f..2b20daa746 100644 --- a/cloudformation/policies_test.go +++ b/cloudformation/policies_test.go @@ -4,6 +4,8 @@ import ( "github.com/sanathkr/yaml" "github.com/awslabs/goformation/cloudformation" + "github.com/awslabs/goformation/cloudformation/policies" + "github.com/awslabs/goformation/cloudformation/resources" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -15,13 +17,13 @@ var _ = Describe("Goformation", func() { tests := []struct { Name string - Input *cloudformation.UpdatePolicy + Input *policies.UpdatePolicy Expected interface{} }{ { Name: "AutoScalingReplacingUpdate", - Input: &cloudformation.UpdatePolicy{ - AutoScalingReplacingUpdate: &cloudformation.AutoScalingReplacingUpdate{ + Input: &policies.UpdatePolicy{ + AutoScalingReplacingUpdate: &policies.AutoScalingReplacingUpdate{ WillReplace: true, }, }, @@ -33,8 +35,8 @@ var _ = Describe("Goformation", func() { }, { Name: "AutoScalingReplacingUpdate", - Input: &cloudformation.UpdatePolicy{ - AutoScalingRollingUpdate: &cloudformation.AutoScalingRollingUpdate{ + Input: &policies.UpdatePolicy{ + AutoScalingRollingUpdate: &policies.AutoScalingRollingUpdate{ MaxBatchSize: 10, MinInstancesInService: 11, MinSuccessfulInstancesPercent: 12, @@ -56,8 +58,8 @@ var _ = Describe("Goformation", func() { }, { Name: "AutoScalingScheduledAction", - Input: &cloudformation.UpdatePolicy{ - AutoScalingScheduledAction: &cloudformation.AutoScalingScheduledAction{ + Input: &policies.UpdatePolicy{ + AutoScalingScheduledAction: &policies.AutoScalingScheduledAction{ IgnoreUnmodifiedGroupSizeProperties: true, }, }, @@ -69,8 +71,8 @@ var _ = Describe("Goformation", func() { }, { Name: "CodeDeployLambdaAliasUpdate", - Input: &cloudformation.UpdatePolicy{ - CodeDeployLambdaAliasUpdate: &cloudformation.CodeDeployLambdaAliasUpdate{ + Input: &policies.UpdatePolicy{ + CodeDeployLambdaAliasUpdate: &policies.CodeDeployLambdaAliasUpdate{ ApplicationName: "test-application-name", DeploymentGroupName: "test-deployment-group-name", AfterAllowTrafficHook: "test-after-allow-traffic-hook", @@ -93,7 +95,7 @@ var _ = Describe("Goformation", func() { It("should have the correct values for "+test.Name, func() { - asg := cloudformation.AWSAutoScalingAutoScalingGroup{} + asg := resources.AWSAutoScalingAutoScalingGroup{} asg.SetUpdatePolicy(test.Input) template := &cloudformation.Template{ @@ -128,16 +130,16 @@ var _ = Describe("Goformation", func() { tests := []struct { Name string - Input *cloudformation.CreationPolicy + Input *policies.CreationPolicy Expected interface{} }{ { Name: "CreationPolicy", - Input: &cloudformation.CreationPolicy{ - AutoScalingCreationPolicy: &cloudformation.AutoScalingCreationPolicy{ + Input: &policies.CreationPolicy{ + AutoScalingCreationPolicy: &policies.AutoScalingCreationPolicy{ MinSuccessfulInstancesPercent: 10, }, - ResourceSignal: &cloudformation.ResourceSignal{ + ResourceSignal: &policies.ResourceSignal{ Count: 11, Timeout: "test-timeout", }, @@ -159,7 +161,7 @@ var _ = Describe("Goformation", func() { It("should have the correct values for "+test.Name, func() { - asg := cloudformation.AWSAutoScalingAutoScalingGroup{} + asg := resources.AWSAutoScalingAutoScalingGroup{} asg.SetCreationPolicy(test.Input) template := &cloudformation.Template{ @@ -194,7 +196,7 @@ var _ = Describe("Goformation", func() { tests := []struct { Name string - Input cloudformation.DeletionPolicy + Input policies.DeletionPolicy Expected interface{} }{ { @@ -219,7 +221,7 @@ var _ = Describe("Goformation", func() { It("should have the correct values for "+test.Name, func() { - asg := cloudformation.AWSAutoScalingAutoScalingGroup{} + asg := resources.AWSAutoScalingAutoScalingGroup{} asg.SetDeletionPolicy(test.Input) template := &cloudformation.Template{ diff --git a/example/go-to-yaml/main.go b/example/go-to-yaml/main.go index f058eeb6aa..3da9659874 100644 --- a/example/go-to-yaml/main.go +++ b/example/go-to-yaml/main.go @@ -6,6 +6,7 @@ import ( "time" "github.com/awslabs/goformation/cloudformation" + "github.com/awslabs/goformation/cloudformation/resources" ) func main() { @@ -14,12 +15,12 @@ func main() { template := cloudformation.NewTemplate() // Create an Amazon SNS topic, with a unique name based off the current timestamp - template.Resources["MyTopic"] = &cloudformation.AWSSNSTopic{ + template.Resources["MyTopic"] = &resources.AWSSNSTopic{ TopicName: "my-topic-" + strconv.FormatInt(time.Now().Unix(), 10), } // Create a subscription, connected to our topic, that forwards notifications to an email address - template.Resources["MyTopicSubscription"] = &cloudformation.AWSSNSSubscription{ + template.Resources["MyTopicSubscription"] = &resources.AWSSNSSubscription{ TopicArn: cloudformation.Ref("MyTopic"), Protocol: "email", Endpoint: "some.email@example.com", diff --git a/generate/generate.go b/generate/generate.go index 36a09362ab..4d74cfc156 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -278,7 +278,7 @@ func (rg *ResourceGenerator) generateResources(name string, resource Resource, i } // Check if the file has changed since the last time generate ran - fn := "cloudformation/" + filename(name) + fn := "cloudformation/resources/" + filename(name) current, err := ioutil.ReadFile(fn) if err != nil || bytes.Compare(formatted, current) != 0 { @@ -396,7 +396,7 @@ func generatePolymorphicProperty(name string, property Property) { } // Write the file out - if err := ioutil.WriteFile("cloudformation/"+filename(name), formatted, 0644); err != nil { + if err := ioutil.WriteFile("cloudformation/resources/"+filename(name), formatted, 0644); err != nil { fmt.Printf("Error: Failed to write JSON Schema\n%s\n", err) os.Exit(1) } diff --git a/generate/property_test.go b/generate/property_test.go index 2b055a51f2..677fe94330 100644 --- a/generate/property_test.go +++ b/generate/property_test.go @@ -3,7 +3,7 @@ package main_test import ( "encoding/json" - "github.com/awslabs/goformation/cloudformation" + "github.com/awslabs/goformation/cloudformation/resources" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -14,7 +14,7 @@ var _ = Describe("Goformation Code Generator", func() { Context("specified as a Go struct", func() { - property := &cloudformation.AWSServerlessFunction_S3Location{ + property := &resources.AWSServerlessFunction_S3Location{ Bucket: "test-bucket", Key: "test-key", Version: 123, @@ -31,13 +31,13 @@ var _ = Describe("Goformation Code Generator", func() { Context("specified as JSON", func() { property := []byte(`{"Bucket":"test-bucket","Key":"test-key","Version":123}`) - expected := &cloudformation.AWSServerlessFunction_S3Location{ + expected := &resources.AWSServerlessFunction_S3Location{ Bucket: "test-bucket", Key: "test-key", Version: 123, } - result := &cloudformation.AWSServerlessFunction_S3Location{} + result := &resources.AWSServerlessFunction_S3Location{} err := json.Unmarshal(property, result) It("should unmarshal to a Go struct successfully", func() { Expect(result).To(Equal(expected)) @@ -55,7 +55,7 @@ var _ = Describe("Goformation Code Generator", func() { Context("specified as a Go struct", func() { value := "test-primitive-value" - property := &cloudformation.AWSServerlessFunction_CodeUri{ + property := &resources.AWSServerlessFunction_CodeUri{ String: &value, } @@ -72,11 +72,11 @@ var _ = Describe("Goformation Code Generator", func() { property := []byte(`"test-primitive-value"`) value := "test-primitive-value" - expected := &cloudformation.AWSServerlessFunction_CodeUri{ + expected := &resources.AWSServerlessFunction_CodeUri{ String: &value, } - result := &cloudformation.AWSServerlessFunction_CodeUri{} + result := &resources.AWSServerlessFunction_CodeUri{} err := json.Unmarshal(property, result) It("should unmarshal to a Go struct successfully", func() { Expect(result).To(Equal(expected)) @@ -91,8 +91,8 @@ var _ = Describe("Goformation Code Generator", func() { Context("specified as a Go struct", func() { - property := &cloudformation.AWSServerlessFunction_CodeUri{ - S3Location: &cloudformation.AWSServerlessFunction_S3Location{ + property := &resources.AWSServerlessFunction_CodeUri{ + S3Location: &resources.AWSServerlessFunction_S3Location{ Bucket: "test-bucket", Key: "test-key", Version: 123, @@ -113,15 +113,15 @@ var _ = Describe("Goformation Code Generator", func() { property := []byte(`{"Bucket":"test-bucket","Key":"test-key","Version":123}`) - expected := &cloudformation.AWSServerlessFunction_CodeUri{ - S3Location: &cloudformation.AWSServerlessFunction_S3Location{ + expected := &resources.AWSServerlessFunction_CodeUri{ + S3Location: &resources.AWSServerlessFunction_S3Location{ Bucket: "test-bucket", Key: "test-key", Version: 123, }, } - result := &cloudformation.AWSServerlessFunction_CodeUri{} + result := &resources.AWSServerlessFunction_CodeUri{} err := json.Unmarshal(property, result) It("should unmarshal to a Go struct successfully", func() { Expect(result).To(Equal(expected)) diff --git a/generate/resource_test.go b/generate/resource_test.go index bf0846d5ea..f10a06be2a 100644 --- a/generate/resource_test.go +++ b/generate/resource_test.go @@ -3,8 +3,7 @@ package main_test import ( "encoding/json" - "github.com/awslabs/goformation/cloudformation" - + "github.com/awslabs/goformation/cloudformation/resources" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -18,9 +17,9 @@ var _ = Describe("Resource", func() { Context("with a simple primitive used for a polymorphic property", func() { codeuri := "s3://bucket/key" - resource := &cloudformation.AWSServerlessFunction{ + resource := &resources.AWSServerlessFunction{ Runtime: "nodejs6.10", - CodeUri: &cloudformation.AWSServerlessFunction_CodeUri{ + CodeUri: &resources.AWSServerlessFunction_CodeUri{ String: &codeuri, }, } @@ -37,10 +36,10 @@ var _ = Describe("Resource", func() { Context("with a custom type used for a polymorphic property", func() { - resource := &cloudformation.AWSServerlessFunction{ + resource := &resources.AWSServerlessFunction{ Runtime: "nodejs6.10", - CodeUri: &cloudformation.AWSServerlessFunction_CodeUri{ - S3Location: &cloudformation.AWSServerlessFunction_S3Location{ + CodeUri: &resources.AWSServerlessFunction_CodeUri{ + S3Location: &resources.AWSServerlessFunction_S3Location{ Bucket: "test-bucket", Key: "test-key", Version: 123, @@ -68,7 +67,7 @@ var _ = Describe("Resource", func() { Context("with a dependency on another resource", func() { - resource := &cloudformation.AWSEC2Instance{ + resource := &resources.AWSEC2Instance{ ImageId: "ami-0123456789", } resource.SetDependsOn([]string{"MyDependency"}) @@ -85,7 +84,7 @@ var _ = Describe("Resource", func() { Context("with a metadata attribute", func() { - resource := &cloudformation.AWSS3Bucket{ + resource := &resources.AWSS3Bucket{ BucketName: "MyBucket", } resource.SetMetadata(map[string]interface{}{"Object1": "Location1", "Object2": "Location2"}) @@ -107,12 +106,12 @@ var _ = Describe("Resource", func() { Context("with a dependency on another resource", func() { property := []byte(`{"Type":"AWS::EC2::Instance","Properties":{"ImageId":"ami-0123456789"},"DependsOn":["MyDependency"]}`) - expected := &cloudformation.AWSEC2Instance{ + expected := &resources.AWSEC2Instance{ ImageId: "ami-0123456789", } expected.SetDependsOn([]string{"MyDependency"}) - result := &cloudformation.AWSEC2Instance{} + result := &resources.AWSEC2Instance{} err := json.Unmarshal(property, result) It("should unmarshal to a Go struct successfully", func() { Expect(result).To(Equal(expected)) @@ -124,12 +123,12 @@ var _ = Describe("Resource", func() { Context("with a metadata attribute", func() { property := []byte(`{"Type":"AWS::S3::Bucket","Properties":{"BucketName":"MyBucket"},"Metadata":{"Object1":"Location1","Object2":"Location2"}}`) - expected := &cloudformation.AWSS3Bucket{ + expected := &resources.AWSS3Bucket{ BucketName: "MyBucket", } expected.SetMetadata(map[string]interface{}{"Object1": "Location1", "Object2": "Location2"}) - result := &cloudformation.AWSS3Bucket{} + result := &resources.AWSS3Bucket{} err := json.Unmarshal(property, result) It("should unmarshal to a Go struct successfully", func() { Expect(result).To(Equal(expected)) @@ -148,9 +147,9 @@ var _ = Describe("Resource", func() { Context("with a list type", func() { - subproperty := &cloudformation.AWSServerlessFunction_S3Event{ + subproperty := &resources.AWSServerlessFunction_S3Event{ Bucket: "my-bucket", - Events: &cloudformation.AWSServerlessFunction_Events{ + Events: &resources.AWSServerlessFunction_Events{ StringArray: &[]string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"}, }, } @@ -168,9 +167,9 @@ var _ = Describe("Resource", func() { Context("with a primitive type", func() { event := "s3:ObjectCreated:*" - subproperty := &cloudformation.AWSServerlessFunction_S3Event{ + subproperty := &resources.AWSServerlessFunction_S3Event{ Bucket: "my-bucket", - Events: &cloudformation.AWSServerlessFunction_Events{ + Events: &resources.AWSServerlessFunction_Events{ String: &event, }, } diff --git a/generate/templates/all.template b/generate/templates/all.template index 53a34725c5..ab189b6a97 100644 --- a/generate/templates/all.template +++ b/generate/templates/all.template @@ -2,23 +2,24 @@ package cloudformation import ( "fmt" + "github.com/awslabs/goformation/cloudformation/resources" ) // AllResources fetches an iterable map all CloudFormation and SAM resources func AllResources() map[string]Resource { return map[string]Resource{ {{range $name, $resource := .Resources}} - "{{$name}}": &{{$resource}}{},{{end}} + "{{$name}}": &resources.{{$resource}}{},{{end}} } } {{range $name, $resource := .Resources}} // GetAll{{$resource}}Resources retrieves all {{$resource}} items from an AWS CloudFormation template -func (t *Template) GetAll{{$resource}}Resources () map[string]*{{$resource}} { - results := map[string]*{{$resource}}{} +func (t *Template) GetAll{{$resource}}Resources () map[string]*resources.{{$resource}} { + results := map[string]*resources.{{$resource}}{} for name, untyped := range t.Resources { switch resource := untyped.(type) { - case *{{$resource}}: + case *resources.{{$resource}}: results[name] = resource } } @@ -27,10 +28,10 @@ func (t *Template) GetAll{{$resource}}Resources () map[string]*{{$resource}} { // Get{{$resource}}WithName retrieves all {{$resource}} items from an AWS CloudFormation template // whose logical ID matches the provided name. Returns an error if not found. -func (t *Template) Get{{$resource}}WithName (name string) (*{{$resource}}, error) { +func (t *Template) Get{{$resource}}WithName (name string) (*resources.{{$resource}}, error) { if untyped, ok := t.Resources[name]; ok { switch resource := untyped.(type) { - case *{{$resource}}: + case *resources.{{$resource}}: return resource, nil } } diff --git a/generate/templates/polymorphic-property.template b/generate/templates/polymorphic-property.template index a362d202b8..a4dafb367f 100644 --- a/generate/templates/polymorphic-property.template +++ b/generate/templates/polymorphic-property.template @@ -1,4 +1,4 @@ -package cloudformation +package resources import ( "encoding/json" diff --git a/generate/templates/resource.template b/generate/templates/resource.template index fe683178eb..3d9a31aa7b 100644 --- a/generate/templates/resource.template +++ b/generate/templates/resource.template @@ -1,11 +1,14 @@ -package cloudformation +package resources {{if not .IsCustomProperty}} import ( "encoding/json" "fmt" "bytes" + "github.com/awslabs/goformation/cloudformation/policies" ) +{{else}} +import "github.com/awslabs/goformation/cloudformation/policies" {{end}} // {{.StructName}} AWS CloudFormation Resource ({{.Name}}) @@ -18,13 +21,13 @@ type {{.StructName}} struct { {{$name}} {{if (or ($property.IsPolymorphic) ($property.IsCustomType) )}}*{{end}}{{$property.GoType $.Basename $name}} `json:"{{$name}}{{if (not (and ($property.IsNumeric) ($property.Required)))}},omitempty{{end}}"` {{end}} {{if .HasUpdatePolicy }}// _updatePolicy represents a CloudFormation UpdatePolicy - _updatePolicy *UpdatePolicy{{ end }} + _updatePolicy *policies.UpdatePolicy{{ end }} {{if .HasCreationPolicy }}// _creationPolicy represents a CloudFormation CreationPolicy - _creationPolicy *CreationPolicy + _creationPolicy *policies.CreationPolicy {{ end }} // _deletionPolicy represents a CloudFormation DeletionPolicy - _deletionPolicy DeletionPolicy + _deletionPolicy policies.DeletionPolicy // _dependsOn stores the logical ID of the resources to be created before this resource _dependsOn []string @@ -61,20 +64,20 @@ func (r *{{.StructName}}) SetMetadata(metadata map[string]interface{}) { } // SetDeletionPolicy applies an AWS CloudFormation DeletionPolicy to this resource // see: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html -func (r *{{.StructName}}) SetDeletionPolicy(policy DeletionPolicy) { +func (r *{{.StructName}}) SetDeletionPolicy(policy policies.DeletionPolicy) { r._deletionPolicy = policy } {{if .HasUpdatePolicy}} // SetUpdatePolicy applies an AWS CloudFormation UpdatePolicy to this resource // see: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html -func (r *{{.StructName}}) SetUpdatePolicy(policy *UpdatePolicy) { +func (r *{{.StructName}}) SetUpdatePolicy(policy *policies.UpdatePolicy) { r._updatePolicy = policy } {{end}} {{if .HasCreationPolicy}} // SetCreationPolicy applies an AWS CloudFormation CreationPolicy to this resource // see: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-creationpolicy.html -func (r *{{.StructName}}) SetCreationPolicy(policy *CreationPolicy) { +func (r *{{.StructName}}) SetCreationPolicy(policy *policies.CreationPolicy) { r._creationPolicy = policy } {{end}} @@ -88,9 +91,9 @@ func (r {{.StructName}}) MarshalJSON() ([]byte, error) { {{if .IsCustomProperty}}Properties{{else}}Properties Properties{{end}} DependsOn []string `json:"DependsOn,omitempty"` Metadata map[string]interface{} `json:"Metadata,omitempty"` - DeletionPolicy DeletionPolicy `json:"DeletionPolicy,omitempty"` - {{if .HasUpdatePolicy}}UpdatePolicy *UpdatePolicy `json:"UpdatePolicy,omitempty"`{{end}} - {{if .HasCreationPolicy}}CreationPolicy *CreationPolicy `json:"CreationPolicy,omitempty"`{{end}} + DeletionPolicy policies.DeletionPolicy `json:"DeletionPolicy,omitempty"` + {{if .HasUpdatePolicy}}UpdatePolicy *policies.UpdatePolicy `json:"UpdatePolicy,omitempty"`{{end}} + {{if .HasCreationPolicy}}CreationPolicy *policies.CreationPolicy `json:"CreationPolicy,omitempty"`{{end}} }{ Type: r.AWSCloudFormationType(), Properties: (Properties)(r), diff --git a/goformation_test.go b/goformation_test.go index b3215390f3..beb6db89b5 100644 --- a/goformation_test.go +++ b/goformation_test.go @@ -3,6 +3,8 @@ package goformation_test import ( "encoding/json" + "github.com/awslabs/goformation/cloudformation/resources" + "github.com/sanathkr/yaml" "github.com/awslabs/goformation" @@ -69,18 +71,18 @@ var _ = Describe("Goformation", func() { template := cloudformation.NewTemplate() - template.Resources["MySNSTopic"] = &cloudformation.AWSSNSTopic{ + template.Resources["MySNSTopic"] = &resources.AWSSNSTopic{ DisplayName: "test-sns-topic-display-name", TopicName: "test-sns-topic-name", - Subscription: []cloudformation.AWSSNSTopic_Subscription{ - cloudformation.AWSSNSTopic_Subscription{ + Subscription: []resources.AWSSNSTopic_Subscription{ + resources.AWSSNSTopic_Subscription{ Endpoint: "test-sns-topic-subscription-endpoint", Protocol: "test-sns-topic-subscription-protocol", }, }, } - template.Resources["MyRoute53HostedZone"] = &cloudformation.AWSRoute53HostedZone{ + template.Resources["MyRoute53HostedZone"] = &resources.AWSRoute53HostedZone{ Name: "example.com", } @@ -128,18 +130,18 @@ var _ = Describe("Goformation", func() { expected := cloudformation.NewTemplate() - expected.Resources["MySNSTopic"] = &cloudformation.AWSSNSTopic{ + expected.Resources["MySNSTopic"] = &resources.AWSSNSTopic{ DisplayName: "test-sns-topic-display-name", TopicName: "test-sns-topic-name", - Subscription: []cloudformation.AWSSNSTopic_Subscription{ - cloudformation.AWSSNSTopic_Subscription{ + Subscription: []resources.AWSSNSTopic_Subscription{ + resources.AWSSNSTopic_Subscription{ Endpoint: "test-sns-topic-subscription-endpoint", Protocol: "test-sns-topic-subscription-protocol", }, }, } - expected.Resources["MyRoute53HostedZone"] = &cloudformation.AWSRoute53HostedZone{ + expected.Resources["MyRoute53HostedZone"] = &resources.AWSRoute53HostedZone{ Name: "example.com", } @@ -296,7 +298,7 @@ var _ = Describe("Goformation", func() { template := &cloudformation.Template{ Resources: cloudformation.Resources{ - "MyLambdaFunction": &cloudformation.AWSLambdaFunction{ + "MyLambdaFunction": &resources.AWSLambdaFunction{ Handler: "nodejs6.10", }, }, @@ -310,7 +312,7 @@ var _ = Describe("Goformation", func() { function, err := template.GetAWSLambdaFunctionWithName("MyLambdaFunction") It("should be able to retrieve a specific Lambda function with GetAWSLambdaFunctionWithName(template, name)", func() { Expect(err).To(BeNil()) - Expect(function).To(BeAssignableToTypeOf(&cloudformation.AWSLambdaFunction{})) + Expect(function).To(BeAssignableToTypeOf(&resources.AWSLambdaFunction{})) }) It("should have the correct Handler property", func() { @@ -325,10 +327,10 @@ var _ = Describe("Goformation", func() { template := &cloudformation.Template{ Resources: cloudformation.Resources{ - "MySAMFunction": &cloudformation.AWSServerlessFunction{ + "MySAMFunction": &resources.AWSServerlessFunction{ Handler: "nodejs6.10", - CodeUri: &cloudformation.AWSServerlessFunction_CodeUri{ - S3Location: &cloudformation.AWSServerlessFunction_S3Location{ + CodeUri: &resources.AWSServerlessFunction_CodeUri{ + S3Location: &resources.AWSServerlessFunction_S3Location{ Bucket: "test-bucket", Key: "test-key", Version: 100, @@ -357,9 +359,9 @@ var _ = Describe("Goformation", func() { codeuri := "./some-folder" template := &cloudformation.Template{ Resources: cloudformation.Resources{ - "MySAMFunction": &cloudformation.AWSServerlessFunction{ + "MySAMFunction": &resources.AWSServerlessFunction{ Handler: "nodejs6.10", - CodeUri: &cloudformation.AWSServerlessFunction_CodeUri{ + CodeUri: &resources.AWSServerlessFunction_CodeUri{ String: &codeuri, }, }, @@ -527,8 +529,8 @@ var _ = Describe("Goformation", func() { }) Context("with a SNS event source", func() { - event := cloudformation.AWSServerlessFunction_Properties{ - SNSEvent: &cloudformation.AWSServerlessFunction_SNSEvent{ + event := resources.AWSServerlessFunction_Properties{ + SNSEvent: &resources.AWSServerlessFunction_SNSEvent{ Topic: "MyTopic", }, } @@ -543,7 +545,7 @@ var _ = Describe("Goformation", func() { Context("with an SNS event source created from JSON", func() { eventString := `{"Topic":"MyTopic"}` eventJson := []byte(eventString) - event := cloudformation.AWSServerlessFunction_Properties{} + event := resources.AWSServerlessFunction_Properties{} event.UnmarshalJSON(eventJson) It("should marshal properties correctly", func() { @@ -557,10 +559,10 @@ var _ = Describe("Goformation", func() { template := &cloudformation.Template{ Resources: cloudformation.Resources{ - "TestBucket": &cloudformation.AWSS3Bucket{ + "TestBucket": &resources.AWSS3Bucket{ BucketName: "test-bucket", }, - "TestBucketPolicy": &cloudformation.AWSS3BucketPolicy{ + "TestBucketPolicy": &resources.AWSS3BucketPolicy{ Bucket: cloudformation.Ref("TestBucket"), }, }, @@ -779,7 +781,7 @@ var _ = Describe("Goformation", func() { template := &cloudformation.Template{ Resources: cloudformation.Resources{ - "TestBucket": &cloudformation.AWSS3Bucket{ + "TestBucket": &resources.AWSS3Bucket{ BucketName: cloudformation.Join("/", []string{ cloudformation.Join("-", []string{"test", "bucket"}), }), @@ -840,10 +842,10 @@ var _ = Describe("Goformation", func() { template := &cloudformation.Template{ Resources: cloudformation.Resources{ - "TestBucket": &cloudformation.AWSS3Bucket{ + "TestBucket": &resources.AWSS3Bucket{ BucketName: "test-bucket", }, - "TestBucketPolicy": &cloudformation.AWSS3BucketPolicy{ + "TestBucketPolicy": &resources.AWSS3BucketPolicy{ Bucket: cloudformation.GetAtt("TestBucket", "WebsiteURL"), }, },