This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (42 loc) · 1.27 KB
/
index.js
File metadata and controls
49 lines (42 loc) · 1.27 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
'use strict';
const AWS = require('aws-sdk')
const Promise = require('bluebird')
class Plugin {
constructor(serverless, options) {
this.serverless = serverless
this.hooks = {
'after:deploy:deploy': this.afterDeploy.bind(this),
}
}
getRegion() {
return this.serverless.getProvider('aws').getRegion()
}
getPitr() {
return this.serverless.service.custom && this.serverless.service.custom.pitr || []
}
processItem(item) {
if (!item.tableName) {
return Promise.reject(new Error('serverless-dynamodb-pitr: "tableName" is missing'))
}
if (item.enabled === undefined) {
return Promise.reject(new Error('serverless-dynamodb-pitr: "enabled" is missing'))
}
const params = {
PointInTimeRecoverySpecification: {
PointInTimeRecoveryEnabled: !!item.enabled
},
TableName: item.tableName
};
const dynamodb = new AWS.DynamoDB({ region: this.getRegion()})
return dynamodb.updateContinuousBackups(params).promise()
.then(() => {
this.serverless.cli.log(`Point-in-time recovery ${(item.enabled) ? 'enabled' : 'disabled'} on ${item.tableName}`)
})
}
afterDeploy() {
Promise.all(
this.getPitr().map(item => this.processItem(item))
)
}
}
module.exports = Plugin;