Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Data Source for AWS instances to get instances that are in a state other than running #4950

Merged

Conversation

dieterdemeyer
Copy link
Contributor

Fixes #3339

Changes proposed in this pull request:

  • Also fetch AWS instances that have an instance state other than running. The only state not taken into account is terminated.

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccAWSAvailabilityZones'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run=TestAccAWSAvailabilityZones -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAWSAvailabilityZones_basic
--- FAIL: TestAccAWSAvailabilityZones_basic (0.00s)
	provider_test.go:62: AWS_ACCESS_KEY_ID must be set for acceptance tests
=== RUN   TestAccAWSAvailabilityZones_stateFilter
--- FAIL: TestAccAWSAvailabilityZones_stateFilter (0.00s)
	provider_test.go:62: AWS_ACCESS_KEY_ID must be set for acceptance tests
FAIL
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	0.045s
make: *** [testacc] Error 1
...

@ghost ghost added the size/XS Managed by automation to categorize the size of a PR. label Jun 22, 2018
@dieterdemeyer dieterdemeyer changed the title Also retrieve AWS instances that are in a state other than running Allow Data Source for AWS instances to get instances that are in a state other than running Jun 22, 2018
@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. breaking-change Introduces a breaking change in current functionality; usually deferred to the next major release. service/ec2 Issues and PRs that pertain to the ec2 service. labels Jun 22, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dieterdemeyer 👋 Thanks for submitting this!

While you have the right intentions here, as written this could be a breaking change in behavior for some Terraform configurations. Some may be relying on the existing behavior of only running. Please see the below for ideas of how to implement this without breaking backwards compatibility.

Also I'm noting in your acceptance test output a few things:

make testacc TESTARGS='-run=TestAccAWSAvailabilityZones'

The pull request template only provides an example of which acceptance tests to run. Following typical Go conventions, the test names can be found in aws/data_source_aws_instances_test.go. So in this case: make testacc TESTARGS='-run=TestAccAWSInstancesDataSource' will run the appropriate acceptance tests for this data source.

provider_test.go:62: AWS_ACCESS_KEY_ID must be set for acceptance tests

You'll need to appropriately set either the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables or the AWS_PROFILE environment variables. The testing suite will spin up real infrastructure on AWS and requires credentials to do so.

Please reach out with any questions or if you do not have time to implement the feedback. Thanks!

@@ -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")},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enhancement here needs to allow the instance-state to either be configurable, while leaving the existing default behavior to not break backwards compatibility. This can be accomplished two ways:

Less preferably as a string attribute, but provided as an example, e.g.

"instance_state_name": {
  Type: schema.TypeString,
  Optional: true,
  Default: ec2.InstanceStateNameRunning,
  ValidateFunc: validation.StringInSlice([]string{
    ec2.InstanceStateNamePending,
    ec2.InstanceStateNameRunning,
    ec2.InstanceStateNameShuttingDown,
    ec2.InstanceStateNameStopped,
    ec2.InstanceStateNameStopping,
    ec2.InstanceStateNameTerminated,
  }, false),
},

Where it is referenced in the code with:

	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			&ec2.Filter{
				Name: aws.String("instance-state-name"),
				Values: []*string{aws.String(d.Get("instance_state_name").(string))},
			},
		},
	}

Or, more preferably, as a set so you can configure one or more instance states:

"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),
  },
},

Where it is referenced in the code with:

	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: instanceStateNames,
			},
		},
	}

Either case should implement a new acceptance test that creates two instances and immediately calls the data source that accepts the pending (and probably running as well) instance states.

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Jun 22, 2018
@dieterdemeyer
Copy link
Contributor Author

@bflad,

Thanks for your feedback.
I will implement the required changes and get back to you.

@dieterdemeyer
Copy link
Contributor Author

@bflad,

I made the required changes locally and now I'm trying to write a test case.
Is it allowed to manually setup some AWS instances and run the test case against them ?
If not, I'm not sure how I could create a test case that creates 2 instances where 1 of the instances is in a pending or stopped state that is required for successfully testing this new feature given that I need to write Terraform code to initiate the EC2 instances...
See also #190

@bflad
Copy link
Contributor

bflad commented Jul 2, 2018

