Skip to content

Commit

Permalink
Merge pull request #1746 from orium/dsousa/FLPROD-397-fix-redirect-up…
Browse files Browse the repository at this point in the history
…date-default-values

Fix default values for redirect list updates.
  • Loading branch information
jacobbednarz committed Jul 7, 2022
2 parents e57e327 + 1d2dfdb commit 772767e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .changelog/1746.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_list: fix default values for redirect list updates
```
16 changes: 8 additions & 8 deletions docs/resources/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ resource "cloudflare_list" "example" {
redirect {
source_url = "example.com/foo"
target_url = "https://foo.example.com"
include_subdomains = true
subpath_matching = true
include_subdomains = "enabled"
subpath_matching = "enabled"
status_code = 301
preserve_query_string = true
preserve_path_suffix = false
preserve_query_string = "enabled"
preserve_path_suffix = "disabled"
}
}
comment = "two"
Expand Down Expand Up @@ -114,11 +114,11 @@ Required:

Optional:

- `include_subdomains` (Boolean) Whether the redirect also matches subdomains of the source url.
- `preserve_path_suffix` (Boolean) Whether to preserve the path suffix when doing subpath matching.
- `preserve_query_string` (Boolean) Whether the redirect target url should keep the query string of the request's url.
- `include_subdomains` (String) Whether the redirect also matches subdomains of the source url. Available values: `disabled`, `enabled`.
- `preserve_path_suffix` (String) Whether to preserve the path suffix when doing subpath matching. Available values: `disabled`, `enabled`.
- `preserve_query_string` (String) Whether the redirect target url should keep the query string of the request's url. Available values: `disabled`, `enabled`.
- `status_code` (Number) The status code to be used when redirecting a request.
- `subpath_matching` (Boolean) Whether the redirect also matches subpaths of the source url.
- `subpath_matching` (String) Whether the redirect also matches subpaths of the source url. Available values: `disabled`, `enabled`.

## Import

Expand Down
8 changes: 4 additions & 4 deletions examples/resources/cloudflare_list/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ resource "cloudflare_list" "example" {
redirect {
source_url = "example.com/foo"
target_url = "https://foo.example.com"
include_subdomains = true
subpath_matching = true
include_subdomains = "enabled"
subpath_matching = "enabled"
status_code = 301
preserve_query_string = true
preserve_path_suffix = false
preserve_query_string = "enabled"
preserve_path_suffix = "disabled"
}
}
comment = "two"
Expand Down
65 changes: 41 additions & 24 deletions internal/provider/resource_cloudflare_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,30 @@ func resourceCloudflareListRead(ctx context.Context, d *schema.ResourceData, met
value["ip"] = *i.IP
}
if i.Redirect != nil {
optBoolToString := func(b *bool) string {
if b != nil {
switch *b {
case true:
return "enabled"
case false:
return "disabled"
}
}
return ""
}
statusCode := 0
if i.Redirect.StatusCode != nil {
statusCode = *i.Redirect.StatusCode
}

value["redirect"] = []map[string]interface{}{{
"source_url": i.Redirect.SourceUrl,
"include_subdomains": i.Redirect.IncludeSubdomains,
"include_subdomains": optBoolToString(i.Redirect.IncludeSubdomains),
"target_url": i.Redirect.TargetUrl,
"status_code": i.Redirect.StatusCode,
"preserve_query_string": i.Redirect.PreserveQueryString,
"subpath_matching": i.Redirect.SubpathMatching,
"preserve_path_suffix": i.Redirect.PreservePathSuffix,
"status_code": statusCode,
"preserve_query_string": optBoolToString(i.Redirect.PreserveQueryString),
"subpath_matching": optBoolToString(i.Redirect.SubpathMatching),
"preserve_path_suffix": optBoolToString(i.Redirect.PreservePathSuffix),
}}
}

Expand Down Expand Up @@ -185,15 +201,15 @@ func buildListItemsCreateRequest(resource *schema.ResourceData, items []interfac
for i, item := range items {
value := item.(map[string]interface{})["value"].([]interface{})[0].(map[string]interface{})

_, hasIP := resource.GetOkExists(fmt.Sprintf("item.%d.value.0.ip", i))
_, hasIP := resource.GetOk(fmt.Sprintf("item.%d.value.0.ip", i))

var ip *string = nil
if hasIP {
maybeIP := value["ip"].(string)
ip = &maybeIP
}

_, hasRedirect := resource.GetOkExists(fmt.Sprintf("item.%d.value.0.redirect", i))
_, hasRedirect := resource.GetOk(fmt.Sprintf("item.%d.value.0.redirect", i))

var redirect *cloudflare.Redirect = nil
if hasRedirect {
Expand All @@ -208,27 +224,28 @@ func buildListItemsCreateRequest(resource *schema.ResourceData, items []interfac
var preserveQueryString *bool = nil
var preservePathSuffix *bool = nil

hasField := func(field string) bool {
_, has := resource.GetOkExists(fmt.Sprintf("item.%d.value.0.redirect.0.%s", i, field))
return has
stringToOptBool := func(s string) *bool {
switch s {
case "enabled":
return cloudflare.BoolPtr(true)
case "disabled":
return cloudflare.BoolPtr(false)
default:
return nil
}
}

if hasField("include_subdomains") {
includeSubdomains = cloudflare.BoolPtr(r["include_subdomains"].(bool))
}
if hasField("subpath_matching") {
subpathMatching = cloudflare.BoolPtr(r["subpath_matching"].(bool))
}
if hasField("status_code") {
statusCode = cloudflare.IntPtr(r["status_code"].(int))
}
if hasField("preserve_query_string") {
preserveQueryString = cloudflare.BoolPtr(r["preserve_query_string"].(bool))
}
if hasField("preserve_path_suffix") {
preservePathSuffix = cloudflare.BoolPtr(r["preserve_path_suffix"].(bool))
includeSubdomains = stringToOptBool(r["include_subdomains"].(string))
subpathMatching = stringToOptBool(r["subpath_matching"].(string))

vint := r["status_code"].(int)
if vint != 0 {
statusCode = cloudflare.IntPtr(vint)
}

preserveQueryString = stringToOptBool(r["preserve_query_string"].(string))
preservePathSuffix = stringToOptBool(r["preserve_path_suffix"].(string))

redirect = &cloudflare.Redirect{
SourceUrl: sourceUrl,
IncludeSubdomains: includeSubdomains,
Expand Down
47 changes: 41 additions & 6 deletions internal/provider/resource_cloudflare_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ func TestAccCloudflareList_Update(t *testing.T) {
resource.TestCheckResourceAttr(nameRedirect, "item.#", "2"),
),
},
{
PreConfig: func() {
initialID = list.ID
},
Config: testAccCheckCloudflareListRedirectUpdateTargetUrl(rndRedirect, rndRedirect, rndRedirect, accountID),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareListExists(nameRedirect, &list),
func(state *terraform.State) error {
if initialID != list.ID {
return fmt.Errorf("wanted update but List got recreated (id changed %q -> %q)", initialID, list.ID)
}
return nil
},
resource.TestCheckResourceAttr(nameRedirect, "item.#", "1"),
),
},
},
})
}
Expand Down Expand Up @@ -252,8 +268,8 @@ func testAccCheckCloudflareListRedirectUpdate(ID, name, description, accountID s
redirect {
source_url = "cloudflare.com/blog"
target_url = "https://blog.cloudflare.com"
}
}
}
}
comment = "one"
}
Expand All @@ -262,14 +278,33 @@ func testAccCheckCloudflareListRedirectUpdate(ID, name, description, accountID s
redirect {
source_url = "cloudflare.com/foo"
target_url = "https://foo.cloudflare.com"
include_subdomains = true
subpath_matching = true
include_subdomains = "enabled"
subpath_matching = "enabled"
status_code = 301
preserve_query_string = true
preserve_path_suffix = false
preserve_query_string = "enabled"
preserve_path_suffix = "disabled"
}
}
comment = "two"
}
}`, ID, name, description, accountID)
}

