Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
Use the built-in getProvider() and region. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarks authored and sbstjn committed Jun 9, 2017
1 parent 7c44fa9 commit 396be37
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 190 deletions.
6 changes: 2 additions & 4 deletions package.json
Expand Up @@ -26,9 +26,7 @@
"url": "https://github.com/Jimdo/serverless-dynamodb-ttl/issues"
},
"homepage": "https://github.com/Jimdo/serverless-dynamodb-ttl#readme",
"dependencies": {
"aws-sdk": "^2.45.0"
},
"dependencies": {},
"standard": {
"envs": [
"node",
Expand All @@ -40,10 +38,10 @@
]
},
"devDependencies": {
"aws-sdk-mock": "^1.7.0",
"coveralls": "^2.13.0",
"dot-json": "^1.0.3",
"jest": "^19.0.2",
"sinon": "^2.3.2",
"snazzy": "^7.0.0",
"standard": "^10.0.2"
}
Expand Down
38 changes: 11 additions & 27 deletions src/plugin.js
Expand Up @@ -2,12 +2,12 @@

const assert = require('assert')
const util = require('util')
const AWS = require('aws-sdk')

class Plugin {
constructor (serverless, options) {
this.serverless = serverless
this.options = options || {}
this.provider = serverless.getProvider('aws')

this.hooks = {
'after:deploy:deploy': this.afterDeploy.bind(this)
Expand All @@ -26,23 +26,11 @@ class Plugin {
assert(this.list().length > 0, 'No configuration found')
}

configure () {
this.region = this.serverless.service.provider.region

if (this.options && this.options.region) {
this.region = this.options.region
}

this.dynamodb = new AWS.DynamoDB({ region: this.region })
}

afterDeploy () {
return Promise.resolve().then(
this.validate.bind(this)
).then(
this.configure.bind(this)
).then(
() => this.serverless.cli.log(util.format('Enabling TTL setting(s) for DynamoDB (%s)', this.region))
() => this.serverless.cli.log(util.format('Enabling TTL setting(s) for DynamoDB (%s)', this.options.region))
).then(
() => this.list().map(
data => this.check(data.table).then(
Expand All @@ -55,25 +43,21 @@ class Plugin {
}

check (table) {
return this.dynamodb.describeTimeToLive(
{
TableName: table
}
).promise().then(
return this.provider.request('DynamoDB', 'describeTimeToLive', {
TableName: table
}).then(
res => res.TimeToLiveDescription.TimeToLiveStatus === 'ENABLED'
)
}

enable (data) {
return this.dynamodb.updateTimeToLive(
{
TableName: data.table,
TimeToLiveSpecification: {
AttributeName: data.field,
Enabled: true
}
return this.provider.request('DynamoDB', 'updateTimeToLive', {
TableName: data.table,
TimeToLiveSpecification: {
AttributeName: data.field,
Enabled: true
}
).promise()
})
}

list () {
Expand Down

0 comments on commit 396be37

Please sign in to comment.