Skip to content

Commit

Permalink
data-source/aws_prefix_list: Using name argument no longer override…
Browse files Browse the repository at this point in the history
…s other arguments (#16739)

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccDataSourceAwsPrefixList_nameDoesNotOverrideFilter (2.68s)
--- PASS: TestAccDataSourceAwsPrefixList_basic (12.81s)
--- PASS: TestAccDataSourceAwsPrefixList_filter (12.83s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- PASS: TestAccDataSourceAwsPrefixList_nameDoesNotOverrideFilter (1.60s)
--- PASS: TestAccDataSourceAwsPrefixList_filter (7.48s)
--- PASS: TestAccDataSourceAwsPrefixList_basic (7.61s)
```
  • Loading branch information
roberth-k authored Dec 15, 2020
1 parent 039fb67 commit 55a7464
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
11 changes: 6 additions & 5 deletions aws/data_source_aws_prefix_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error
if prefixListID := d.Get("prefix_list_id"); prefixListID != "" {
req.PrefixListIds = aws.StringSlice([]string{prefixListID.(string)})
}
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"prefix-list-name": d.Get("name").(string),
},
)
if prefixListName := d.Get("name"); prefixListName.(string) != "" {
req.Filters = append(req.Filters, &ec2.Filter{
Name: aws.String("prefix-list-name"),
Values: aws.StringSlice([]string{prefixListName.(string)}),
})
}

log.Printf("[DEBUG] Reading Prefix List: %s", req)
resp, err := conn.DescribePrefixLists(req)
Expand Down
78 changes: 68 additions & 10 deletions aws/data_source_aws_prefix_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package aws

import (
"fmt"
"regexp"
"strconv"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -41,7 +44,41 @@ func TestAccDataSourceAwsPrefixList_filter(t *testing.T) {
})
}

func TestAccDataSourceAwsPrefixList_nameDoesNotOverrideFilter(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter,
ExpectError: regexp.MustCompile(`no matching prefix list found`),
},
},
})
}

func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {
getPrefixListId := func(name string) (string, error) {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

input := ec2.DescribePrefixListsInput{
Filters: buildEC2AttributeFilterList(map[string]string{
"prefix-list-name": name,
}),
}

output, err := conn.DescribePrefixLists(&input)
if err != nil {
return "", err
}

if len(output.PrefixLists) != 1 {
return "", fmt.Errorf("prefix list %s not found", name)
}

return aws.StringValue(output.PrefixLists[0].PrefixListId), nil
}

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
Expand All @@ -50,17 +87,21 @@ func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {

attr := rs.Primary.Attributes

if attr["name"] != "com.amazonaws.us-west-2.s3" {
region := testAccGetRegion()
prefixListName := fmt.Sprintf("com.amazonaws.%s.s3", region)
prefixListId, err := getPrefixListId(prefixListName)
if err != nil {
return err
}

if attr["name"] != prefixListName {
return fmt.Errorf("bad name %s", attr["name"])
}
if attr["id"] != "pl-68a54001" {
if attr["id"] != prefixListId {
return fmt.Errorf("bad id %s", attr["id"])
}

var (
cidrBlockSize int
err error
)
var cidrBlockSize int

if cidrBlockSize, err = strconv.Atoi(attr["cidr_blocks.#"]); err != nil {
return err
Expand All @@ -74,27 +115,44 @@ func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {
}

const testAccDataSourceAwsPrefixListConfig = `
data "aws_region" "current" {}
data "aws_prefix_list" "s3_by_id" {
prefix_list_id = "pl-68a54001"
prefix_list_id = data.aws_prefix_list.s3_by_name.id
}
data "aws_prefix_list" "s3_by_name" {
name = "com.amazonaws.us-west-2.s3"
name = "com.amazonaws.${data.aws_region.current.name}.s3"
}
`

const testAccDataSourceAwsPrefixListConfigFilter = `
data "aws_region" "current" {}
data "aws_prefix_list" "s3_by_name" {
filter {
name = "prefix-list-name"
values = ["com.amazonaws.us-west-2.s3"]
values = ["com.amazonaws.${data.aws_region.current.name}.s3"]
}
}
data "aws_prefix_list" "s3_by_id" {
filter {
name = "prefix-list-id"
values = ["pl-68a54001"]
values = [data.aws_prefix_list.s3_by_name.id]
}
}
`

const testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter = `
data "aws_region" "current" {}
data "aws_prefix_list" "test" {
name = "com.amazonaws.${data.aws_region.current.name}.dynamodb"
filter {
name = "prefix-list-name"
values = ["com.amazonaws.${data.aws_region.current.name}.s3"]
}
}
`
2 changes: 1 addition & 1 deletion website/docs/d/prefix_list.html.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
subcategory: "VPC"
layout: "aws"
page_title: "AWS: aws_prefix-list"
page_title: "AWS: aws_prefix_list"
description: |-
Provides details about a specific prefix list
---
Expand Down

0 comments on commit 55a7464

Please sign in to comment.