func testAccCheckCloudflareListRedirectUpdateTargetUrl(ID, name, description, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_list" "%[1]s" {
account_id = "%[4]s"
name = "%[2]s"
description = "%[3]s"
kind = "redirect"
item {
value {
redirect {
source_url = "cloudflare.com/blog"
target_url = "https://theblog.cloudflare.com"
}
}
}
}`, ID, name, description, accountID)
}
29 changes: 17 additions & 12 deletions internal/provider/schema_cloudflare_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -69,29 +70,33 @@ var listItemElem = &schema.Resource{
Required: true,
},
"include_subdomains": {
Description: "Whether the redirect also matches subdomains of the source url.",
Type: schema.TypeBool,
Optional: true,
Description: fmt.Sprintf("Whether the redirect also matches subdomains of the source url. %s", renderAvailableDocumentationValuesStringSlice([]string{"disabled", "enabled"})),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"disabled", "enabled"}, false),
},
"subpath_matching": {
Description: "Whether the redirect also matches subpaths of the source url.",
Type: schema.TypeBool,
Optional: true,
Description: fmt.Sprintf("Whether the redirect also matches subpaths of the source url. %s", renderAvailableDocumentationValuesStringSlice([]string{"disabled", "enabled"})),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"disabled", "enabled"}, false),
},
"status_code": {
Description: "The status code to be used when redirecting a request.",
Type: schema.TypeInt,
Optional: true,
},
"preserve_query_string": {
Description: "Whether the redirect target url should keep the query string of the request's url.",
Type: schema.TypeBool,
Optional: true,
Description: fmt.Sprintf("Whether the redirect target url should keep the query string of the request's url. %s", renderAvailableDocumentationValuesStringSlice([]string{"disabled", "enabled"})),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"disabled", "enabled"}, false),
},
"preserve_path_suffix": {
Description: "Whether to preserve the path suffix when doing subpath matching.",
Type: schema.TypeBool,
Optional: true,
Description: fmt.Sprintf("Whether to preserve the path suffix when doing subpath matching. %s", renderAvailableDocumentationValuesStringSlice([]string{"disabled", "enabled"})),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"disabled", "enabled"}, false),
},
},
},
Expand Down

0 comments on commit 772767e

Please sign in to comment.