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

Commit

Permalink
Update configuration handling for region/provider (#10)
Browse files Browse the repository at this point in the history
* Check for AWS provider usage in plugin

* Update region handling for DynamoDB SDK

* Fix lint error

* Update test cases

* Update README.md

* Clean up validate() and constructor() function
  • Loading branch information
sbstjn committed May 12, 2017
1 parent c667568 commit 7c44fa9
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ custom:
field: your-ttl-property-name
```

That's it! After the next deployment serverless will make sure to configure your TTL property in DynamoDB.
That's it! After the next deployment (`sls deploy`) serverless will configure your TTL properties in DynamoDB.

## License

Expand Down
34 changes: 25 additions & 9 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,48 @@ class Plugin {
this.serverless = serverless
this.options = options || {}

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

this.hooks = {
'after:deploy:deploy': this.afterDeploy.bind(this)
}
}

validate () {
assert(this.options && !this.options.noDeploy, 'noDeploy')
assert(this.configuration().constructor === Array, 'invalid configuration found')
assert(this.configuration().length > 0, 'no configuration found')
assert(this.serverless, 'Invalid serverless configuration')
assert(this.serverless.service, 'Invalid serverless configuration')
assert(this.serverless.service.provider, 'Invalid serverless configuration')
assert(this.serverless.service.provider.name, 'Invalid serverless configuration')
assert(this.serverless.service.provider.name === 'aws', 'Only supported for AWS provider')

assert(this.options && !this.options.noDeploy, 'Used --noDeploy flag')
assert(this.list().constructor === Array, 'Invalid configuration found')
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.serverless.cli.log('Enabling TTL setting(s) for DynamoDB')
this.configure.bind(this)
).then(
() => this.serverless.cli.log(util.format('Enabling TTL setting(s) for DynamoDB (%s)', this.region))
).then(
() => this.configuration().map(
() => this.list().map(
data => this.check(data.table).then(
enabled => enabled || this.enable(data)
)
)
).catch(
err => this.serverless.cli.log(util.format('Skipping TTL setting(s) for DynamoDB: %s', err.message))
err => this.serverless.cli.log(util.format('Skipping TTL setting(s) for DynamoDB: %s!', err.message))
)
}

Expand All @@ -60,7 +76,7 @@ class Plugin {
).promise()
}

configuration () {
list () {
try {
return this.serverless.service.custom.dynamodb.ttl || []
} catch (e) {
Expand Down
161 changes: 148 additions & 13 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('Plugin', () => {
const config = {
cli: { log: () => {} },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: {
ttl: [
Expand All @@ -26,16 +30,103 @@ describe('Plugin', () => {

const test = new Plugin(config)

expect(test.configuration()).toContainEqual({ table: 'my-table-1', field: 'my-field-1' })
expect(test.configuration()).toContainEqual({ table: 'my-table-2', field: 'my-field-2' })
expect(test.list()).toContainEqual({ table: 'my-table-1', field: 'my-field-1' })
expect(test.list()).toContainEqual({ table: 'my-table-2', field: 'my-field-2' })
})

it('Skips on noDeploy', () => {
let log = jest.fn()

const config = { cli: { log }, service: { } }
const config = {
cli: { log },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
}
}
}

return new Plugin(config, { noDeploy: true }).afterDeploy().then(
() => expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: noDeploy')
() => expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: Used --noDeploy flag!')
)
})

it('Skips on non-aws provider', () => {
let log = jest.fn()

const config = {
cli: { log },
service: {
provider: {
name: 'google',
region: 'us-east-1'
}
}
}

return new Plugin(config, { }).afterDeploy().then(
() => expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: Only supported for AWS provider!')
)
})

it('Use default service region', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, { TimeToLiveDescription: { TimeToLiveStatus: false } }))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log },
service: {
provider: {
name: 'aws',
region: 'us-example-1'
},
custom: {
dynamodb: { ttl: [ { table: 'my-table-1', field: 'my-field-1' } ] }
}
}
}

return new Plugin(config, { }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Enabling TTL setting(s) for DynamoDB (us-example-1)')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(1)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(1)
}
)
})

it('Use custom region with --region', () => {
let log = jest.fn()
let dynamoDBdescribeTimeToLiveSpy = jest.fn((_, cb) => cb(null, { TimeToLiveDescription: { TimeToLiveStatus: false } }))
let dynamoDBupdateTimeToLiveSpy = jest.fn((_, cb) => cb(null, ''))

AWS.mock('DynamoDB', 'describeTimeToLive', dynamoDBdescribeTimeToLiveSpy)
AWS.mock('DynamoDB', 'updateTimeToLive', dynamoDBupdateTimeToLiveSpy)

const config = {
cli: { log },
service: {
provider: {
name: 'aws',
region: 'us-example-1'
},
custom: {
dynamodb: { ttl: [ { table: 'my-table-1', field: 'my-field-1' } ] }
}
}
}

return new Plugin(config, { region: 'us-awesome-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Enabling TTL setting(s) for DynamoDB (us-awesome-1)')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(1)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(1)
}
)
})

Expand All @@ -49,12 +140,20 @@ describe('Plugin', () => {

const config = {
cli: { log },
service: { custom: { dynamodb: { } } }
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: { }
}
}
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: no configuration found')
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: No configuration found!')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
Expand All @@ -71,12 +170,18 @@ describe('Plugin', () => {

const config = {
cli: { log },
service: { custom: { } }
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: { }
}
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: no configuration found')
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: No configuration found!')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
Expand All @@ -93,12 +198,17 @@ describe('Plugin', () => {

const config = {
cli: { log },
service: { }
service: {
provider: {
name: 'aws',
region: 'us-east-1'
}
}
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: no configuration found')
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: No configuration found!')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
Expand All @@ -115,12 +225,17 @@ describe('Plugin', () => {

const config = {
cli: { log },
service: { custom: { dynamodb: { ttl: { invalid: true } } } }
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: { dynamodb: { ttl: { invalid: true } } } }
}

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: invalid configuration found')
expect(log).toBeCalledWith('Skipping TTL setting(s) for DynamoDB: Invalid configuration found!')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(0)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(0)
}
Expand All @@ -139,6 +254,10 @@ describe('Plugin', () => {
const config = {
cli: { log },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: { ttl: [ { table: 'my-table-1', field: 'my-field-1' } ] }
}
Expand All @@ -147,7 +266,7 @@ describe('Plugin', () => {

return new Plugin(config, { region: 'eu-west-1' }).afterDeploy().then(
() => {
expect(log).toBeCalledWith('Enabling TTL setting(s) for DynamoDB')
expect(log).toBeCalledWith('Enabling TTL setting(s) for DynamoDB (eu-west-1)')
expect(dynamoDBdescribeTimeToLiveSpy).toHaveBeenCalledTimes(1)
expect(dynamoDBupdateTimeToLiveSpy).toHaveBeenCalledTimes(1)
}
Expand All @@ -164,6 +283,10 @@ describe('Plugin', () => {
const config = {
cli: { log: () => {} },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: {
ttl: [ { table: 'my-table-1', field: 'my-field-1' } ]
Expand All @@ -190,6 +313,10 @@ describe('Plugin', () => {
const config = {
cli: { log: () => {} },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: {
ttl: [
Expand Down Expand Up @@ -219,6 +346,10 @@ describe('Plugin', () => {
const config = {
cli: { log: () => {} },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: {
ttl: [
Expand Down Expand Up @@ -251,6 +382,10 @@ describe('Plugin', () => {
const config = {
cli: { log: () => {} },
service: {
provider: {
name: 'aws',
region: 'us-east-1'
},
custom: {
dynamodb: {
ttl: [
Expand Down

0 comments on commit 7c44fa9

Please sign in to comment.