Skip to content

Commit

Permalink
Merge pull request #1206 from cloudskiff/api_gateway_yaml_hotfix
Browse files Browse the repository at this point in the history
Hotfix #1203
  • Loading branch information
eliecharra authored Nov 4, 2021
2 parents 0394cfe + 7383603 commit 9ad5528
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/fatih/color v1.9.0
github.com/getkin/kin-openapi v0.75.0
github.com/getsentry/sentry-go v0.10.0
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/hashicorp/go-getter v1.5.3
github.com/hashicorp/go-hclog v0.9.2
Expand Down
15 changes: 13 additions & 2 deletions pkg/middlewares/aws_api_gateway_rest_api_expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
"github.com/ghodss/yaml"
)

// Explodes api gateway rest api body attribute to dedicated resources as per Terraform documentation (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_rest_api)
Expand Down Expand Up @@ -49,7 +50,12 @@ func (m *AwsApiGatewayRestApiExpander) handleBody(api *resource.Resource, result

docV3 := &openapi3.T{}
if err := json.Unmarshal([]byte(*body), &docV3); err != nil {
return err
if _, ok := err.(*json.SyntaxError); ok {
err = yaml.Unmarshal([]byte(*body), &docV3)
}
if err != nil {
return err
}
}
// It's an OpenAPI v3 document
if docV3.OpenAPI != "" {
Expand All @@ -58,7 +64,12 @@ func (m *AwsApiGatewayRestApiExpander) handleBody(api *resource.Resource, result

docV2 := &openapi2.T{}
if err := json.Unmarshal([]byte(*body), &docV2); err != nil {
return err
if _, ok := err.(*json.SyntaxError); ok {
err = yaml.Unmarshal([]byte(*body), &docV2)
}
if err != nil {
return err
}
}
// It's an OpenAPI v2 document
if docV2.Swagger != "" {
Expand Down
188 changes: 186 additions & 2 deletions pkg/middlewares/aws_api_gateway_rest_api_expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestAwsApiGatewayRestApiExpander_Execute(t *testing.T) {
expected []*resource.Resource
}{
{
name: "create aws_api_gateway_resource from OpenAPI v3 document",
name: "create aws_api_gateway_resource from OpenAPI v3 JSON document",
mocks: func(factory *terraform.MockResourceFactory) {
factory.On(
"CreateAbstractResource",
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestAwsApiGatewayRestApiExpander_Execute(t *testing.T) {
},
},
{
name: "create aws_api_gateway_resource from OpenAPI v2 document",
name: "create aws_api_gateway_resource from OpenAPI v2 JSON document",
mocks: func(factory *terraform.MockResourceFactory) {
factory.On(
"CreateAbstractResource",
Expand Down Expand Up @@ -602,6 +602,190 @@ func TestAwsApiGatewayRestApiExpander_Execute(t *testing.T) {
},
},
},
{
name: "create api gateway resources from OpenAPI v3 YAML document",
mocks: func(factory *terraform.MockResourceFactory) {
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayResourceResourceType,
"bar",
map[string]interface{}{
"rest_api_id": "foo",
"path": "/{path+}",
},
).Once().Return(&resource.Resource{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
Attrs: &resource.Attributes{
"rest_api_id": "foo",
"path": "/{path+}",
},
})
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayMethodResourceType,
"agm-foo-bar-GET",
map[string]interface{}{},
).Once().Return(&resource.Resource{
Id: "agm-foo-bar-GET",
Type: aws.AwsApiGatewayMethodResourceType,
Attrs: &resource.Attributes{},
})
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayMethodResponseResourceType,
"agmr-foo-bar-GET-200",
map[string]interface{}{},
).Once().Return(&resource.Resource{
Id: "agmr-foo-bar-GET-200",
Type: aws.AwsApiGatewayMethodResponseResourceType,
Attrs: &resource.Attributes{},
})
},
resourcesFromState: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"body": "---\nopenapi: \"3.0.1\"\ninfo:\n title: baz\n description: ComputelessBlog\npaths:\n /{path+}:\n get:\n responses:\n 200:\n description: \"200 response\"\n content:\n text/html:\n schema:\n $ref: \"#/components/schemas/Empty\"\n x-amazon-apigateway-integration:\n type: \"mock\"\n responses:\n default:\n statusCode: \"200\"\n passthroughBehavior: \"never\"\n httpMethod: \"POST\"\ncomponents:\n schemas:\n Empty:\n type: object\n title: Empty Schema\n description: Empty Schema",
},
},
},
remoteResources: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
Attrs: &resource.Attributes{
"rest_api_id": "foo",
"path": "/{path+}",
},
},
},
expected: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"body": "---\nopenapi: \"3.0.1\"\ninfo:\n title: baz\n description: ComputelessBlog\npaths:\n /{path+}:\n get:\n responses:\n 200:\n description: \"200 response\"\n content:\n text/html:\n schema:\n $ref: \"#/components/schemas/Empty\"\n x-amazon-apigateway-integration:\n type: \"mock\"\n responses:\n default:\n statusCode: \"200\"\n passthroughBehavior: \"never\"\n httpMethod: \"POST\"\ncomponents:\n schemas:\n Empty:\n type: object\n title: Empty Schema\n description: Empty Schema",
},
},
{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
Attrs: &resource.Attributes{
"rest_api_id": "foo",
"path": "/{path+}",
},
},
{
Id: "agm-foo-bar-GET",
Type: aws.AwsApiGatewayMethodResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "agmr-foo-bar-GET-200",
Type: aws.AwsApiGatewayMethodResponseResourceType,
Attrs: &resource.Attributes{},
},
},
},
{
name: "create api gateway resources from OpenAPI v2 YAML document",
mocks: func(factory *terraform.MockResourceFactory) {
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayResourceResourceType,
"bar",
map[string]interface{}{
"rest_api_id": "foo",
"path": "/webhook",
},
).Once().Return(&resource.Resource{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
Attrs: &resource.Attributes{
"rest_api_id": "foo",
"path": "/webhook",
},
})
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayMethodResourceType,
"agm-foo-bar-OPTIONS",
map[string]interface{}{},
).Once().Return(&resource.Resource{
Id: "agm-foo-bar-OPTIONS",
Type: aws.AwsApiGatewayMethodResourceType,
Attrs: &resource.Attributes{},
})
factory.On(
"CreateAbstractResource",
aws.AwsApiGatewayMethodResponseResourceType,
"agmr-foo-bar-OPTIONS-200",
map[string]interface{}{},
).Once().Return(&resource.Resource{
Id: "agmr-foo-bar-OPTIONS-200",
Type: aws.AwsApiGatewayMethodResponseResourceType,
Attrs: &resource.Attributes{},
})
},
resourcesFromState: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"body": "---\nswagger: '2.0'\ninfo:\n version: '1.0'\n title: test\nschemes:\n - https\npaths:\n /webhook:\n options:\n consumes:\n - application/json\n produces:\n - application/json\n responses:\n '200':\n description: 200 response\n schema:\n $ref: \\\"#/definitions/Empty\\\"\n x-amazon-apigateway-integration:\n responses:\n default:\n statusCode: '200'\n requestTemplates:\n application/json: '{\\\"statusCode\\\": 200}'\n passthroughBehavior: when_no_match\n type: mock\n\n",
},
},
},
remoteResources: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
Attrs: &resource.Attributes{
"rest_api_id": "foo",
"path": "/webhook",
},
},
},
expected: []*resource.Resource{
{
Id: "foo",
Type: aws.AwsApiGatewayRestApiResourceType,
Attrs: &resource.Attributes{
"body": "---\nswagger: '2.0'\ninfo:\n version: '1.0'\n title: test\nschemes:\n - https\npaths:\n /webhook:\n options:\n consumes:\n - application/json\n produces:\n - application/json\n responses:\n '200':\n description: 200 response\n schema:\n $ref: \\\"#/definitions/Empty\\\"\n x-amazon-apigateway-integration:\n responses:\n default:\n statusCode: '200'\n requestTemplates:\n application/json: '{\\\"statusCode\\\": 200}'\n passthroughBehavior: when_no_match\n type: mock\n\n",
},
},
{
Id: "bar",
Type: aws.AwsApiGatewayResourceResourceType,
Attrs: &resource.Attributes{
"rest_api_id": "foo",
"path": "/webhook",
},
},
{
Id: "agm-foo-bar-OPTIONS",
Type: aws.AwsApiGatewayMethodResourceType,
Attrs: &resource.Attributes{},
},
{
Id: "agmr-foo-bar-OPTIONS-200",
Type: aws.AwsApiGatewayMethodResponseResourceType,
Attrs: &resource.Attributes{},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 9ad5528

Please sign in to comment.