Add instance_tags as an additional filter

This commit is contained in:
Jake Champlin 2017-01-18 13:14:49 -05:00
parent 9cbd67dd0b
commit c99bc8a807
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
3 changed files with 54 additions and 6 deletions

View File

@ -14,8 +14,9 @@ func dataSourceAwsInstance() *schema.Resource {
Read: dataSourceAwsInstanceRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"tags": dataSourceTagsSchema(),
"filter": dataSourceFiltersSchema(),
"tags": dataSourceTagsSchema(),
"instance_tags": tagsSchemaComputed(),
"instance_id": {
Type: schema.TypeString,
Optional: true,
@ -208,9 +209,10 @@ func dataSourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
filters, filtersOk := d.GetOk("filter")
instanceID, instanceIDOk := d.GetOk("instance_id")
tags, tagsOk := d.GetOk("instance_tags")
if filtersOk == false && instanceIDOk == false {
return fmt.Errorf("One of filters, or instance_id must be assigned")
if filtersOk == false && instanceIDOk == false && tagsOk == false {
return fmt.Errorf("One of filters, instance_tags, or instance_id must be assigned")
}
// Build up search parameters
@ -221,6 +223,11 @@ func dataSourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
if instanceIDOk {
params.InstanceIds = []*string{aws.String(instanceID.(string))}
}
if tagsOk {
params.Filters = append(params.Filters, buildEC2TagFilterList(
tagsFromMap(tags.(map[string]interface{})),
)...)
}
// Perform the lookup
resp, err := conn.DescribeInstances(params)

View File

@ -26,6 +26,26 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) {
})
}
func TestAccAWSInstanceDataSource_tags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccInstanceDataSourceConfig_Tags,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.aws_instance.web-instance", "ami", "ami-4fccb37f"),
resource.TestCheckResourceAttr(
"data.aws_instance.web-instance", "tags.#", "1"),
resource.TestCheckResourceAttr(
"data.aws_instance.web-instance", "instance_type", "m1.small"),
),
},
},
})
}
func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -259,6 +279,24 @@ data "aws_instance" "web-instance" {
}
`
// Use the tags attribute to filter
const testAccInstanceDataSourceConfig_Tags = `
resource "aws_instance" "web" {
# us-west-2
ami = "ami-4fccb37f"
instance_type = "m1.small"
tags {
Name = "HelloWorld"
}
}
data "aws_instance" "web-instance" {
instance_tags {
Name = "${aws_instance.web.tags["Name"]}"
}
}
`
// filter on tag, populate more attributes
const testAccInstanceDataSourceConfig_AzUserData = `
resource "aws_instance" "foo" {

View File

@ -29,13 +29,16 @@ data "aws_instance" "foo" {
## Argument Reference
* `instance_id` - (Optional) Specify the exact Instance ID to populate the data source with.
* `instance_id` - (Optional) Specify the exact Instance ID to populate the data source with.
* `instance_tags` - (Optional) A mapping of tags, each pair of which must
exactly match a pair on the desired Instance.
* `filter` - (Optional) One or more name/value pairs to filter off of. There are
several valid keys, for a full reference, check out
[describe-instances in the AWS CLI reference][1].
~> **NOTE:** At least one of `filter` or `instance_id` must be specified.
~> **NOTE:** At least one of `filter`, `instance_tags`, or `instance_id` must be specified.
~> **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