-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.go
123 lines (102 loc) · 2.94 KB
/
API.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package aws
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
rcmd "github.com/tcotav/rcmd/client"
"net/url"
"regexp"
"strings"
)
const SPLIT = "="
type Hostdef struct {
InstanceId string
Name string
PrivateIpAddress string
InstanceType string
PublicIpAddress string
Tags []string
Result rcmd.HostCmdReturn
}
func GetHostList(inData string, excludeData string) ([]Hostdef, error) {
// create an array of ec2filters
var filterArray []*ec2.Filter
for _, kvpair := range strings.Split(inData, ",") {
kvset := strings.Split(kvpair, SPLIT)
if len(kvset) != 2 {
return nil, errors.New(fmt.Sprintf("invalid tag input: %s", kvpair))
}
// values expects an []string
var valList []*string
valList = append(valList, &kvset[1])
tagName := fmt.Sprintf("tag:%s", kvset[0])
filterArray = append(filterArray, &ec2.Filter{Name: &tagName, Values: valList})
}
ec2svc := ec2.New(session.New())
params := &ec2.DescribeInstancesInput{
Filters: filterArray,
}
resp, err := ec2svc.DescribeInstances(params)
if err != nil {
return nil, err
}
var excludeList map[string]string
if excludeData != "" {
excludeList = make(map[string]string)
for _, kvpair := range strings.Split(excludeData, ",") {
kvset := strings.Split(kvpair, SPLIT)
// TODO -- what about matching any tag k regardless of value
if len(kvset) != 2 {
return nil, errors.New(fmt.Sprintf("invalid tag input: %s", kvpair))
}
excludeList[kvset[0]] = kvset[1]
}
}
var retHosts []Hostdef
// Loop through the instances. They don't always have a name-tag so set it
// to None if we can't find anything.
fSkipHost := false
for idx, _ := range resp.Reservations {
for _, inst := range resp.Reservations[idx].Instances {
fSkipHost = false
// We need to see if the Name is one of the tags. It's not always
// present and not required in Ec2.
name := "None"
var tagList []string
for _, keys := range inst.Tags {
if *keys.Key == "Name" {
name = url.QueryEscape(*keys.Value)
}
if _, ok := excludeList[*keys.Key]; ok { //match on key
// now match regex the value
excludeVal, _ := excludeList[*keys.Key]
match, _ := regexp.MatchString(excludeVal, *keys.Value)
if match {
fSkipHost = true
break // exclude this host because we matched k=v
}
}
tagList = append(tagList, fmt.Sprintf("%s=%s", *keys.Key, *keys.Value))
}
if fSkipHost {
continue
}
// confirm that
publicIpAddress := ""
if inst.PublicIpAddress != nil {
publicIpAddress = *inst.PublicIpAddress
}
awshost := Hostdef{
InstanceId: *inst.InstanceId,
Name: name,
PrivateIpAddress: *inst.PrivateIpAddress,
InstanceType: *inst.InstanceType,
PublicIpAddress: publicIpAddress,
Tags: tagList,
}
retHosts = append(retHosts, awshost)
}
}
return retHosts, nil
}