forked from awslabs/goformation
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): AWS::Serverless:Api.Cors (awslabs#246)
AWS::Serverless::Api.Cors should be either a string, or a [cors configuration object](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration). Fixes awslabs#244
- Loading branch information
1 parent
665e4b4
commit 62fd56a
Showing
8 changed files
with
297 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package serverless | ||
|
||
import ( | ||
"encoding/json" | ||
"sort" | ||
|
||
"github.com/awslabs/goformation/v4/cloudformation/utils" | ||
) | ||
|
||
// Api_Cors is a helper struct that can hold either a String or CorsConfiguration value | ||
type Api_Cors struct { | ||
String *string | ||
|
||
CorsConfiguration *Api_CorsConfiguration | ||
} | ||
|
||
func (r Api_Cors) value() interface{} { | ||
ret := []interface{}{} | ||
|
||
if r.String != nil { | ||
ret = append(ret, r.String) | ||
} | ||
|
||
if r.CorsConfiguration != nil { | ||
ret = append(ret, *r.CorsConfiguration) | ||
} | ||
|
||
sort.Sort(utils.ByJSONLength(ret)) // Heuristic to select best attribute | ||
if len(ret) > 0 { | ||
return ret[0] | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r Api_Cors) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(r.value()) | ||
} | ||
|
||
// Hook into the marshaller | ||
func (r *Api_Cors) 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.CorsConfiguration) | ||
|
||
case []interface{}: | ||
|
||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
cloudformation/serverless/aws-serverless-api_corsconfiguration.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package serverless | ||
|
||
import ( | ||
"github.com/awslabs/goformation/v4/cloudformation/policies" | ||
) | ||
|
||
// Api_CorsConfiguration AWS CloudFormation Resource (AWS::Serverless::Api.CorsConfiguration) | ||
// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration | ||
type Api_CorsConfiguration struct { | ||
|
||
// AllowCredentials AWS CloudFormation Property | ||
// Required: false | ||
// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration | ||
AllowCredentials bool `json:"AllowCredentials,omitempty"` | ||
|
||
// AllowHeaders AWS CloudFormation Property | ||
// Required: false | ||
// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration | ||
AllowHeaders string `json:"AllowHeaders,omitempty"` | ||
|
||
// AllowMethods AWS CloudFormation Property | ||
// Required: false | ||
// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration | ||
AllowMethods string `json:"AllowMethods,omitempty"` | ||
|
||
// AllowOrigin AWS CloudFormation Property | ||
// Required: true | ||
// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration | ||
AllowOrigin string `json:"AllowOrigin,omitempty"` | ||
|
||
// MaxAge AWS CloudFormation Property | ||
// Required: false | ||
// See: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#cors-configuration | ||
MaxAge string `json:"MaxAge,omitempty"` | ||
|
||
// AWSCloudFormationDeletionPolicy represents a CloudFormation DeletionPolicy | ||
AWSCloudFormationDeletionPolicy policies.DeletionPolicy `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:"-"` | ||
} | ||
|
||
// AWSCloudFormationType returns the AWS CloudFormation resource type | ||
func (r *Api_CorsConfiguration) AWSCloudFormationType() string { | ||
return "AWS::Serverless::Api.CorsConfiguration" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
test/yaml/aws-serverless-api-string-or-cors-configuration.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: SAM template for testing AWS::Serverless::Api.Cors with both string and Cors Configuration object | ||
Resources: | ||
|
||
RestApiWithCorsConfiguration: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
Name: !Sub "${AWS::StackName}-rest-api" | ||
StageName: Prod | ||
TracingEnabled: false | ||
EndpointConfiguration: EDGE | ||
MethodSettings: | ||
- DataTraceEnabled: false | ||
HttpMethod: "*" | ||
LoggingLevel: ERROR | ||
MetricsEnabled: true | ||
ResourcePath: /* | ||
Cors: | ||
AllowHeaders: "'Authorization,authorization'" | ||
AllowOrigin: "'*'" | ||
|
||
RestApiWithCorsString: | ||
Type: AWS::Serverless::Api | ||
Properties: | ||
Name: !Sub "${AWS::StackName}-rest-api" | ||
StageName: Prod | ||
TracingEnabled: false | ||
EndpointConfiguration: EDGE | ||
MethodSettings: | ||
- DataTraceEnabled: false | ||
HttpMethod: "*" | ||
LoggingLevel: ERROR | ||
MetricsEnabled: true | ||
ResourcePath: /* | ||
Cors: "'www.example.com'" | ||
|
||
|