forked from hashicorp/terraform-provider-aws
/
data_source_aws_autoscaling_groups.go
133 lines (114 loc) · 3.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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},
},
"arns": {
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": {
Type: schema.TypeString,
Required: true,
},
"values": {
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 rawName []string
var rawArn []string
var err error
tf := d.Get("filter").(*schema.Set)
if tf.Len() > 0 {
input := &autoscaling.DescribeTagsInput{
Filters: expandAsgTagFilters(tf.List()),
}
err = conn.DescribeTagsPages(input, func(resp *autoscaling.DescribeTagsOutput, lastPage bool) bool {
for _, v := range resp.Tags {
rawName = append(rawName, aws.StringValue(v.ResourceId))
}
return !lastPage
})
maxAutoScalingGroupNames := 1600
for i := 0; i < len(rawName); i += maxAutoScalingGroupNames {
end := i + maxAutoScalingGroupNames
if end > len(rawName) {
end = len(rawName)
}
nameInput := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: aws.StringSlice(rawName[i:end]),
MaxRecords: aws.Int64(100),
}
err = conn.DescribeAutoScalingGroupsPages(nameInput, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
for _, group := range resp.AutoScalingGroups {
rawArn = append(rawArn, aws.StringValue(group.AutoScalingGroupARN))
}
return !lastPage
})
}
} else {
err = conn.DescribeAutoScalingGroupsPages(&autoscaling.DescribeAutoScalingGroupsInput{}, func(resp *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
for _, group := range resp.AutoScalingGroups {
rawName = append(rawName, aws.StringValue(group.AutoScalingGroupName))
rawArn = append(rawArn, aws.StringValue(group.AutoScalingGroupARN))
}
return !lastPage
})
}
if err != nil {
return fmt.Errorf("Error fetching Autoscaling Groups: %s", err)
}
sort.Strings(rawName)
sort.Strings(rawArn)
if err := d.Set("names", rawName); err != nil {
return fmt.Errorf("[WARN] Error setting Autoscaling Group Names: %s", err)
}
if err := d.Set("arns", rawArn); err != nil {
return fmt.Errorf("[WARN] Error setting Autoscaling Group Arns: %s", err)
}
return nil
}
func expandAsgTagFilters(in []interface{}) []*autoscaling.Filter {
out := make([]*autoscaling.Filter, 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
}