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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions aws/data_source_aws_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Name: aws.String("instance-state-name"),
Values: []*string{
aws.String("running"),
aws.String("stopped"),
aws.String("stopping"),
aws.String("pending"),
aws.String("shutting-down"),
},
},
},
}
Expand Down