From 701fb3d6621d1e738af9eea0c906020d9e0c190e Mon Sep 17 00:00:00 2001 From: Jarred Ward Date: Thu, 30 Mar 2023 06:18:04 -0700 Subject: [PATCH] feat(sam): add SAM CognitoEvent EventSource (#570) --- .../aws-serverless-function_cognitoevent.go | 42 ++++++++++ .../serverless/function_properties.go | 12 ++- cloudformation/serverless/function_trigger.go | 76 +++++++++++++++++++ generate/sam-2016-10-31.json | 24 +++++- schema/sam.go | 31 ++++++++ schema/sam.schema.json | 31 ++++++++ 6 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 cloudformation/serverless/aws-serverless-function_cognitoevent.go create mode 100644 cloudformation/serverless/function_trigger.go diff --git a/cloudformation/serverless/aws-serverless-function_cognitoevent.go b/cloudformation/serverless/aws-serverless-function_cognitoevent.go new file mode 100644 index 0000000000..bb28c99524 --- /dev/null +++ b/cloudformation/serverless/aws-serverless-function_cognitoevent.go @@ -0,0 +1,42 @@ +// Code generated by "go generate". Please don't change this file directly. + +package serverless + +import ( + "github.com/awslabs/goformation/v7/cloudformation/policies" +) + +// Function_CognitoEvent AWS CloudFormation Resource (AWS::Serverless::Function.CognitoEvent) +// See: https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#cognito +type Function_CognitoEvent struct { + + // Trigger AWS CloudFormation Property + // Required: true + // See: https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#cognito + Trigger *Function_Trigger `json:"Trigger"` + + // UserPool AWS CloudFormation Property + // Required: true + // See: https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#cognito + UserPool string `json:"UserPool"` + + // 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 *Function_CognitoEvent) AWSCloudFormationType() string { + return "AWS::Serverless::Function.CognitoEvent" +} diff --git a/cloudformation/serverless/function_properties.go b/cloudformation/serverless/function_properties.go index 7f2017e00b..f781da0c14 100644 --- a/cloudformation/serverless/function_properties.go +++ b/cloudformation/serverless/function_properties.go @@ -11,7 +11,7 @@ import ( "github.com/awslabs/goformation/v7/cloudformation/utils" ) -// Function_Properties is a helper struct that can hold either a S3Event, SNSEvent, SQSEvent, KinesisEvent, DynamoDBEvent, ApiEvent, ScheduleEvent, CloudWatchEventEvent, CloudWatchLogsEvent, IoTRuleEvent, AlexaSkillEvent, EventBridgeRuleEvent, or HttpApiEvent value +// Function_Properties is a helper struct that can hold either a S3Event, SNSEvent, SQSEvent, KinesisEvent, DynamoDBEvent, ApiEvent, ScheduleEvent, CloudWatchEventEvent, CloudWatchLogsEvent, IoTRuleEvent, AlexaSkillEvent, EventBridgeRuleEvent, HttpApiEvent, or CognitoEvent value type Function_Properties struct { S3Event *Function_S3Event SNSEvent *Function_SNSEvent @@ -26,6 +26,7 @@ type Function_Properties struct { AlexaSkillEvent *Function_AlexaSkillEvent EventBridgeRuleEvent *Function_EventBridgeRuleEvent HttpApiEvent *Function_HttpApiEvent + CognitoEvent *Function_CognitoEvent } func (r Function_Properties) value() interface{} { @@ -83,6 +84,10 @@ func (r Function_Properties) value() interface{} { ret = append(ret, *r.HttpApiEvent) } + if r.CognitoEvent != nil { + ret = append(ret, *r.CognitoEvent) + } + sort.Sort(utils.ByJSONLength(ret)) // Heuristic to select best attribute if len(ret) > 0 { return ret[0] @@ -179,6 +184,11 @@ func (r *Function_Properties) UnmarshalJSON(b []byte) error { } reader.Seek(0, io.SeekStart) + if err := decoder.Decode(&r.CognitoEvent); err != nil { + r.CognitoEvent = nil + } + reader.Seek(0, io.SeekStart) + case []interface{}: } diff --git a/cloudformation/serverless/function_trigger.go b/cloudformation/serverless/function_trigger.go new file mode 100644 index 0000000000..3970a6ac02 --- /dev/null +++ b/cloudformation/serverless/function_trigger.go @@ -0,0 +1,76 @@ +// Code generated by "go generate". Please don't change this file directly. + +package serverless + +import ( + "bytes" + "encoding/json" + "io" + "sort" + + "github.com/awslabs/goformation/v7/cloudformation/utils" +) + +// Function_Trigger is a helper struct that can hold either a String or String value +type Function_Trigger struct { + String *string + + StringArray *[]string +} + +func (r Function_Trigger) value() interface{} { + ret := []interface{}{} + + if r.String != nil { + ret = append(ret, r.String) + } + + if r.StringArray != nil { + ret = append(ret, r.StringArray) + } + + sort.Sort(utils.ByJSONLength(ret)) // Heuristic to select best attribute + if len(ret) > 0 { + return ret[0] + } + + return nil +} + +func (r Function_Trigger) MarshalJSON() ([]byte, error) { + return json.Marshal(r.value()) +} + +// Hook into the marshaller +func (r *Function_Trigger) 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 []string: + r.StringArray = &val + + case map[string]interface{}: + val = val // This ensures val is used to stop an error + + reader := bytes.NewReader(b) + decoder := json.NewDecoder(reader) + decoder.DisallowUnknownFields() + reader.Seek(0, io.SeekStart) + + case []interface{}: + + json.Unmarshal(b, &r.StringArray) + + } + + return nil +} diff --git a/generate/sam-2016-10-31.json b/generate/sam-2016-10-31.json index 3db5518f6a..d907d0feba 100644 --- a/generate/sam-2016-10-31.json +++ b/generate/sam-2016-10-31.json @@ -857,7 +857,8 @@ "IoTRuleEvent", "AlexaSkillEvent", "EventBridgeRuleEvent", - "HttpApiEvent" + "HttpApiEvent", + "CognitoEvent" ], "UpdateType": "Immutable" } @@ -1149,6 +1150,27 @@ } } }, + "AWS::Serverless::Function.CognitoEvent": { + "Documentation": "https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#cognito", + "Properties": { + "UserPool": { + "Documentation": "https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#cognito", + "Required": true, + "PrimitiveType": "String", + "UpdateType": "Immutable" + }, + "Trigger": { + "Documentation": "https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#cognito", + "Required": true, + "PrimitiveTypes": [ + "String" + ], + "PrimitiveItemTypes": [ + "String" + ] + } + } + }, "AWS::Serverless::Function.RouteSettings": { "Documentation": "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigatewayv2-stage-routesettings.html", "Properties": { diff --git a/schema/sam.go b/schema/sam.go index 09c0bd595a..c9ebcf2e6e 100644 --- a/schema/sam.go +++ b/schema/sam.go @@ -173474,6 +173474,34 @@ var SamSchema = `{ ], "type": "object" }, + "AWS::Serverless::Function.CognitoEvent": { + "additionalProperties": false, + "properties": { + "Trigger": { + "anyOf": [ + { + "type": [ + "string" + ] + }, + { + "items": { + "type": "string" + }, + "type": "array" + } + ] + }, + "UserPool": { + "type": "string" + } + }, + "required": [ + "Trigger", + "UserPool" + ], + "type": "object" + }, "AWS::Serverless::Function.CollectionSAMPT": { "additionalProperties": false, "properties": { @@ -173711,6 +173739,9 @@ var SamSchema = `{ }, { "$ref": "#/definitions/AWS::Serverless::Function.HttpApiEvent" + }, + { + "$ref": "#/definitions/AWS::Serverless::Function.CognitoEvent" } ] }, diff --git a/schema/sam.schema.json b/schema/sam.schema.json index 83505be2cd..a3f400fc41 100644 --- a/schema/sam.schema.json +++ b/schema/sam.schema.json @@ -173469,6 +173469,34 @@ ], "type": "object" }, + "AWS::Serverless::Function.CognitoEvent": { + "additionalProperties": false, + "properties": { + "Trigger": { + "anyOf": [ + { + "type": [ + "string" + ] + }, + { + "items": { + "type": "string" + }, + "type": "array" + } + ] + }, + "UserPool": { + "type": "string" + } + }, + "required": [ + "Trigger", + "UserPool" + ], + "type": "object" + }, "AWS::Serverless::Function.CollectionSAMPT": { "additionalProperties": false, "properties": { @@ -173706,6 +173734,9 @@ }, { "$ref": "#/definitions/AWS::Serverless::Function.HttpApiEvent" + }, + { + "$ref": "#/definitions/AWS::Serverless::Function.CognitoEvent" } ] },