Skip to content

Commit

Permalink
filter: Improve validation of expression
Browse files Browse the repository at this point in the history
While making changes to filters you need to run `apply` to actually find
out whether the expression is valid. This is a slow feedback cycle for
developers and there is an API endpoint that allows you to validate
expressions before using them so it's a no brainer to improve the
validation in the schema.

This updates the `ValidateFunc` for `expression` to call out to the
validation API endpoint and raise those exceptions earlier in the
development cycle.

Fixes #846
  • Loading branch information
jacobbednarz committed Oct 28, 2020
1 parent c361b4c commit 20dc090
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cloudflare/resource_cloudflare_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"html"
"log"
"os"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
Expand Down Expand Up @@ -37,6 +38,22 @@ func resourceCloudflareFilter() *schema.Resource {
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.TrimSpace(new) == old
},
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
// Validating the filter expression doesn't support API tokens (yet!)
// so use API key and email for now. Establishing a new client here
// isn't the best solution either however we don't have the `meta`
// interface available that holds the configured client.
api, err := cloudflare.New(os.Getenv("CLOUDFLARE_API_KEY"), os.Getenv("CLOUDFLARE_EMAIL"))
if err != nil {
log.Fatal(err)
}

expression := val.(string)
if err := api.ValidateFilterExpression(expression); err != nil {
errs = append(errs, fmt.Errorf("filter expression is invalid: %s", err))
}
return
},
},
"description": {
Type: schema.TypeString,
Expand Down
37 changes: 37 additions & 0 deletions cloudflare/resource_cloudflare_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"testing"

"github.com/cloudflare/cloudflare-go"
Expand Down Expand Up @@ -66,6 +67,42 @@ func TestAccFilterSimple(t *testing.T) {
})
}

func TestAccFilterInvalid(t *testing.T) {
rnd := generateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testFilterConfig(rnd, zoneID, "true", "this is notes", "invalid expression"),
ExpectError: regexp.MustCompile("config is invalid: filter expression is invalid"),
},
},
})
}
func TestAccFilterInvalidOver4kbString(t *testing.T) {
rnd := generateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

output := ""
for i := 1; i < 4100; i++ {
output += "x"
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testFilterConfig(rnd, zoneID, "true", "this is notes", output),
ExpectError: regexp.MustCompile("config is invalid: filter expression is invalid"),
},
},
})
}

func testFilterConfig(resourceID, zoneID, paused, description, expression string) string {
return fmt.Sprintf(`
resource "cloudflare_filter" "%[1]s" {
Expand Down

0 comments on commit 20dc090

Please sign in to comment.