Skip to content

Commit

Permalink
Merge pull request hashicorp#1197 from lukehoban/restapi_body
Browse files Browse the repository at this point in the history
Add a body property to API Gateway RestAPI for Swagger import support
  • Loading branch information
Ninir authored Aug 29, 2017
2 parents afb8ae2 + e26bae0 commit a62196e
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
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
}
`
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

0 comments on commit a62196e

Please sign in to comment.