provider/aws: Support filtering in ASG data source (#14501)
This commit is contained in:
parent
04c62ae406
commit
740c92fc67
|
@ -6,6 +6,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
)
|
)
|
||||||
|
@ -20,6 +21,24 @@ func dataSourceAwsAutoscalingGroups() *schema.Resource {
|
||||||
Computed: true,
|
Computed: true,
|
||||||
Elem: &schema.Schema{Type: schema.TypeString},
|
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.")
|
log.Printf("[DEBUG] Reading Autoscaling Groups.")
|
||||||
d.SetId(time.Now().UTC().String())
|
d.SetId(time.Now().UTC().String())
|
||||||
|
|
||||||
resp, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
|
var raw []string
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error fetching Autoscaling Groups: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
raw := make([]string, len(resp.AutoScalingGroups))
|
tf := d.Get("filter").(*schema.Set)
|
||||||
for i, v := range resp.AutoScalingGroups {
|
if tf.Len() > 0 {
|
||||||
raw[i] = *v.AutoScalingGroupName
|
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)
|
sort.Strings(raw)
|
||||||
|
@ -49,3 +86,17 @@ func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
`, rInt1, rInt2, rInt3)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,17 @@ ASGs within a specific region. This will allow you to pass a list of AutoScaling
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
```hcl
|
```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" {
|
resource "aws_autoscaling_notification" "slack_notifications" {
|
||||||
group_names = ["${data.aws_autoscaling_groups.groups.names}"]
|
group_names = ["${data.aws_autoscaling_groups.groups.names}"]
|
||||||
|
@ -32,7 +42,9 @@ resource "aws_autoscaling_notification" "slack_notifications" {
|
||||||
|
|
||||||
## Argument Reference
|
## 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
|
## Attributes Reference
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue