This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecs.go
99 lines (84 loc) · 2.46 KB
/
ecs.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
package awsctr
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/juju/errors"
)
// ContainerInstance -
type ContainerInstance struct {
ID string
Connected bool
}
// ContainerInstances -
type ContainerInstances []ContainerInstance
// ECS -
type ECS interface {
GetContainerInstances(clusterName string) (insts ContainerInstances, err error)
}
// NewECS -
func NewECS(sess *session.Session) ECS {
return &ecsImpl{
client: ecs.New(sess),
}
}
type ecsService interface {
ListContainerInstances(*ecs.ListContainerInstancesInput) (*ecs.ListContainerInstancesOutput, error)
DescribeContainerInstances(*ecs.DescribeContainerInstancesInput) (*ecs.DescribeContainerInstancesOutput, error)
}
type ecsImpl struct {
client ecsService
}
func (c *ecsImpl) GetContainerInstances(clusterName string) (insts ContainerInstances, err error) {
instanceArns, err := c.getContainerInstanceArns(clusterName)
if err != nil {
return nil, err
}
status, err := c.getContainerInstancesStatus(clusterName, instanceArns)
if err != nil {
return nil, err
}
insts, err = getInstances(status)
if err != nil {
return nil, err
}
return insts, nil
}
func (c *ecsImpl) getContainerInstanceArns(clusterName string) (instanceArns []*string, err error) {
stat := containerInstanceStatus
instances, err := c.client.ListContainerInstances(&ecs.ListContainerInstancesInput{
Cluster: &clusterName,
Status: &stat,
})
if err != nil {
return nil, err
}
for _, arn := range instances.ContainerInstanceArns {
instanceArns = append(instanceArns, arn)
}
return instanceArns, nil
}
func (c *ecsImpl) getContainerInstancesStatus(clusterName string, instanceArns []*string) (status *ecs.DescribeContainerInstancesOutput, err error) {
status, err = c.client.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{
Cluster: &clusterName,
ContainerInstances: instanceArns,
})
if err != nil {
return nil, err
}
return status, nil
}
func getInstances(status *ecs.DescribeContainerInstancesOutput) (insts ContainerInstances, err error) {
for _, stat := range status.ContainerInstances {
inst := ContainerInstance{
ID: aws.StringValue(stat.Ec2InstanceId),
Connected: aws.BoolValue(stat.AgentConnected),
}
if inst.ID != "" {
insts = append(insts, inst)
} else {
return nil, errors.Errorf("connection is failure but missing instance id: %v", inst)
}
}
return insts, nil
}