@dieterdemeyer testing against pending and running should always catch aws_instance resources after creation. I wouldn't worry about stopped instances. Otherwise, you'll need to implement a PreConfig for the TestStep that manually calls EC2 API calls to stop the instances (in a retry loop) after creation since we do not currently implement the ability to manage aws_instance as stopped within Terraform itself.

No worries if you cannot figure out the testing. I would push up what you have so far.

@ghost ghost added size/M Managed by automation to categorize the size of a PR. and removed size/XS Managed by automation to categorize the size of a PR. labels Jul 3, 2018
@dieterdemeyer
Copy link
Contributor Author

@bflad I modified the required code and tested this locally with one of our AWS accounts. This seems to work.
But my test case doesn't seem to pass. Could you have a look at what I'm missing here ?

My output is as follows:

make testacc TESTARGS='-run=TestAccAWSInstancesDataSource'

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run=TestAccAWSInstancesDataSource -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAWSInstancesDataSource_basic
--- PASS: TestAccAWSInstancesDataSource_basic (149.74s)
=== RUN   TestAccAWSInstancesDataSource_tags
--- PASS: TestAccAWSInstancesDataSource_tags (137.58s)
=== RUN   TestAccAWSInstancesDataSource_instance_state_names
--- FAIL: TestAccAWSInstancesDataSource_instance_state_names (5.29s)
	testing.go:518: Step 0 error: Error refreshing: 1 error(s) occurred:

		* data.aws_instances.test: 1 error(s) occurred:

		* data.aws_instances.test: data.aws_instances.test: Your query returned no results. Please change your search criteria and try again.
FAIL
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	292.668s
make: *** [testacc] Error 1


data "aws_instances" "test" {
instance_tags {
Name = "*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To properly setup the resource and data source ordering, you can reference the aws_instance resources created above 😄

Name = "${aws_instance.test.0.tags["Name"]}"

This should be enough to delay the data source to catch the two new instances 👍 Let me know if this still is causing an issue and we can dive in deeper. Thanks for your work here!

@@ -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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should list the default behavior of returning only running here 👍

@bflad bflad removed the breaking-change Introduces a breaking change in current functionality; usually deferred to the next major release. label Jul 3, 2018
@ghost ghost added the size/M Managed by automation to categorize the size of a PR. label Jul 3, 2018
@dieterdemeyer
Copy link
Contributor Author

@bflad Thanks for your assistance. I've updated the code and ran the tests. It seems to work with your suggestion and you can find the output below:

make testacc TESTARGS='-run=TestAccAWSInstancesDataSource'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run=TestAccAWSInstancesDataSource -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAWSInstancesDataSource_basic
--- PASS: TestAccAWSInstancesDataSource_basic (128.95s)
=== RUN   TestAccAWSInstancesDataSource_tags
--- PASS: TestAccAWSInstancesDataSource_tags (130.73s)
=== RUN   TestAccAWSInstancesDataSource_instance_state_names
--- PASS: TestAccAWSInstancesDataSource_instance_state_names (128.10s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	387.825s

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Jul 4, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks @dieterdemeyer! 🚀

make testacc TEST=./aws TESTARGS='-run=TestAccAWSInstancesDataSource'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSInstancesDataSource -timeout 120m
=== RUN   TestAccAWSInstancesDataSource_basic
--- PASS: TestAccAWSInstancesDataSource_basic (100.72s)
=== RUN   TestAccAWSInstancesDataSource_tags
--- PASS: TestAccAWSInstancesDataSource_tags (109.24s)
=== RUN   TestAccAWSInstancesDataSource_instance_state_names
--- PASS: TestAccAWSInstancesDataSource_instance_state_names (99.12s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	309.122s

@bflad bflad added this to the v1.26.0 milestone Jul 4, 2018
@bflad bflad merged commit 9ea5e04 into hashicorp:master Jul 4, 2018
bflad added a commit that referenced this pull request Jul 4, 2018
@dieterdemeyer
Copy link
Contributor Author

dieterdemeyer commented Jul 4, 2018

@bflad Thanks for getting this merged !
Any idea when a new release will be made that includes this feature ?
Going by the ChangeLog.md, this provider is released weekly. Is that correct ?

@bflad
Copy link
Contributor

bflad commented Jul 4, 2018

This has been released in version 1.26.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 4, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service. size/M Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

data_source_aws_instances.go hard coded for instance-state-name running
2 participants