diff --git a/aws/data_source_aws_prefix_list.go b/aws/data_source_aws_prefix_list.go index 82c8a2ae37f..fdb0fe24a39 100644 --- a/aws/data_source_aws_prefix_list.go +++ b/aws/data_source_aws_prefix_list.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "sort" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" @@ -45,19 +46,22 @@ 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) - if err != nil { + switch { + case err != nil: return err - } - if resp == nil || len(resp.PrefixLists) == 0 { + case resp == nil || len(resp.PrefixLists) == 0: return fmt.Errorf("no matching prefix list found; the prefix list ID or name may be invalid or not exist in the current region") + case len(resp.PrefixLists) > 1: + return fmt.Errorf("more than one prefix list matched the given set of criteria") } pl := resp.PrefixLists[0] @@ -65,11 +69,11 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error d.SetId(*pl.PrefixListId) d.Set("name", pl.PrefixListName) - cidrs := make([]string, len(pl.Cidrs)) - for i, v := range pl.Cidrs { - cidrs[i] = *v + cidrs := aws.StringValueSlice(pl.Cidrs) + sort.Strings(cidrs) + if err := d.Set("cidr_blocks", cidrs); err != nil { + return fmt.Errorf("failed to set cidr blocks of prefix list %s: %s", d.Id(), err) } - d.Set("cidr_blocks", cidrs) return nil } diff --git a/aws/data_source_aws_prefix_list_test.go b/aws/data_source_aws_prefix_list_test.go index e74778a01b3..ae351ae06b0 100644 --- a/aws/data_source_aws_prefix_list_test.go +++ b/aws/data_source_aws_prefix_list_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "strconv" "testing" @@ -98,3 +99,47 @@ data "aws_prefix_list" "s3_by_id" { } } ` + +func TestAccDataSourceAwsPrefixList_matchesTooMany(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsPrefixListConfig_matchesTooMany, + ExpectError: regexp.MustCompile(`more than one prefix list matched the given set of criteria`), + }, + }, + }) +} + +const testAccDataSourceAwsPrefixListConfig_matchesTooMany = ` +data "aws_prefix_list" "test" {} +` + +func TestAccDataSourceAwsPrefixList_nameDoesNotOverrideFilter(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + // The vanilla DescribePrefixLists API only supports filtering by + // id and name. In this case, the `name` attribute and `prefix-list-id` + // filter have been set up such that they conflict, thus proving + // that both criteria took effect. + Config: testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter, + ExpectError: regexp.MustCompile(`no matching prefix list found`), + }, + }, + }) +} + +const testAccDataSourceAwsPrefixListConfig_nameDoesNotOverrideFilter = ` +data "aws_prefix_list" "test" { + name = "com.amazonaws.us-west-2.s3" + filter { + name = "prefix-list-id" + values = ["pl-00a54069"] # com.amazonaws.us-west-2.dynamodb + } +} +` diff --git a/website/docs/d/prefix_list.html.markdown b/website/docs/d/prefix_list.html.markdown index 0caeb0809e8..b2b5c8ff356 100644 --- a/website/docs/d/prefix_list.html.markdown +++ b/website/docs/d/prefix_list.html.markdown @@ -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 --- @@ -44,6 +44,15 @@ resource "aws_network_acl_rule" "private_s3" { } ``` +### Find the regional DynamoDB prefix list + +```hcl +data "aws_region" "current" {} +data "aws_prefix_list" "dynamo" { + name = "com.amazonaws.${data.aws_region.current.name}.dynamodb" +} +``` + ### Filter ```hcl