Skip to content

Commit

Permalink
filter: Propagate schema validation error to end user
Browse files Browse the repository at this point in the history
cloudflare#848 introduced better validation for filters using the Terraform
schema. Despite handling the error, the error wasn't returned to the end
user and instead the RPC exception is shown the end user.

```
Error: rpc error: code = Unavailable desc = transport is closing
```

This fixes the user experience by returning the errors which now passes
the exception onto the end user instead of swallowing it or causing a
panic.
  • Loading branch information
jacobbednarz committed Nov 5, 2020
1 parent 22c22ec commit c3010c0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cloudflare/resource_cloudflare_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
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 @@ -45,7 +46,8 @@ func resourceCloudflareFilter() *schema.Resource {
// 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)
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)
Expand Down
38 changes: 38 additions & 0 deletions cloudflare/resource_cloudflare_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ func TestAccFilterInvalid(t *testing.T) {
},
})
}

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")
Expand Down

0 comments on commit c3010c0

Please sign in to comment.