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 all commits
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
32 changes: 32 additions & 0 deletions aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -37,6 +38,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 +82,18 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}

d.SetId(*gateway.Id)

if body, ok := d.GetOk("body"); ok {
log.Printf("[DEBUG] Initializing API Gateway from OpenAPI spec %s", d.Id())
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
RestApiId: gateway.Id,
Mode: aws.String(apigateway.PutModeOverwrite),
Body: []byte(body.(string)),
})
if err != nil {
return errwrap.Wrapf("Error creating API Gateway specification: {{err}}", err)
}
}

if err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil {
return err
}
Expand Down Expand Up @@ -155,6 +173,20 @@ 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 {
log.Printf("[DEBUG] Updating API Gateway from OpenAPI spec: %s", d.Id())
_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
RestApiId: aws.String(d.Id()),
Mode: aws.String(apigateway.PutModeOverwrite),
Body: []byte(body.(string)),
})
if err != nil {
return errwrap.Wrapf("Error updating API Gateway specification: {{err}}", err)
}
}
}

_, 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)

`
12 changes: 12 additions & 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,18 @@ 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.

__Note__: If the `body` argument is provided, the OpenAPI specification will be used to configure the resources, methods and integrations for the Rest API. If this argument is provided, the following resources should not be managed as separate ones, as updates may cause manual resource updates to be overwritten:

* `aws_api_gateway_resource`
* `aws_api_gateway_method`
* `aws_api_gateway_method_response`
* `aws_api_gateway_method_settings`
* `aws_api_gateway_integration`
* `aws_api_gateway_integration_response`
* `aws_api_gateway_gateway_response`
* `aws_api_gateway_model`

## Attributes Reference

Expand Down