From a5b061e811278262f9b14974fb9e16da3081ed7e Mon Sep 17 00:00:00 2001 From: Travis Beauvais Date: Tue, 3 Jul 2018 14:57:08 -0700 Subject: [PATCH 1/2] forwarding urls are lists --- cloudflare/resource_cloudflare_page_rule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudflare/resource_cloudflare_page_rule.go b/cloudflare/resource_cloudflare_page_rule.go index 127dd2947f..6beec6518f 100644 --- a/cloudflare/resource_cloudflare_page_rule.go +++ b/cloudflare/resource_cloudflare_page_rule.go @@ -422,7 +422,7 @@ func transformFromCloudFlarePageRuleAction(pageRuleAction *cloudflare.PageRuleAc break case pageRuleAction.ID == "forwarding_url": - value = pageRuleAction.Value.(map[string]interface{}) + value = []interface{}{pageRuleAction.Value.(map[string]interface{})} break default: From b8ce4e019f6dcddda78ccc7bd61ae87939723f9e Mon Sep 17 00:00:00 2001 From: Travis Beauvais Date: Mon, 9 Jul 2018 08:55:08 -0700 Subject: [PATCH 2/2] add test for transforming page rule forwarding_url action --- .../resource_cloudflare_page_rule_test.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cloudflare/resource_cloudflare_page_rule_test.go b/cloudflare/resource_cloudflare_page_rule_test.go index 6e9d81a966..53b7be2111 100644 --- a/cloudflare/resource_cloudflare_page_rule_test.go +++ b/cloudflare/resource_cloudflare_page_rule_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "reflect" "regexp" "github.com/cloudflare/cloudflare-go" @@ -83,6 +84,11 @@ func TestAccCloudFlarePageRule_ForwardingOnly(t *testing.T) { "cloudflare_page_rule.test", "zone", zone), resource.TestCheckResourceAttr( "cloudflare_page_rule.test", "target", fmt.Sprintf("%s/", target)), + resource.TestCheckResourceAttr( + "cloudflare_page_rule.test", + "actions.0.forwarding_url.0.url", + fmt.Sprintf("http://%s/forward", zone), + ), ), }, }, @@ -187,6 +193,33 @@ func TestAccCloudFlarePageRule_CreateAfterManualDestroy(t *testing.T) { }) } +func TestTranformForwardingURL(t *testing.T) { + key, val, err := transformFromCloudFlarePageRuleAction(&cloudflare.PageRuleAction{ + ID: "forwarding_url", + Value: map[string]interface{}{ + "url": "http://test.com/forward", + "status_code": 302, + }, + }) + if err != nil { + t.Fatalf("Unexpected error transforming page rule action: %s", err) + } + + if key != "forwarding_url" { + t.Fatalf("Unexpected key transforming page rule action. Expected \"forwarding_url\", got \"%s\"", key) + } + + // the transformed value for a forwarding_url should be [{url: "", "status_code": 302}] (single item slice where the + // element in the slice is a map) + if sl, isSlice := val.([]interface{}); !isSlice { + t.Fatalf("Unexpected value type from transforming page rule action. Expected slice, got %s", reflect.TypeOf(val).Kind()) + } else if len(sl) != 1 { + t.Fatalf("Unexpected slice length after transforming page rule action. Expected 1, got %d", len(sl)) + } else if _, isMap := sl[0].(map[string]interface{}); !isMap { + t.Fatalf("Unexpected type in slice after tranforming page rule action. Expected map[string]interface{}, got %s", reflect.TypeOf(sl[0]).Kind()) + } +} + func testAccCheckCloudFlarePageRuleRecreated(before, after *cloudflare.PageRule) resource.TestCheckFunc { return func(s *terraform.State) error { if before.ID == after.ID {