forked from solo-io/unik
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_instances.go
90 lines (86 loc) · 2.93 KB
/
list_instances.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
package aws
import (
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/emc-advanced-dev/pkg/errors"
"github.com/emc-advanced-dev/unik/pkg/types"
)
func (p *AwsProvider) ListInstances() ([]*types.Instance, error) {
if len(p.state.GetInstances()) < 1 {
return []*types.Instance{}, nil
}
instanceIds := []*string{}
for instanceId := range p.state.GetInstances() {
instanceIds = append(instanceIds, aws.String(instanceId))
}
param := &ec2.DescribeInstancesInput{
InstanceIds: instanceIds,
}
output, err := p.newEC2().DescribeInstances(param)
if err != nil {
return nil, errors.New("running ec2 describe instances ", err)
}
updatedInstances := []*types.Instance{}
for _, reservation := range output.Reservations {
for _, ec2Instance := range reservation.Instances {
logrus.WithField("ec2instance", ec2Instance).Debugf("aws returned instance %s", *ec2Instance.InstanceId)
var instanceId string
if ec2Instance.InstanceId != nil {
instanceId = *ec2Instance.InstanceId
}
if instanceId == "" {
logrus.Warnf("instance %v does not have readable instanceId, moving on", *ec2Instance)
continue
}
instanceState := parseInstanceState(ec2Instance.State)
if instanceState == types.InstanceState_Unknown {
logrus.Warnf("instance %s state is unknown (%s), moving on", instanceId, *ec2Instance.State.Name)
continue
}
if instanceState == types.InstanceState_Terminated {
logrus.Warnf("instance %s state is terminated, removing it from state", instanceId)
if err := p.state.ModifyInstances(func(instances map[string]*types.Instance) error {
delete(instances, instanceId)
return nil
}); err != nil {
return nil, errors.New("modifying instance map in state", err)
}
continue
}
instance, ok := p.state.GetInstances()[instanceId]
if !ok {
logrus.WithFields(logrus.Fields{"ec2Instance": ec2Instance}).Errorf("found an instance that unik has no record of")
continue
}
instance.State = instanceState
if ec2Instance.PublicIpAddress != nil {
instance.IpAddress = *ec2Instance.PublicIpAddress
}
if err := p.state.ModifyInstances(func(instances map[string]*types.Instance) error {
instances[instance.Id] = instance
return nil
}); err != nil {
return nil, errors.New("modifying instance map in state", err)
}
updatedInstances = append(updatedInstances, instance)
}
}
return updatedInstances, nil
}
func parseInstanceState(ec2State *ec2.InstanceState) types.InstanceState {
if ec2State == nil {
return types.InstanceState_Unknown
}
switch *ec2State.Name {
case ec2.InstanceStateNameRunning:
return types.InstanceState_Running
case ec2.InstanceStateNamePending:
return types.InstanceState_Pending
case ec2.InstanceStateNameStopped:
return types.InstanceState_Stopped
case ec2.InstanceStateNameTerminated:
return types.InstanceState_Terminated
}
return types.InstanceState_Unknown
}