-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds-func.js
executable file
·160 lines (133 loc) · 3.6 KB
/
rds-func.js
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
const defaultTag = {
Key: 'ClonedBy',
Value: 'Lambda'
}
const regionTag = { Key: 'SourceRegion',Value: ''}
class RDSFunc{
constructor(AWS) {
this.rds = new AWS.RDS();
}
describeClusters(){
var clusters = this.rds.describeDBClusters({}).promise()
.then((data)=>{
return data.DBClusters; // successful response
})
return clusters;
}
describeInstances(){
var instances = this.rds.describeDBInstances({}).promise()
.then((data)=>{
return data.DBInstances
})
return instances;
}
describeClustersAutomatedSnapshot(cluster){
const params = {
DBClusterIdentifier: cluster,
SnapshotType: "automated",
MaxRecords: 50
}
var snaps = this.rds.describeDBClusterSnapshots(params).promise()
.then((data)=>{
return data.DBClusterSnapshots; // successful response
})
return snaps;
}
describeClustersManualSnapshot(cluster){
const params = {
DBClusterIdentifier: cluster,
SnapshotType: "manual",
MaxRecords: 50
}
var snaps = this.rds.describeDBClusterSnapshots(params).promise()
.then((data)=>{
return data.DBClusterSnapshots; // successful response
})
return snaps;
}
describeInstanceAutomatedSnapshot(instance){
const params = {
DBInstanceIdentifier: instance,
SnapshotType: "automated",
MaxRecords: 50
}
var snaps = this.rds.describeDBSnapshots(params).promise()
.then((data)=>{
return data.DBSnapshots; // successful response
})
return snaps;
}
describeInstanceManualSnapshot(instance){
const params = {
DBInstanceIdentifier: instance,
SnapshotType: "manual",
MaxRecords: 50
}
var snaps = this.rds.describeDBSnapshots(params).promise()
.then((data)=>{
return data.DBSnapshots; // successful response
})
return snaps;
}
copyClusterSnapshot(snap,region){
var paramClone = {
SourceDBClusterSnapshotIdentifier: snap,
TargetDBClusterSnapshotIdentifier: this.renameSnapshot(snap),
Tags: [
defaultTag,
]
};
if(region){
paramClone.SourceRegion=region
regionTag.Value = region
paramClone.Tags.push(regionTag)
}
var newSnapName = this.rds.copyDBClusterSnapshot(paramClone).promise()
.then((data)=>{
return paramClone.TargetDBClusterSnapshotIdentifier
});
return newSnapName
}
copyInstanceSnapshot(snap,region){
var paramClone = {
SourceDBSnapshotIdentifier: snap,
TargetDBSnapshotIdentifier: this.renameSnapshot(snap),
Tags: [
defaultTag,
]
};
if(region){
paramClone.SourceRegion=region
regionTag.Value = region
paramClone.Tags.push(regionTag)
}
console.log(paramClone)
var newSnapName = this.rds.copyDBSnapshot(paramClone).promise()
.then((data)=>{
return paramClone.TargetDBSnapshotIdentifier
});
return newSnapName
}
deleteClusterSnapshot(snap){
this.rds.deleteDBClusterSnapshot({DBClusterSnapshotIdentifier:snap}).promise()
.then((data)=>{
});
}
deleteInstanceSnapshot(snap){
this.rds.deleteDBSnapshot({DBSnapshotIdentifier:snap}).promise()
.then((data)=>{
});
}
renameSnapshot(snapName){
if(snapName.includes('arn:')){
var i = snapName.indexOf("snapshot:rds");
var tmpName = snapName.substring(i, snapName.length)
return tmpName.replace('snapshot:','').replace("rds:", "manual-");
}else{
return snapName.replace("rds:", "manual-")
}
}
}
module.exports = {
RDSFunc
}