-
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.
- Loading branch information
Pascal van Buijtene
committed
Jun 16, 2020
1 parent
5c645e7
commit 6ab410d
Showing
5 changed files
with
208 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,83 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/wafv2" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func dataSourceAwsWafv2WebACL() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsWafv2WebACLRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"scope": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
wafv2.ScopeCloudfront, | ||
wafv2.ScopeRegional, | ||
}, false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsWafv2WebACLRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).wafv2conn | ||
name := d.Get("name").(string) | ||
|
||
var foundWebACL *wafv2.WebACLSummary | ||
input := &wafv2.ListWebACLsInput{ | ||
Scope: aws.String(d.Get("scope").(string)), | ||
Limit: aws.Int64(100), | ||
} | ||
|
||
for { | ||
resp, err := conn.ListWebACLs(input) | ||
if err != nil { | ||
return fmt.Errorf("Error reading WAFv2 WebACLs: %s", err) | ||
} | ||
|
||
if resp == nil || resp.WebACLs == nil { | ||
return fmt.Errorf("Error reading WAFv2 WebACLs") | ||
} | ||
|
||
for _, webACL := range resp.WebACLs { | ||
if aws.StringValue(webACL.Name) == name { | ||
foundWebACL = webACL | ||
break | ||
} | ||
} | ||
|
||
if resp.NextMarker == nil || foundWebACL != nil { | ||
break | ||
} | ||
input.NextMarker = resp.NextMarker | ||
} | ||
|
||
if foundWebACL == nil { | ||
return fmt.Errorf("WAFv2 WebACL not found for name: %s", name) | ||
} | ||
|
||
d.SetId(aws.StringValue(foundWebACL.Id)) | ||
d.Set("arn", aws.StringValue(foundWebACL.ARN)) | ||
d.Set("description", aws.StringValue(foundWebACL.Description)) | ||
|
||
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,86 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAwsWafv2WebACL_Basic(t *testing.T) { | ||
name := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_wafv2_web_acl.test" | ||
datasourceName := "data.aws_wafv2_web_acl.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAwsWafv2WebACL_NonExistent(name), | ||
ExpectError: regexp.MustCompile(`WAFv2 WebACL not found`), | ||
}, | ||
{ | ||
Config: testAccDataSourceAwsWafv2WebACL_Name(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"), | ||
testAccMatchResourceAttrRegionalARN(datasourceName, "arn", "wafv2", regexp.MustCompile(fmt.Sprintf("regional/webacl/%v/.+$", name))), | ||
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"), | ||
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"), | ||
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"), | ||
resource.TestCheckResourceAttrPair(datasourceName, "scope", resourceName, "scope"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsWafv2WebACL_Name(name string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_wafv2_web_acl" "test" { | ||
name = "%s" | ||
scope = "REGIONAL" | ||
default_action { | ||
block {} | ||
} | ||
visibility_config { | ||
cloudwatch_metrics_enabled = false | ||
metric_name = "friendly-rule-metric-name" | ||
sampled_requests_enabled = false | ||
} | ||
} | ||
data "aws_wafv2_web_acl" "test" { | ||
name = aws_wafv2_web_acl.test.name | ||
scope = "REGIONAL" | ||
} | ||
`, name) | ||
} | ||
|
||
func testAccDataSourceAwsWafv2WebACL_NonExistent(name string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_wafv2_web_acl" "test" { | ||
name = "%s" | ||
scope = "REGIONAL" | ||
default_action { | ||
block {} | ||
} | ||
visibility_config { | ||
cloudwatch_metrics_enabled = false | ||
metric_name = "friendly-rule-metric-name" | ||
sampled_requests_enabled = false | ||
} | ||
} | ||
data "aws_wafv2_web_acl" "test" { | ||
name = "tf-acc-test-does-not-exist" | ||
scope = "REGIONAL" | ||
} | ||
`, name) | ||
} |
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,35 @@ | ||
--- | ||
subcategory: "WAFv2" | ||
layout: "aws" | ||
page_title: "AWS: aws_wafv2_web_acl" | ||
description: |- | ||
Retrieves the summary of a WAFv2 Web ACL. | ||
--- | ||
|
||
# Data Source: aws_wafv2_web_acl | ||
|
||
Retrieves the summary of a WAFv2 Web ACL. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_wafv2_web_acl" "example" { | ||
name = "some-web-acl" | ||
scope = "REGIONAL" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the WAFv2 Web ACL. | ||
* `scope` - (Required) Specifies whether this is for an AWS CloudFront distribution or for a regional application. Valid values are `CLOUDFRONT` or `REGIONAL`. To work with CloudFront, you must also specify the region `us-east-1` (N. Virginia) on the AWS provider. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `arn` - The Amazon Resource Name (ARN) of the entity. | ||
* `description` - The description of the WebACL that helps with identification. | ||
* `id` - The unique identifier of the WebACL. |