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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WAF rate based rule data source #10124

Merged
merged 4 commits into from
Oct 11, 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
60 changes: 60 additions & 0 deletions aws/data_source_aws_waf_rate_based_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/waf"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAwsWafRateBasedRule() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsWafRateBasedRuleRead,

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

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

rules := make([]*waf.RuleSummary, 0)
// ListRulesInput does not have a name parameter for filtering
input := &waf.ListRateBasedRulesInput{}
for {
output, err := conn.ListRateBasedRules(input)
if err != nil {
return fmt.Errorf("error reading WAF Rate Based Rules: %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 Rate Based Rules not found for name: %s", name)
}

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

rule := rules[0]

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

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

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

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

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

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

func testAccDataSourceAwsWafRateBasedRuleConfig_Name(name string) string {
return fmt.Sprintf(`
resource "aws_waf_rate_based_rule" "wafrule" {
name = %[1]q
metric_name = "WafruleTest"
rate_key = "IP"
rate_limit = 2000
}

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

const testAccDataSourceAwsWafRateBasedRuleConfig_NonExistent = `
data "aws_waf_rate_based_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 @@ -279,6 +279,7 @@ func Provider() terraform.ResourceProvider {
"aws_vpn_gateway": dataSourceAwsVpnGateway(),
"aws_waf_ipset": dataSourceAwsWafIpSet(),
"aws_waf_rule": dataSourceAwsWafRule(),
"aws_waf_rate_based_rule": dataSourceAwsWafRateBasedRule(),
"aws_waf_web_acl": dataSourceAwsWafWebAcl(),
"aws_wafregional_ipset": dataSourceAwsWafRegionalIpSet(),
"aws_wafregional_rule": dataSourceAwsWafRegionalRule(),
Expand Down
6 changes: 3 additions & 3 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3146,9 +3146,6 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/aws/d/waf_rule.html">aws_waf_rule</a>
</li>
<li>
<a href="/docs/providers/aws/d/waf_web_acl.html">aws_waf_web_acl</a>
</li>
Expand All @@ -3158,6 +3155,9 @@
<li>
<a href="/docs/providers/aws/d/waf_rule.html">aws_waf_rule</a>
</li>
<li>
<a href="/docs/providers/aws/d/waf_rate_based_rule.html">aws_waf_rate_based_rule</a>
</li>
</ul>
</li>
<li>
Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/waf_rate_based_rule.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
diogoazevedos marked this conversation as resolved.
Show resolved Hide resolved
layout: "aws"
page_title: "AWS: aws_waf_rate_based_rule"
sidebar_current: "docs-aws-datasource-waf-rate-based-rule"
description: |-
Retrieves an AWS WAF rate based rule id.
---

# Data Source: aws_waf_rate_based_rule

`aws_waf_rate_based_rule` Retrieves a WAF Rate Based Rule Resource Id.

## Example Usage

```hcl
data "aws_waf_rate_based_rule" "example" {
name = "tfWAFRateBasedRule"
}

```

## Argument Reference

The following arguments are supported:

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

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

* `id` - The ID of the WAF rate based rule.