forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_aws_autoscaling_groups.go
102 lines (85 loc) · 2.29 KB
/
data_source_aws_autoscaling_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package aws
import (
"fmt"
"log"
"sort"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsAutoscalingGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsAutoscalingGroupsRead,
Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
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,
},
},
},
},
},
}
}
func dataSourceAwsAutoscalingGroupsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).autoscalingconn
log.Printf("[DEBUG] Reading Autoscaling Groups.")
d.SetId(time.Now().UTC().String())
var raw []string
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)
if err := d.Set("names", raw); err != nil {
return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err)
}
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
}