diff options
author | Nick White <git@njw.name> | 2019-09-24 15:25:48 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2019-09-24 15:25:48 +0100 |
commit | 4bd6d88c0ecd1da3b9af03a61b2c895cbe26fd7f (patch) | |
tree | 9371596e24b96c9a71b2e61c874b71127d80cea7 /bookpipeline/aws.go | |
parent | dc9d2911f67d8c7da4d6b761afc7cf882f21b8a7 (diff) |
Rewrite GetInstanceDetails so page function is separate
Diffstat (limited to 'bookpipeline/aws.go')
-rw-r--r-- | bookpipeline/aws.go | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/bookpipeline/aws.go b/bookpipeline/aws.go index 063bc9f..d4cd306 100644 --- a/bookpipeline/aws.go +++ b/bookpipeline/aws.go @@ -268,33 +268,40 @@ func (a *AwsConn) GetLogger() *log.Logger { return a.Logger } -// TODO: split pages function so it can be encapsulated by -// downstream and to feed a channel -func (a *AwsConn) GetInstanceDetails() ([]InstanceDetails, error) { +func instanceDetailsFromPage(page *ec2.DescribeInstancesOutput) []InstanceDetails { var details []InstanceDetails - err := a.ec2svc.DescribeInstancesPages(&ec2.DescribeInstancesInput{}, func(page *ec2.DescribeInstancesOutput, lastPage bool) bool { - for _, r := range page.Reservations { - for _, i := range r.Instances { - var d InstanceDetails + for _, r := range page.Reservations { + for _, i := range r.Instances { + var d InstanceDetails - for _, t := range i.Tags { - if *t.Key == "Name" { - d.Name = *t.Value - } - } - if i.PublicIpAddress != nil { - d.Ip = *i.PublicIpAddress - } - if i.SpotInstanceRequestId != nil { - d.Spot = *i.SpotInstanceRequestId + for _, t := range i.Tags { + if *t.Key == "Name" { + d.Name = *t.Value } - d.Type = *i.InstanceType - d.Id = *i.InstanceId - d.LaunchTime = i.LaunchTime.String() - d.State = *i.State.Name - - details = append(details, d) } + if i.PublicIpAddress != nil { + d.Ip = *i.PublicIpAddress + } + if i.SpotInstanceRequestId != nil { + d.Spot = *i.SpotInstanceRequestId + } + d.Type = *i.InstanceType + d.Id = *i.InstanceId + d.LaunchTime = i.LaunchTime.String() + d.State = *i.State.Name + + details = append(details, d) + } + } + + return details +} + +func (a *AwsConn) GetInstanceDetails() ([]InstanceDetails, error) { + var details []InstanceDetails + err := a.ec2svc.DescribeInstancesPages(&ec2.DescribeInstancesInput{}, func(page *ec2.DescribeInstancesOutput, lastPage bool) bool { + for _, d := range instanceDetailsFromPage(page) { + details = append(details, d) } return !lastPage }) |