Skip to content

Commit

Permalink
Merge pull request #9484 from terraform-providers/rfd-wafregional-ipset
Browse files Browse the repository at this point in the history
Add WAF Regional IP set data source
  • Loading branch information
ryndaniels authored Jul 25, 2019
2 parents ea6b65a + 51aa713 commit e48ade3
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
59 changes: 59 additions & 0 deletions aws/data_source_aws_wafregional_ipset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 dataSourceAwsWafRegionalIpSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceAWSWafRegionalIpSetRead,

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

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

ipsets := make([]*waf.IPSetSummary, 0)
// ListIPSetsInput does not have a name parameter for filtering or a paginator
input := &waf.ListIPSetsInput{}
for {
output, err := conn.ListIPSets(input)
if err != nil {
return fmt.Errorf("Error reading WAF Regional IP sets: %s", err)
}
for _, ipset := range output.IPSets {
if aws.StringValue(ipset.Name) == name {
ipsets = append(ipsets, ipset)
}
}

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

if len(ipsets) == 0 {
return fmt.Errorf("WAF Regional IP Set not found for name: %s", name)
}
if len(ipsets) > 1 {
return fmt.Errorf("Multiple WAF Regional IP Sets found for name: %s", name)
}

ipset := ipsets[0]
d.SetId(aws.StringValue(ipset.IPSetId))

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

import (
"fmt"
"regexp"
"testing"

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

func TestAccDataSourceAwsWafRegionalIPSet_Basic(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_wafregional_ipset.ipset"
datasourceName := "data.aws_wafregional_ipset.ipset"

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

func testAccDataSourceAwsWafRegionalIPSet_Name(name string) string {
return fmt.Sprintf(`
resource "aws_wafregional_ipset" "ipset" {
name = %[1]q
}
data "aws_wafregional_ipset" "ipset" {
name = "${aws_wafregional_ipset.ipset.name}"
}
`, name)
}

const testAccDataSourceAwsWafRegionalIPSet_NonExistent = `
data "aws_wafregional_ipset" "ipset" {
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 @@ -276,6 +276,7 @@ func Provider() terraform.ResourceProvider {
"aws_waf_ipset": dataSourceAwsWafIpSet(),
"aws_waf_rule": dataSourceAwsWafRule(),
"aws_waf_web_acl": dataSourceAwsWafWebAcl(),
"aws_wafregional_ipset": dataSourceAwsWafRegionalIpSet(),
"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 @@ -3151,6 +3151,9 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/aws/d/wafregional_ipset.html">aws_wafregional_ipset</a>
</li>
<li>
<a href="/docs/providers/aws/d/wafregional_rule.html">aws_wafregional_rule</a>
</li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/wafregional_ipset.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "aws"
page_title: "AWS: aws_wafregional_ipset"
sidebar_current: "docs-aws-datasource-wafregional-ipset"
description: |-
Retrieves an AWS WAF Regional IP set id.
---

# Data Source: aws_wafregional_ipset

`aws_wafregional_ipset` Retrieves a WAF Regional IP Set Resource Id.

## Example Usage

```hcl
data "aws_wafregional_ipset" "example" {
name = "tfWAFRegionalIPSet"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the WAF Regional IP set.

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

* `id` - The ID of the WAF Regional IP set.

0 comments on commit e48ade3

Please sign in to comment.