From 9831362ebae1e623f36770b8115985582773ad24 Mon Sep 17 00:00:00 2001 From: Victor Jimenez Date: Fri, 16 Jun 2017 16:47:39 +0200 Subject: [PATCH 1/4] Add support for specifying cache key parameters in API Gateway. --- aws/resource_aws_api_gateway_integration.go | 43 ++++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_api_gateway_integration.go b/aws/resource_aws_api_gateway_integration.go index f782e11ea67..47897fff4c3 100644 --- a/aws/resource_aws_api_gateway_integration.go +++ b/aws/resource_aws_api_gateway_integration.go @@ -101,6 +101,13 @@ 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, + }, }, } } @@ -150,6 +157,11 @@ 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()) + } + _, err := conn.PutIntegration(&apigateway.PutIntegrationInput{ HttpMethod: aws.String(d.Get("http_method").(string)), ResourceId: aws.String(d.Get("resource_id").(string)), @@ -160,8 +172,8 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa RequestParameters: aws.StringMap(parameters), RequestTemplates: aws.StringMap(templates), Credentials: credentials, - CacheNamespace: nil, - CacheKeyParameters: nil, + CacheNamespace: aws.String(d.Get("resource_id").(string)), + CacheKeyParameters: cacheKeyParameters, PassthroughBehavior: passthroughBehavior, ContentHandling: contentHandling, }) @@ -216,6 +228,8 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface d.Set("content_handling", integration.ContentHandling) } + d.Set("cache_key_parameters", flattenStringList(integration.CacheKeyParameters)) + return nil } @@ -303,6 +317,31 @@ 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(""), + }) + } + } + params := &apigateway.UpdateIntegrationInput{ HttpMethod: aws.String(d.Get("http_method").(string)), ResourceId: aws.String(d.Get("resource_id").(string)), From 0c0d08920bb91b65b6d27c92c0745048c812e637 Mon Sep 17 00:00:00 2001 From: Victor Jimenez Date: Mon, 19 Jun 2017 11:17:04 +0200 Subject: [PATCH 2/4] Add acceptance test. --- ...source_aws_api_gateway_integration_test.go | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/aws/resource_aws_api_gateway_integration_test.go b/aws/resource_aws_api_gateway_integration_test.go index ff9c2338719..7e9b64af7af 100644 --- a/aws/resource_aws_api_gateway_integration_test.go +++ b/aws/resource_aws_api_gateway_integration_test.go @@ -89,6 +89,28 @@ func TestAccAWSAPIGatewayIntegration_basic(t *testing.T) { resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"), ), }, + + { + 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", "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{ }"), + ), + }, }, }) } @@ -279,3 +301,55 @@ 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"] + + type = "HTTP" + uri = "https://www.google.de" + integration_http_method = "GET" + passthrough_behavior = "WHEN_NO_MATCH" + content_handling = "CONVERT_TO_TEXT" +} +` From ee82eec1972a93b8412d69fa1e687635e1d0dddc Mon Sep 17 00:00:00 2001 From: Victor Jimenez Date: Tue, 27 Jun 2017 00:20:02 +0200 Subject: [PATCH 3/4] Add cache_namespace parameter; test refactoring. --- aws/resource_aws_api_gateway_integration.go | 28 ++++++++++++++++++- ...source_aws_api_gateway_integration_test.go | 13 +++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_api_gateway_integration.go b/aws/resource_aws_api_gateway_integration.go index 47897fff4c3..e51558e57ef 100644 --- a/aws/resource_aws_api_gateway_integration.go +++ b/aws/resource_aws_api_gateway_integration.go @@ -108,6 +108,11 @@ func resourceAwsApiGatewayIntegration() *schema.Resource { Set: schema.HashString, Optional: true, }, + + "cache_namespace": { + Type: schema.TypeString, + Optional: true, + }, }, } } @@ -162,6 +167,15 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa 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)), @@ -172,7 +186,7 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa RequestParameters: aws.StringMap(parameters), RequestTemplates: aws.StringMap(templates), Credentials: credentials, - CacheNamespace: aws.String(d.Get("resource_id").(string)), + CacheNamespace: cacheNamespace, CacheKeyParameters: cacheKeyParameters, PassthroughBehavior: passthroughBehavior, ContentHandling: contentHandling, @@ -230,6 +244,10 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface d.Set("cache_key_parameters", flattenStringList(integration.CacheKeyParameters)) + if integration.CacheNamespace != nil { + d.Set("cache_namespace", integration.CacheNamespace) + } + return nil } @@ -342,6 +360,14 @@ func resourceAwsApiGatewayIntegrationUpdate(d *schema.ResourceData, meta interfa } } + 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)), diff --git a/aws/resource_aws_api_gateway_integration_test.go b/aws/resource_aws_api_gateway_integration_test.go index 7e9b64af7af..2b72f806a43 100644 --- a/aws/resource_aws_api_gateway_integration_test.go +++ b/aws/resource_aws_api_gateway_integration_test.go @@ -89,7 +89,18 @@ func TestAccAWSAPIGatewayIntegration_basic(t *testing.T) { resource.TestCheckResourceAttr("aws_api_gateway_integration.test", "request_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"), ), }, + }, + }) +} + +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( @@ -106,6 +117,7 @@ func TestAccAWSAPIGatewayIntegration_basic(t *testing.T) { 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{ }"), @@ -345,6 +357,7 @@ resource "aws_api_gateway_integration" "test" { } cache_key_parameters = ["method.request.path.param"] + cache_namespace = "foobar" type = "HTTP" uri = "https://www.google.de" From f817e9775e37788e962720b72ff91b3217ca1134 Mon Sep 17 00:00:00 2001 From: Victor Jimenez Date: Tue, 27 Jun 2017 22:04:34 +0200 Subject: [PATCH 4/4] Add 'computed' flag to cache_namespace field. --- aws/resource_aws_api_gateway_integration.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/resource_aws_api_gateway_integration.go b/aws/resource_aws_api_gateway_integration.go index e51558e57ef..351e55c5ee1 100644 --- a/aws/resource_aws_api_gateway_integration.go +++ b/aws/resource_aws_api_gateway_integration.go @@ -112,6 +112,7 @@ func resourceAwsApiGatewayIntegration() *schema.Resource { "cache_namespace": { Type: schema.TypeString, Optional: true, + Computed: true, }, }, }