From 736a2ddda05411a025d9c639d004672e9b47c751 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 16 Nov 2021 08:24:46 -0500 Subject: [PATCH 1/2] r/aws_cloudfront_cache_policy: Fix support for 0 values of 'default_ttl', 'max_ttl' and 'min_ttl'. --- internal/service/cloudfront/cache_policy.go | 28 +++----- .../service/cloudfront/cache_policy_test.go | 69 +++++++++++++++++++ 2 files changed, 77 insertions(+), 20 deletions(-) diff --git a/internal/service/cloudfront/cache_policy.go b/internal/service/cloudfront/cache_policy.go index df8960740ff..7a92ca028b6 100644 --- a/internal/service/cloudfront/cache_policy.go +++ b/internal/service/cloudfront/cache_policy.go @@ -161,22 +161,16 @@ func resourceCachePolicyCreate(d *schema.ResourceData, meta interface{}) error { name := d.Get("name").(string) apiObject := &cloudfront.CachePolicyConfig{ - MinTTL: aws.Int64(int64(d.Get("min_ttl").(int))), - Name: aws.String(name), + DefaultTTL: aws.Int64(int64(d.Get("default_ttl").(int))), + MaxTTL: aws.Int64(int64(d.Get("max_ttl").(int))), + MinTTL: aws.Int64(int64(d.Get("min_ttl").(int))), + Name: aws.String(name), } if v, ok := d.GetOk("comment"); ok { apiObject.Comment = aws.String(v.(string)) } - if v, ok := d.GetOk("default_ttl"); ok { - apiObject.DefaultTTL = aws.Int64(int64(v.(int))) - } - - if v, ok := d.GetOk("max_ttl"); ok { - apiObject.MaxTTL = aws.Int64(int64(v.(int))) - } - if v, ok := d.GetOk("parameters_in_cache_key_and_forwarded_to_origin"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { apiObject.ParametersInCacheKeyAndForwardedToOrigin = expandParametersInCacheKeyAndForwardedToOrigin(v.([]interface{})[0].(map[string]interface{})) } @@ -238,22 +232,16 @@ func resourceCachePolicyUpdate(d *schema.ResourceData, meta interface{}) error { // "When you update a cache policy configuration, all the fields are updated with the values provided in the request. You cannot update some fields independent of others." // apiObject := &cloudfront.CachePolicyConfig{ - MinTTL: aws.Int64(int64(d.Get("min_ttl").(int))), - Name: aws.String(d.Get("name").(string)), + DefaultTTL: aws.Int64(int64(d.Get("default_ttl").(int))), + MaxTTL: aws.Int64(int64(d.Get("max_ttl").(int))), + MinTTL: aws.Int64(int64(d.Get("min_ttl").(int))), + Name: aws.String(d.Get("name").(string)), } if v, ok := d.GetOk("comment"); ok { apiObject.Comment = aws.String(v.(string)) } - if v, ok := d.GetOk("default_ttl"); ok { - apiObject.DefaultTTL = aws.Int64(int64(v.(int))) - } - - if v, ok := d.GetOk("max_ttl"); ok { - apiObject.MaxTTL = aws.Int64(int64(v.(int))) - } - if v, ok := d.GetOk("parameters_in_cache_key_and_forwarded_to_origin"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil { apiObject.ParametersInCacheKeyAndForwardedToOrigin = expandParametersInCacheKeyAndForwardedToOrigin(v.([]interface{})[0].(map[string]interface{})) } diff --git a/internal/service/cloudfront/cache_policy_test.go b/internal/service/cloudfront/cache_policy_test.go index 0d3eb38d37e..9a9fe41936c 100644 --- a/internal/service/cloudfront/cache_policy_test.go +++ b/internal/service/cloudfront/cache_policy_test.go @@ -160,6 +160,49 @@ func TestAccCloudFrontCachePolicy_Items(t *testing.T) { }) } +func TestAccCloudFrontCachePolicy_ZeroTTLs(t *testing.T) { + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_cloudfront_cache_policy.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(cloudfront.EndpointsID, t) }, + ErrorCheck: acctest.ErrorCheck(t, cloudfront.EndpointsID), + Providers: acctest.Providers, + CheckDestroy: testAccCheckCloudFrontCachePolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCachePolicyZeroTTLsConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontCachePolicyExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "comment", ""), + resource.TestCheckResourceAttr(resourceName, "default_ttl", "0"), + resource.TestCheckResourceAttrSet(resourceName, "etag"), + resource.TestCheckResourceAttr(resourceName, "min_ttl", "0"), + resource.TestCheckResourceAttr(resourceName, "max_ttl", "0"), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.#", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.cookies_config.#", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.cookies_config.0.cookie_behavior", "none"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.cookies_config.0.cookies.#", "0"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.enable_accept_encoding_brotli", "false"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.enable_accept_encoding_gzip", "false"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.headers_config.#", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.headers_config.0.header_behavior", "none"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.headers_config.0.headers.#", "0"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.query_strings_config.#", "1"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.query_strings_config.0.query_string_behavior", "none"), + resource.TestCheckResourceAttr(resourceName, "parameters_in_cache_key_and_forwarded_to_origin.0.query_strings_config.0.query_strings.#", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckCloudFrontCachePolicyDestroy(s *terraform.State) error { conn := acctest.Provider.Meta().(*conns.AWSClient).CloudFrontConn @@ -306,3 +349,29 @@ resource "aws_cloudfront_cache_policy" "test" { } `, rName) } + +func testAccCachePolicyZeroTTLsConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_cloudfront_cache_policy" "test" { + name = %[1]q + + default_ttl = 0 + max_ttl = 0 + min_ttl = 0 + + parameters_in_cache_key_and_forwarded_to_origin { + cookies_config { + cookie_behavior = "none" + } + + headers_config { + header_behavior = "none" + } + + query_strings_config { + query_string_behavior = "none" + } + } +} +`, rName) +} From 1122a79945996fa52a64e0756a72413fb8e4af08 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 16 Nov 2021 08:27:33 -0500 Subject: [PATCH 2/2] Add CHANGELOG entries. --- .changelog/21793.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/21793.txt diff --git a/.changelog/21793.txt b/.changelog/21793.txt new file mode 100644 index 00000000000..1519ccd7c16 --- /dev/null +++ b/.changelog/21793.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_cloudfront_cache_policy: Fix 0 values for `default_ttl`, `max_ttl` and `min_ttl` arguments +``` \ No newline at end of file