Skip to content

Commit 495c4b3

Browse files
jrnt30stack72
authored andcommitted
provider/aws - CloudFront custom_error_response fixes for missing (hashicorp#6382)
* provider/aws - CloudFront custom_error_response fixes for missing - Omit custom_error_response response_* fields when not explicitly set via config for SDK call - Adding a test case to ensure that the response_error gets converted to an empty string properly, versus "0". (Thanks @vancluever) Fixes hashicorp#6342 * - Fixing ACC test case resource names
1 parent 63514d2 commit 495c4b3

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎builtin/providers/aws/cloudfront_distribution_configuration_structure.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,15 @@ func expandCustomErrorResponse(m map[string]interface{}) *cloudfront.CustomError
719719
if v, ok := m["error_caching_min_ttl"]; ok {
720720
er.ErrorCachingMinTTL = aws.Int64(int64(v.(int)))
721721
}
722-
if v, ok := m["response_code"]; ok {
722+
if v, ok := m["response_code"]; ok && v.(int) != 0 {
723723
er.ResponseCode = aws.String(strconv.Itoa(v.(int)))
724+
} else {
725+
er.ResponseCode = aws.String("")
724726
}
725727
if v, ok := m["response_page_path"]; ok {
726728
er.ResponsePagePath = aws.String(v.(string))
727729
}
730+
728731
return &er
729732
}
730733

‎builtin/providers/aws/cloudfront_distribution_configuration_structure_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ func customErrorResponsesConfFirst() map[string]interface{} {
191191
return customErrorResponsesConf()[0].(map[string]interface{})
192192
}
193193

194+
func customErrorResponseConfNoResponseCode() map[string]interface{} {
195+
er := customErrorResponsesConf()[0].(map[string]interface{})
196+
er["response_code"] = 0
197+
er["response_page_path"] = ""
198+
return er
199+
}
200+
194201
func viewerCertificateConfSetCloudFrontDefault() map[string]interface{} {
195202
return map[string]interface{}{
196203
"acm_certificate_arn": "",
@@ -759,6 +766,17 @@ func TestCloudFrontStructure_expandCustomErrorResponse(t *testing.T) {
759766
}
760767
}
761768

769+
func TestCloudFrontStructure_expandCustomErrorResponse_emptyResponseCode(t *testing.T) {
770+
data := customErrorResponseConfNoResponseCode()
771+
er := expandCustomErrorResponse(data)
772+
if *er.ResponseCode != "" {
773+
t.Fatalf("Expected ResponseCode to be empty string, got %v", *er.ResponseCode)
774+
}
775+
if *er.ResponsePagePath != "" {
776+
t.Fatalf("Expected ResponsePagePath to be empty string, got %v", *er.ResponsePagePath)
777+
}
778+
}
779+
762780
func TestCloudFrontStructure_flattenCustomErrorResponse(t *testing.T) {
763781
in := customErrorResponsesConfFirst()
764782
er := expandCustomErrorResponse(in)

‎builtin/providers/aws/resource_aws_cloudfront_distribution_test.go‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ func TestAccAWSCloudFrontDistribution_noOptionalItemsConfig(t *testing.T) {
105105
})
106106
}
107107

108+
func TestAccAWSCloudFrontDistribution_noCustomErrorResponseConfig(t *testing.T) {
109+
resource.Test(t, resource.TestCase{
110+
PreCheck: func() { testAccPreCheck(t) },
111+
Providers: testAccProviders,
112+
CheckDestroy: testAccCheckCloudFrontDistributionDestroy,
113+
Steps: []resource.TestStep{
114+
resource.TestStep{
115+
Config: testAccAWSCloudFrontDistributionNoCustomErroResponseInfo,
116+
Check: resource.ComposeTestCheckFunc(
117+
testAccCheckCloudFrontDistributionExistence(
118+
"aws_cloudfront_distribution.no_custom_error_responses",
119+
),
120+
),
121+
},
122+
},
123+
})
124+
}
125+
108126
func testAccCheckCloudFrontDistributionDestroy(s *terraform.State) error {
109127
for k, rs := range s.RootModule().Resources {
110128
if rs.Type != "aws_cloudfront_distribution" {
@@ -373,6 +391,57 @@ resource "aws_cloudfront_distribution" "multi_origin_distribution" {
373391
}
374392
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())
375393

394+
var testAccAWSCloudFrontDistributionNoCustomErroResponseInfo = fmt.Sprintf(`
395+
variable rand_id {
396+
default = %d
397+
}
398+
399+
resource "aws_cloudfront_distribution" "no_custom_error_responses" {
400+
origin {
401+
domain_name = "www.example.com"
402+
origin_id = "myCustomOrigin"
403+
custom_origin_config {
404+
http_port = 80
405+
https_port = 443
406+
origin_protocol_policy = "http-only"
407+
origin_ssl_protocols = [ "SSLv3", "TLSv1" ]
408+
}
409+
}
410+
enabled = true
411+
comment = "Some comment"
412+
default_cache_behavior {
413+
allowed_methods = [ "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT" ]
414+
cached_methods = [ "GET", "HEAD" ]
415+
target_origin_id = "myCustomOrigin"
416+
smooth_streaming = false
417+
forwarded_values {
418+
query_string = false
419+
cookies {
420+
forward = "all"
421+
}
422+
}
423+
viewer_protocol_policy = "allow-all"
424+
min_ttl = 0
425+
default_ttl = 3600
426+
max_ttl = 86400
427+
}
428+
custom_error_response {
429+
error_code = 404
430+
error_caching_min_ttl = 30
431+
}
432+
restrictions {
433+
geo_restriction {
434+
restriction_type = "whitelist"
435+
locations = [ "US", "CA", "GB", "DE" ]
436+
}
437+
}
438+
viewer_certificate {
439+
cloudfront_default_certificate = true
440+
}
441+
%s
442+
}
443+
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int(), testAccAWSCloudFrontDistributionRetainConfig())
444+
376445
var testAccAWSCloudFrontDistributionNoOptionalItemsConfig = fmt.Sprintf(`
377446
variable rand_id {
378447
default = %d

0 commit comments

Comments
 (0)