-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.go
66 lines (54 loc) · 1.36 KB
/
ec2.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
package aws
import (
"github.com/cquery/importer/lib"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
type EC2Resources []*ec2.Reservation
func (e EC2Resources) Update(updater lib.Updater) error {
for _, r := range e {
for _, i := range r.Instances {
updateSet := updater.Set("ec2")
updateSet.AddString("instance_id", *i.InstanceId)
updateSet.AddString("image_id", *i.ImageId)
updateSet.AddString("instance_type", *i.InstanceType)
updateSet.AddString("state_name", *i.State.Name)
if i.PrivateIpAddress != nil {
updateSet.AddString("private_ip", *i.PrivateIpAddress)
}
for _, t := range i.Tags {
if *t.Key == "Name" {
updateSet.AddString("name", *t.Value)
}
}
if err := updater.Update(updateSet); err != nil {
return err
}
}
}
return nil
}
type EC2APICaller struct {
service *ec2.EC2
}
func NewEC2APICaller(awsConfig *aws.Config) lib.APICaller {
sess, _ := session.NewSession()
svc := ec2.New(sess, awsConfig)
e := &EC2APICaller{
service: svc,
}
return e
}
func (e *EC2APICaller) Call() (lib.Resources, error) {
params := &ec2.DescribeInstancesInput{}
resp, err := e.service.DescribeInstances(params)
if err != nil {
return nil, err
}
res := EC2Resources(resp.Reservations)
return res, err
}
func init() {
Add("ec2", NewEC2APICaller)
}