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 4 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"]
}
}
`
16 changes: 16 additions & 0 deletions website/docs/d/prefix_list.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ 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
Expand All @@ -53,6 +64,9 @@ 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) One or more name/value pairs to use as filters. There are
several valid keys, for a full reference, check out
[describe-prefix-lists in the AWS CLI reference][1].
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved

## Attributes Reference

Expand All @@ -64,3 +78,5 @@ In addition to all arguments above, the following attributes are exported:

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

[1]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-prefix-lists.html