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

forwarding_urls in page rules are lists #79

Merged
merged 2 commits into from
Jul 9, 2018
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
2 changes: 1 addition & 1 deletion cloudflare/resource_cloudflare_page_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions cloudflare/resource_cloudflare_page_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"reflect"
"regexp"

"github.com/cloudflare/cloudflare-go"
Expand Down Expand Up @@ -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),
),
),
},
},
Expand Down Expand Up @@ -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 {
Expand Down