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

resource/aws_api_gateway_rest_api: Add support for content encoding #3642

Merged
merged 1 commit into from
Mar 13, 2018
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: 31 additions & 1 deletion aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strconv"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -42,6 +43,13 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
Optional: true,
},

"minimum_compression_size": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: validateIntegerInRange(-1, 10485760),
},

"root_resource_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -74,6 +82,11 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}
params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{}))
}

minimumCompressionSize := d.Get("minimum_compression_size").(int)
if minimumCompressionSize > -1 {
params.MinimumCompressionSize = aws.Int64(int64(minimumCompressionSize))
}

gateway, err := conn.CreateRestApi(params)
if err != nil {
return fmt.Errorf("Error creating API Gateway: %s", err)
Expand Down Expand Up @@ -139,7 +152,11 @@ func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{})
d.Set("name", api.Name)
d.Set("description", api.Description)
d.Set("binary_media_types", api.BinaryMediaTypes)

if api.MinimumCompressionSize == nil {
d.Set("minimum_compression_size", -1)
} else {
d.Set("minimum_compression_size", api.MinimumCompressionSize)
}
if err := d.Set("created_date", api.CreatedDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting created_date: %s", err)
}
Expand All @@ -166,6 +183,19 @@ func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*api
})
}

if d.HasChange("minimum_compression_size") {
minimumCompressionSize := d.Get("minimum_compression_size").(int)
var value string
if minimumCompressionSize > -1 {
value = strconv.Itoa(minimumCompressionSize)
}
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/minimumCompressionSize"),
Value: aws.String(value),
})
}

if d.HasChange("binary_media_types") {
o, n := d.GetChange("binary_media_types")
prefix := "binaryMediaTypes"
Expand Down
47 changes: 47 additions & 0 deletions aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "bar"),
testAccCheckAWSAPIGatewayRestAPIMinimumCompressionSizeAttribute(&conf, 0),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "name", "bar"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "description", ""),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "minimum_compression_size", "0"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckNoResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types"),
),
Expand All @@ -36,13 +38,24 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"),
testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(&conf, "test"),
testAccCheckAWSAPIGatewayRestAPIMinimumCompressionSizeAttribute(&conf, 10485760),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "name", "test"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "description", "test"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "minimum_compression_size", "10485760"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types.#", "1"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types.0", "application/octet-stream"),
),
},

{
Config: testAccAWSAPIGatewayRestAPIDisableCompressionConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPIMinimumCompressionSizeAttributeIsNil(&conf),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "minimum_compression_size", "-1"),
),
},
},
})
}
Expand Down Expand Up @@ -102,6 +115,29 @@ func testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(conf *apigateway.RestA
}
}

func testAccCheckAWSAPIGatewayRestAPIMinimumCompressionSizeAttribute(conf *apigateway.RestApi, minimumCompressionSize int64) resource.TestCheckFunc {
return func(s *terraform.State) error {
if conf.MinimumCompressionSize == nil {
return fmt.Errorf("MinimumCompressionSize should not be nil")
}
if *conf.MinimumCompressionSize != minimumCompressionSize {
return fmt.Errorf("Wrong MinimumCompressionSize: %d", *conf.MinimumCompressionSize)
}

return nil
}
}

func testAccCheckAWSAPIGatewayRestAPIMinimumCompressionSizeAttributeIsNil(conf *apigateway.RestApi) resource.TestCheckFunc {
return func(s *terraform.State) error {
if conf.MinimumCompressionSize != nil {
return fmt.Errorf("MinimumCompressionSize should be nil: %d", *conf.MinimumCompressionSize)
}

return nil
}
}

func testAccCheckAWSAPIGatewayRestAPIRoutes(conf *apigateway.RestApi, routes []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
Expand Down Expand Up @@ -191,6 +227,7 @@ func testAccCheckAWSAPIGatewayRestAPIDestroy(s *terraform.State) error {
const testAccAWSAPIGatewayRestAPIConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "bar"
minimum_compression_size = 0
}
`

Expand All @@ -199,6 +236,16 @@ resource "aws_api_gateway_rest_api" "test" {
name = "test"
description = "test"
binary_media_types = ["application/octet-stream"]
minimum_compression_size = 10485760
}
`

const testAccAWSAPIGatewayRestAPIDisableCompressionConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
description = "test"
binary_media_types = ["application/octet-stream"]
minimum_compression_size = -1
}
`

Expand Down
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.
* `minimum_compression_size` - (Optional) Minimum response size to compress for the REST API. Integer between -1 and 10485760 (10MB). Setting a value greater than -1 will enable compression, -1 disables compression (default).
* `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:
Expand Down