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{
"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]

View File

@ -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"
}
`

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
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