Skip to content

Commit

Permalink
fix(schema): Add cdkmetada resource (awslabs#418)
Browse files Browse the repository at this point in the history
* testing

* adding cdkmetada as resource

* cleaning

* cleaning unnecessary prints
  • Loading branch information
ismferd authored Dec 30, 2021
1 parent 8f23eb5 commit 3d1b1f9
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cloudformation/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudformation

import (
"fmt"

"github.com/awslabs/goformation/v5/cloudformation/accessanalyzer"
"github.com/awslabs/goformation/v5/cloudformation/acmpca"
"github.com/awslabs/goformation/v5/cloudformation/amazonmq"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/awslabs/goformation/v5/cloudformation/batch"
"github.com/awslabs/goformation/v5/cloudformation/budgets"
"github.com/awslabs/goformation/v5/cloudformation/cassandra"
"github.com/awslabs/goformation/v5/cloudformation/cdkmetadata"
"github.com/awslabs/goformation/v5/cloudformation/ce"
"github.com/awslabs/goformation/v5/cloudformation/certificatemanager"
"github.com/awslabs/goformation/v5/cloudformation/chatbot"
Expand Down Expand Up @@ -83,6 +85,7 @@ import (
"github.com/awslabs/goformation/v5/cloudformation/frauddetector"
"github.com/awslabs/goformation/v5/cloudformation/fsx"
"github.com/awslabs/goformation/v5/cloudformation/gamelift"
"github.com/awslabs/goformation/v5/cloudformation/global"
"github.com/awslabs/goformation/v5/cloudformation/globalaccelerator"
"github.com/awslabs/goformation/v5/cloudformation/glue"
"github.com/awslabs/goformation/v5/cloudformation/greengrass"
Expand Down Expand Up @@ -183,13 +186,12 @@ import (
"github.com/awslabs/goformation/v5/cloudformation/wisdom"
"github.com/awslabs/goformation/v5/cloudformation/workspaces"
"github.com/awslabs/goformation/v5/cloudformation/xray"

"github.com/awslabs/goformation/v5/cloudformation/global"
)

// AllResources fetches an iterable map all CloudFormation and SAM resources
func AllResources() map[string]Resource {
return map[string]Resource{
"AWS::CDK::Metadata": &cdkmetadata.CDKMetadata{},
"AWS::ACMPCA::Certificate": &acmpca.Certificate{},
"AWS::ACMPCA::CertificateAuthority": &acmpca.CertificateAuthority{},
"AWS::ACMPCA::CertificateAuthorityActivation": &acmpca.CertificateAuthorityActivation{},
Expand Down
59 changes: 59 additions & 0 deletions cloudformation/cdkmetadata/aws-cdkmetadata-cdkmetadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cdkmetadata

import (
"bytes"
"encoding/json"
"fmt"
)

// CDKMetadata AWS CloudFormation Resource (AWS::CDK::Metadata)
type CDKMetadata struct {
Analytics string `json:"Analytics,omitempty"`

Metadata map[string]interface{} `json:"-"`
}

// AWSCloudFormationType returns the AWS CloudFormation resource type
func (r *CDKMetadata) AWSCloudFormationType() string {
return "AWS::CDK::Metadata"
}

// MarshalJSON is a custom JSON marshalling hook that embeds this object into
// an AWS CloudFormation JSON resource's 'Properties' field and adds a 'Type'.
func (r CDKMetadata) MarshalJSON() ([]byte, error) {
type Properties CDKMetadata
return json.Marshal(&struct {
Type string
Properties Properties
Metadata map[string]interface{} `json:"Metadata,omitempty"`
}{
Type: r.AWSCloudFormationType(),
Properties: (Properties)(r),
Metadata: r.Metadata,
})
}

// UnmarshalJSON is a custom JSON unmarshalling hook that strips the outer
// AWS CloudFormation resource object, and just keeps the 'Properties' field.
func (r *CDKMetadata) UnmarshalJSON(b []byte) error {
type Properties CDKMetadata
res := &struct {
Type string
Properties *Properties
Metadata map[string]interface{}
}{}

dec := json.NewDecoder(bytes.NewReader(b))
dec.DisallowUnknownFields() // Force error if unknown field is found

if err := dec.Decode(&res); err != nil {
fmt.Printf("ERROR: %s\n", err)
return err
}

// If the resource has no Properties set, it could be nil
if res.Properties != nil {
*r = CDKMetadata(*res.Properties)
}
return nil
}
1 change: 1 addition & 0 deletions cloudformation/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (resources *Resources) UnmarshalJSON(b []byte) error {

func (globals *Globals) UnmarshalJSON(b []byte) error {
// Globals

var rawGlobals map[string]*json.RawMessage
err := json.Unmarshal(b, &rawGlobals)

Expand Down
3 changes: 2 additions & 1 deletion goformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func ParseYAML(data []byte) (*cloudformation.Template, error) {
// ParseYAMLWithOptions an AWS CloudFormation template (expects a []byte of valid YAML)
// Parsing can be tweaked via the specified options.
func ParseYAMLWithOptions(data []byte, options *intrinsics.ProcessorOptions) (*cloudformation.Template, error) {

// Process all AWS CloudFormation intrinsic functions (e.g. Fn::Join)
intrinsified, err := intrinsics.ProcessYAML(data, options)
if err != nil {
Expand Down Expand Up @@ -73,8 +74,8 @@ func ParseJSONWithOptions(data []byte, options *intrinsics.ProcessorOptions) (*c
}

func unmarshal(data []byte) (*cloudformation.Template, error) {

template := &cloudformation.Template{}

if err := json.Unmarshal(data, template); err != nil {
return nil, err
}
Expand Down
27 changes: 27 additions & 0 deletions goformation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,33 @@ var _ = Describe("Goformation", func() {

})

Context("with a CDKMetada template", func() {

template, err := goformation.Open("test/yaml/cdkmetadata.yaml")
It("should successfully validate the template", func() {
Expect(err).To(BeNil())
Expect(template).ShouldNot(BeNil())
})

It("should correctly Marshal the cdkmetadata resource", func() {
data, err := template.JSON()
Expect(err).To(BeNil())

var result map[string]interface{}
if err := json.Unmarshal(data, &result); err != nil {
Fail(err.Error())
}

resources, ok := result["Resources"].(map[string]interface{})
Expect(ok).To(BeTrue())
Expect(resources).To(HaveLen(1))
Expect(resources).To(HaveKey("CDKMetadata"))

mcr := resources["CDKMetadata"].(map[string]interface{})
Expect(mcr["Properties"]).To(HaveKey("Analytics"))
})
})

Context("with a template that contains conditional resources", func() {

template := &cloudformation.Template{
Expand Down
1 change: 0 additions & 1 deletion intrinsics/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func ProcessYAML(input []byte, options *ProcessorOptions) ([]byte, error) {
// AWS CloudFormation intrinsic functions, resolves them, and then returns
// the resulting interface{} object.
func ProcessJSON(input []byte, options *ProcessorOptions) ([]byte, error) {

// First, unmarshal the JSON to a generic interface{} type
var unmarshalled interface{}
if err := json.Unmarshal(input, &unmarshalled); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions schema/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18958,6 +18958,19 @@ var CloudformationSchema = `{
],
"type": "object"
},
"AWS::CDK::Metadata": {
"Type": "AWS::CDK::Metadata",
"Properties": {
"Analytics": {
"type": "string"
}
},
"Metadata": {
"aws:cdk:path": {
"type": "string"
}
}
},
"AWS::CloudFormation::CustomResource": {
"additionalProperties": false,
"properties": {
Expand Down
13 changes: 13 additions & 0 deletions schema/cloudformation.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18955,6 +18955,19 @@
],
"type": "object"
},
"AWS::CDK::Metadata": {
"Type": "AWS::CDK::Metadata",
"Properties": {
"Analytics": {
"type": "string"
}
},
"Metadata": {
"aws:cdk:path": {
"type": "string"
}
}
},
"AWS::CloudFormation::CustomResource": {
"additionalProperties": false,
"properties": {
Expand Down
9 changes: 9 additions & 0 deletions test/yaml/cdkmetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

AWSTemplateFormatVersion: 2010-09-09
Resources:
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:foobar
Metadata:
aws:cdk:path: foobar/CDKMetadata/Default

0 comments on commit 3d1b1f9

Please sign in to comment.