Skip to content

Commit

Permalink
Apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal van Buijtene committed Jun 2, 2020
1 parent c5bea1d commit 43237d8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 61 deletions.
29 changes: 7 additions & 22 deletions aws/resource_aws_wafv2_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ func resourceAwsWafv2RuleGroupRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("Error setting visibility_config: %s", err)
}

tags, err := keyvaluetags.Wafv2ListTags(conn, *resp.RuleGroup.ARN)
arn := aws.StringValue(resp.RuleGroup.ARN)
tags, err := keyvaluetags.Wafv2ListTags(conn, arn)
if err != nil {
return fmt.Errorf("Error listing tags for WAFv2 RuleGroup (%s): %s", *resp.RuleGroup.ARN, err)
return fmt.Errorf("Error listing tags for WAFv2 RuleGroup (%s): %s", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
Expand Down Expand Up @@ -553,7 +554,7 @@ func wafv2FieldToMatchSchema() *schema.Schema {
validation.StringLenBetween(1, 40),
// The value is returned in lower case by the API.
// Trying to solve it with StateFunc and/or DiffSuppressFunc resulted in hash problem of the rule field or didn't work.
validateWafv2StringIsLowerCase(),
validation.StringMatch(regexp.MustCompile(`^[a-z0-9-_]+$`), "must contain only lowercase alphanumeric characters, underscores, and hyphens"),
),
},
},
Expand All @@ -572,7 +573,7 @@ func wafv2FieldToMatchSchema() *schema.Schema {
validation.StringLenBetween(1, 30),
// The value is returned in lower case by the API.
// Trying to solve it with StateFunc and/or DiffSuppressFunc resulted in hash problem of the rule field or didn't work.
validateWafv2StringIsLowerCase(),
validation.StringMatch(regexp.MustCompile(`^[a-z0-9-_]+$`), "must contain only lowercase alphanumeric characters, underscores, and hyphens"),
),
},
},
Expand All @@ -584,22 +585,6 @@ func wafv2FieldToMatchSchema() *schema.Schema {
}
}

func validateWafv2StringIsLowerCase() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("Expected type of %s to be string", k))
return
}

if strings.ToLower(v) != v {
es = append(es, fmt.Errorf("Expected value of %s to only contain lower case characters", v))
return
}
return
}
}

