Skip to content

Commit

Permalink
[Refactor] moving generated resources and policies into their own pac…
Browse files Browse the repository at this point in the history
…kages
  • Loading branch information
graham jenson committed Mar 10, 2019
1 parent 651d072 commit 9d3038a
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 104 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -45,6 +45,7 @@ import (
"time"

"github.com/awslabs/goformation/cloudformation"
"github.com/awslabs/goformation/cloudformation/resources"
)

func main() {
Expand All @@ -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",
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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`:

```
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
36 changes: 19 additions & 17 deletions cloudformation/policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
},
},
Expand All @@ -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,
Expand All @@ -56,8 +58,8 @@ var _ = Describe("Goformation", func() {
},
{
Name: "AutoScalingScheduledAction",
Input: &cloudformation.UpdatePolicy{
AutoScalingScheduledAction: &cloudformation.AutoScalingScheduledAction{
Input: &policies.UpdatePolicy{
AutoScalingScheduledAction: &policies.AutoScalingScheduledAction{
IgnoreUnmodifiedGroupSizeProperties: true,
},
},
Expand All @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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",
},
Expand All @@ -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{
Expand Down Expand Up @@ -194,7 +196,7 @@ var _ = Describe("Goformation", func() {

tests := []struct {
Name string
Input cloudformation.DeletionPolicy
Input policies.DeletionPolicy
Expected interface{}
}{
{
Expand All @@ -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{
Expand Down
5 changes: 3 additions & 2 deletions example/go-to-yaml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/awslabs/goformation/cloudformation"
"github.com/awslabs/goformation/cloudformation/resources"
)

func main() {
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 12 additions & 12 deletions generate/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand All @@ -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))
Expand All @@ -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,
}

Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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))
Expand Down
Loading

0 comments on commit 9d3038a

Please sign in to comment.