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

d/aws_wafregional_web_acl: Add WAFRegional Web ACL lookup datasource #9321

Merged
merged 3 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
64 changes: 64 additions & 0 deletions aws/data_source_aws_waf_web_acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package aws
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please name this file aws/data_source_aws_wafregional_web_acl.go to match the data source naming? Thanks!


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

func dataSourceAwsWafRegionalWebAcl() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsWafRegionalWebAclRead,

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove the id attribute from the schema? It is implicit with all Terraform resources. Thanks!

Type: schema.TypeString,
Computed: true,
},
},
}
}

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

acls := make([]*waf.WebACLSummary, 0)
// ListWebACLsInput does not have a name parameter for filtering
input := &waf.ListWebACLsInput{}
for {
output, err := conn.ListWebACLs(input)
if err != nil {
return fmt.Errorf("error reading web ACLs: %s", err)
}
for _, acl := range output.WebACLs {
if aws.StringValue(acl.Name) == name {
acls = append(acls, acl)
}
}

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

if len(acls) == 0 {
return fmt.Errorf("web ACLs not found for name: %s", name)
}

if len(acls) > 1 {
return fmt.Errorf("multiple web ACLs found for name: %s", name)
}

acl := acls[0]

d.SetId(aws.StringValue(acl.WebACLId))

return nil
}
78 changes: 78 additions & 0 deletions aws/data_source_aws_waf_web_acl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package aws
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please name this file aws/data_source_aws_wafregional_web_acl_test.go to match the data source naming? Thanks!


import (
"fmt"
"regexp"
"testing"

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

func TestAccDataSourceAwsWafRegionalWebAcl_Basic(t *testing.T) {
name := "tf-acc-test"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please randomize the naming to prevent issues where the test may have left existing resources? Please see also the Uses Randomized Infrastructure Naming item of the Contributing Guide Thanks!

Suggested change
name := "tf-acc-test"
name := acctest.RandomWithPrefix("tf-acc-test")

resourceName := "aws_wafregional_web_acl.web_acl"
datasourceName := "data.aws_wafregional_web_acl.web_acl"

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

func testAccDataSourceAwsWafRegionalWebAclConfig_Name(name string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we do not need to verify the rules within the Web ACL and they are optional in the resource, can you please remove the extra infrastructure from the test configuration? Thanks!

func testAccDataSourceAwsWafRegionalWebAclConfig_Name(name string) string {
	return fmt.Sprintf(`
resource "aws_wafregional_web_acl" "web_acl" {
  name        = %[1]q
  metric_name = "tfWebACL"

  default_action {
    type = "ALLOW"
  }
}

data "aws_wafregional_web_acl" "web_acl" {
  name = "${aws_wafregional_web_acl.web_acl.name}"
}
`, name)
}

return fmt.Sprintf(`
resource "aws_wafregional_rule" "wafrule" {
name = "%s"
metric_name = "WafruleTest"
predicate {
data_id = "${aws_wafregional_ipset.test.id}"
negated = false
type = "IPMatch"
}
}
resource "aws_wafregional_ipset" "test" {
name = "%s"
ip_set_descriptor {
type = "IPV4"
value = "10.0.0.0/8"
}
}
resource "aws_wafregional_web_acl" "web_acl" {
name = "%s"
metric_name = "tfWebACL"
default_action {
type = "ALLOW"
}
rule {
action {
type = "BLOCK"
}
priority = 1
rule_id = "${aws_wafregional_rule.wafrule.id}"
type = "REGULAR"
}
}
data "aws_wafregional_web_acl" "web_acl" {
name = "${aws_wafregional_web_acl.web_acl.name}"
}
`, name, name, name)
}

const testAccDataSourceAwsWafRegionalWebAclConfig_NonExistent = `
data "aws_wafregional_web_acl" "web_acl" {
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 @@ -272,6 +272,7 @@ func Provider() terraform.ResourceProvider {
"aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(),
"aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(),
"aws_vpn_gateway": dataSourceAwsVpnGateway(),
"aws_wafregional_web_acl": dataSourceAwsWafRegionalWebAcl(),
"aws_workspaces_bundle": dataSourceAwsWorkspaceBundle(),

// Adding the Aliases for the ALB -> LB Rename
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@
<li>
<a href="/docs/providers/aws/d/vpn_gateway.html">aws_vpn_gateway</a>
</li>
<li>
<a href="/docs/providers/aws/d/wafregional_web_acl.html">aws_wafregional_web_acl</a>
</li>
<li>
<a href="/docs/providers/aws/d/workspaces_bundle.html">aws_workspaces_bundle</a>
</li>
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/wafregional_web_acl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add .markdown to the end of the file name here? Otherwise the website page does not load properly. You can verify this with make website.

Screen Shot 2019-07-17 at 11 36 19 AM

layout: "aws"
page_title: "AWS: aws_wafregional_web_acl"
sidebar_current: "docs-aws-datasource-wafregional-web-acl"
description: |-
Retrieves a WAF Regional Web ACL id.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix the YAML here? Otherwise the website page does not load properly. You can verify this with make website.

Suggested change
Retrieves a WAF Regional Web ACL id.
Retrieves a WAF Regional Web ACL id.

Screen Shot 2019-07-17 at 11 36 59 AM

---

# Data Source: aws_wafregional_web_acl

`aws_wafregional_web_acl` Retrieves a WAF Regional Web ACL Resource Id.

## Example Usage

```hcl
data "aws_wafregional_web_acl" "example" {
name = "tfWAFRule"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform formatting:

Suggested change
name = "tfWAFRule"
name = "tfWAFRule"

}
```

## Argument Reference

The following arguments are supported:

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

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

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