func wafv2TextTransformationSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Expand Down Expand Up @@ -1162,7 +1147,7 @@ func flattenWafv2ByteMatchStatement(b *wafv2.ByteMatchStatement) interface{} {

m := map[string]interface{}{
"field_to_match": flattenWafv2FieldToMatch(b.FieldToMatch),
"positional_constraint": b.PositionalConstraint,
"positional_constraint": aws.StringValue(b.PositionalConstraint),
"search_string": string(b.SearchString),
"text_transformation": flattenWafv2TextTransformations(b.TextTransformations),
}
Expand Down Expand Up @@ -1353,7 +1338,7 @@ func flattenWafv2VisibilityConfig(config *wafv2.VisibilityConfig) interface{} {

m := map[string]interface{}{
"cloudwatch_metrics_enabled": aws.BoolValue(config.CloudWatchMetricsEnabled),
"metric_name": aws.String(*config.MetricName),
"metric_name": aws.StringValue(config.MetricName),
"sampled_requests_enabled": aws.BoolValue(config.SampledRequestsEnabled),
}

Expand Down
68 changes: 35 additions & 33 deletions aws/resource_aws_wafv2_rule_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func TestAccAwsWafv2RuleGroup_Basic(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -31,14 +31,13 @@ func TestAccAwsWafv2RuleGroup_Basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.metric_name", "friendly-metric-name"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.sampled_requests_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.Tag1", "Value1"),
resource.TestCheckResourceAttr(resourceName, "tags.Tag2", "Value2"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Expand All @@ -50,6 +49,7 @@ func TestAccAwsWafv2RuleGroup_Basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", "Updated"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.metric_name", "friendly-metric-name"),
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestAccAwsWafv2RuleGroup_Basic(t *testing.T) {

func TestAccAwsWafv2RuleGroup_ByteMatchStatement(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestAccAwsWafv2RuleGroup_ByteMatchStatement(t *testing.T) {

func TestAccAwsWafv2RuleGroup_ByteMatchStatement_FieldToMatch(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -292,8 +292,8 @@ func TestAccAwsWafv2RuleGroup_ByteMatchStatement_FieldToMatch(t *testing.T) {

func TestAccAwsWafv2RuleGroup_ChangeNameForceNew(t *testing.T) {
var before, after wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupNewName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
ruleGroupNewName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -309,6 +309,7 @@ func TestAccAwsWafv2RuleGroup_ChangeNameForceNew(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -324,6 +325,7 @@ func TestAccAwsWafv2RuleGroup_ChangeNameForceNew(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupNewName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupNewName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -337,7 +339,7 @@ func TestAccAwsWafv2RuleGroup_ChangeNameForceNew(t *testing.T) {

func TestAccAwsWafv2RuleGroup_ChangeCapacityForceNew(t *testing.T) {
var before, after wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -353,6 +355,7 @@ func TestAccAwsWafv2RuleGroup_ChangeCapacityForceNew(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -361,13 +364,14 @@ func TestAccAwsWafv2RuleGroup_ChangeCapacityForceNew(t *testing.T) {
),
},
{
Config: testAccAwsWafv2RuleGroupConfig_Update_capacity(ruleGroupName),
Config: testAccAwsWafv2RuleGroupConfig_UpdateCapacity(ruleGroupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsWafv2RuleGroupExists(resourceName, &after),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/rulegroup/.+$`)),
resource.TestCheckResourceAttr(resourceName, "capacity", "3"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -381,7 +385,7 @@ func TestAccAwsWafv2RuleGroup_ChangeCapacityForceNew(t *testing.T) {

func TestAccAwsWafv2RuleGroup_ChangeMetricNameForceNew(t *testing.T) {
var before, after wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -397,6 +401,7 @@ func TestAccAwsWafv2RuleGroup_ChangeMetricNameForceNew(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -405,13 +410,14 @@ func TestAccAwsWafv2RuleGroup_ChangeMetricNameForceNew(t *testing.T) {
),
},
{
Config: testAccAwsWafv2RuleGroupConfig_Update_metricName(ruleGroupName),
Config: testAccAwsWafv2RuleGroupConfig_UpdateMetricName(ruleGroupName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsWafv2RuleGroupExists(resourceName, &after),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "wafv2", regexp.MustCompile(`regional/rulegroup/.+$`)),
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -425,7 +431,7 @@ func TestAccAwsWafv2RuleGroup_ChangeMetricNameForceNew(t *testing.T) {

func TestAccAwsWafv2RuleGroup_Disappears(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -447,7 +453,7 @@ func TestAccAwsWafv2RuleGroup_Disappears(t *testing.T) {

func TestAccAwsWafv2RuleGroup_GeoMatchStatement(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -495,7 +501,7 @@ func TestAccAwsWafv2RuleGroup_GeoMatchStatement(t *testing.T) {
func TestAccAwsWafv2RuleGroup_IpSetReferenceStatement(t *testing.T) {
var v wafv2.RuleGroup
var idx int
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -527,7 +533,7 @@ func TestAccAwsWafv2RuleGroup_IpSetReferenceStatement(t *testing.T) {

func TestAccAwsWafv2RuleGroup_LogicalRuleStatements(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -593,7 +599,7 @@ func TestAccAwsWafv2RuleGroup_LogicalRuleStatements(t *testing.T) {

func TestAccAwsWafv2RuleGroup_Minimal(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -609,6 +615,7 @@ func TestAccAwsWafv2RuleGroup_Minimal(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "name", ruleGroupName),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttr(resourceName, "rule.#", "0"),
resource.TestCheckResourceAttr(resourceName, "scope", wafv2.ScopeRegional),
resource.TestCheckResourceAttr(resourceName, "visibility_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "visibility_config.0.cloudwatch_metrics_enabled", "false"),
Expand All @@ -623,7 +630,7 @@ func TestAccAwsWafv2RuleGroup_Minimal(t *testing.T) {
func TestAccAwsWafv2RuleGroup_RegexPatternSetReferenceStatement(t *testing.T) {
var v wafv2.RuleGroup
var idx int
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -657,7 +664,7 @@ func TestAccAwsWafv2RuleGroup_RegexPatternSetReferenceStatement(t *testing.T) {

func TestAccAwsWafv2RuleGroup_RuleAction(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -728,7 +735,7 @@ func TestAccAwsWafv2RuleGroup_RuleAction(t *testing.T) {

func TestAccAwsWafv2RuleGroup_SizeConstraintStatement(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -778,7 +785,7 @@ func TestAccAwsWafv2RuleGroup_SizeConstraintStatement(t *testing.T) {

func TestAccAwsWafv2RuleGroup_SqliMatchStatement(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -834,7 +841,7 @@ func TestAccAwsWafv2RuleGroup_SqliMatchStatement(t *testing.T) {

func TestAccAwsWafv2RuleGroup_Tags(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -882,7 +889,7 @@ func TestAccAwsWafv2RuleGroup_Tags(t *testing.T) {

func TestAccAwsWafv2RuleGroup_XssMatchStatement(t *testing.T) {
var v wafv2.RuleGroup
ruleGroupName := fmt.Sprintf("rule-group-%s", acctest.RandString(5))
ruleGroupName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafv2_rule_group.test"

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -958,7 +965,7 @@ func computeWafv2IpSetRefStatementIndex(r *wafv2.RuleGroup, idx *int) resource.T
"geo_match_statement": []interface{}{},
"ip_set_reference_statement": []interface{}{
map[string]interface{}{
"arn": *r.Rules[0].Statement.IPSetReferenceStatement.ARN,
"arn": aws.StringValue(r.Rules[0].Statement.IPSetReferenceStatement.ARN),
},
},
"not_statement": []interface{}{},
Expand Down Expand Up @@ -1010,7 +1017,7 @@ func computeWafv2RegexSetRefStatementIndex(r *wafv2.RuleGroup, idx *int) resourc
"or_statement": []interface{}{},
"regex_pattern_set_reference_statement": []interface{}{
map[string]interface{}{
"arn": *r.Rules[0].Statement.RegexPatternSetReferenceStatement.ARN,
"arn": aws.StringValue(r.Rules[0].Statement.RegexPatternSetReferenceStatement.ARN),
"field_to_match": []interface{}{
map[string]interface{}{
"all_query_arguments": []interface{}{},
Expand Down Expand Up @@ -1131,11 +1138,6 @@ resource "aws_wafv2_rule_group" "test" {
description = "%s"
scope = "REGIONAL"
tags = {
Tag1 = "Value1"
Tag2 = "Value2"
}
visibility_config {
cloudwatch_metrics_enabled = false
metric_name = "friendly-metric-name"
Expand Down Expand Up @@ -1219,7 +1221,7 @@ resource "aws_wafv2_rule_group" "test" {
`, name)
}

func testAccAwsWafv2RuleGroupConfig_Update_capacity(name string) string {
func testAccAwsWafv2RuleGroupConfig_UpdateCapacity(name string) string {
return fmt.Sprintf(`
resource "aws_wafv2_rule_group" "test" {
capacity = 3
Expand All @@ -1236,7 +1238,7 @@ resource "aws_wafv2_rule_group" "test" {
`, name, name)
}

func testAccAwsWafv2RuleGroupConfig_Update_metricName(name string) string {
func testAccAwsWafv2RuleGroupConfig_UpdateMetricName(name string) string {
return fmt.Sprintf(`
resource "aws_wafv2_rule_group" "test" {
capacity = 2
Expand Down Expand Up @@ -2091,7 +2093,7 @@ resource "aws_wafv2_regex_pattern_set" "test" {
name = "regex-pattern-set-%s"
scope = "REGIONAL"
regular_expression_list {
regular_expression {
regex_string = "one"
}
}
Expand Down
Loading

0 comments on commit 43237d8

Please sign in to comment.