-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws.go
143 lines (118 loc) · 2.69 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cloudman
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"log"
)
type AWSCloud struct {
credentials AWSCredentials
session *session.Session
}
func NewAWSCloud(c AWSCredentials) *AWSCloud {
creds := credentials.NewStaticCredentials(c.AccessKey, c.AccessSecret, "")
sess, err := session.NewSession(&aws.Config{
Credentials: creds,
})
if err != nil {
log.Println(err)
}
out := AWSCloud{
credentials: c,
session: sess,
}
return &out
}
type AWSCredentials struct {
AccessKey string
AccessSecret string
}
func (c *AWSCloud) GetInstanceListAllRegions() ([]InstanceListResponse, error) {
rList := []string{
"ca-central-1",
"eu-central-1",
"eu-west-1",
"eu-west-2",
"eu-south-1",
"eu-west-3",
"eu-north-1",
"me-south-1",
"sa-east-1",
"ap-northeast-3",
"ap-northeast-2",
"ap-southeast-1",
"ap-southeast-2",
"ap-northeast-1",
"us-east-2",
"us-east-1",
"us-west-1",
"us-west-2",
"af-south-1",
"ap-east-1",
"ap-south-1",
}
var out []InstanceListResponse
recvChan := make(chan []InstanceListResponse, len(rList))
for _, region := range rList {
go func(reg string) {
res, _ := c.GetInstanceList(reg)
recvChan <- res
}(region)
}
for j := 0; j < len(rList); j++ {
list := <-recvChan
out = append(out, list...)
}
return out, nil
}
func (c *AWSCloud) GetInstanceList(region string) ([]InstanceListResponse, error) {
sess := c.session.Copy(&aws.Config{
Region: aws.String(region),
})
client := ec2.New(sess)
input := &ec2.DescribeInstancesInput{}
result, err := client.DescribeInstances(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
// TODO enable this with logging levels
//log.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Println(err.Error())
}
return nil, err
}
var out []InstanceListResponse
for _, res := range result.Reservations {
for _, ins := range res.Instances {
name := c.getInstanceName(ins.Tags)
obj := InstanceListResponse{
Name: name,
Id: *ins.InstanceId,
Status: *ins.State.Name,
LaunchDate: ins.LaunchTime.String(),
Region: region,
MachineType: *ins.InstanceType,
}
out = append(out, obj)
}
}
return out, nil
}
func (c *AWSCloud) getInstanceName(t []*ec2.Tag) string {
name := ""
for _, tag := range t {
if *tag.Key == "Name" {
name = *tag.Value
}
}
return name
}
func (c *AWSCloud) GetInstanceDetails() {
}