From 6e39ebe6e598e68f366b61b721e1ada2bdf268af Mon Sep 17 00:00:00 2001 From: Paul Maddox Date: Tue, 5 Jan 2021 08:11:26 +0400 Subject: [PATCH] fix(schema): S3Location or String support for AWS::Serverless::LayerVersion.ContentUri (#339) * S3Location support for LayerVersion.ContentUri Update to SAM schema Fixes #337 * go generate Regenerate resources to support S3Location property for AWS::Serverless::LayerVersion.ContentUri --- .../serverless/aws-serverless-layerversion.go | 2 +- .../aws-serverless-layerversion_s3location.go | 45 +++++++++++++ .../serverless/layerversion_contenturi.go | 64 +++++++++++++++++++ generate/sam-2016-10-31.json | 30 ++++++++- schema/sam.go | 30 ++++++++- schema/sam.schema.json | 30 ++++++++- 6 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 cloudformation/serverless/aws-serverless-layerversion_s3location.go create mode 100644 cloudformation/serverless/layerversion_contenturi.go diff --git a/cloudformation/serverless/aws-serverless-layerversion.go b/cloudformation/serverless/aws-serverless-layerversion.go index 5734bcdeb1..8bf6b9d00c 100644 --- a/cloudformation/serverless/aws-serverless-layerversion.go +++ b/cloudformation/serverless/aws-serverless-layerversion.go @@ -20,7 +20,7 @@ type LayerVersion struct { // ContentUri AWS CloudFormation Property // Required: false // See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlesslayerversion - ContentUri string `json:"ContentUri,omitempty"` + ContentUri *LayerVersion_ContentUri `json:"ContentUri,omitempty"` // Description AWS CloudFormation Property // Required: false diff --git a/cloudformation/serverless/aws-serverless-layerversion_s3location.go b/cloudformation/serverless/aws-serverless-layerversion_s3location.go new file mode 100644 index 0000000000..25cc3721c5 --- /dev/null +++ b/cloudformation/serverless/aws-serverless-layerversion_s3location.go @@ -0,0 +1,45 @@ +package serverless + +import ( + "github.com/awslabs/goformation/v4/cloudformation/policies" +) + +// LayerVersion_S3Location AWS CloudFormation Resource (AWS::Serverless::LayerVersion.S3Location) +// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#s3-location-object +type LayerVersion_S3Location struct { + + // Bucket AWS CloudFormation Property + // Required: true + // See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Bucket string `json:"Bucket,omitempty"` + + // Key AWS CloudFormation Property + // Required: true + // See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Key string `json:"Key,omitempty"` + + // Version AWS CloudFormation Property + // Required: false + // See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Version int `json:"Version,omitempty"` + + // AWSCloudFormationDeletionPolicy represents a CloudFormation DeletionPolicy + AWSCloudFormationDeletionPolicy policies.DeletionPolicy `json:"-"` + + // AWSCloudFormationUpdateReplacePolicy represents a CloudFormation UpdateReplacePolicy + AWSCloudFormationUpdateReplacePolicy policies.UpdateReplacePolicy `json:"-"` + + // AWSCloudFormationDependsOn stores the logical ID of the resources to be created before this resource + AWSCloudFormationDependsOn []string `json:"-"` + + // AWSCloudFormationMetadata stores structured data associated with this resource + AWSCloudFormationMetadata map[string]interface{} `json:"-"` + + // AWSCloudFormationCondition stores the logical ID of the condition that must be satisfied for this resource to be created + AWSCloudFormationCondition string `json:"-"` +} + +// AWSCloudFormationType returns the AWS CloudFormation resource type +func (r *LayerVersion_S3Location) AWSCloudFormationType() string { + return "AWS::Serverless::LayerVersion.S3Location" +} diff --git a/cloudformation/serverless/layerversion_contenturi.go b/cloudformation/serverless/layerversion_contenturi.go new file mode 100644 index 0000000000..1e6a1dbc48 --- /dev/null +++ b/cloudformation/serverless/layerversion_contenturi.go @@ -0,0 +1,64 @@ +package serverless + +import ( + "encoding/json" + "sort" + + "github.com/awslabs/goformation/v4/cloudformation/utils" +) + +// LayerVersion_ContentUri is a helper struct that can hold either a String or S3Location value +type LayerVersion_ContentUri struct { + String *string + + S3Location *LayerVersion_S3Location +} + +func (r LayerVersion_ContentUri) value() interface{} { + ret := []interface{}{} + + if r.String != nil { + ret = append(ret, r.String) + } + + if r.S3Location != nil { + ret = append(ret, *r.S3Location) + } + + sort.Sort(utils.ByJSONLength(ret)) // Heuristic to select best attribute + if len(ret) > 0 { + return ret[0] + } + + return nil +} + +func (r LayerVersion_ContentUri) MarshalJSON() ([]byte, error) { + return json.Marshal(r.value()) +} + +// Hook into the marshaller +func (r *LayerVersion_ContentUri) UnmarshalJSON(b []byte) error { + + // Unmarshal into interface{} to check it's type + var typecheck interface{} + if err := json.Unmarshal(b, &typecheck); err != nil { + return err + } + + switch val := typecheck.(type) { + + case string: + r.String = &val + + case map[string]interface{}: + val = val // This ensures val is used to stop an error + + json.Unmarshal(b, &r.S3Location) + + case []interface{}: + + } + + return nil +} diff --git a/generate/sam-2016-10-31.json b/generate/sam-2016-10-31.json index c202d2ae9e..e9298f78e6 100644 --- a/generate/sam-2016-10-31.json +++ b/generate/sam-2016-10-31.json @@ -371,7 +371,12 @@ "ContentUri": { "Documentation": "https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlesslayerversion", "Required": false, - "PrimitiveType": "String", + "PrimitiveTypes": [ + "String" + ], + "Types": [ + "S3Location" + ], "UpdateType": "Immutable" }, "CompatibleRuntimes": { @@ -522,6 +527,29 @@ } } }, + "AWS::Serverless::LayerVersion.S3Location": { + "Documentation": "https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#s3-location-object", + "Properties": { + "Bucket": { + "Documentation": "https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction", + "Required": true, + "PrimitiveType": "String", + "UpdateType": "Immutable" + }, + "Key": { + "Documentation": "https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction", + "Required": true, + "PrimitiveType": "String", + "UpdateType": "Immutable" + }, + "Version": { + "Documentation": "https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction", + "Required": false, + "PrimitiveType": "Integer", + "UpdateType": "Immutable" + } + } + }, "AWS::Serverless::Function.FileSystemConfig": { "Documentation": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-filesystemconfig.html#cfn-lambda-function-filesystemconfig-localmountpath", "Properties": { diff --git a/schema/sam.go b/schema/sam.go index e67eeaaf89..ac5d227328 100644 --- a/schema/sam.go +++ b/schema/sam.go @@ -85179,7 +85179,16 @@ var SamSchema = `{ "type": "array" }, "ContentUri": { - "type": "string" + "anyOf": [ + { + "type": [ + "string" + ] + }, + { + "$ref": "#/definitions/AWS::Serverless::LayerVersion.S3Location" + } + ] }, "Description": { "type": "string" @@ -85216,6 +85225,25 @@ var SamSchema = `{ ], "type": "object" }, + "AWS::Serverless::LayerVersion.S3Location": { + "additionalProperties": false, + "properties": { + "Bucket": { + "type": "string" + }, + "Key": { + "type": "string" + }, + "Version": { + "type": "number" + } + }, + "required": [ + "Bucket", + "Key" + ], + "type": "object" + }, "AWS::Serverless::SimpleTable": { "additionalProperties": false, "properties": { diff --git a/schema/sam.schema.json b/schema/sam.schema.json index 2ac969a88b..c9d623058f 100644 --- a/schema/sam.schema.json +++ b/schema/sam.schema.json @@ -85176,7 +85176,16 @@ "type": "array" }, "ContentUri": { - "type": "string" + "anyOf": [ + { + "type": [ + "string" + ] + }, + { + "$ref": "#/definitions/AWS::Serverless::LayerVersion.S3Location" + } + ] }, "Description": { "type": "string" @@ -85213,6 +85222,25 @@ ], "type": "object" }, + "AWS::Serverless::LayerVersion.S3Location": { + "additionalProperties": false, + "properties": { + "Bucket": { + "type": "string" + }, + "Key": { + "type": "string" + }, + "Version": { + "type": "number" + } + }, + "required": [ + "Bucket", + "Key" + ], + "type": "object" + }, "AWS::Serverless::SimpleTable": { "additionalProperties": false, "properties": {