-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
121 lines (105 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
package main
import (
"fmt"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// Cache stores the list of instances
type Cache struct {
instances []*ec2.Instance
time time.Time
}
// API is a wrapper around the EC2 API, with caching
type API struct {
session *session.Session
service *ec2.EC2
cache *Cache
instancesChan chan []*ec2.Instance
errChan chan error
}
// NewAPI builds an API struct
func NewAPI() *API {
sess := session.Must(session.NewSession())
service := ec2.New(sess)
cache := &Cache{[]*ec2.Instance{}, time.Unix(0, 0)}
instancesChan := make(chan []*ec2.Instance)
errChan := make(chan error)
return &API{sess, service, cache, instancesChan, errChan}
}
// List returns a list of EC2 instances
func (api *API) List(nameFilter string) ([]*ec2.Instance, error) {
if api.cache.time.Add(time.Duration(15) * time.Second).After(time.Now()) {
api.instancesChan <- api.cache.instances
return api.cache.instances, nil
}
params := &ec2.DescribeInstancesInput{}
if nameFilter != "" {
params.Filters = []*ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{
aws.String(strings.Join([]string{"*", nameFilter, "*"}, "")),
},
},
}
}
resp, err := api.service.DescribeInstances(params)
api.errChan <- err
if err != nil {
return nil, err
}
if len(resp.Reservations) == 0 {
return nil, nil
}
instances := []*ec2.Instance{}
for _, r := range resp.Reservations {
for _, i := range r.Instances {
if *i.State.Name != "terminated" {
instances = append(instances, i)
}
}
}
sort.Slice(instances, func(i, j int) bool {
return GetName(instances[i]) < GetName(instances[j])
})
api.cache.instances = instances
api.cache.time = time.Now()
api.instancesChan <- instances
return instances, nil
}
// ExampleList returns a fake list of instances (used for testing)
func (api *API) ExampleList() ([]*ec2.Instance, error) {
count := 10
instances := []*ec2.Instance{}
for i := 0; i < count; i++ {
id := fmt.Sprintf("i-%d", i)
publicDNS := fmt.Sprintf("ec2-52-59-245-%d.eu-central-1.compute.amazonaws.com", i)
privateDNS := fmt.Sprintf("ip-10-0-0-%d.eu-central-1.compute.internal", i)
tagPurpose := ec2.Tag{}
tagPurpose.SetKey("Purpose")
tagPurpose.SetValue("webapp")
instanceType := "t2.medium"
availabilityZone := "eu-central-1"
now := time.Now()
state := "running"
instance := &ec2.Instance{
InstanceId: &id,
PublicDnsName: &publicDNS,
PrivateDnsName: &privateDNS,
Tags: []*ec2.Tag{&tagPurpose},
InstanceType: &instanceType,
Placement: &ec2.Placement{AvailabilityZone: &availabilityZone},
LaunchTime: &now,
State: &ec2.InstanceState{Name: &state},
}
instances = append(instances, instance)
}
api.cache.instances = instances
api.cache.time = time.Now()
api.instancesChan <- instances
return instances, nil
}