Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a body property to API Gateway RestAPI for Swagger import support #1197

Merged
merged 3 commits into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},

"body": {
Type: schema.TypeString,
Optional: true,
},

"root_resource_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -76,6 +81,17 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}

d.SetId(*gateway.Id)

if body, ok := d.GetOk("body"); ok {
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you log in the logs that we are setting the specification?
Just for verbosity & debugging :D

Same for the update would be highly appreciated!

RestApiId: gateway.Id,
Mode: aws.String("overwrite"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the apigateway.PutModeOverwrite constant here and line 178?

Body: []byte(body.(string)),
})
if err != nil {
return fmt.Errorf("Error putting API Gateway specification: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use errwrap.Wrapf("Error creating API Gateway specification: {{err}}", err) there please? (notice the creating word, to make it clear we are putting by creating)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does no harm, but I think the wrapper here isn't necessary as the only place the error will escalate to is the UI, where all that matters is string-based error message user will read, not the original error type.

}
}

if err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil {
return err
}
Expand Down Expand Up @@ -155,6 +171,19 @@ func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Updating API Gateway %s", d.Id())

if d.HasChange("body") {
if body, ok := d.GetOk("body"); ok {
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
RestApiId: aws.String(d.Id()),
Mode: aws.String("overwrite"),
Body: []byte(body.(string)),
})
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go for the same as line 91 here: errwrap.Wrapf("Error updating API Gateway specification: {{err}}", err). Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does no harm, but I think the wrapper here isn't necessary as the only place the error will escalate to is the UI, where all that matters is string-based error message user will read, not the original error type.

}
}
}

_, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{
RestApiId: aws.String(d.Id()),
PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d),
Expand Down
150 changes: 150 additions & 0 deletions aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,47 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
})
}

func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) {
var conf apigateway.RestApi

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayRestAPIConfigOpenAPI,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"),
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/test"}),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "name", "test"),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "description", ""),
resource.TestCheckResourceAttrSet(
"aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckNoResourceAttr(
"aws_api_gateway_rest_api.test", "binary_media_types"),
),
},

{
Config: testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPI,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"),
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/update"}),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "name", "test"),
resource.TestCheckResourceAttrSet(
"aws_api_gateway_rest_api.test", "created_date"),
),
},
},
})
}

func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.Name != name {
Expand All @@ -76,6 +117,37 @@ func testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(conf *apigateway.RestA
}
}

func testAccCheckAWSAPIGatewayRestAPIRoutes(conf *apigateway.RestApi, routes []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway

resp, err := conn.GetResources(&apigateway.GetResourcesInput{
RestApiId: conf.Id,
})
if err != nil {
return err
}

actualRoutePaths := map[string]bool{}
for _, resource := range resp.Items {
actualRoutePaths[*resource.Path] = true
}

for _, route := range routes {
if _, ok := actualRoutePaths[route]; !ok {
return fmt.Errorf("Expected path %v but did not find it in %v", route, actualRoutePaths)
}
delete(actualRoutePaths, route)
}

if len(actualRoutePaths) > 0 {
return fmt.Errorf("Found unexpected paths %v", actualRoutePaths)
}

return nil
}
}

func testAccCheckAWSAPIGatewayRestAPIExists(n string, res *apigateway.RestApi) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -144,3 +216,81 @@ resource "aws_api_gateway_rest_api" "test" {
binary_media_types = ["application/octet-stream"]
}
`

const testAccAWSAPIGatewayRestAPIConfigOpenAPI = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
body = <<EOF
{
"swagger": "2.0",
"info": {
"title": "test",
"version": "2017-04-20T04:08:08Z"
},
"schemes": [
"https"
],
"paths": {
"/test": {
"get": {
"responses": {
"200": {
"description": "200 response"
}
},
"x-amazon-apigateway-integration": {
"type": "HTTP",
"uri": "https://www.google.de",
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": 200
}
}
}
}
}
}
}
EOF
}
`

const testAccAWSAPIGatewayRestAPIUpdateConfigOpenAPI = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
body = <<EOF
{
"swagger": "2.0",
"info": {
"title": "test",
"version": "2017-04-20T04:08:08Z"
},
"schemes": [
"https"
],
"paths": {
"/update": {
"get": {
"responses": {
"200": {
"description": "200 response"
}
},
"x-amazon-apigateway-integration": {
"type": "HTTP",
"uri": "https://www.google.de",
"httpMethod": "GET",
"responses": {
"default": {
"statusCode": 200
}
}
}
}
}
}
}
EOF
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix the space indentation here please? 😄 (mostly at the beginning of the JSON structure)

`
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_rest_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following arguments are supported:
* `name` - (Required) The name of the REST API
* `description` - (Optional) The description of the REST API
* `binary_media_types` - (Optional) The list of binary media types supported by the RestApi. By default, the RestApi supports only UTF-8-encoded text payloads.
* `body` - (Optional) An OpenAPI specification that defines the set of routes and integrations to create as part of the REST API.

## Attributes Reference

Expand Down