Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_wafregional_rule: Add WAFRegional Rule lookup datasource #9319

Merged
merged 5 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions aws/data_source_aws_wafregional_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/waf"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsWafRegionalRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsWafRegionalRuleRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceAwsWafRegionalRuleRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafregionalconn
name := d.Get("name").(string)

rules := make([]*waf.RuleSummary, 0)
// ListRulesInput does not have a name parameter for filtering
input := &waf.ListRulesInput{}
for {
output, err := conn.ListRules(input)
if err != nil {
return fmt.Errorf("error reading WAF Rule: %s", err)
}
for _, rule := range output.Rules {
if aws.StringValue(rule.Name) == name {
rules = append(rules, rule)
}
}

if output.NextMarker == nil {
break
}
input.NextMarker = output.NextMarker
}

if len(rules) == 0 {
return fmt.Errorf("WAF Rule not found for name: %s", name)
}

if len(rules) > 1 {
return fmt.Errorf("multiple WAF Rules found for name: %s", name)
}

rule := rules[0]

d.SetId(aws.StringValue(rule.RuleId))

return nil
}
53 changes: 53 additions & 0 deletions aws/data_source_aws_wafregional_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package aws

import (
"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"regexp"
"testing"

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

func TestAccDataSourceAwsWafRegionalRule_Basic(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafregional_rule.wafrule"
datasourceName := "data.aws_wafregional_rule.wafrule"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsWafRegionalRuleConfig_NonExistent,
ExpectError: regexp.MustCompile(`WAF Rule not found`),
},
{
Config: testAccDataSourceAwsWafRegionalRuleConfig_Name(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
),
},
},
})
}

func testAccDataSourceAwsWafRegionalRuleConfig_Name(name string) string {
return fmt.Sprintf(`
resource "aws_wafregional_rule" "wafrule" {
name = %[1]q
metric_name = "WafruleTest"
}

data "aws_wafregional_rule" "wafrule" {
name = "${aws_wafregional_rule.wafrule.name}"
}
`, name)
}

const testAccDataSourceAwsWafRegionalRuleConfig_NonExistent = `
data "aws_wafregional_rule" "wafrule" {
name = "tf-acc-test-does-not-exist"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func Provider() terraform.ResourceProvider {
"aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(),
"aws_vpn_gateway": dataSourceAwsVpnGateway(),
"aws_waf_web_acl": dataSourceAwsWafWebAcl(),
"aws_wafregional_rule": dataSourceAwsWafRegionalRule(),
"aws_wafregional_web_acl": dataSourceAwsWafRegionalWebAcl(),
"aws_workspaces_bundle": dataSourceAwsWorkspaceBundle(),

Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@
<li>
<a href="/docs/providers/aws/d/waf_web_acl.html">aws_waf_web_acl</a>
</li>
<li>
<a href="/docs/providers/aws/d/wafregional_rule.html">aws_wafregional_rule</a>
</li>
<li>
<a href="/docs/providers/aws/d/wafregional_web_acl.html">aws_wafregional_web_acl</a>
</li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/wafregional_rule.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "aws"
page_title: "AWS: aws_wafregional_rule"
sidebar_current: "docs-aws-datasource-wafregional-rule"
description: |-
Retrieves an AWS WAF Regional rule id.
---

# Data Source: aws_waf_rule

`aws_wafregional_rule` Retrieves a WAF Regional Rule Resource Id.

## Example Usage

```hcl
data "aws_wafregional_rule" "example" {
name = "tfWAFRule"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the WAF rule.

## Attributes Reference
In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the WAF Regional rule.