-
Notifications
You must be signed in to change notification settings - Fork 0
/
elb.go
60 lines (51 loc) · 1.41 KB
/
elb.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
package goqueryaws
import (
// "fmt"
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
)
/*
GetELBsByTag queries AWS ELB by tags and returns a slice of JSON
encoded service.elb.LoadBalancerDescription.
*/
func GetELBsByTag(tagKey string, tagValue string) ([][]byte, error) {
// see https://github.com/aws/aws-sdk-go/blob/master/aws/session/session.go#L30-L33
// for session struct
svc := elb.New(session.Must(session.NewSession()))
var elbs [][]byte
err := svc.DescribeLoadBalancersPages(nil,
func(page *elb.DescribeLoadBalancersOutput, lastPage bool) bool {
for _, elb_desc := range page.LoadBalancerDescriptions {
// Describes the tags associated with an elb.
param := elb.DescribeTagsInput{
LoadBalancerNames: []*string{
aws.String(*elb_desc.LoadBalancerName),
},
}
result, err := svc.DescribeTags(¶m)
if err != nil {
return false
}
for _, tag_desc := range result.TagDescriptions {
for _, tag := range tag_desc.Tags {
if tagKey == *tag.Key && tagValue == *tag.Value {
item, err := json.Marshal(elb_desc)
if err != nil {
// Todo: count the errors then return it as an error.
}
elbs = append(elbs, item)
break
}
}
}
}
if lastPage {
return false
} else {
return true
}
})
return elbs, err
}