-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
63 lines (50 loc) · 1.5 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
62
63
package ec2
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
func getEC2InstanceInfo(context ec2iface.EC2API, instances ...string) (output []*ec2.DescribeInstancesOutput, err error) {
instanceInput := &ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice(instances),
}
results, err := context.DescribeInstances(instanceInput)
if err != nil {
return nil, err
}
output = append(output, results)
for results.NextToken != nil {
if *results.NextToken != "" {
instanceInput.SetNextToken(*results.NextToken)
}
// Get our next page of results
results, err = context.DescribeInstances(instanceInput)
if err != nil {
return output, err
}
output = append(output, results)
}
return output, nil
}
// GetEC2InstanceTags accepts any number of instance strings and returns a populated InstanceTags{} object for each instance
func GetEC2InstanceTags(context ec2iface.EC2API, instances ...string) (tags []InstanceTags, err error) {
instanceInfo, err := getEC2InstanceInfo(context, instances...)
if err != nil {
return nil, err
}
for _, page := range instanceInfo {
for _, res := range page.Reservations {
for _, inst := range res.Instances {
tagStruct := &InstanceTags{
Tags: make(map[string]string),
InstanceID: *inst.InstanceId,
}
for _, tag := range inst.Tags {
tagStruct.Tags[*tag.Key] = *tag.Value
}
tags = append(tags, *tagStruct)
}
}
}
return tags, nil
}