-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
72 lines (66 loc) · 1.57 KB
/
client.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
package ec2
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
//go:generate mockery --name Interface
type Interface interface {
ChangeStatus(ctx context.Context, to int, tagKey string, tagValue string) (err error)
}
type client struct {
ec2 *ec2.Client
}
func NewFromConfig(cfg aws.Config) (*client, error) {
c := &client{}
c.ec2 = ec2.NewFromConfig(cfg)
return c, nil
}
func (c *client) ChangeStatus(ctx context.Context, to int, tagKey string, tagValue string) (err error) {
if to != 0 && to != 1 {
return errors.New("to must have value of 0 or 1")
}
var describeInstancesOutput *ec2.DescribeInstancesOutput
filterName := fmt.Sprintf("tag:%s", tagKey)
describeInstancesOutput, err = c.ec2.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: &filterName,
Values: []string{tagValue},
},
},
})
if err != nil {
return err
}
var instancesIds []string
if describeInstancesOutput == nil {
return nil
}
for _, r := range describeInstancesOutput.Reservations {
for _, i := range r.Instances {
instancesIds = append(instancesIds, *i.InstanceId)
}
}
if len(instancesIds) == 0 {
return nil
}
switch to {
case 0:
if _, err = c.ec2.StopInstances(ctx, &ec2.StopInstancesInput{
InstanceIds: instancesIds,
}); err != nil {
return err
}
case 1:
if _, err = c.ec2.StartInstances(ctx, &ec2.StartInstancesInput{
InstanceIds: instancesIds,
}); err != nil {
return err
}
}
return nil
}