This repository was archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.go
More file actions
135 lines (114 loc) · 2.96 KB
/
db.go
File metadata and controls
135 lines (114 loc) · 2.96 KB
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
package possum
import (
"context"
"time"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
)
func DoDB(ctx context.Context, client rdsiface.RDSAPI, ts time.Time, schedules Schedules) (Changes, error) {
instances, err := getDBInstances(ctx, client)
if err != nil {
return nil, err
}
changes := getDBInstanceChanges(instances, ts, schedules)
err = performDBInstanceChanges(client, changes)
return changes, err
}
type dbInstanceSchedule struct {
resource *rds.DBInstance
schedule string
}
func getDBInstances(ctx context.Context, client rdsiface.RDSAPI) ([]*dbInstanceSchedule, error) {
var list []*dbInstanceSchedule
var instances []*rds.DBInstance
err := client.DescribeDBInstancesPagesWithContext(
ctx,
&rds.DescribeDBInstancesInput{},
func(page *rds.DescribeDBInstancesOutput, lastPage bool) bool {
for _, group := range page.DBInstances {
instances = append(instances, group)
}
return true
},
)
if err != nil {
return list, err
}
for _, instance := range instances {
res, err := client.ListTagsForResourceWithContext(
ctx,
&rds.ListTagsForResourceInput{
ResourceName: instance.DBInstanceArn,
},
)
if err != nil {
return list, err
}
if schedule := getRDSTagValue(res.TagList, scheduleTag); schedule != nil {
list = append(list, &dbInstanceSchedule{
resource: instance,
schedule: *schedule,
})
}
}
return list, nil
}
// @todo extend the schemas with the db maintenance window
func getDBInstanceChanges(list []*dbInstanceSchedule, ts time.Time, schedules Schedules) Changes {
const runningState = "available"
const stoppedState = "stopped"
var changes Changes
for _, a := range list {
dbInstance := a.resource
// skip db list that are in a transitional state
if *dbInstance.DBInstanceStatus != runningState && *dbInstance.DBInstanceStatus != stoppedState {
continue
}
effectiveSchedule := schedules.Find(a.schedule)
if effectiveSchedule == nil {
continue
}
isRunning := *dbInstance.DBInstanceStatus == runningState
act := effectiveSchedule.Action(ts, isRunning)
if act == NoopAction {
continue
}
changes = append(changes, Change{
ID: dbInstance.DBInstanceIdentifier,
Name: *dbInstance.DBInstanceIdentifier,
Action: act,
Type: "rds",
})
}
return changes
}
func performDBInstanceChanges(client rdsiface.RDSAPI, list Changes) error {
for _, a := range list {
switch a.Action {
case StartAction:
_, err := client.StartDBInstance(&rds.StartDBInstanceInput{
DBInstanceIdentifier: a.ID,
})
if err != nil {
return err
}
case StopAction:
_, err := client.StopDBInstance(&rds.StopDBInstanceInput{
DBInstanceIdentifier: a.ID,
})
if err != nil {
return err
}
}
}
return nil
}
// helper to get a specific value out of rds tags
func getRDSTagValue(tags []*rds.Tag, keyName string) *string {
for _, tag := range tags {
if *tag.Key == keyName {
return tag.Value
}
}
return nil
}