From 2db7e9a73d9735811f5fdfb561cea3a9985a0d40 Mon Sep 17 00:00:00 2001 From: Dieter De Meyer Date: Fri, 22 Jun 2018 12:10:36 +0200 Subject: [PATCH 1/3] Also retrieve AWS instances that are in a state other than running --- aws/data_source_aws_instances.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_instances.go b/aws/data_source_aws_instances.go index 7196e3189e7..33f6e8a760d 100644 --- a/aws/data_source_aws_instances.go +++ b/aws/data_source_aws_instances.go @@ -50,8 +50,14 @@ func dataSourceAwsInstancesRead(d *schema.ResourceData, meta interface{}) error params := &ec2.DescribeInstancesInput{ Filters: []*ec2.Filter{ &ec2.Filter{ - Name: aws.String("instance-state-name"), - Values: []*string{aws.String("running")}, + Name: aws.String("instance-state-name"), + Values: []*string{ + aws.String("running"), + aws.String("stopped"), + aws.String("stopping"), + aws.String("pending"), + aws.String("shutting-down"), + }, }, }, } From a2e194b726a40310667022403bd5e26e4e568c35 Mon Sep 17 00:00:00 2001 From: Dieter De Meyer Date: Tue, 3 Jul 2018 10:34:19 +0200 Subject: [PATCH 2/3] Added argument for defining instance state names + added test case --- aws/data_source_aws_instances.go | 31 +++++++++++---- aws/data_source_aws_instances_test.go | 54 ++++++++++++++++++++++++++ website/docs/d/instances.html.markdown | 4 ++ 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/aws/data_source_aws_instances.go b/aws/data_source_aws_instances.go index 33f6e8a760d..e0b094e2042 100644 --- a/aws/data_source_aws_instances.go +++ b/aws/data_source_aws_instances.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" ) func dataSourceAwsInstances() *schema.Resource { @@ -17,6 +18,21 @@ func dataSourceAwsInstances() *schema.Resource { Schema: map[string]*schema.Schema{ "filter": dataSourceFiltersSchema(), "instance_tags": tagsSchemaComputed(), + "instance_state_names": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice([]string{ + ec2.InstanceStateNamePending, + ec2.InstanceStateNameRunning, + ec2.InstanceStateNameShuttingDown, + ec2.InstanceStateNameStopped, + ec2.InstanceStateNameStopping, + ec2.InstanceStateNameTerminated, + }, false), + }, + }, "ids": { Type: schema.TypeList, @@ -47,20 +63,19 @@ func dataSourceAwsInstancesRead(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("One of filters or instance_tags must be assigned") } + instanceStateNames := []*string{aws.String(ec2.InstanceStateNameRunning)} + if v, ok := d.GetOk("instance_state_names"); ok && len(v.(*schema.Set).List()) > 0 { + instanceStateNames = expandStringSet(v.(*schema.Set)) + } params := &ec2.DescribeInstancesInput{ Filters: []*ec2.Filter{ &ec2.Filter{ - Name: aws.String("instance-state-name"), - Values: []*string{ - aws.String("running"), - aws.String("stopped"), - aws.String("stopping"), - aws.String("pending"), - aws.String("shutting-down"), - }, + Name: aws.String("instance-state-name"), + Values: instanceStateNames, }, }, } + if filtersOk { params.Filters = append(params.Filters, buildAwsDataSourceFilters(filters.(*schema.Set))...) diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index 2dd92bfa5b1..60c7f7cb2e3 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -43,6 +43,22 @@ func TestAccAWSInstancesDataSource_tags(t *testing.T) { }) } +func TestAccAWSInstancesDataSource_instance_state_names(t *testing.T) { + rInt := acctest.RandInt() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccInstancesDataSourceConfig_instance_state_names(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_instances.test", "ids.#", "2"), + ), + }, + }, + }) +} + const testAccInstancesDataSourceConfig_ids = ` data "aws_ami" "ubuntu" { most_recent = true @@ -113,3 +129,41 @@ data "aws_instances" "test" { } `, rInt) } + +func testAccInstancesDataSourceConfig_instance_state_names(rInt int) string { + return fmt.Sprintf(` +data "aws_ami" "ubuntu" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["099720109477"] # Canonical +} + +resource "aws_instance" "test" { + count = 2 + ami = "${data.aws_ami.ubuntu.id}" + instance_type = "t2.micro" + tags { + Name = "TfAccTest-HelloWorld" + TestSeed = "%[1]d" + } +} + +data "aws_instances" "test" { + instance_tags { + Name = "*" + } + + instance_state_names = [ "pending", "running" ] +} +`, rInt) +} diff --git a/website/docs/d/instances.html.markdown b/website/docs/d/instances.html.markdown index d09e16c3cc6..c67f990862d 100644 --- a/website/docs/d/instances.html.markdown +++ b/website/docs/d/instances.html.markdown @@ -32,6 +32,8 @@ data "aws_instances" "test" { name = "instance.group-id" values = ["sg-12345678"] } + + instance_state_names = [ "running", "stopped" ] } resource "aws_eip" "test" { @@ -45,6 +47,8 @@ resource "aws_eip" "test" { * `instance_tags` - (Optional) A mapping of tags, each pair of which must exactly match a pair on desired instances. +* `instance_state_names` - (Optional) A list of instance states that should be applicable to the desired instances. The permitted values are: `pending, running, shutting-down, stopped, stopping, terminated` + * `filter` - (Optional) One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out [describe-instances in the AWS CLI reference][1]. From 35790716f0bda46491b771611686459e176a4188 Mon Sep 17 00:00:00 2001 From: Dieter De Meyer Date: Tue, 3 Jul 2018 20:10:20 +0200 Subject: [PATCH 3/3] Fix test case + update documentation --- aws/data_source_aws_instances_test.go | 2 +- website/docs/d/instances.html.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_instances_test.go b/aws/data_source_aws_instances_test.go index 60c7f7cb2e3..a886e3b68af 100644 --- a/aws/data_source_aws_instances_test.go +++ b/aws/data_source_aws_instances_test.go @@ -160,7 +160,7 @@ resource "aws_instance" "test" { data "aws_instances" "test" { instance_tags { - Name = "*" + Name = "${aws_instance.test.0.tags["Name"]}" } instance_state_names = [ "pending", "running" ] diff --git a/website/docs/d/instances.html.markdown b/website/docs/d/instances.html.markdown index c67f990862d..dd16fe7a70e 100644 --- a/website/docs/d/instances.html.markdown +++ b/website/docs/d/instances.html.markdown @@ -47,7 +47,7 @@ resource "aws_eip" "test" { * `instance_tags` - (Optional) A mapping of tags, each pair of which must exactly match a pair on desired instances. -* `instance_state_names` - (Optional) A list of instance states that should be applicable to the desired instances. The permitted values are: `pending, running, shutting-down, stopped, stopping, terminated` +* `instance_state_names` - (Optional) A list of instance states that should be applicable to the desired instances. The permitted values are: `pending, running, shutting-down, stopped, stopping, terminated`. The default value is `running`. * `filter` - (Optional) One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out