-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10563 from flosell/d-waf-subscribed-rule-group
Add data sources for Managed Rules for WAF and WAF Regional
- Loading branch information
Showing
10 changed files
with
595 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package waf | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/waf" | ||
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func FindSubscribedRuleGroupByNameOrMetricName(ctx context.Context, conn *waf.WAF, name string, metricName string) (*waf.SubscribedRuleGroupSummary, error) { | ||
hasName := name != "" | ||
hasMetricName := metricName != "" | ||
hasMatch := false | ||
|
||
if !hasName && !hasMetricName { | ||
return nil, errors.New("must specify either name or metricName") | ||
} | ||
|
||
input := &waf.ListSubscribedRuleGroupsInput{} | ||
|
||
matchingRuleGroup := &waf.SubscribedRuleGroupSummary{} | ||
|
||
for { | ||
output, err := conn.ListSubscribedRuleGroupsWithContext(ctx, input) | ||
|
||
if tfawserr.ErrCodeContains(err, waf.ErrCodeNonexistentItemException) { | ||
return nil, &resource.NotFoundError{ | ||
LastError: err, | ||
LastRequest: input, | ||
} | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, ruleGroup := range output.RuleGroups { | ||
respName := aws.StringValue(ruleGroup.Name) | ||
respMetricName := aws.StringValue(ruleGroup.MetricName) | ||
|
||
if hasName && respName != name { | ||
continue | ||
} | ||
if hasMetricName && respMetricName != metricName { | ||
continue | ||
} | ||
if hasName && hasMetricName && (name != respName || metricName != respMetricName) { | ||
continue | ||
} | ||
// Previous conditionals catch all non-matches | ||
if hasMatch { | ||
return nil, fmt.Errorf("multiple matches found for name %s and metricName %s", name, metricName) | ||
} | ||
|
||
matchingRuleGroup = ruleGroup | ||
hasMatch = true | ||
} | ||
|
||
if output.NextMarker == nil { | ||
break | ||
} | ||
input.NextMarker = output.NextMarker | ||
} | ||
|
||
if !hasMatch { | ||
return nil, fmt.Errorf("no matches found for name %s and metricName %s", name, metricName) | ||
} | ||
|
||
return matchingRuleGroup, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package waf | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
const ( | ||
DSNameSubscribedRuleGroup = "Subscribed Rule Group Data Source" | ||
) | ||
|
||
func DataSourceSubscribedRuleGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadWithoutTimeout: dataSourceSubscribedRuleGroupRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"metric_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSubscribedRuleGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.AWSClient).WAFConn | ||
name, nameOk := d.Get("name").(string) | ||
metricName, metricNameOk := d.Get("metric_name").(string) | ||
|
||
// Error out if string-assertion fails for either name or metricName | ||
if !nameOk || !metricNameOk { | ||
if !nameOk { | ||
name = DSNameSubscribedRuleGroup | ||
} | ||
|
||
err := errors.New("unable to read attributes") | ||
return names.DiagError(names.WAF, names.ErrActionReading, DSNameSubscribedRuleGroup, name, err) | ||
} | ||
|
||
output, err := FindSubscribedRuleGroupByNameOrMetricName(ctx, conn, name, metricName) | ||
|
||
if err != nil { | ||
return names.DiagError(names.WAF, names.ErrActionReading, DSNameSubscribedRuleGroup, name, err) | ||
} | ||
|
||
d.SetId(aws.StringValue(output.RuleGroupId)) | ||
d.Set("metric_name", output.MetricName) | ||
d.Set("name", output.Name) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package waf_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/waf" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
) | ||
|
||
func TestAccWAFSubscribedRuleGroupDataSource_basic(t *testing.T) { | ||
if os.Getenv("WAF_SUBSCRIBED_RULE_GROUP_NAME") == "" { | ||
t.Skip("Environment variable WAF_SUBSCRIBED_RULE_GROUP_NAME is not set") | ||
} | ||
|
||
ruleGroupName := os.Getenv("WAF_SUBSCRIBED_RULE_GROUP_NAME") | ||
|
||
if os.Getenv("WAF_SUBSCRIBED_RULE_GROUP_METRIC_NAME") == "" { | ||
t.Skip("Environment variable WAF_SUBSCRIBED_RULE_GROUP_METRIC_NAME is not set") | ||
} | ||
|
||
metricName := os.Getenv("WAF_SUBSCRIBED_RULE_GROUP_METRIC_NAME") | ||
|
||
datasourceName := "data.aws_waf_subscribed_rule_group.rulegroup" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(waf.EndpointsID, t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, waf.EndpointsID), | ||
CheckDestroy: nil, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSubscribedRuleGroupDataSourceConfig_nonexistent, | ||
ExpectError: regexp.MustCompile(`no matches found`), | ||
}, | ||
{ | ||
Config: testAccSubscribedRuleGroupDataSourceConfig_name(ruleGroupName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(datasourceName, "name", ruleGroupName), | ||
resource.TestCheckResourceAttr(datasourceName, "metric_name", metricName), | ||
), | ||
}, | ||
{ | ||
Config: testAccSubscribedRuleGroupDataSourceConfig_metricName(metricName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(datasourceName, "name", ruleGroupName), | ||
resource.TestCheckResourceAttr(datasourceName, "metric_name", metricName), | ||
), | ||
}, | ||
{ | ||
Config: testAccSubscribedRuleGroupDataSourceConfig_nameAndMetricName(ruleGroupName, metricName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(datasourceName, "name", ruleGroupName), | ||
resource.TestCheckResourceAttr(datasourceName, "metric_name", metricName), | ||
), | ||
}, | ||
{ | ||
Config: testAccSubscribedRuleGroupDataSourceConfig_nameAndMismatchingMetricName(ruleGroupName), | ||
ExpectError: regexp.MustCompile(`no matches found`), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSubscribedRuleGroupDataSourceConfig_name(name string) string { | ||
return fmt.Sprintf(` | ||
data "aws_waf_subscribed_rule_group" "rulegroup" { | ||
name = %[1]q | ||
} | ||
`, name) | ||
} | ||
|
||
func testAccSubscribedRuleGroupDataSourceConfig_metricName(metricName string) string { | ||
return fmt.Sprintf(` | ||
data "aws_waf_subscribed_rule_group" "rulegroup" { | ||
metric_name = %[1]q | ||
} | ||
`, metricName) | ||
} | ||
|
||
func testAccSubscribedRuleGroupDataSourceConfig_nameAndMetricName(name string, metricName string) string { | ||
return fmt.Sprintf(` | ||
data "aws_waf_subscribed_rule_group" "rulegroup" { | ||
name = %[1]q | ||
metric_name = %[2]q | ||
} | ||
`, name, metricName) | ||
} | ||
|
||
func testAccSubscribedRuleGroupDataSourceConfig_nameAndMismatchingMetricName(name string) string { | ||
return fmt.Sprintf(` | ||
data "aws_waf_subscribed_rule_group" "rulegroup" { | ||
name = %[1]q | ||
metric_name = "tf-acc-test-does-not-exist" | ||
} | ||
`, name) | ||
} | ||
|
||
const testAccSubscribedRuleGroupDataSourceConfig_nonexistent = ` | ||
data "aws_waf_subscribed_rule_group" "rulegroup" { | ||
name = "tf-acc-test-does-not-exist" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.