Skip to content

Commit

Permalink
Check for properties with no type in CFN spec (#301)
Browse files Browse the repository at this point in the history
* Check for properties with no type in CFN spec

If the CloudFormation Resource Specification has a resource property
that does not include any type information then this should be a
breaking error and generation should fail.

See #300
  • Loading branch information
PaulMaddox authored Aug 25, 2020
1 parent 2dbbc97 commit 828d966
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ func (rg *ResourceGenerator) processSpec(specname string, data []byte) (*CloudFo
return nil, err
}

// Check that all of resource properties have a valid type
// see: https://github.com/awslabs/goformation/issues/300
invalid := []string{}
for rname, resource := range spec.Resources {
for pname, property := range resource.Properties {
if !property.HasValidType() {
invalid = append(invalid, fmt.Sprintf("\t%s.%s", rname, pname))
}
}
}
if len(invalid) > 0 {
return nil, fmt.Errorf("the following resource properties have no type information in the CloudFormation Resource Specification:\n%s\n", strings.Join(invalid, "\n"))
}

// Add the resources processed to the ResourceGenerator output
for name := range spec.Resources {

Expand Down
16 changes: 16 additions & 0 deletions generate/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ func (p Property) Schema(name, parent string) string {

}

// HasValidType checks whether a property has a valid type defined
// It is possible that an invalid CloudFormation Resource Specification is published
// that does not have any type information for a property. If this happens, then
// generation should fail with an error message.
func (p Property) HasValidType() bool {
invalid := p.ItemType == "" &&
p.PrimitiveType == "" &&
p.PrimitiveItemType == "" &&
p.Type == "" &&
len(p.ItemTypes) == 0 &&
len(p.PrimitiveTypes) == 0 &&
len(p.PrimitiveItemTypes) == 0 &&
len(p.Types) == 0
return !invalid
}

// IsPolymorphic checks whether a property can be multiple different types
func (p Property) IsPolymorphic() bool {
return len(p.PrimitiveTypes) > 0 || len(p.PrimitiveItemTypes) > 0 || len(p.PrimitiveItemTypes) > 0 || len(p.ItemTypes) > 0 || len(p.Types) > 0
Expand Down

0 comments on commit 828d966

Please sign in to comment.