diff options
author | Nick White <git@njw.name> | 2019-09-24 15:05:02 +0100 |
---|---|---|
committer | Nick White <git@njw.name> | 2019-09-24 15:05:02 +0100 |
commit | dc9d2911f67d8c7da4d6b761afc7cf882f21b8a7 (patch) | |
tree | 0b91d6c863c535243a2b58dda0add477c93bfd21 /bookpipeline/aws.go | |
parent | 565fd83952af7001c2f720ef6fe5b0fc3bf8cf17 (diff) |
Move ec2 stuff out of lspipeline and into aws.go
Diffstat (limited to 'bookpipeline/aws.go')
-rw-r--r-- | bookpipeline/aws.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/bookpipeline/aws.go b/bookpipeline/aws.go index f3cdbfa..063bc9f 100644 --- a/bookpipeline/aws.go +++ b/bookpipeline/aws.go @@ -10,6 +10,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/aws/aws-sdk-go/service/sqs" @@ -22,6 +23,10 @@ type Qmsg struct { Id, Handle, Body string } +type InstanceDetails struct { + Id, Name, Ip, Spot, Type, State, LaunchTime string +} + type AwsConn struct { // these need to be set before running Init() Region string @@ -29,6 +34,7 @@ type AwsConn struct { // these are used internally sess *session.Session + ec2svc *ec2.EC2 s3svc *s3.S3 sqssvc *sqs.SQS downloader *s3manager.Downloader @@ -37,6 +43,7 @@ type AwsConn struct { wipstorageid string } +// TODO: split this up, as not everything is needed for different uses func (a *AwsConn) Init() error { if a.Region == "" { return errors.New("No Region set") @@ -52,6 +59,7 @@ func (a *AwsConn) Init() error { if err != nil { return errors.New(fmt.Sprintf("Failed to set up aws session: %s", err)) } + a.ec2svc = ec2.New(a.sess) a.s3svc = s3.New(a.sess) a.sqssvc = sqs.New(a.sess) a.downloader = s3manager.NewDownloader(a.sess) @@ -259,3 +267,36 @@ func (a *AwsConn) Upload(bucket string, key string, path string) error { 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) { + 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 _, 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 + } + d.Type = *i.InstanceType + d.Id = *i.InstanceId + d.LaunchTime = i.LaunchTime.String() + d.State = *i.State.Name + + details = append(details, d) + } + } + return !lastPage + }) + return details, err +} |