Skip to content

Commit

Permalink
BREAKING CHANGE: resources within a Template are now strongly typed, …
Browse files Browse the repository at this point in the history
…rather than map[string]interface{}

This change adds some nice benefits such as helping detect changes earlier in templates, as well as allowing clients to know the resource type returned from template.Resources.
  • Loading branch information
Graham Jenson authored and PaulMaddox committed Mar 10, 2019
1 parent 62f3961 commit 9ecf3fc
Show file tree
Hide file tree
Showing 383 changed files with 9,480 additions and 23,072 deletions.
61 changes: 0 additions & 61 deletions cloudformation/alexa-ask-skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloudformation
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -122,63 +121,3 @@ func (r *AlexaASKSkill) UnmarshalJSON(b []byte) error {

return nil
}

// GetAllAlexaASKSkillResources retrieves all AlexaASKSkill items from an AWS CloudFormation template
func (t *Template) GetAllAlexaASKSkillResources() map[string]*AlexaASKSkill {
results := map[string]*AlexaASKSkill{}
for name, untyped := range t.Resources {
switch resource := untyped.(type) {
case AlexaASKSkill:
results[name] = &resource
case *AlexaASKSkill:
// We found a strongly typed resource of the correct type; use it
results[name] = resource
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "Alexa::ASK::Skill" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AlexaASKSkill
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
results[name] = &result
}
}
}
}
}
}
return results
}

// GetAlexaASKSkillWithName retrieves all AlexaASKSkill items from an AWS CloudFormation template
// whose logical ID matches the provided name. Returns an error if not found.
func (t *Template) GetAlexaASKSkillWithName(name string) (*AlexaASKSkill, error) {
if untyped, ok := t.Resources[name]; ok {
switch resource := untyped.(type) {
case AlexaASKSkill:
return &resource, nil
case *AlexaASKSkill:
// We found a strongly typed resource of the correct type; use it
return resource, nil
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "Alexa::ASK::Skill" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AlexaASKSkill
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
return &result, nil
}
}
}
}
}
}
return nil, errors.New("resource not found")
}
9,576 changes: 9,206 additions & 370 deletions cloudformation/all.go

Large diffs are not rendered by default.

61 changes: 0 additions & 61 deletions cloudformation/aws-amazonmq-broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloudformation
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -177,63 +176,3 @@ func (r *AWSAmazonMQBroker) UnmarshalJSON(b []byte) error {

return nil
}

// GetAllAWSAmazonMQBrokerResources retrieves all AWSAmazonMQBroker items from an AWS CloudFormation template
func (t *Template) GetAllAWSAmazonMQBrokerResources() map[string]*AWSAmazonMQBroker {
results := map[string]*AWSAmazonMQBroker{}
for name, untyped := range t.Resources {
switch resource := untyped.(type) {
case AWSAmazonMQBroker:
results[name] = &resource
case *AWSAmazonMQBroker:
// We found a strongly typed resource of the correct type; use it
results[name] = resource
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::AmazonMQ::Broker" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSAmazonMQBroker
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
results[name] = &result
}
}
}
}
}
}
return results
}

// GetAWSAmazonMQBrokerWithName retrieves all AWSAmazonMQBroker items from an AWS CloudFormation template
// whose logical ID matches the provided name. Returns an error if not found.
func (t *Template) GetAWSAmazonMQBrokerWithName(name string) (*AWSAmazonMQBroker, error) {
if untyped, ok := t.Resources[name]; ok {
switch resource := untyped.(type) {
case AWSAmazonMQBroker:
return &resource, nil
case *AWSAmazonMQBroker:
// We found a strongly typed resource of the correct type; use it
return resource, nil
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::AmazonMQ::Broker" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSAmazonMQBroker
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
return &result, nil
}
}
}
}
}
}
return nil, errors.New("resource not found")
}
61 changes: 0 additions & 61 deletions cloudformation/aws-amazonmq-configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloudformation
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -137,63 +136,3 @@ func (r *AWSAmazonMQConfiguration) UnmarshalJSON(b []byte) error {

return nil
}

// GetAllAWSAmazonMQConfigurationResources retrieves all AWSAmazonMQConfiguration items from an AWS CloudFormation template
func (t *Template) GetAllAWSAmazonMQConfigurationResources() map[string]*AWSAmazonMQConfiguration {
results := map[string]*AWSAmazonMQConfiguration{}
for name, untyped := range t.Resources {
switch resource := untyped.(type) {
case AWSAmazonMQConfiguration:
results[name] = &resource
case *AWSAmazonMQConfiguration:
// We found a strongly typed resource of the correct type; use it
results[name] = resource
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::AmazonMQ::Configuration" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSAmazonMQConfiguration
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
results[name] = &result
}
}
}
}
}
}
return results
}

