-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
ec2_instances.go
78 lines (66 loc) · 2.18 KB
/
ec2_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
package searchers
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
aw "github.com/deanishe/awgo"
"github.com/rkoval/alfred-aws-console-services-workflow/awsworkflow"
"github.com/rkoval/alfred-aws-console-services-workflow/caching"
"github.com/rkoval/alfred-aws-console-services-workflow/util"
)
type EC2InstanceSearcher struct{}
func (s EC2InstanceSearcher) Search(wf *aw.Workflow, query string, session *session.Session, forceFetch bool, fullQuery string) error {
cacheName := util.GetCurrentFilename()
instances := caching.LoadEc2InstanceArrayFromCache(wf, session, cacheName, s.fetch, forceFetch, fullQuery)
for _, instance := range instances {
s.addToWorkflow(wf, query, session.Config, instance)
}
return nil
}
func (s EC2InstanceSearcher) fetch(session *session.Session) ([]ec2.Instance, error) {
svc := ec2.New(session)
NextToken := ""
var instances []ec2.Instance
for {
params := &ec2.DescribeInstancesInput{
MaxResults: aws.Int64(1000), // get as many as we can
NextToken: aws.String(NextToken),
}
resp, err := svc.DescribeInstances(params)
if err != nil {
return nil, err
}
for _, reservation := range resp.Reservations {
for i := range reservation.Instances {
instances = append(instances, *reservation.Instances[i])
}
}
if resp.NextToken != nil {
NextToken = *resp.NextToken
} else {
break
}
}
return instances, nil
}
func (s EC2InstanceSearcher) addToWorkflow(wf *aw.Workflow, query string, config *aws.Config, instance ec2.Instance) {
var title string
subtitle := util.GetEC2InstanceStateEmoji(*instance.State)
name := util.GetEC2TagValue(instance.Tags, "Name")
if name != "" {
title = name
subtitle += " " + *instance.InstanceId
} else {
title = *instance.InstanceId
}
subtitle += " " + *instance.InstanceType
item := util.NewURLItem(wf, title).
Subtitle(subtitle).
Arg(fmt.Sprintf("https://%s.console.aws.amazon.com/ec2/v2/home?region=%s#Instances:search=%s", *config.Region, *config.Region, *instance.InstanceId)).
Icon(awsworkflow.GetImageIcon("ec2"))
if strings.HasPrefix(query, "i-") {
item.Match(*instance.InstanceId)
}
}