Skip to content

Commit 67d8294

Browse files
tenyoTenyo Grozev
and
Tenyo Grozev
authored
Add support for restoring database/cluster from a snapshot (#19)
* add support for creating database instance from snapshot * support for restoring clusters * readme * Support for creating and deleting (manual) snapshots (#20) * support for deleting snapshots * add support for creating snapshots * fixup * move to orchestrator functions * fix readme Co-authored-by: Tenyo Grozev <[email protected]> * fixup * fix else-if workflow Co-authored-by: Tenyo Grozev <[email protected]>
1 parent 4ad0d80 commit 67d8294

File tree

6 files changed

+531
-58
lines changed

6 files changed

+531
-58
lines changed

README.md

+128-52
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ POST http://127.0.0.1:3000/v1/rds/{account}
4747

4848
All instance creation parameters are listed at https://docs.aws.amazon.com/sdk-for-go/api/service/rds/#CreateDBInstanceInput
4949

50-
To create an Aurora cluster with one database instance:
50+
To create an Aurora provisioned cluster with one database instance:
5151

5252
```
5353
POST http://127.0.0.1:3000/v1/rds/{account}
@@ -103,6 +103,63 @@ POST http://127.0.0.1:3000/v1/rds/{account}
103103
}
104104
```
105105

106+
### Restoring a database from snapshot
107+
108+
You can use the same endpoint for creating a database but just specify the name of the snapshot (`SnapshotIdentifier`) in the input.
109+
110+
For example, to restore a Postgres database instance:
111+
112+
```
113+
POST http://127.0.0.1:3000/v1/rds/{account}
114+
{
115+
"Instance":{
116+
"DBInstanceIdentifier":"restored-mypostgres",
117+
"SnapshotIdentifier": "rds:spinup-db000001-2021-08-08-08-08",
118+
"VpcSecurityGroupIds":[
119+
"sg-12345678"
120+
]
121+
}
122+
}
123+
```
124+
125+
To restore an Aurora provisioned cluster (you need to specify the size of the database instance):
126+
127+
```
128+
POST http://127.0.0.1:3000/v1/rds/{account}
129+
{
130+
"Cluster":{
131+
"DBClusterIdentifier":"restored-myaurora",
132+
"SnapshotIdentifier": "rds:spinup-db000fff-2021-08-09-10-11",
133+
"VpcSecurityGroupIds":[
134+
"sg-12345678"
135+
]
136+
},
137+
"Instance": {
138+
"DBInstanceClass": "db.t3.small"
139+
}
140+
}
141+
```
142+
143+
To restore an Aurora serverless cluster:
144+
145+
```
146+
POST http://127.0.0.1:3000/v1/rds/{account}
147+
{
148+
"Cluster":{
149+
"DBClusterIdentifier":"test-restored-svrless-aurora",
150+
"SnapshotIdentifier": "rds:spinup-db123abc-2021-08-07-06-05",
151+
"ScalingConfiguration": {
152+
"AutoPause": true,
153+
"MaxCapacity": 1,
154+
"MinCapacity": 1,
155+
"SecondsUntilAutoPause": 300
156+
},
157+
"VpcSecurityGroupIds":[
158+
"sg-12345678"
159+
]
160+
}
161+
}```
162+
106163
### Getting details about a database
107164
108165
To get details about a specific database instance or cluster:
@@ -139,57 +196,6 @@ To get details about _all_ database instances in the given account (to list both
139196
GET http://127.0.0.1:3000/v1/rds/{account}[?all=true]
140197
```
141198
142-
### Modifying database parameters
143-
144-
You can specify either cluster or instance parameters in the PUT to modify a cluster or an instance.
145-
146-
For example, to change the master password for an Aurora cluster:
147-
148-
```
149-
PUT http://127.0.0.1:3000/v1/rds/{account}/myaurora
150-
{
151-
"Cluster": {
152-
"MasterUserPassword": "EXAMPLE",
153-
"ApplyImmediately": true
154-
}
155-
}
156-
```
157-
158-
### Updating tags for a database
159-
160-
You can pass a list of tags (Key/Value pairs) to add or updated on the given database. If there is an RDS cluster and instance with the same name, the tags for both will be updated.
161-
162-
```
163-
PUT http://127.0.0.1:3000/v1/rds/{account}/myaurora
164-
{
165-
"Tags": [
166-
{
167-
"Key": "NewTag",
168-
"Value": "new"
169-
}
170-
]
171-
}
172-
```
173-
174-
### Deleting a database
175-
176-
By default, a final snapshot is _not_ created when deleting a database instance. You can override that by adding `snapshot=true` query parameter.
177-
178-
```
179-
DELETE http://127.0.0.1:3000/v1/rds/{account}/mypostgres[?snapshot=true]
180-
```
181-
182-
The API will check if the database instance belongs to a cluster and will automatically delete the cluster if this is the last member.
183-
184-
### Stopping and starting a database/cluster
185-
186-
```
187-
PUT http://127.0.0.1:3000/v1/rds/{account}/myaurora/power
188-
{
189-
"state": "stop|start"
190-
}
191-
```
192-
193199
### Getting a list of snapshots for a database/cluster
194200
195201
This will return list of snapshots (with details) for the specified database in either `DBClusterSnapshots` or `DBSnapshots`, depending if it's a cluster or an instance.
@@ -281,6 +287,76 @@ GET http://127.0.0.1:3000/v1/rds/{account}/snapshots/rds:mydbinstance-2021-07-22
281287
}
282288
```
283289
290+
### Creating a database snapshot
291+
292+
This will create a manual snapshot of the specified database.
293+
294+
```
295+
POST http://127.0.0.1:3000/v1/rds/{account}/{db}/snapshots
296+
{
297+
"SnapshotIdentifier": "mytestbackup-1"
298+
}
299+
```
300+
301+
### Deleting a specific snapshot
302+
303+
This will delete a manual snapshot (automatic snapshots cannot be deleted but can be controlled by the backup retention period).
304+
305+
```
306+
DELETE http://127.0.0.1:3000/v1/rds/{account}/snapshots/mytestbackup-1
307+
```
308+
309+
### Modifying database parameters
310+
311+
You can specify either cluster or instance parameters in the PUT to modify a cluster or an instance.
312+
313+
For example, to change the master password for an Aurora cluster:
314+
315+
```
316+
PUT http://127.0.0.1:3000/v1/rds/{account}/myaurora
317+
{
318+
"Cluster": {
319+
"MasterUserPassword": "EXAMPLE",
320+
"ApplyImmediately": true
321+
}
322+
}
323+
```
324+
325+
### Updating tags for a database
326+
327+
You can pass a list of tags (Key/Value pairs) to add or updated on the given database. If there is an RDS cluster and instance with the same name, the tags for both will be updated.
328+
329+
```
330+
PUT http://127.0.0.1:3000/v1/rds/{account}/myaurora
331+
{
332+
"Tags": [
333+
{
334+
"Key": "NewTag",
335+
"Value": "new"
336+
}
337+
]
338+
}
339+
```
340+
341+
### Deleting a database
342+
343+
By default, a final snapshot is _not_ created when deleting a database instance. You can override that by adding `snapshot=true` query parameter.
344+
345+
```
346+
DELETE http://127.0.0.1:3000/v1/rds/{account}/mypostgres[?snapshot=true]
347+
```
348+
349+
The API will check if the database instance belongs to a cluster and will automatically delete the cluster if this is the last member.
350+
351+
### Stopping and starting a database/cluster
352+
353+
```
354+
PUT http://127.0.0.1:3000/v1/rds/{account}/myaurora/power
355+
{
356+
"state": "stop|start"
357+
}
358+
```
359+
284360
## Development
285361
286362
- Install Buffalo framework (v0.13+): https://gobuffalo.io/en/docs/installation

actions/app.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ func App() *buffalo.App {
107107
rdsV1API.GET("/{db}", DatabasesGet)
108108
rdsV1API.PUT("/{db}", DatabasesPut)
109109
rdsV1API.PUT("/{db}/power", DatabasesPutState)
110-
rdsV1API.GET("/{db}/snapshots", SnapshotsList)
111110
rdsV1API.DELETE("/{db}", DatabasesDelete)
111+
rdsV1API.POST("/{db}/snapshots", SnapshotsPost)
112+
rdsV1API.GET("/{db}/snapshots", SnapshotsList)
112113
rdsV1API.GET("/snapshots/{snap}", SnapshotsGet)
114+
rdsV1API.DELETE("/snapshots/{snap}", SnapshotsDelete)
113115

114116
log.Printf("Started rds-api in org %s", Org)
115117
}

actions/databases.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,19 @@ func DatabasesPost(c buffalo.Context) error {
140140
client: rdsClient,
141141
}
142142

143-
resp, err := orch.databaseCreate(c, &req)
144-
if err != nil {
145-
return handleError(c, err)
143+
var resp *DatabaseResponse
144+
var err error
145+
146+
if (req.Cluster != nil && req.Cluster.SnapshotIdentifier != nil) || (req.Instance != nil && req.Instance.SnapshotIdentifier != nil) {
147+
// restoring database from snapshot
148+
if resp, err = orch.databaseRestore(c, &req); err != nil {
149+
return handleError(c, err)
150+
}
151+
} else {
152+
// creating database from scratch
153+
if resp, err = orch.databaseCreate(c, &req); err != nil {
154+
return handleError(c, err)
155+
}
146156
}
147157

148158
return c.Render(200, r.JSON(resp))

0 commit comments

Comments
 (0)