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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/aws_prefix_lists - add filter #12416

Merged
merged 7 commits into from
Mar 26, 2020
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
7 changes: 6 additions & 1 deletion aws/data_source_aws_prefix_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ func dataSourceAwsPrefixList() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": dataSourceFiltersSchema(),
},
}
}

func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribePrefixListsInput{}
filters, filtersOk := d.GetOk("filter")

req := &ec2.DescribePrefixListsInput{}
if filtersOk {
req.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
}
if prefixListID := d.Get("prefix_list_id"); prefixListID != "" {
req.PrefixListIds = aws.StringSlice([]string{prefixListID.(string)})
}
Expand Down
34 changes: 33 additions & 1 deletion aws/data_source_aws_prefix_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccDataSourceAwsPrefixList(t *testing.T) {
func TestAccDataSourceAwsPrefixList_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -25,6 +25,22 @@ func TestAccDataSourceAwsPrefixList(t *testing.T) {
})
}

func TestAccDataSourceAwsPrefixList_filter(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPrefixListConfigFilter,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_id"),
testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_name"),
),
},
},
})
}

func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -66,3 +82,19 @@ data "aws_prefix_list" "s3_by_name" {
name = "com.amazonaws.us-west-2.s3"
}
`

const testAccDataSourceAwsPrefixListConfigFilter = `
data "aws_prefix_list" "s3_by_name" {
filter {
name = "prefix-list-name"
values = ["com.amazonaws.us-west-2.s3"]
}
}

data "aws_prefix_list" "s3_by_id" {
filter {
name = "prefix-list-id"
values = ["pl-68a54001"]
}
}
`
25 changes: 20 additions & 5 deletions website/docs/d/prefix_list.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,38 @@ resource "aws_network_acl_rule" "private_s3" {
}
```

### Filter

```hcl
data "aws_prefix_list" "test" {
filter {
name = "prefix-list-id"
values = ["pl-68a54001"]
}
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available
prefix lists. The given filters must match exactly one prefix list
whose data will be exported as attributes.

* `prefix_list_id` - (Optional) The ID of the prefix list to select.

* `name` - (Optional) The name of the prefix list to select.
* `filter` - (Optional) Configuration block(s) for filtering. Detailed below.

### filter Configuration Block

The following arguments are supported by the `filter` configuration block:

* `name` - (Required) The name of the filter field. Valid values can be found in the [EC2 DescribePrefixLists API Reference](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribePrefixLists.html).
* `values` - (Required) Set of values that are accepted for the given filter field. Results will be selected if any given value matches.

## Attributes Reference

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

* `id` - The ID of the selected prefix list.

* `name` - The name of the selected prefix list.

* `cidr_blocks` - The list of CIDR blocks for the AWS service associated
with the prefix list.
* `cidr_blocks` - The list of CIDR blocks for the AWS service associated with the prefix list.