forked from buger/cloud-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
66 lines (52 loc) · 1.5 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
55
56
57
58
59
60
61
62
63
64
65
66
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"
}
if _, ok := config["output_format"]; !ok {
config["output_format"] = "Cloud: {cloud} \tMatched by: {tag_name} = {tag_value} \tAddr: {addr}"
}
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})
}
ci := config["connection_interface"]
if ci == "private_ip" {
instances[inst.PrivateIPAddress] = tags
} else if ci == "public_ip" {
instances[inst.IPAddress] = tags
} else if ci == "private_dns" {
instances[inst.PrivateDNSName] = tags
} else {
instances[inst.DNSName] = tags
}
}
}
}
return
}