github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/aws/list_instances.go (about) 1 package aws 2 3 import ( 4 "github.com/sirupsen/logrus" 5 "github.com/aws/aws-sdk-go/aws" 6 "github.com/aws/aws-sdk-go/service/ec2" 7 "github.com/emc-advanced-dev/pkg/errors" 8 "github.com/solo-io/unik/pkg/types" 9 ) 10 11 func (p *AwsProvider) ListInstances() ([]*types.Instance, error) { 12 if len(p.state.GetInstances()) < 1 { 13 return []*types.Instance{}, nil 14 } 15 16 instanceIds := []*string{} 17 for instanceId := range p.state.GetInstances() { 18 instanceIds = append(instanceIds, aws.String(instanceId)) 19 } 20 param := &ec2.DescribeInstancesInput{ 21 InstanceIds: instanceIds, 22 } 23 output, err := p.newEC2().DescribeInstances(param) 24 if err != nil { 25 return nil, errors.New("running ec2 describe instances ", err) 26 } 27 updatedInstances := []*types.Instance{} 28 for _, reservation := range output.Reservations { 29 for _, ec2Instance := range reservation.Instances { 30 logrus.WithField("ec2instance", ec2Instance).Debugf("aws returned instance %s", *ec2Instance.InstanceId) 31 var instanceId string 32 if ec2Instance.InstanceId != nil { 33 instanceId = *ec2Instance.InstanceId 34 } 35 if instanceId == "" { 36 logrus.Warnf("instance %v does not have readable instanceId, moving on", *ec2Instance) 37 continue 38 } 39 instanceState := parseInstanceState(ec2Instance.State) 40 if instanceState == types.InstanceState_Unknown { 41 logrus.Warnf("instance %s state is unknown (%s), moving on", instanceId, *ec2Instance.State.Name) 42 continue 43 } 44 if instanceState == types.InstanceState_Terminated { 45 logrus.Warnf("instance %s state is terminated, removing it from state", instanceId) 46 if err := p.state.ModifyInstances(func(instances map[string]*types.Instance) error { 47 delete(instances, instanceId) 48 return nil 49 }); err != nil { 50 return nil, errors.New("modifying instance map in state", err) 51 } 52 continue 53 } 54 instance, ok := p.state.GetInstances()[instanceId] 55 if !ok { 56 logrus.WithFields(logrus.Fields{"ec2Instance": ec2Instance}).Errorf("found an instance that unik has no record of") 57 continue 58 } 59 instance.State = instanceState 60 if ec2Instance.PublicIpAddress != nil { 61 instance.IpAddress = *ec2Instance.PublicIpAddress 62 } 63 if err := p.state.ModifyInstances(func(instances map[string]*types.Instance) error { 64 instances[instance.Id] = instance 65 return nil 66 }); err != nil { 67 return nil, errors.New("modifying instance map in state", err) 68 } 69 updatedInstances = append(updatedInstances, instance) 70 } 71 } 72 return updatedInstances, nil 73 } 74 75 func parseInstanceState(ec2State *ec2.InstanceState) types.InstanceState { 76 if ec2State == nil { 77 return types.InstanceState_Unknown 78 } 79 switch *ec2State.Name { 80 case ec2.InstanceStateNameRunning: 81 return types.InstanceState_Running 82 case ec2.InstanceStateNamePending: 83 return types.InstanceState_Pending 84 case ec2.InstanceStateNameStopped: 85 return types.InstanceState_Stopped 86 case ec2.InstanceStateNameTerminated: 87 return types.InstanceState_Terminated 88 } 89 return types.InstanceState_Unknown 90 }