Skip to content

Commit

Permalink
Add support for binary media type to AWS API Gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettes committed Dec 16, 2016
1 parent 7995875 commit 0e6b2a4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
13 changes: 13 additions & 0 deletions builtin/providers/aws/resource_aws_api_gateway_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func resourceAwsApiGatewayIntegration() *schema.Resource {
Deprecated: "Use field request_parameters instead",
},

"content_handling": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateApiGatewayIntegrationContentHandling,
},

"passthrough_behavior": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -131,6 +137,11 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
credentials = aws.String(val.(string))
}

var contentHandling *string
if val, ok := d.GetOk("content_handling"); ok {
contentHandling = aws.String(val.(string))
}

_, err := conn.PutIntegration(&apigateway.PutIntegrationInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
Expand All @@ -144,6 +155,7 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
CacheNamespace: nil,
CacheKeyParameters: nil,
PassthroughBehavior: passthroughBehavior,
ContentHandling: contentHandling,
})
if err != nil {
return fmt.Errorf("Error creating API Gateway Integration: %s", err)
Expand Down Expand Up @@ -185,6 +197,7 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface
d.Set("request_parameters", aws.StringValueMap(integration.RequestParameters))
d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters))
d.Set("passthrough_behavior", integration.PassthroughBehavior)
d.Set("content_handling", integration.ContentHandling)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func TestAccAWSAPIGatewayIntegration_basic(t *testing.T) {
"aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration.test", "content_handling", ""),
),
},

Expand All @@ -52,6 +54,8 @@ func TestAccAWSAPIGatewayIntegration_basic(t *testing.T) {
"aws_api_gateway_integration.test", "uri", ""),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration.test", "passthrough_behavior", "NEVER"),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_BINARY"),
),
},
},
Expand All @@ -66,6 +70,9 @@ func testAccCheckAWSAPIGatewayMockIntegrationAttributes(conf *apigateway.Integra
if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'updated'" {
return fmt.Errorf("wrong updated RequestParameters for header.X-Authorization")
}
if *conf.ContentHandling != "CONVERT_TO_BINARY" {
return fmt.Errorf("wrong ContentHandling: %q", *conf.ContentHandling)
}
return nil
}
}
Expand Down Expand Up @@ -232,6 +239,7 @@ resource "aws_api_gateway_integration" "test" {
type = "MOCK"
passthrough_behavior = "NEVER"
content_handling = "CONVERT_TO_BINARY"
}
`
20 changes: 18 additions & 2 deletions builtin/providers/aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
Optional: true,
},

"binary_media_types": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"root_resource_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -51,10 +58,18 @@ func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}
if d.Get("description").(string) != "" {
description = aws.String(d.Get("description").(string))
}
gateway, err := conn.CreateRestApi(&apigateway.CreateRestApiInput{

params := &apigateway.CreateRestApiInput{
Name: aws.String(d.Get("name").(string)),
Description: description,
})
}

binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types")
if binaryMediaTypesOk {
params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{}))
}

gateway, err := conn.CreateRestApi(params)
if err != nil {
return fmt.Errorf("Error creating API Gateway: %s", err)
}
Expand Down Expand Up @@ -105,6 +120,7 @@ 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 err := d.Set("created_date", api.CreatedDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting created_date: %s", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
"aws_api_gateway_rest_api.test", "description", ""),
resource.TestCheckResourceAttrSet(
"aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "binary_media_types", ""),
),
},

Expand All @@ -44,6 +46,10 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
"aws_api_gateway_rest_api.test", "description", "test"),
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"),
),
},
},
Expand Down Expand Up @@ -135,5 +141,6 @@ const testAccAWSAPIGatewayRestAPIUpdateConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
description = "test"
binary_media_types = ["application/octet-stream"]
}
`
16 changes: 16 additions & 0 deletions builtin/providers/aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,22 @@ func validateApiGatewayIntegrationType(v interface{}, k string) (ws []string, er
return
}

func validateApiGatewayIntegrationContentHandling(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

validTypes := map[string]bool{
"CONVERT_TO_BINARY": true,
"CONVERT_TO_TEXT": true,
}

if _, ok := validTypes[value]; !ok {
errors = append(errors, fmt.Errorf(
"%q contains an invalid integration type %q. Valid types are either %q or %q.",
k, value, "CONVERT_TO_BINARY", "CONVERT_TO_TEXT"))
}
return
}

func validateSQSQueueName(v interface{}, k string) (errors []error) {
value := v.(string)
if len(value) > 80 {
Expand Down

0 comments on commit 0e6b2a4

Please sign in to comment.