-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers.go
61 lines (46 loc) · 1.58 KB
/
helpers.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
package ec2
import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
func GetEC2InstanceInfo(client ec2iface.EC2API, instances []*string) (output []*ec2.Instance, err error) {
keyedInstances := make(map[string]*ec2.Instance)
// Set up our DI input object
diInput := &ec2.DescribeInstancesInput{
InstanceIds: instances,
}
describeInstancesPager := func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
for _, reservation := range page.Reservations {
for _, i := range reservation.Instances {
keyedInstances[*i.InstanceId] = i
}
}
// If it's not the last page, continue
return !lastPage
}
// Fetch all the instances described
if err = client.DescribeInstancesPages(diInput, describeInstancesPager); err != nil {
return nil, fmt.Errorf("Could not describe EC2 instances\n%v", err)
}
for _, i := range instances {
output = append(output, keyedInstances[*i])
}
return output, nil
}
// GetEC2InstanceTags accepts any number of instance strings and returns a populated InstanceTags{} object for each instance
func GetEC2InstanceTags(client ec2iface.EC2API, instances []*string) (ec2Tags map[string]Tags, err error) {
instanceInfo, err := GetEC2InstanceInfo(client, instances)
if err != nil {
return nil, fmt.Errorf("Error when trying to retrieve EC2 instance tags\n%v", err)
}
ec2Tags = make(map[string]Tags)
for _, i := range instanceInfo {
tagMap := make(map[string]string)
for _, tag := range i.Tags {
tagMap[*tag.Key] = *tag.Value
}
ec2Tags[*i.InstanceId] = tagMap
}
return ec2Tags, nil
}