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 support for specifying cache key parameters in API Gateway. #893

Merged
merged 4 commits into from
Jun 27, 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
70 changes: 68 additions & 2 deletions aws/resource_aws_api_gateway_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ func resourceAwsApiGatewayIntegration() *schema.Resource {
ForceNew: true,
ValidateFunc: validateApiGatewayIntegrationPassthroughBehavior,
},

"cache_key_parameters": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Optional: true,
},

"cache_namespace": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -150,6 +163,20 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
contentHandling = aws.String(val.(string))
}

var cacheKeyParameters []*string
if v, ok := d.GetOk("cache_key_parameters"); ok {
cacheKeyParameters = expandStringList(v.(*schema.Set).List())
}

var cacheNamespace *string
if cacheKeyParameters != nil {
// Use resource_id unless user provides a custom name
cacheNamespace = aws.String(d.Get("resource_id").(string))
}
if v, ok := d.GetOk("cache_namespace"); ok {
cacheNamespace = aws.String(v.(string))
}

_, err := conn.PutIntegration(&apigateway.PutIntegrationInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
Expand All @@ -160,8 +187,8 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
RequestParameters: aws.StringMap(parameters),
RequestTemplates: aws.StringMap(templates),
Credentials: credentials,
CacheNamespace: nil,
CacheKeyParameters: nil,
CacheNamespace: cacheNamespace,
CacheKeyParameters: cacheKeyParameters,
PassthroughBehavior: passthroughBehavior,
ContentHandling: contentHandling,
})
Expand Down Expand Up @@ -216,6 +243,12 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface
d.Set("content_handling", integration.ContentHandling)
}

d.Set("cache_key_parameters", flattenStringList(integration.CacheKeyParameters))

if integration.CacheNamespace != nil {
d.Set("cache_namespace", integration.CacheNamespace)
}

return nil
}

Expand Down Expand Up @@ -303,6 +336,39 @@ func resourceAwsApiGatewayIntegrationUpdate(d *schema.ResourceData, meta interfa
}
}

if d.HasChange("cache_key_parameters") {
o, n := d.GetChange("cache_key_parameters")

os := o.(*schema.Set)
ns := n.(*schema.Set)

removalList := os.Difference(ns)
for _, v := range removalList.List() {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("remove"),
Path: aws.String(fmt.Sprintf("/cacheKeyParameters/%s", v.(string))),
Value: aws.String(""),
})
}

additionList := ns.Difference(os)
for _, v := range additionList.List() {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("add"),
Path: aws.String(fmt.Sprintf("/cacheKeyParameters/%s", v.(string))),
Value: aws.String(""),
})
}
}

if d.HasChange("cache_namespace") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/cacheNamespace"),
Value: aws.String(d.Get("cache_namespace").(string)),
})
}

params := &apigateway.UpdateIntegrationInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
Expand Down
87 changes: 87 additions & 0 deletions aws/resource_aws_api_gateway_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ func TestAccAWSAPIGatewayIntegration_basic(t *testing.T) {
})
}

func TestAccAWSAPIGatewayIntegration_cache_key_parameters(t *testing.T) {
var conf apigateway.Integration

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayIntegrationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayIntegrationConfigCacheKeyParameters,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayIntegrationExists("aws_api_gateway_integration.test", &conf),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "type", "HTTP"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "integration_http_method", "GET"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "uri", "https://www.google.de"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "passthrough_behavior", "WHEN_NO_MATCH"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "content_handling", "CONVERT_TO_TEXT"),
resource.TestCheckNoResourceAttr("aws_api_gateway_integration.test", "credentials"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.%", "3"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-Authorization", "'static'"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.header.X-Foo", "'Bar'"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_parameters.integration.request.path.param", "method.request.path.param"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "cache_key_parameters.#", "1"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "cache_key_parameters.550492954", "method.request.path.param"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "cache_namespace", "foobar"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.%", "2"),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/json", ""),
resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"),
),
},
},
})
}

func testAccCheckAWSAPIGatewayIntegrationExists(n string, res *apigateway.Integration) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -279,3 +313,56 @@ resource "aws_api_gateway_integration" "test" {
content_handling = "CONVERT_TO_TEXT"
}
`

const testAccAWSAPIGatewayIntegrationConfigCacheKeyParameters = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}

resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
path_part = "{param}"
}

resource "aws_api_gateway_method" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "GET"
authorization = "NONE"

request_models = {
"application/json" = "Error"
}

request_parameters = {
"method.request.path.param" = true
}
}

resource "aws_api_gateway_integration" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"

request_templates = {
"application/json" = ""
"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
}

request_parameters = {
"integration.request.header.X-Authorization" = "'static'"
"integration.request.header.X-Foo" = "'Bar'"
"integration.request.path.param" = "method.request.path.param"
}

cache_key_parameters = ["method.request.path.param"]
cache_namespace = "foobar"

type = "HTTP"
uri = "https://www.google.de"
integration_http_method = "GET"
passthrough_behavior = "WHEN_NO_MATCH"
content_handling = "CONVERT_TO_TEXT"
}
`