provider/aws: rename local_name_filter attribute to name_regex

Renamed the local_name_filter attribute to name_regex and made it clear in the
docs that this runs locally and could have a performance impact on a large set
of AMIs returned from AWS.
This commit is contained in:
Shawn Silva 2016-08-23 08:44:07 -04:00
parent 6977fff406
commit bf68590f02
3 changed files with 22 additions and 18 deletions

View File

@ -44,7 +44,7 @@ func dataSourceAwsAmi() *schema.Resource {
}, },
}, },
}, },
"local_name_filter": &schema.Schema{ "name_regex": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
@ -212,11 +212,11 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
executableUsers, executableUsersOk := d.GetOk("executable_users") executableUsers, executableUsersOk := d.GetOk("executable_users")
filters, filtersOk := d.GetOk("filter") filters, filtersOk := d.GetOk("filter")
localNameFilter, localNameFilterOk := d.GetOk("local_name_filter") nameRegex, nameRegexOk := d.GetOk("name_regex")
owners, ownersOk := d.GetOk("owners") owners, ownersOk := d.GetOk("owners")
if executableUsersOk == false && filtersOk == false && localNameFilterOk == false && ownersOk == false { if executableUsersOk == false && filtersOk == false && nameRegexOk == false && ownersOk == false {
return fmt.Errorf("One of executable_users, filters, local_name_filter, or owners must be assigned") return fmt.Errorf("One of executable_users, filters, name_regex, or owners must be assigned")
} }
params := &ec2.DescribeImagesInput{} params := &ec2.DescribeImagesInput{}
@ -236,8 +236,8 @@ func dataSourceAwsAmiRead(d *schema.ResourceData, meta interface{}) error {
} }
var filteredImages []*ec2.Image var filteredImages []*ec2.Image
if localNameFilterOk == true { if nameRegexOk == true {
r := regexp.MustCompile(localNameFilter.(string)) r := regexp.MustCompile(nameRegex.(string))
for _, image := range resp.Images { for _, image := range resp.Images {
if r.MatchString(*image.Name) == true { if r.MatchString(*image.Name) == true {
filteredImages = append(filteredImages, image) filteredImages = append(filteredImages, image)

View File

@ -145,10 +145,10 @@ func TestAccAWSAmiDataSource_localNameFilter(t *testing.T) {
Providers: testAccProviders, Providers: testAccProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccCheckAwsAmiDataSourceLocalNameFilterConfig, Config: testAccCheckAwsAmiDataSourceNameRegexConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAwsAmiDataSourceID("data.aws_ami.local_filtered_ami"), testAccCheckAwsAmiDataSourceID("data.aws_ami.name_regex_filtered_ami"),
resource.TestMatchResourceAttr("data.aws_ami.local_filtered_ami", "image_id", regexp.MustCompile("^ami-")), resource.TestMatchResourceAttr("data.aws_ami.name_regex_filtered_ami", "image_id", regexp.MustCompile("^ami-")),
), ),
}, },
}, },
@ -262,15 +262,15 @@ data "aws_ami" "amazon_ami" {
} }
` `
// Testing local_name_filter parameter // Testing name_regex parameter
const testAccCheckAwsAmiDataSourceLocalNameFilterConfig = ` const testAccCheckAwsAmiDataSourceNameRegexConfig = `
data "aws_ami" "local_filtered_ami" { data "aws_ami" "name_regex_filtered_ami" {
most_recent = true most_recent = true
owners = ["amazon"] owners = ["amazon"]
filter { filter {
name = "name" name = "name"
values = ["amzn-ami-*"] values = ["amzn-ami-*"]
} }
local_name_filter = "^amzn-ami-\\d{3}[5].*-ecs-optimized" name_regex = "^amzn-ami-\\d{3}[5].*-ecs-optimized"
} }
` `

View File

@ -25,7 +25,7 @@ data "aws_ami" "nat_ami" {
name = "name" name = "name"
values = ["amzn-ami-vpc-nat*"] values = ["amzn-ami-vpc-nat*"]
} }
local_name_filter = "^myami-\\d{3}" name_regex = "^myami-\\d{3}"
owners = ["self"] owners = ["self"]
} }
``` ```
@ -42,13 +42,17 @@ recent AMI.
several valid keys, for a full reference, check out several valid keys, for a full reference, check out
[describe-images in the AWS CLI reference][1]. [describe-images in the AWS CLI reference][1].
* `local_name_filter` - (Optional) A regex string to apply to the AMI list returned
by AWS. This allows more advanced filtering not supported from the AWS API.
* `owners` - (Optional) Limit search to specific AMI owners. Valid items are the numeric * `owners` - (Optional) Limit search to specific AMI owners. Valid items are the numeric
account ID, `amazon`, or `self`. account ID, `amazon`, or `self`.
~> **NOTE:** At least one of `executable_users`, `filter`, or `owners` must be specified. * `name_regex` - (Optional) A regex string to apply to the AMI list returned
by AWS. This allows more advanced filtering not supported from the AWS API. This
filtering is done locally on what AWS returns, and could have a performance
impact if the result is large. It is recommended to combine this with other
options to narrow down the list AWS returns.
~> **NOTE:** At least one of `executable_users`, `filter`, `owners`, or
`name_regex` must be specified.
~> **NOTE:** If more or less than a single match is returned by the search, ~> **NOTE:** If more or less than a single match is returned by the search,
Terraform will fail. Ensure that your search is specific enough to return Terraform will fail. Ensure that your search is specific enough to return