Skip to content

Commit

Permalink
Use helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
pvanbuijtene committed Mar 15, 2018
1 parent cc2d84c commit 61478f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
34 changes: 14 additions & 20 deletions aws/resource_aws_wafregional_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/service/wafregional"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsWafRegionalRule() *schema.Resource {
Expand Down Expand Up @@ -38,28 +40,20 @@ func resourceAwsWafRegionalRule() *schema.Resource {
Required: true,
},
"data_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 128 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters", k))
}
return
},
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if value != "IPMatch" && value != "ByteMatch" && value != "SqlInjectionMatch" && value != "SizeConstraint" && value != "XssMatch" {
errors = append(errors, fmt.Errorf(
"%q must be one of IPMatch | ByteMatch | SqlInjectionMatch | SizeConstraint | XssMatch", k))
}
return
},
ValidateFunc: validation.StringInSlice([]string{
"IPMatch",
"ByteMatch",
"SqlInjectionMatch",
"SizeConstraint",
"XssMatch",
}, false),
},
},
},
Expand Down Expand Up @@ -99,7 +93,7 @@ func resourceAwsWafRegionalRuleRead(d *schema.ResourceData, meta interface{}) er

resp, err := conn.GetRule(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "WAFNonexistentItemException" {
if isAWSErr(err, wafregional.ErrCodeWAFNonexistentItemException, "") {
log.Printf("[WARN] WAF Rule (%s) not found, error code (404)", d.Id())
d.SetId("")
return nil
Expand Down
4 changes: 3 additions & 1 deletion aws/resource_aws_wafregional_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/wafregional"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

Expand Down Expand Up @@ -159,7 +161,7 @@ func testAccCheckAWSWafRegionalRuleDestroy(s *terraform.State) error {

// Return nil if the Rule is already destroyed
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "WAFNonexistentItemException" {
if isAWSErr(awsErr, wafregional.ErrCodeWAFNonexistentItemException, "") {
return nil
}
}
Expand Down

0 comments on commit 61478f2

Please sign in to comment.