diff --git a/aws/data_source_aws_wafregional_web_acl.go b/aws/data_source_aws_wafregional_web_acl.go new file mode 100644 index 00000000000..585479a197a --- /dev/null +++ b/aws/data_source_aws_wafregional_web_acl.go @@ -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 +} diff --git a/aws/data_source_aws_wafregional_web_acl_test.go b/aws/data_source_aws_wafregional_web_acl_test.go new file mode 100644 index 00000000000..fe969cf8454 --- /dev/null +++ b/aws/data_source_aws_wafregional_web_acl_test.go @@ -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" +} +` diff --git a/aws/provider.go b/aws/provider.go index 4d7c6f312ca..7f1a1a6af68 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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 diff --git a/website/aws.erb b/website/aws.erb index 9f9b5088f4b..282463058df 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -451,6 +451,9 @@