// GetAWSAmazonMQConfigurationWithName retrieves all AWSAmazonMQConfiguration items from an AWS CloudFormation template
// whose logical ID matches the provided name. Returns an error if not found.
func (t *Template) GetAWSAmazonMQConfigurationWithName(name string) (*AWSAmazonMQConfiguration, error) {
if untyped, ok := t.Resources[name]; ok {
switch resource := untyped.(type) {
case AWSAmazonMQConfiguration:
return &resource, nil
case *AWSAmazonMQConfiguration:
// We found a strongly typed resource of the correct type; use it
return resource, nil
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::AmazonMQ::Configuration" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSAmazonMQConfiguration
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
return &result, nil
}
}
}
}
}
}
return nil, errors.New("resource not found")
}
61 changes: 0 additions & 61 deletions cloudformation/aws-amazonmq-configurationassociation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloudformation
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -117,63 +116,3 @@ func (r *AWSAmazonMQConfigurationAssociation) UnmarshalJSON(b []byte) error {

return nil
}

// GetAllAWSAmazonMQConfigurationAssociationResources retrieves all AWSAmazonMQConfigurationAssociation items from an AWS CloudFormation template
func (t *Template) GetAllAWSAmazonMQConfigurationAssociationResources() map[string]*AWSAmazonMQConfigurationAssociation {
results := map[string]*AWSAmazonMQConfigurationAssociation{}
for name, untyped := range t.Resources {
switch resource := untyped.(type) {
case AWSAmazonMQConfigurationAssociation:
results[name] = &resource
case *AWSAmazonMQConfigurationAssociation:
// We found a strongly typed resource of the correct type; use it
results[name] = resource
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::AmazonMQ::ConfigurationAssociation" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSAmazonMQConfigurationAssociation
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
results[name] = &result
}
}
}
}
}
}
return results
}

// GetAWSAmazonMQConfigurationAssociationWithName retrieves all AWSAmazonMQConfigurationAssociation items from an AWS CloudFormation template
// whose logical ID matches the provided name. Returns an error if not found.
func (t *Template) GetAWSAmazonMQConfigurationAssociationWithName(name string) (*AWSAmazonMQConfigurationAssociation, error) {
if untyped, ok := t.Resources[name]; ok {
switch resource := untyped.(type) {
case AWSAmazonMQConfigurationAssociation:
return &resource, nil
case *AWSAmazonMQConfigurationAssociation:
// We found a strongly typed resource of the correct type; use it
return resource, nil
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::AmazonMQ::ConfigurationAssociation" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSAmazonMQConfigurationAssociation
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
return &result, nil
}
}
}
}
}
}
return nil, errors.New("resource not found")
}
61 changes: 0 additions & 61 deletions cloudformation/aws-apigateway-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloudformation
import (
"bytes"
"encoding/json"
"errors"
"fmt"
)

Expand Down Expand Up @@ -112,63 +111,3 @@ func (r *AWSApiGatewayAccount) UnmarshalJSON(b []byte) error {

return nil
}

// GetAllAWSApiGatewayAccountResources retrieves all AWSApiGatewayAccount items from an AWS CloudFormation template
func (t *Template) GetAllAWSApiGatewayAccountResources() map[string]*AWSApiGatewayAccount {
results := map[string]*AWSApiGatewayAccount{}
for name, untyped := range t.Resources {
switch resource := untyped.(type) {
case AWSApiGatewayAccount:
results[name] = &resource
case *AWSApiGatewayAccount:
// We found a strongly typed resource of the correct type; use it
results[name] = resource
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::ApiGateway::Account" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSApiGatewayAccount
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
results[name] = &result
}
}
}
}
}
}
return results
}

// GetAWSApiGatewayAccountWithName retrieves all AWSApiGatewayAccount items from an AWS CloudFormation template
// whose logical ID matches the provided name. Returns an error if not found.
func (t *Template) GetAWSApiGatewayAccountWithName(name string) (*AWSApiGatewayAccount, error) {
if untyped, ok := t.Resources[name]; ok {
switch resource := untyped.(type) {
case AWSApiGatewayAccount:
return &resource, nil
case *AWSApiGatewayAccount:
// We found a strongly typed resource of the correct type; use it
return resource, nil
case map[string]interface{}:
// We found an untyped resource (likely from JSON) which *might* be
// the correct type, but we need to check it's 'Type' field
if resType, ok := resource["Type"]; ok {
if resType == "AWS::ApiGateway::Account" {
// The resource is correct, unmarshal it into the results
if b, err := json.Marshal(resource); err == nil {
var result AWSApiGatewayAccount
if err := json.Unmarshal(b, &result); err == nil {
t.Resources[name] = &result
return &result, nil
}
}
}
}
}
}
return nil, errors.New("resource not found")
}
Loading

0 comments on commit 9ecf3fc

Please sign in to comment.