-
Notifications
You must be signed in to change notification settings - Fork 4
/
upstream_aws.go
82 lines (66 loc) · 2.02 KB
/
upstream_aws.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
package main
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
)
type AutoScalingGroupUpstreamSource struct {
AutoScalingGroupId string
Region string
}
// UpdatePeers uses the AWS SDK to update the list of peers available in an AutoScalingGroup
func (as AutoScalingGroupUpstreamSource) UpdatePeers() ([]string, error) {
sess := session.Must(session.NewSession())
asg := autoscaling.New(sess, &aws.Config{Region: aws.String(as.Region)})
ec := ec2.New(sess, &aws.Config{Region: aws.String(as.Region)})
group, err := asg.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{aws.String(as.AutoScalingGroupId)},
})
if err != nil {
return nil, err
}
if len(group.AutoScalingGroups) != 1 {
return nil, errors.New(fmt.Sprintf("No AutoScalingGroup found '%s'", as.AutoScalingGroupId))
}
ids := make([]*string, len(group.AutoScalingGroups[0].Instances))
for i, inst := range group.AutoScalingGroups[0].Instances {
ids[i] = inst.InstanceId
}
instances, err := ec.DescribeInstances(&ec2.DescribeInstancesInput{
InstanceIds: ids,
Filters: []*ec2.Filter{
{
Name: aws.String("instance-state-name"),
Values: []*string{ aws.String("running") },
},
},
})
if err != nil {
return nil, err
}
peers := make([]string, len(ids))
j := 0
for _, reservation := range instances.Reservations {
for _, inst := range reservation.Instances {
peers[j] = *inst.PrivateIpAddress
j++
}
}
return peers, nil
}
func NewAutoScalingGroupUpstreamSource(region string, asgID string) (*AutoScalingGroupUpstreamSource, error) {
return &AutoScalingGroupUpstreamSource{
Region: region,
AutoScalingGroupId: asgID,
}, nil
}
func MustNewAutoScalingGroupUpstreamSource(region string, asgID string) *AutoScalingGroupUpstreamSource {
asgus, err := NewAutoScalingGroupUpstreamSource(region, asgID)
if err != nil {
panic(err)
}
return asgus
}