Skip to content

Commit

Permalink
Merge pull request #848 from jacobbednarz/validate-filter-expression-…
Browse files Browse the repository at this point in the history
…in-schema

filter: Improve validation of expression
  • Loading branch information
jacobbednarz authored Oct 28, 2020
2 parents c361b4c + 20dc090 commit 55cf727
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 55cf727

Please sign in to comment.