forked from buger/cloud-ssh
/
aws.go
54 lines (40 loc) · 1.07 KB
/
aws.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
package main
import (
"launchpad.net/goamz/aws"
"launchpad.net/goamz/ec2"
"log"
)
func getEC2Instances(config map[string]string) (instances Instances) {
instances = make(Instances)
if _, ok := config["access_key"]; !ok {
log.Fatal("Missing access_key for ", config["name"], " AWS cloud")
}
if _, ok := config["secret_key"]; !ok {
log.Fatal("Missing secret_key for ", config["name"], " AWS cloud")
}
if _, ok := config["region"]; !ok {
config["region"] = "us-east-1"
}
auth := aws.Auth{AccessKey: config["access_key"], SecretKey: config["secret_key"]}
e := ec2.New(auth, aws.Regions[config["region"]])
resp, err := e.Instances(nil, nil)
if err != nil {
log.Println(err)
return
}
for _, res := range resp.Reservations {
for _, inst := range res.Instances {
if inst.DNSName != "" {
var tags []Tag
for _, tag := range inst.Tags {
tags = append(tags, Tag{tag.Key, tag.Value})
}
for _, sg := range inst.SecurityGroups {
tags = append(tags, Tag{"Security group", sg.Name})
}
instances[inst.DNSName] = tags
}
}
}
return
}