-
Notifications
You must be signed in to change notification settings - Fork 2
/
logic.go
73 lines (57 loc) · 1.47 KB
/
logic.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
package list
import (
"io"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hirakiuc/ec2s/internal/cache"
"github.com/hirakiuc/ec2s/internal/common"
"github.com/hirakiuc/ec2s/internal/formatter"
)
// Reference Code
// http://qiita.com/draco/items/59c94ab8c66314d3a9f0
// Sample Code
// https://github.com/aws/aws-sdk-go/blob/7e9078c250876f26da48aaf36b8dce6a462ecd8a/service/ec2/examples_test.go#L2876
func vpcID(instance *ec2.Instance) string {
if instance.VpcId == nil {
return ""
}
return *instance.VpcId
}
func loadVpcCache() (*cache.VpcCache, error) {
cache := cache.GetVpcCache()
if err := cache.MakeCache(); err != nil {
return nil, err
}
return cache, nil
}
// ShowEc2Instances shows EC2 instances.
func ShowEc2Instances(writer io.Writer, options common.FilterInterface) error {
vpcCache, err := loadVpcCache()
if err != nil {
return err
}
instanceCache := cache.GetEc2InstanceCache()
service := common.Ec2Service()
params, err := options.InstancesFilter()
if err != nil {
return err
}
res, err := service.DescribeInstances(params)
if err != nil {
logger.Error("failed to load EC2 instances.\n")
return err
}
table := common.NewTableWriter(writer)
formatter := formatter.NewEc2Formatter()
for _, r := range res.Reservations {
for _, i := range r.Instances {
vpc := vpcCache.ReadEntry(vpcID(i))
table.Append(formatter.Format(vpc, i))
instanceCache.WriteEntry(
*i.InstanceId,
i,
)
}
}
table.Render()
return nil
}