Skip to content

Commit

Permalink
waf_{group,package,rule}: simplify error check handling
Browse files Browse the repository at this point in the history
Removes custom Terraform handler for error codes and instead relies on some
changes from cloudflare/cloudflare-go#616 to make it more user friendly.
  • Loading branch information
jacobbednarz committed Apr 11, 2021
1 parent fb26394 commit ac123e9
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 69 deletions.
11 changes: 1 addition & 10 deletions cloudflare/resource_cloudflare_waf_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

const CLOUDFLARE_INVALID_OR_REMOVED_WAF_RULE_SET_ID_ERROR = 1003

func resourceCloudflareWAFGroup() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareWAFGroupCreate,
Expand Down Expand Up @@ -53,13 +51,6 @@ func resourceCloudflareWAFGroup() *schema.Resource {
}
}

func errorIsWAFGroupNotFound(err error) bool {
return cloudflareErrorIsOneOfCodes(err, []int{
CLOUDFLARE_INVALID_OR_REMOVED_WAF_PACKAGE_ID_ERROR,
CLOUDFLARE_INVALID_OR_REMOVED_WAF_RULE_SET_ID_ERROR,
})
}

func resourceCloudflareWAFGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

Expand All @@ -69,7 +60,7 @@ func resourceCloudflareWAFGroupRead(d *schema.ResourceData, meta interface{}) er

group, err := client.WAFGroup(context.Background(), zoneID, packageID, groupID)
if err != nil {
if errorIsWAFGroupNotFound(err) {
if err.(*cloudflare.APIRequestError).InternalErrorCodeIs(1002) || err.(*cloudflare.APIRequestError).InternalErrorCodeIs(1003) {
d.SetId("")
return nil
}
Expand Down
10 changes: 1 addition & 9 deletions cloudflare/resource_cloudflare_waf_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

const CLOUDFLARE_INVALID_OR_REMOVED_WAF_PACKAGE_ID_ERROR = 1002

func resourceCloudflareWAFPackage() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareWAFPackageCreate,
Expand Down Expand Up @@ -54,12 +52,6 @@ func resourceCloudflareWAFPackage() *schema.Resource {
}
}

func errorIsWAFPackageNotFound(err error) bool {
return cloudflareErrorIsOneOfCodes(err, []int{
CLOUDFLARE_INVALID_OR_REMOVED_WAF_PACKAGE_ID_ERROR,
})
}

func resourceCloudflareWAFPackageRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

Expand All @@ -68,7 +60,7 @@ func resourceCloudflareWAFPackageRead(d *schema.ResourceData, meta interface{})

pkg, err := client.WAFPackage(context.Background(), zoneID, packageID)
if err != nil {
if errorIsWAFPackageNotFound(err) {
if err.(*cloudflare.APIRequestError).InternalErrorCodeIs(1002) {
d.SetId("")
return nil
}
Expand Down
11 changes: 1 addition & 10 deletions cloudflare/resource_cloudflare_waf_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

const CLOUDFLARE_INVALID_OR_REMOVED_WAF_RULE_ID_ERROR = 1004

func resourceCloudflareWAFRule() *schema.Resource {
return &schema.Resource{
Create: resourceCloudflareWAFRuleCreate,
Expand Down Expand Up @@ -53,13 +51,6 @@ func resourceCloudflareWAFRule() *schema.Resource {
}
}

func errorIsWAFRuleNotFound(err error) bool {
return cloudflareErrorIsOneOfCodes(err, []int{
CLOUDFLARE_INVALID_OR_REMOVED_WAF_PACKAGE_ID_ERROR,
CLOUDFLARE_INVALID_OR_REMOVED_WAF_RULE_ID_ERROR,
})
}

func resourceCloudflareWAFRuleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudflare.API)

Expand All @@ -69,7 +60,7 @@ func resourceCloudflareWAFRuleRead(d *schema.ResourceData, meta interface{}) err

rule, err := client.WAFRule(context.Background(), zoneID, packageID, ruleID)
if err != nil {
if errorIsWAFRuleNotFound(err) {
if err.(*cloudflare.APIRequestError).InternalErrorCodeIs(1002) || err.(*cloudflare.APIRequestError).InternalErrorCodeIs(1004) {
d.SetId("")
return nil
}
Expand Down
40 changes: 0 additions & 40 deletions cloudflare/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package cloudflare
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"log"
"reflect"
"regexp"
"sort"
"strings"

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

func expandInterfaceToStringList(list interface{}) []string {
Expand Down Expand Up @@ -113,43 +110,6 @@ func findIndex(a []interface{}, x interface{}) (int, bool) {
return 0, false
}

type CloudflareAPIError struct {
Code int `json:"code"`
Message string `json:"message"`
}

type CloudflareAPIErrorResponse struct {
Errors []CloudflareAPIError `json:"errors"`
}

func cloudflareErrorIsOneOfCodes(err error, codes []int) bool {
errorMsg := errors.Cause(err).Error()

// We will parse the error message only if it's an error 400, in which
// case we need to verify the kind of error.
r := regexp.MustCompile(`^HTTP status 400: content "(.*)"$`)
submatchs := r.FindStringSubmatch(errorMsg)
if submatchs != nil {
jsonData := strings.Replace(submatchs[1], "\\\"", "\"", -1)
log.Printf("[DEBUG][cloudflareErrorIsCode] error matching status 400, content: %#v", jsonData)

var cfer CloudflareAPIErrorResponse
unmarshalErr := json.Unmarshal([]byte(jsonData), &cfer)

// We check that there is only one error and that its code
// matches what we expected
if unmarshalErr == nil && len(cfer.Errors) == 1 {
for _, code := range codes {
if cfer.Errors[0].Code == code {
return true
}
}
}
}

return false
}

func boolFromString(status string) bool {
if status == "on" {
return true
Expand Down

0 comments on commit ac123e9

Please sign in to comment.