-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
179 lines (147 loc) · 4.46 KB
/
query.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package ebs
import (
"encoding/json"
"errors"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ernestio/ernestaws/credentials"
)
// Collection ....
type Collection struct {
ProviderType string `json:"_provider"`
ComponentType string `json:"_component"`
ComponentID string `json:"_component_id"`
State string `json:"_state"`
Action string `json:"_action"`
Service string `json:"service"`
AWSAccessKeyID string `json:"aws_access_key_id"`
AWSSecretAccessKey string `json:"aws_secret_access_key"`
DatacenterRegion string `json:"datacenter_region"`
Tags map[string]string `json:"tags"`
Results []interface{} `json:"components"`
ErrorMessage string `json:"error,omitempty"`
Subject string `json:"-"`
Body []byte `json:"-"`
CryptoKey string `json:"-"`
}
// GetBody : Gets the body for this event
func (col *Collection) GetBody() []byte {
var err error
if col.Body, err = json.Marshal(col); err != nil {
log.Println(err.Error())
}
return col.Body
}
// GetSubject : Gets the subject for this event
func (col *Collection) GetSubject() string {
return col.Subject
}
// Process : starts processing the current message
func (col *Collection) Process() (err error) {
if err := json.Unmarshal(col.Body, &col); err != nil {
col.Error(err)
return err
}
if err := col.Validate(); err != nil {
col.Error(err)
return err
}
return nil
}
// Error : Will respond the current event with an error
func (col *Collection) Error(err error) {
log.Printf("Error: %s", err.Error())
col.ErrorMessage = err.Error()
col.State = "errored"
col.Body, err = json.Marshal(col)
}
// Complete : sets the state of the event to completed
func (col *Collection) Complete() {
col.State = "completed"
}
// Validate checks if all criteria are met
func (col *Collection) Validate() error {
if col.AWSAccessKeyID == "" || col.AWSSecretAccessKey == "" {
return ErrDatacenterCredentialsInvalid
}
return nil
}
// Get : Gets a object on aws
func (col *Collection) Get() error {
return errors.New(col.Subject + " not supported")
}
// Create : Creates an object on aws
func (col *Collection) Create() error {
return errors.New(col.Subject + " not supported")
}
// Update : Updates an object on aws
func (col *Collection) Update() error {
return errors.New(col.Subject + " not supported")
}
// Delete : Delete an object on aws
func (col *Collection) Delete() error {
return errors.New(col.Subject + " not supported")
}
// Find : Find ebs on aws
func (col *Collection) Find() error {
svc := col.getEC2Client()
req := &ec2.DescribeVolumesInput{
Filters: mapFilters(col.Tags),
}
resp, err := svc.DescribeVolumes(req)
if err != nil {
return err
}
for _, v := range resp.Volumes {
col.Results = append(col.Results, toEvent(v))
}
return nil
}
func (col *Collection) getEC2Client() *ec2.EC2 {
creds, _ := credentials.NewStaticCredentials(col.AWSAccessKeyID, col.AWSSecretAccessKey, col.CryptoKey)
return ec2.New(session.New(), &aws.Config{
Region: aws.String(col.DatacenterRegion),
Credentials: creds,
})
}
func mapFilters(tags map[string]string) []*ec2.Filter {
var f []*ec2.Filter
for key, val := range tags {
f = append(f, &ec2.Filter{
Name: aws.String("tag:" + key),
Values: []*string{aws.String(val)},
})
}
return f
}
func mapEC2Tags(input []*ec2.Tag) map[string]string {
t := make(map[string]string)
for _, tag := range input {
t[*tag.Key] = *tag.Value
}
return t
}
// toEvent converts an ec2 instance object to an ernest event
func toEvent(v *ec2.Volume) *Event {
tags := mapEC2Tags(v.Tags)
name := tags["Name"]
return &Event{
ProviderType: "aws",
ComponentType: "ebs_volume",
ComponentID: "ebs_volume::" + name,
Name: aws.String(name),
VolumeAWSID: v.VolumeId,
AvailabilityZone: v.AvailabilityZone,
VolumeType: v.VolumeType,
Size: v.Size,
Iops: v.Iops,
Encrypted: v.Encrypted,
EncryptionKeyID: v.KmsKeyId,
Tags: tags,
}
}