-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.go
160 lines (139 loc) · 3.72 KB
/
ec2.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"text/tabwriter"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
// EC2Client -
type EC2Client interface {
ListInstances(string) (Ec2Instances, error)
}
// ec2Client -
type ec2Client struct {
client ec2iface.EC2API
}
// NewEC2Client is construct of ec2 object.
func NewEC2Client(svc ec2iface.EC2API) EC2Client {
return &ec2Client{
client: svc,
}
}
// Ec2Instances is list of EC2 instance.
type Ec2Instances []*ec2.Instance
func (instance Ec2Instances) Len() int {
return len(instance)
}
func (instance Ec2Instances) Swap(i, j int) {
instance[i], instance[j] = instance[j], instance[i]
}
func (instance Ec2Instances) Less(i, j int) bool {
return GetTagValue(instance[i], "Name") < GetTagValue(instance[j], "Name")
}
// ParseFilter parse filter option.
func ParseFilter(filters string) []*ec2.Filter {
// filters e.g. "Name=tag:Foo,Values=Bar Name=instance-type,Values=m1.small"
var ec2Filter []*ec2.Filter
re := regexp.MustCompile(`Name=(.+),Values=(.+)`)
for _, i := range strings.Fields(filters) {
matches := re.FindAllStringSubmatch(i, -1)
ec2Filter = append(ec2Filter, &ec2.Filter{
Name: aws.String(matches[0][1]),
Values: []*string{
aws.String(matches[0][2]),
},
})
}
return ec2Filter
}
// GenerateSession generate session.
func GenerateSession(region, profile string) (*session.Session, error) {
sessOpt := session.Options{
SharedConfigState: session.SharedConfigEnable,
}
if len(region) != 0 {
sessOpt.Config = aws.Config{Region: aws.String(region)}
}
if len(profile) != 0 {
sessOpt.Profile = profile
}
return session.NewSessionWithOptions(sessOpt)
}
// ListInstances lists one or more of your instances.
func (svc *ec2Client) ListInstances(filters string) (Ec2Instances, error) {
var instances Ec2Instances
params := &ec2.DescribeInstancesInput{}
if len(filters) != 0 {
params = &ec2.DescribeInstancesInput{
Filters: ParseFilter(filters),
}
}
resp, err := svc.client.DescribeInstances(params)
if err != nil {
return nil, err
}
if len(resp.Reservations) == 0 {
return Ec2Instances{}, nil
}
for _, res := range resp.Reservations {
for _, instance := range res.Instances {
instances = append(instances, instance)
}
}
sort.Sort(instances)
return instances, nil
}
// PrintInstances is output stdout.
func PrintInstances(instances Ec2Instances) {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 2, 8, 2, ' ', 0)
for _, instance := range instances {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
GetTagValue(instance, "Name"),
GetPrivateIPAddress(instance),
GetPublicIPAddress(instance),
*instance.InstanceId,
*instance.InstanceType,
*instance.Placement.AvailabilityZone,
*instance.State.Name,
GetPlatform(instance),
)
}
w.Flush()
}
// GetTagValue returns values of EC2 tag.
func GetTagValue(instance *ec2.Instance, tagName string) string {
for _, t := range instance.Tags {
if *t.Key == tagName {
return *t.Value
}
}
return ""
}
// GetPrivateIPAddress returns value of EC2 private ip address, if there is a value.
func GetPrivateIPAddress(instance *ec2.Instance) string {
if instance.PrivateIpAddress != nil {
return *instance.PrivateIpAddress
}
return ""
}
// GetPublicIPAddress returns value of EC2 public ip address, if there is a value.
func GetPublicIPAddress(instance *ec2.Instance) string {
if instance.PublicIpAddress != nil {
return *instance.PublicIpAddress
}
return ""
}
// GetPlatform returns platform name(linux or windows).
func GetPlatform(instance *ec2.Instance) string {
if instance.Platform != nil {
return *instance.Platform
}
return "linux"
}