-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsec2.go
123 lines (106 loc) · 2.92 KB
/
awsec2.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 (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ytakahashi/gecco/config"
)
// IEc2Service is an interface for ec2 services
type IEc2Service interface {
initEc2Service() *ec2.EC2
start(*ec2.EC2, bool, string) (*ec2.StartInstancesOutput, error)
stop(*ec2.EC2, bool, string) (*ec2.StopInstancesOutput, error)
handleError(error) bool
}
// Ec2Service Ec2 Service
type Ec2Service struct{}
func (s Ec2Service) initEc2Service() *ec2.EC2 {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
return ec2.New(sess)
}
func (s Ec2Service) start(ec2Svc *ec2.EC2, dryRun bool, instanceID string) (*ec2.StartInstancesOutput, error) {
input := &ec2.StartInstancesInput{
InstanceIds: []*string{
aws.String(instanceID),
},
DryRun: aws.Bool(dryRun),
}
return ec2Svc.StartInstances(input)
}
func (s Ec2Service) stop(ec2Svc *ec2.EC2, dryRun bool, instanceID string) (*ec2.StopInstancesOutput, error) {
input := &ec2.StopInstancesInput{
InstanceIds: []*string{
aws.String(instanceID),
},
DryRun: aws.Bool(dryRun),
}
return ec2Svc.StopInstances(input)
}
func (s Ec2Service) handleError(err error) bool {
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "DryRunOperation" {
return true
}
return false
}
func (instances Ec2Instances) toStringSlice() []string {
sl := make([]string, 0)
for _, i := range instances {
sl = append(sl, i.instanceID+" ("+i.status+"), Tags="+i.tags.toString())
}
return sl
}
func createInput(options config.FilterOption) ec2.DescribeInstancesInput {
filters := make([]*ec2.Filter, 0)
if options.Status != "" {
filter := ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String(options.Status)},
}
filters = append(filters, &filter)
} else {
filter := ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{
aws.String("running"),
aws.String("pending"),
aws.String("stopping"),
aws.String("shutting-down"),
aws.String("terminated"),
aws.String("stopped"),
},
}
filters = append(filters, &filter)
}
if options.TagKey != "" && options.TagValue != "" {
filter := ec2.Filter{
Name: aws.String("tag:" + options.TagKey),
Values: []*string{aws.String(options.TagValue)},
}
filters = append(filters, &filter)
}
return ec2.DescribeInstancesInput{
Filters: filters,
}
}
func newEc2Instance(i ec2.Instance) Ec2Instance {
tags := make(tags, 0)
for _, t := range i.Tags {
tag := tag{key: *t.Key, value: *t.Value}
tags = append(tags, tag)
}
var status string
s := i.State
if s != nil {
status = aws.StringValue(s.Name)
}
return Ec2Instance{
instanceID: aws.StringValue(i.InstanceId),
instanceType: aws.StringValue(i.InstanceType),
status: status,
tags: tags,
}
}