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

r/aws_cloudfront_cache_policy: Fix support for 0 values of default_ttl, max_ttl and min_ttl #21793

Merged
merged 2 commits into from
Nov 16, 2021
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
3 changes: 3 additions & 0 deletions .changelog/21793.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cloudfront_cache_policy: Fix 0 values for `default_ttl`, `max_ttl` and `min_ttl` arguments
```
28 changes: 8 additions & 20 deletions internal/service/cloudfront/cache_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
}
Expand Down Expand Up @@ -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{}))
}
Expand Down
69 changes: 69 additions & 0 deletions internal/service/cloudfront/cache_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}