/
db_rds.go
425 lines (334 loc) · 12.6 KB
/
db_rds.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package aws
import (
"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"
log "github.com/sirupsen/logrus"
"github.com/Qovery/pleco/pkg/common"
)
type rdsDatabase struct {
common.CloudProviderResource
DBInstanceStatus string
ParameterGroups []*rds.DBParameterGroupStatus
SubnetGroup *rds.DBSubnetGroup
}
type RDSSubnetGroup struct {
common.CloudProviderResource
ID string
}
func RdsSession(sess session.Session, region string) *rds.RDS {
return rds.New(&sess, &aws.Config{Region: aws.String(region)})
}
func listRDSDatabases(svc rds.RDS) []*rds.DBInstance {
result, err := svc.DescribeDBInstances(&rds.DescribeDBInstancesInput{})
if err != nil {
log.Errorf("Can't get RDS databases in %s: %s", *svc.Config.Region, err.Error())
return []*rds.DBInstance{}
}
return result.DBInstances
}
func listExpiredRDSDatabases(svc rds.RDS, options *AwsOptions) []rdsDatabase {
dbs := listRDSDatabases(svc)
if len(dbs) == 0 {
return nil
}
var expiredDatabases []rdsDatabase
for _, instance := range dbs {
if *instance.DBInstanceStatus == "deleting" {
continue
}
if instance.TagList == nil {
log.Errorf("No tags for instance %s in %s", *instance.DBInstanceIdentifier, *svc.Config.Region)
continue
}
if instance.InstanceCreateTime == nil {
log.Errorf("No creation date for instance %s in %s", *instance.DBInstanceIdentifier, *svc.Config.Region)
continue
}
essentialTags := common.GetEssentialTags(instance.TagList, options.TagName)
time, _ := time.Parse(time.RFC3339, instance.InstanceCreateTime.Format(time.RFC3339))
if instance.DBInstanceIdentifier != nil {
database := rdsDatabase{
CloudProviderResource: common.CloudProviderResource{
Identifier: *instance.DBInstanceIdentifier,
Description: "RDS Database: " + *instance.DBInstanceIdentifier,
CreationDate: time,
TTL: essentialTags.TTL,
Tag: essentialTags.Tag,
IsProtected: essentialTags.IsProtected,
},
DBInstanceStatus: *instance.DBInstanceStatus,
SubnetGroup: instance.DBSubnetGroup,
ParameterGroups: instance.DBParameterGroups,
}
if database.CloudProviderResource.IsResourceExpired(options.TagValue, options.DisableTTLCheck) {
expiredDatabases = append(expiredDatabases, database)
}
}
}
return expiredDatabases
}
func DeleteRDSDatabase(svc rds.RDS, database rdsDatabase) {
if database.DBInstanceStatus == "deleting" {
log.Infof("RDS instance %s is already in deletion process, skipping...", database.Identifier)
return
} else {
log.Infof("Deleting RDS database %s in %s, expired after %d seconds",
database.Identifier, *svc.Config.Region, database.TTL)
}
_, instanceErr := svc.DeleteDBInstance(
&rds.DeleteDBInstanceInput{
DBInstanceIdentifier: aws.String(database.Identifier),
DeleteAutomatedBackups: aws.Bool(true),
SkipFinalSnapshot: aws.Bool(true),
},
)
if instanceErr != nil {
log.Errorf("Can't delete RDS instance %s in %s: %s", database.Identifier, *svc.Config.Region, instanceErr.Error())
} else {
DeleteRDSSubnetGroup(svc, *database.SubnetGroup.DBSubnetGroupName)
for _, parameterGroup := range database.ParameterGroups {
deleteRDSParameterGroups(svc, *parameterGroup.DBParameterGroupName)
}
}
}
func GetRDSInstanceInfos(svc rds.RDS, databaseIdentifier string) (rdsDatabase, error) {
input := rds.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(databaseIdentifier),
}
result, err := svc.DescribeDBInstances(&input)
// ignore if creation is in progress to avoid nil fields
if err != nil || *result.DBInstances[0].DBInstanceStatus == "creating" {
return rdsDatabase{
CloudProviderResource: common.CloudProviderResource{
Identifier: *result.DBInstances[0].DBInstanceIdentifier,
Description: "RDS Database: " + *result.DBInstances[0].DBInstanceIdentifier,
CreationDate: time.Time{},
TTL: 0,
Tag: "",
IsProtected: false,
},
DBInstanceStatus: *result.DBInstances[0].DBInstanceStatus,
}, err
}
time, _ := time.Parse(time.RFC3339, result.DBInstances[0].InstanceCreateTime.Format(time.RFC3339))
return rdsDatabase{
CloudProviderResource: common.CloudProviderResource{
Identifier: *result.DBInstances[0].DBInstanceIdentifier,
Description: "RDS Database: " + *result.DBInstances[0].DBInstanceIdentifier,
CreationDate: time,
TTL: -1,
Tag: "",
IsProtected: false,
},
DBInstanceStatus: *result.DBInstances[0].DBInstanceStatus,
}, nil
}
func DeleteExpiredRDSDatabases(sessions AWSSessions, options AwsOptions) {
expiredDatabases := listExpiredRDSDatabases(*sessions.RDS, &options)
region := *sessions.RDS.Config.Region
count, start := common.ElemToDeleteFormattedInfos("expired RDS database", len(expiredDatabases), region)
log.Info(count)
if options.DryRun || len(expiredDatabases) == 0 || expiredDatabases == nil {
return
}
log.Info(start)
for _, database := range expiredDatabases {
DeleteRDSDatabase(*sessions.RDS, database)
}
}
func DeleteRDSSubnetGroup(svc rds.RDS, dbSubnetGroupName string) {
_, err := svc.DeleteDBSubnetGroup(
&rds.DeleteDBSubnetGroupInput{
DBSubnetGroupName: aws.String(dbSubnetGroupName),
})
if err != nil {
log.Errorf("Can't delete RDS Subnet Group %s in region %s: %s", dbSubnetGroupName, *svc.Config.Region, err.Error())
}
}
func deleteRDSParameterGroups(svc rds.RDS, dbParameterGroupName string) {
_, err := svc.DeleteDBParameterGroup(
&rds.DeleteDBParameterGroupInput{
DBParameterGroupName: aws.String(dbParameterGroupName),
})
if err != nil {
log.Errorf("Can't delete RDS parameter group %s in region %s: %s", dbParameterGroupName, *svc.Config.Region, err.Error())
}
}
func listRDSSubnetGroups(svc rds.RDS) []*rds.DBSubnetGroup {
result, err := svc.DescribeDBSubnetGroups(
&rds.DescribeDBSubnetGroupsInput{})
if err != nil {
log.Errorf("Can't list RDS subnet groups in region %s: %s", *svc.Config.Region, err.Error())
}
return result.DBSubnetGroups
}
func getRDSSubnetGroupTags(svc rds.RDS, subnetGroupName string) []*rds.Tag {
result, err := svc.ListTagsForResource(
&rds.ListTagsForResourceInput{ResourceName: aws.String(subnetGroupName)})
if err != nil {
log.Errorf("Can't get RDS subnet groups tags for %s: %s", subnetGroupName, err.Error())
}
return result.TagList
}
func getExpiredRDSSubnetGroups(svc rds.RDS, options *AwsOptions) []RDSSubnetGroup {
SGs := listRDSSubnetGroups(svc)
expiredRDSSubnetGroups := []RDSSubnetGroup{}
for _, SG := range SGs {
tags := getRDSSubnetGroupTags(svc, *SG.DBSubnetGroupArn)
essentialTags := common.GetEssentialTags(tags, options.TagName)
rDSSubnetGroup := RDSSubnetGroup{
CloudProviderResource: common.CloudProviderResource{
Identifier: *SG.DBSubnetGroupName,
Description: "DB Subnet Group: " + *SG.DBSubnetGroupName,
CreationDate: essentialTags.CreationDate,
TTL: essentialTags.TTL,
Tag: essentialTags.Tag,
IsProtected: essentialTags.IsProtected,
},
ID: *SG.DBSubnetGroupArn,
}
if rDSSubnetGroup.IsResourceExpired(options.TagValue, options.DisableTTLCheck) {
expiredRDSSubnetGroups = append(expiredRDSSubnetGroups, rDSSubnetGroup)
}
}
return expiredRDSSubnetGroups
}
func DeleteExpiredRDSSubnetGroups(sessions AWSSessions, options AwsOptions) {
region := *sessions.RDS.Config.Region
expiredRDSSubnetGroups := getExpiredRDSSubnetGroups(*sessions.RDS, &options)
count, start := common.ElemToDeleteFormattedInfos("expired RDS subnet group", len(expiredRDSSubnetGroups), region)
log.Info(count)
if options.DryRun || len(expiredRDSSubnetGroups) == 0 {
return
}
log.Info(start)
for _, expiredRDSSubnetGroup := range expiredRDSSubnetGroups {
DeleteRDSSubnetGroup(*sessions.RDS, expiredRDSSubnetGroup.Identifier)
}
}
type RDSParameterGroups struct {
common.CloudProviderResource
ID string
}
func listParametersGroups(svc rds.RDS) []*rds.DBParameterGroup {
results, err := svc.DescribeDBParameterGroups(&rds.DescribeDBParameterGroupsInput{})
if err != nil {
log.Errorf("Can't get RDS Parameter Groups in %s: %s", *svc.Config.Region, err.Error())
return nil
}
return results.DBParameterGroups
}
func getCompleteRDSParameterGroups(svc rds.RDS, options *AwsOptions) []RDSParameterGroups {
results := listParametersGroups(svc)
completeRDSParameterGroups := []RDSParameterGroups{}
for _, result := range results {
if strings.Contains(*result.DBParameterGroupName, "default") && !options.IsDestroyingCommand {
log.Debugf("%s is a default rds parameter group, skipping...", *result.DBParameterGroupName)
continue
}
tags, tagsErr := svc.ListTagsForResource(&rds.ListTagsForResourceInput{ResourceName: aws.String(*result.DBParameterGroupArn)})
if tagsErr != nil {
log.Errorf("Can't get RDS Parameter Groups Tags in %s: %s", *svc.Config.Region, tagsErr.Error())
return completeRDSParameterGroups
}
essentialTags := common.GetEssentialTags(tags.TagList, options.TagName)
completeRDSParameterGroups = append(completeRDSParameterGroups, RDSParameterGroups{
CloudProviderResource: common.CloudProviderResource{
Identifier: *result.DBParameterGroupName,
Description: "RDS Parameter Group: " + *result.DBParameterGroupName,
CreationDate: essentialTags.CreationDate,
TTL: essentialTags.TTL,
Tag: essentialTags.Tag,
IsProtected: essentialTags.IsProtected,
},
ID: *result.DBParameterGroupArn,
})
}
return completeRDSParameterGroups
}
func listExpiredCompleteRDSParameterGroups(svc rds.RDS, options *AwsOptions) []RDSParameterGroups {
completeRDSParameterGroups := getCompleteRDSParameterGroups(svc, options)
expiredCompleteRDSParameterGroups := []RDSParameterGroups{}
for _, item := range completeRDSParameterGroups {
if item.IsResourceExpired(options.TagValue, options.DisableTTLCheck) {
expiredCompleteRDSParameterGroups = append(expiredCompleteRDSParameterGroups, item)
}
}
return expiredCompleteRDSParameterGroups
}
func DeleteExpiredCompleteRDSParameterGroups(sessions AWSSessions, options AwsOptions) {
expiredRDSParameterGroups := listExpiredCompleteRDSParameterGroups(*sessions.RDS, &options)
region := *sessions.RDS.Config.Region
count, start := common.ElemToDeleteFormattedInfos("expired RDS Parameter Group", len(expiredRDSParameterGroups), region)
log.Info(count)
if options.DryRun || len(expiredRDSParameterGroups) == 0 || expiredRDSParameterGroups == nil {
return
}
log.Info(start)
for _, dbParameterGroup := range expiredRDSParameterGroups {
deleteRDSParameterGroups(*sessions.RDS, dbParameterGroup.Identifier)
}
}
func listSnapshots(svc rds.RDS) []*rds.DBSnapshot {
result, err := svc.DescribeDBSnapshots(&rds.DescribeDBSnapshotsInput{SnapshotType: aws.String("manual")})
if err != nil {
log.Errorf("Can't list RDS snapshots in region %s: %s", *svc.Config.Region, err.Error())
}
return result.DBSnapshots
}
func getExpiredSnapshots(svc rds.RDS, options *AwsOptions) []*rds.DBSnapshot {
dbs := listRDSDatabases(svc)
snaps := listSnapshots(svc)
expiredSnaps := []*rds.DBSnapshot{}
if len(dbs) == 0 {
for _, snap := range snaps {
if common.CheckSnapshot(snap) &&
(options.IsDestroyingCommand || snap.SnapshotCreateTime.Before(time.Now().UTC().Add(3*time.Hour))) {
expiredSnaps = append(expiredSnaps, snap)
}
}
return expiredSnaps
}
snapsChecking := make(map[string]*rds.DBSnapshot)
for _, snap := range snaps {
if common.CheckSnapshot(snap) {
snapsChecking[*snap.DBSnapshotIdentifier] = snap
}
snapsChecking[*snap.DBSnapshotIdentifier] = snap
}
for _, db := range dbs {
if db.DBClusterIdentifier != nil {
snapsChecking[*db.DBClusterIdentifier] = nil
}
}
for _, snap := range snapsChecking {
if snap != nil {
expiredSnaps = append(expiredSnaps, snap)
}
}
return expiredSnaps
}
func deleteSnapshot(svc rds.RDS, snapName string) {
_, err := svc.DeleteDBSnapshot(&rds.DeleteDBSnapshotInput{DBSnapshotIdentifier: aws.String(snapName)})
if err != nil {
log.Errorf("Can't delete RDS snapshot %s in region %s: %s", snapName, *svc.Config.Region, err.Error())
} else {
log.Debugf("RDS snapshot %s in %s deleted.", snapName, *svc.Config.Region)
}
}
func DeleteExpiredSnapshots(sessions AWSSessions, options AwsOptions) {
expiredSnapshots := getExpiredSnapshots(*sessions.RDS, &options)
region := *sessions.RDS.Config.Region
count, start := common.ElemToDeleteFormattedInfos("expired RDS snapshot", len(expiredSnapshots), region)
log.Info(count)
if options.DryRun || len(expiredSnapshots) == 0 || expiredSnapshots == nil {
return
}
log.Info(start)
for _, snapshot := range expiredSnapshots {
deleteSnapshot(*sessions.RDS, *snapshot.DBSnapshotIdentifier)
}
}