-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9321 from robh007/wafregional-web-acl-datasource
d/aws_wafregional_web_acl: Add WAFRegional Web ACL lookup datasource
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/helper/schema" | ||
) | ||
|
||
func dataSourceAwsWafRegionalWebAcl() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsWafRegionalWebAclRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAwsWafRegionalWebAcl_Basic(t *testing.T) { | ||
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 { | ||
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) | ||
} | ||
|
||
const testAccDataSourceAwsWafRegionalWebAclConfig_NonExistent = ` | ||
data "aws_wafregional_web_acl" "web_acl" { | ||
name = "tf-acc-test-does-not-exist" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
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. | ||
--- | ||
|
||
# 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" | ||
} | ||
``` | ||
|
||
## 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. |