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

Revert schema based filter validation #863

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
19 changes: 0 additions & 19 deletions cloudflare/resource_cloudflare_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"fmt"
"html"
"log"
"os"
"strings"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/pkg/errors"
)

func resourceCloudflareFilter() *schema.Resource {
Expand Down Expand Up @@ -39,23 +37,6 @@ 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 {
errs = append(errs, errors.New("cloudflare_api_key and cloudflare_email are required for validating filter expressions but they are missing"))
return
}

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
75 changes: 0 additions & 75 deletions cloudflare/resource_cloudflare_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"os"
"regexp"
"testing"

"github.com/cloudflare/cloudflare-go"
Expand Down Expand Up @@ -67,80 +66,6 @@ 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 TestAccFilterMissingCredentials(t *testing.T) {
// Intentionally unset all credentials to trigger the lack of credentials
// check schema validation.
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" {
defer func(apiToken string) {
os.Setenv("CLOUDFLARE_API_TOKEN", apiToken)
}(os.Getenv("CLOUDFLARE_API_TOKEN"))
os.Setenv("CLOUDFLARE_API_TOKEN", "")
}

if os.Getenv("CLOUDFLARE_API_KEY") != "" {
defer func(apiKey string) {
os.Setenv("CLOUDFLARE_API_KEY", apiKey)
}(os.Getenv("CLOUDFLARE_API_KEY"))
os.Setenv("CLOUDFLARE_API_KEY", "")
}

if os.Getenv("CLOUDFLARE_EMAIL") != "" {
defer func(email string) {
os.Setenv("CLOUDFLARE_EMAIL", email)
}(os.Getenv("CLOUDFLARE_EMAIL"))
os.Setenv("CLOUDFLARE_EMAIL", "")
}

rnd := generateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testFilterConfig(rnd, zoneID, "true", "this is notes", "invalid expression"),
ExpectError: regexp.MustCompile("cloudflare_api_key and cloudflare_email are required for validating filter expressions but they are missing"),
},
},
})
}
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