aws_prefix_list data source - Add ability to select PL by name. (#10248)

This commit is contained in:
Kit Ewbank 2016-11-20 09:21:55 -05:00 committed by Paul Stack
parent 7619d66002
commit 3255921b6e
3 changed files with 23 additions and 10 deletions

View File

@ -16,17 +16,18 @@ func dataSourceAwsPrefixList() *schema.Resource {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"prefix_list_id": &schema.Schema{ "prefix_list_id": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
}, },
// Computed values. // Computed values.
"id": &schema.Schema{ "id": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"cidr_blocks": &schema.Schema{ "cidr_blocks": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Computed: true, Computed: true,
@ -42,8 +43,13 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error
req := &ec2.DescribePrefixListsInput{} req := &ec2.DescribePrefixListsInput{}
if prefixListID := d.Get("prefix_list_id"); prefixListID != "" { 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) log.Printf("[DEBUG] DescribePrefixLists %s\n", req)
resp, err := conn.DescribePrefixLists(req) resp, err := conn.DescribePrefixLists(req)
@ -51,7 +57,7 @@ func dataSourceAwsPrefixListRead(d *schema.ResourceData, meta interface{}) error
return err return err
} }
if resp == nil || len(resp.PrefixLists) == 0 { 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] pl := resp.PrefixLists[0]

View File

@ -17,7 +17,8 @@ func TestAccDataSourceAwsPrefixList(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccDataSourceAwsPrefixListConfig, Config: testAccDataSourceAwsPrefixListConfig,
Check: resource.ComposeTestCheckFunc( 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" region = "us-west-2"
} }
data "aws_prefix_list" "s3" { data "aws_prefix_list" "s3_by_id" {
prefix_list_id = "pl-68a54001" prefix_list_id = "pl-68a54001"
} }
data "aws_prefix_list" "s3_by_name" {
name = "com.amazonaws.us-west-2.s3"
}
` `

View File

@ -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 prefix lists. The given filters must match exactly one prefix list
whose data will be exported as attributes. 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 ## Attributes Reference