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 IP set data source #9481

Merged
merged 4 commits into from
Jul 25, 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
59 changes: 59 additions & 0 deletions aws/data_source_aws_waf_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 dataSourceAwsWafIpSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceAWSWafIpSetRead,

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

func dataSourceAWSWafIpSetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).wafconn
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 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 IP Set not found for name: %s", name)
}
if len(ipsets) > 1 {
return fmt.Errorf("Multiple WAF 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_waf_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 TestAccDataSourceAwsWafIPSet_Basic(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_waf_ipset.ipset"
datasourceName := "data.aws_waf_ipset.ipset"

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

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

const testAccDataSourceAwsWafIPSet_NonExistent = `
data "aws_waf_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 @@ -273,6 +273,7 @@ func Provider() terraform.ResourceProvider {
"aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(),
"aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(),
"aws_vpn_gateway": dataSourceAwsVpnGateway(),
"aws_waf_ipset": dataSourceAwsWafIpSet(),
"aws_waf_rule": dataSourceAwsWafRule(),
"aws_waf_web_acl": dataSourceAwsWafWebAcl(),
"aws_wafregional_rule": dataSourceAwsWafRegionalRule(),
Expand Down
6 changes: 6 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3094,6 +3094,12 @@
<li>
<a href="/docs/providers/aws/d/waf_web_acl.html">aws_waf_web_acl</a>
</li>
<li>
<a href="/docs/providers/aws/d/waf_ipset.html">aws_waf_ipset</a>
</li>
<li>
<a href="/docs/providers/aws/d/waf_rule.html">aws_waf_rule</a>
ryndaniels marked this conversation as resolved.
Show resolved Hide resolved
</li>
</ul>
</li>
<li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/waf_ipset.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "aws"
page_title: "AWS: aws_waf_ipset"
sidebar_current: "docs-aws-datasource-waf-ipset"
description: |-
Retrieves an AWS WAF IP set id.
---

# Data Source: aws_waf_ipset

`aws_waf_ipset` Retrieves a WAF IP Set Resource Id.

## Example Usage

```hcl
data "aws_waf_ipset" "example" {
name = "tfWAFIPSet"
}
```

## Argument Reference

The following arguments are supported:

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

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

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