-
Notifications
You must be signed in to change notification settings - Fork 0
/
godisco.go
68 lines (54 loc) · 1.24 KB
/
godisco.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
package godisco
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"errors"
)
type Godisco struct {}
var tagKey = "Service"
var region = "eu-west-1"
func (gd *Godisco) SetTagKey(newTagKey string) {
tagKey = newTagKey
}
func (gd *Godisco) SetRegion(newRegion string) {
region = newRegion
}
func (gd *Godisco) GetIPs(service string) (ips []string, err error) {
svc := ec2.New(session.New(), aws.NewConfig().WithRegion(region))
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:" + tagKey),
Values: []*string{
aws.String(service),
},
},
},
}
resp, ec2err := svc.DescribeInstances(params)
if ec2err != nil {
err = ec2err
return
}
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
if *instance.State.Name == "running" {
ips = append(ips, *instance.PrivateIpAddress)
}
}
}
if len(ips) == 0 {
err = errors.New("No IPs found for the service " + service)
}
return
}
func (gd *Godisco) GetFirstIp(service string) (ip string, err error) {
ips, ipsErr := gd.GetIPs(service)
if ipsErr != nil {
err = ipsErr
} else {
ip = ips[0]
}
return
}