/
instanceCollection.go
141 lines (125 loc) · 3.23 KB
/
instanceCollection.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
package modules
import (
"fmt"
"io"
"strconv"
"strings"
"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/olekukonko/tablewriter"
"github.com/spf13/viper"
)
//Instances Instance Collection type
type Instances []Instance
//GetInstances find by region
func GetInstances(region string) Instances {
res := getReservations(region)
instances := instancesFromReservations(res)
instances.fillImageDetails(region)
return instances
}
func mapDescriptionToUser(description *string) string {
desc := ""
if description != nil {
desc = *description
}
if strings.Contains(desc, "Ubuntu") {
return "ubuntu"
}
if strings.Contains(desc, "Centos") {
return "centos"
}
return "ec2-user"
}
func (instances Instances) fillImageDetails(region string) {
imageIds := instances.CollectString(func(inst Instance) string {
return inst.ImageId
})
uniqueIds := RemoveDuplicatesUnordered(imageIds)
sess, err := session.NewSession()
if err != nil {
panic(err)
}
cfgs := &aws.Config{Region: ®ion}
svc := ec2.New(sess, cfgs)
input := &ec2.DescribeImagesInput{ImageIds: uniqueIds}
result, err := svc.DescribeImages(input)
for _, image := range result.Images {
for idx, inst := range instances {
if inst.ImageId == *image.ImageId {
instances[idx].User = mapDescriptionToUser(image.Description)
}
}
}
}
func getReservations(region string) []*ec2.Reservation {
sess, err := session.NewSession()
if err != nil {
panic(err)
}
fmt.Println("Fetching instances.....")
cfgs := &aws.Config{Region: ®ion}
svc := ec2.New(sess, cfgs)
resp, err := svc.DescribeInstances(nil)
if err != nil {
panic(err)
}
return resp.Reservations
}
func instancesFromReservations(reservations []*ec2.Reservation) Instances {
instances := []Instance{}
for _, res := range reservations {
for _, inst := range res.Instances {
instances = append(instances, convertFromAwsType(inst))
}
}
return instances
}
//CollectString collects
func (instances Instances) CollectString(f func(Instance) string) []string {
result := make([]string, len(instances))
for i, item := range instances {
result[i] = f(item)
}
return result
}
//FilterByName filters collection by instance name
func (instances Instances) FilterByName(name string) Instances {
return instances.Filter(func(i Instance) bool {
return strings.Contains(i.Name, name)
})
}
//CreateTable creates a formated table of instances
func (instances Instances) CreateTable(writer io.Writer) {
table := tablewriter.NewWriter(writer)
tags := viper.GetStringSlice("tags")
fmt.Println(tags)
headers := []string{"Num", "Id", "State", "Public", "Private"}
headers = append(headers, tags...)
table.SetHeader(headers)
for pos, inst := range instances {
row := []string{strconv.Itoa(pos),
inst.Id,
inst.State,
inst.Host,
inst.PrivateIp,
}
for _, tag := range tags {
row = append(row, inst.GetKey(tag))
}
// row = append(row, tags...)
table.Append(row)
// fmt.Println(pos, " ", inst.Name, " ", inst.State, " ", inst.Host)
}
table.Render()
}
func (instances Instances) Filter(f func(Instance) bool) Instances {
var vsf Instances
for _, v := range instances {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}