Skip to content

Commit

Permalink
ds/aws_prefix_list: Fix hardcoded regions
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Dec 4, 2020
1 parent 346cac9 commit c384457
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
24 changes: 13 additions & 11 deletions aws/data_source_aws_prefix_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,22 @@ func dataSourceAwsPrefixList() *schema.Resource {
func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

filters, filtersOk := d.GetOk("filter")

req := &ec2.DescribePrefixListsInput{}
if filtersOk {
req.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))

if v, ok := d.GetOk("prefix_list_id"); ok {
req.PrefixListIds = aws.StringSlice([]string{v.(string)})
}

if v, ok := d.GetOk("filter"); ok {
req.Filters = buildAwsDataSourceFilters(v.(*schema.Set))
}
if prefixListID := d.Get("prefix_list_id"); prefixListID != "" {
req.PrefixListIds = aws.StringSlice([]string{prefixListID.(string)})

if v, ok := d.GetOk("name"); ok {
req.Filters = append(req.Filters, &ec2.Filter{
Name: aws.String("prefix-list-name"),
Values: aws.StringSlice([]string{v.(string)}),
})
}
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"prefix-list-name": d.Get("name").(string),
},
)

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

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

Expand All @@ -19,6 +20,10 @@ func TestAccDataSourceAwsPrefixList_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_id"),
testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_name"),
resource.TestMatchResourceAttr("data.aws_prefix_list.s3_by_name", "id", regexp.MustCompile(`^pl-[0-9a-z]{8}$`)),
testAccCheckResourceAttrRegionalReverseDnsService("data.aws_prefix_list.s3_by_name", "name", "s3"),
resource.TestMatchResourceAttr("data.aws_prefix_list.s3_by_id", "id", regexp.MustCompile(`^pl-[0-9a-z]{8}$`)),
testAccCheckResourceAttrRegionalReverseDnsService("data.aws_prefix_list.s3_by_id", "name", "s3"),
),
},
},
Expand All @@ -35,6 +40,10 @@ func TestAccDataSourceAwsPrefixList_filter(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_id"),
testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_name"),
resource.TestMatchResourceAttr("data.aws_prefix_list.s3_by_name", "id", regexp.MustCompile(`^pl-[0-9a-z]{8}$`)),
testAccCheckResourceAttrRegionalReverseDnsService("data.aws_prefix_list.s3_by_name", "name", "s3"),
resource.TestMatchResourceAttr("data.aws_prefix_list.s3_by_id", "id", regexp.MustCompile(`^pl-[0-9a-z]{8}$`)),
testAccCheckResourceAttrRegionalReverseDnsService("data.aws_prefix_list.s3_by_id", "name", "s3"),
),
},
},
Expand All @@ -50,13 +59,6 @@ func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {

attr := rs.Primary.Attributes

if attr["name"] != "com.amazonaws.us-west-2.s3" {
return fmt.Errorf("bad name %s", attr["name"])
}
if attr["id"] != "pl-68a54001" {
return fmt.Errorf("bad id %s", attr["id"])
}

var (
cidrBlockSize int
err error
Expand All @@ -74,27 +76,31 @@ func testAccDataSourceAwsPrefixListCheck(name string) resource.TestCheckFunc {
}

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

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]
}
}
`

0 comments on commit c384457

Please sign in to comment.