From 3255921b6e9832c9036a361685d5f86904ca28d3 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 20 Nov 2016 09:21:55 -0500 Subject: [PATCH] aws_prefix_list data source - Add ability to select PL by name. (#10248) --- .../aws/data_source_aws_prefix_list.go | 20 ++++++++++++------- .../aws/data_source_aws_prefix_list_test.go | 9 +++++++-- .../providers/aws/d/prefix_list.html.markdown | 4 +++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_prefix_list.go b/builtin/providers/aws/data_source_aws_prefix_list.go index 3dafdecfd..8bed85506 100644 --- a/builtin/providers/aws/data_source_aws_prefix_list.go +++ b/builtin/providers/aws/data_source_aws_prefix_list.go @@ -16,17 +16,18 @@ func dataSourceAwsPrefixList() *schema.Resource { Schema: map[string]*schema.Schema{ "prefix_list_id": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, }, // Computed values. "id": &schema.Schema{ Type: schema.TypeString, Computed: true, }, - "name": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, "cidr_blocks": &schema.Schema{ Type: schema.TypeList, Computed: true, @@ -42,8 +43,13 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error req := &ec2.DescribePrefixListsInput{} if prefixListID := d.Get("prefix_list_id"); prefixListID != "" { - req.PrefixListIds = []*string{aws.String(prefixListID.(string))} + req.PrefixListIds = aws.StringSlice([]string{prefixListID.(string)}) } + req.Filters = buildEC2AttributeFilterList( + map[string]string{ + "prefix-list-name": d.Get("name").(string), + }, + ) log.Printf("[DEBUG] DescribePrefixLists %s\n", req) resp, err := conn.DescribePrefixLists(req) @@ -51,7 +57,7 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error return err } if resp == nil || len(resp.PrefixLists) == 0 { - return fmt.Errorf("no matching prefix list found; the prefix list ID may be invalid or not exist in the current region") + return fmt.Errorf("no matching prefix list found; the prefix list ID or name may be invalid or not exist in the current region") } pl := resp.PrefixLists[0] diff --git a/builtin/providers/aws/data_source_aws_prefix_list_test.go b/builtin/providers/aws/data_source_aws_prefix_list_test.go index 27da3776b..c9ad308d0 100644 --- a/builtin/providers/aws/data_source_aws_prefix_list_test.go +++ b/builtin/providers/aws/data_source_aws_prefix_list_test.go @@ -17,7 +17,8 @@ func TestAccDataSourceAwsPrefixList(t *testing.T) { resource.TestStep{ Config: testAccDataSourceAwsPrefixListConfig, Check: resource.ComposeTestCheckFunc( - testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3"), + testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_id"), + testAccDataSourceAwsPrefixListCheck("data.aws_prefix_list.s3_by_name"), ), }, }, @@ -61,7 +62,11 @@ provider "aws" { region = "us-west-2" } -data "aws_prefix_list" "s3" { +data "aws_prefix_list" "s3_by_id" { prefix_list_id = "pl-68a54001" } + +data "aws_prefix_list" "s3_by_name" { + name = "com.amazonaws.us-west-2.s3" +} ` diff --git a/website/source/docs/providers/aws/d/prefix_list.html.markdown b/website/source/docs/providers/aws/d/prefix_list.html.markdown index 94de7397f..bc46c9c12 100644 --- a/website/source/docs/providers/aws/d/prefix_list.html.markdown +++ b/website/source/docs/providers/aws/d/prefix_list.html.markdown @@ -49,7 +49,9 @@ 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` - (Required) The ID of the prefix list to select. +* `prefix_list_id` - (Optional) The ID of the prefix list to select. + +* `name` - (Optional) The name of the prefix list to select. ## Attributes Reference