provider/aws: Support filtering in ASG data source (#14501)

This commit is contained in:
Radek Simko 2017-05-18 17:40:49 +02:00 committed by GitHub
parent 04c62ae406
commit 740c92fc67
3 changed files with 83 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import (
"sort"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/hashicorp/terraform/helper/schema"
)
@ -20,6 +21,24 @@ func dataSourceAwsAutoscalingGroups() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"values": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
},
}
}
@ -30,14 +49,32 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Reading Autoscaling Groups.")
d.SetId(time.Now().UTC().String())
resp, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
if err != nil {
return fmt.Errorf("Error fetching Autoscaling Groups: %s", err)
}
var raw []string
raw := make([]string, len(resp.AutoScalingGroups))
for i, v := range resp.AutoScalingGroups {
raw[i] = *v.AutoScalingGroupName
tf := d.Get("filter").(*schema.Set)
if tf.Len() > 0 {
out, err := conn.DescribeTags(&autoscaling.DescribeTagsInput{
Filters: expandAsgTagFilters(tf.List()),
})
if err != nil {
return err
}
raw = make([]string, len(out.Tags))
for i, v := range out.Tags {
raw[i] = *v.ResourceId
}
} else {
resp, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
if err != nil {
return fmt.Errorf("Error fetching Autoscaling Groups: %s", err)
}
raw = make([]string, len(resp.AutoScalingGroups))
for i, v := range resp.AutoScalingGroups {
raw[i] = *v.AutoScalingGroupName
}
}
sort.Strings(raw)
@ -49,3 +86,17 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{}
return nil
}
func expandAsgTagFilters(in []interface{}) []*autoscaling.Filter {
out := make([]*autoscaling.Filter, len(in), len(in))
for i, filter := range in {
m := filter.(map[string]interface{})
values := expandStringList(m["values"].(*schema.Set).List())
out[i] = &autoscaling.Filter{
Name: aws.String(m["name"].(string)),
Values: values,
}
}
return out
}

View File

@ -202,6 +202,16 @@ resource "aws_autoscaling_group" "barbaz" {
}
}
data "aws_autoscaling_groups" "group_list" {}
data "aws_autoscaling_groups" "group_list" {
filter {
name = "key"
values = ["Foo"]
}
filter {
name = "value"
values = ["foo-bar"]
}
}
`, rInt1, rInt2, rInt3)
}

View File

@ -14,7 +14,17 @@ ASGs within a specific region. This will allow you to pass a list of AutoScaling
## Example Usage
```hcl
data "aws_autoscaling_groups" "groups" {}
data "aws_autoscaling_groups" "groups" {
filter {
name = "key"
values = ["Team"]
}
filter {
name = "value"
values = ["Pets"]
}
}
resource "aws_autoscaling_notification" "slack_notifications" {
group_names = ["${data.aws_autoscaling_groups.groups.names}"]
@ -32,7 +42,9 @@ resource "aws_autoscaling_notification" "slack_notifications" {
## Argument Reference
The data source currently takes no arguments as it uses the current region in which the provider is currently operating.
* `filter` - (Optional) A filter used to scope the list e.g. by tags. See [related docs](http://docs.aws.amazon.com/AutoScaling/latest/APIReference/API_Filter.html).
* `name` - (Required) The name of the filter. The valid values are: `auto-scaling-group`, `key`, `value`, and `propagate-at-launch`.
* `values` - (Required) The value of the filter.
## Attributes Reference