/
event.go
335 lines (272 loc) · 8.98 KB
/
event.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* 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 rdscluster
import (
"encoding/json"
"errors"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/ernestio/ernestaws"
"github.com/ernestio/ernestaws/credentials"
)
var (
// ErrDatacenterIDInvalid ...
ErrDatacenterIDInvalid = errors.New("Datacenter VPC ID invalid")
// ErrDatacenterRegionInvalid ...
ErrDatacenterRegionInvalid = errors.New("Datacenter Region invalid")
// ErrDatacenterCredentialsInvalid ...
ErrDatacenterCredentialsInvalid = errors.New("Datacenter credentials invalid")
// ErrRDSClusterNameInvalid ...
ErrRDSClusterNameInvalid = errors.New("RDS cluster name invalid")
// ErrRDSClusterEngineTypeInvalid ...
ErrRDSClusterEngineTypeInvalid = errors.New("RDS cluster engine invalid")
)
// Event stores the network data
type Event struct {
ProviderType string `json:"_provider"`
ComponentType string `json:"_component"`
ComponentID string `json:"_component_id"`
State string `json:"_state"`
Action string `json:"_action"`
ARN *string `json:"arn"`
Name *string `json:"name"`
Engine *string `json:"engine"`
EngineVersion *string `json:"engine_version,omitempty"`
Port *int64 `json:"port,omitempty"`
Endpoint *string `json:"endpoint,omitempty"`
AvailabilityZones []*string `json:"availability_zones"`
SecurityGroups []string `json:"security_groups"`
SecurityGroupAWSIDs []*string `json:"security_group_aws_ids"`
Networks []string `json:"networks"`
NetworkAWSIDs []*string `json:"network_aws_ids"`
DatabaseName *string `json:"database_name,omitempty"`
DatabaseUsername *string `json:"database_username,omitempty"`
DatabasePassword *string `json:"database_password,omitempty"`
BackupRetention *int64 `json:"backup_retention,omitempty"`
BackupWindow *string `json:"backup_window,omitempty"`
MaintenanceWindow *string `json:"maintenance_window,omitempty"`
ReplicationSource *string `json:"replication_source,omitempty"`
FinalSnapshot *bool `json:"final_snapshot"`
Tags map[string]string `json:"tags"`
DatacenterType string `json:"datacenter_type"`
DatacenterName string `json:"datacenter_name"`
DatacenterRegion string `json:"datacenter_region"`
AccessKeyID string `json:"aws_access_key_id"`
SecretAccessKey string `json:"aws_secret_access_key"`
Service string `json:"service"`
ErrorMessage string `json:"error,omitempty"`
Subject string `json:"-"`
Body []byte `json:"-"`
CryptoKey string `json:"-"`
}
// New : Constructor
func New(subject string, body []byte, cryptoKey string) ernestaws.Event {
if strings.Split(subject, ".")[1] == "find" {
return &Collection{Subject: subject, Body: body, CryptoKey: cryptoKey}
}
return &Event{Subject: subject, Body: body, CryptoKey: cryptoKey}
}
// Validate checks if all criteria are met
func (ev *Event) Validate() error {
if ev.DatacenterRegion == "" {
return ErrDatacenterRegionInvalid
}
if ev.AccessKeyID == "" || ev.SecretAccessKey == "" {
return ErrDatacenterCredentialsInvalid
}
if ev.Name == nil {
return ErrRDSClusterNameInvalid
}
if ev.Engine == nil {
return ErrRDSClusterEngineTypeInvalid
}
return nil
}
// Process : starts processing the current message
func (ev *Event) Process() (err error) {
if err := json.Unmarshal(ev.Body, &ev); err != nil {
ev.Error(err)
return err
}
if err := ev.Validate(); err != nil {
ev.Error(err)
return err
}
return nil
}
// Error : Will respond the current event with an error
func (ev *Event) Error(err error) {
log.Printf("Error: %s", err.Error())
ev.ErrorMessage = err.Error()
ev.State = "errored"
ev.Body, err = json.Marshal(ev)
}
// Complete : sets the state of the event to completed
func (ev *Event) Complete() {
ev.State = "completed"
}
// Find : Find an object on aws
func (ev *Event) Find() error {
return errors.New(ev.Subject + " not supported")
}
// Create : Creates a nat object on aws
func (ev *Event) Create() error {
svc := ev.getRDSClient()
subnetGroup, err := createSubnetGroup(ev)
if err != nil {
return err
}
req := &rds.CreateDBClusterInput{
DBClusterIdentifier: ev.Name,
Engine: ev.Engine,
EngineVersion: ev.EngineVersion,
Port: ev.Port,
AvailabilityZones: ev.AvailabilityZones,
DatabaseName: ev.DatabaseName,
MasterUsername: ev.DatabaseUsername,
MasterUserPassword: ev.DatabasePassword,
VpcSecurityGroupIds: ev.SecurityGroupAWSIDs,
DBSubnetGroupName: subnetGroup,
BackupRetentionPeriod: ev.BackupRetention,
PreferredBackupWindow: ev.BackupWindow,
PreferredMaintenanceWindow: ev.MaintenanceWindow,
ReplicationSourceIdentifier: ev.ReplicationSource,
}
resp, err := svc.CreateDBCluster(req)
if err != nil {
return err
}
ev.ARN = resp.DBCluster.DBClusterArn
ev.Endpoint = resp.DBCluster.Endpoint
return ev.setTags()
}
// Update : Updates a nat object on aws
func (ev *Event) Update() error {
svc := ev.getRDSClient()
_, err := updateSubnetGroup(ev)
if err != nil {
return err
}
req := &rds.ModifyDBClusterInput{
DBClusterIdentifier: ev.Name,
Port: ev.Port,
MasterUserPassword: ev.DatabasePassword,
BackupRetentionPeriod: ev.BackupRetention,
PreferredBackupWindow: ev.BackupWindow,
PreferredMaintenanceWindow: ev.MaintenanceWindow,
VpcSecurityGroupIds: ev.SecurityGroupAWSIDs,
ApplyImmediately: aws.Bool(true),
}
_, err = svc.ModifyDBCluster(req)
if err != nil {
return err
}
return ev.setTags()
}
// Delete : Deletes a nat object on aws
func (ev *Event) Delete() error {
svc := ev.getRDSClient()
req := &rds.DeleteDBClusterInput{
DBClusterIdentifier: ev.Name,
}
if *ev.FinalSnapshot {
req.FinalDBSnapshotIdentifier = aws.String(*ev.Name + "-Final-Snapshot")
} else {
req.SkipFinalSnapshot = aws.Bool(true)
}
_, err := svc.DeleteDBCluster(req)
if err != nil {
return err
}
waitUntilClusterDeleted(ev)
return deleteSubnetGroup(ev)
}
// Get : Gets a nat object on aws
func (ev *Event) Get() error {
return errors.New(ev.Subject + " not supported")
}
// GetBody : Gets the body for this event
func (ev *Event) GetBody() []byte {
var err error
if ev.Body, err = json.Marshal(ev); err != nil {
log.Println(err.Error())
}
return ev.Body
}
// GetSubject : Gets the subject for this event
func (ev *Event) GetSubject() string {
return ev.Subject
}
func (ev *Event) getRDSClient() *rds.RDS {
creds, _ := credentials.NewStaticCredentials(ev.AccessKeyID, ev.SecretAccessKey, ev.CryptoKey)
return rds.New(session.New(), &aws.Config{
Region: aws.String(ev.DatacenterRegion),
Credentials: creds,
})
}
func (ev *Event) setTags() error {
svc := ev.getRDSClient()
req := &rds.AddTagsToResourceInput{
ResourceName: ev.ARN,
}
for key, val := range ev.Tags {
req.Tags = append(req.Tags, &rds.Tag{
Key: &key,
Value: &val,
})
}
_, err := svc.AddTagsToResource(req)
return err
}
func createSubnetGroup(ev *Event) (*string, error) {
svc := ev.getRDSClient()
if len(ev.NetworkAWSIDs) < 1 {
return nil, nil
}
req := &rds.CreateDBSubnetGroupInput{
DBSubnetGroupDescription: aws.String(*ev.Name + "-sg"),
DBSubnetGroupName: aws.String(*ev.Name + "-sg"),
SubnetIds: ev.NetworkAWSIDs,
}
_, err := svc.CreateDBSubnetGroup(req)
return req.DBSubnetGroupName, err
}
func updateSubnetGroup(ev *Event) (*string, error) {
svc := ev.getRDSClient()
if len(ev.NetworkAWSIDs) < 1 {
return nil, nil
}
req := &rds.ModifyDBSubnetGroupInput{
DBSubnetGroupName: aws.String(*ev.Name + "-sg"),
DBSubnetGroupDescription: aws.String(*ev.Name + "-sg"),
SubnetIds: ev.NetworkAWSIDs,
}
_, err := svc.ModifyDBSubnetGroup(req)
return req.DBSubnetGroupName, err
}
func deleteSubnetGroup(ev *Event) error {
svc := ev.getRDSClient()
req := &rds.DeleteDBSubnetGroupInput{
DBSubnetGroupName: aws.String(*ev.Name + "-sg"),
}
_, err := svc.DeleteDBSubnetGroup(req)
return err
}
func waitUntilClusterDeleted(ev *Event) {
svc := ev.getRDSClient()
req := &rds.DescribeDBClustersInput{
DBClusterIdentifier: ev.Name,
}
for {
_, err := svc.DescribeDBClusters(req)
if err != nil {
return
}
time.Sleep(time.Second * 2)
}
}