From 740c92fc6733b14e3c41dc7e40719a987619a301 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 18 May 2017 17:40:49 +0200 Subject: [PATCH] provider/aws: Support filtering in ASG data source (#14501) --- .../aws/data_source_aws_autoscaling_groups.go | 65 +++++++++++++++++-- ...data_source_aws_autoscaling_groups_test.go | 12 +++- .../aws/d/autoscaling_groups.html.markdown | 16 ++++- 3 files changed, 83 insertions(+), 10 deletions(-) diff --git a/builtin/providers/aws/data_source_aws_autoscaling_groups.go b/builtin/providers/aws/data_source_aws_autoscaling_groups.go index 3f2328286..f43f21d4e 100644 --- a/builtin/providers/aws/data_source_aws_autoscaling_groups.go +++ b/builtin/providers/aws/data_source_aws_autoscaling_groups.go @@ -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 +} diff --git a/builtin/providers/aws/data_source_aws_autoscaling_groups_test.go b/builtin/providers/aws/data_source_aws_autoscaling_groups_test.go index 958d089c3..3a6ba7644 100644 --- a/builtin/providers/aws/data_source_aws_autoscaling_groups_test.go +++ b/builtin/providers/aws/data_source_aws_autoscaling_groups_test.go @@ -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) } diff --git a/website/source/docs/providers/aws/d/autoscaling_groups.html.markdown b/website/source/docs/providers/aws/d/autoscaling_groups.html.markdown index 2acbdc2ab..28faf2766 100644 --- a/website/source/docs/providers/aws/d/autoscaling_groups.html.markdown +++ b/website/source/docs/providers/aws/d/autoscaling_groups.html.markdown @@ -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