Skip to content

Commit

Permalink
Added option for defining timeToLiveSpecification as part of table …
Browse files Browse the repository at this point in the history
…definition
  • Loading branch information
J3tto committed Dec 14, 2020
1 parent 4719759 commit 5c7fd98
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ inputs:
KeyType: HASH
Projection:
ProjectionType: 'ALL'
timeToLiveSpecification: # (optional)
AttributeName: attribute2
Enabled: false
region: us-east-1
```
Expand Down
8 changes: 7 additions & 1 deletion src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { mergeDeepRight, pick } = require('ramda');
const AWS = require('aws-sdk');
// eslint-disable-next-line import/no-unresolved
const { Component } = require('@serverless/core');
const { log, createTable, deleteTable, describeTable, updateTable } = require('./utils');
const { log, createTable, deleteTable, describeTable, updateTable, updateTimeToLive } = require('./utils');

const outputsList = ['name', 'arn', 'region'];

Expand All @@ -27,6 +27,7 @@ const defaults = {
name: null,
region: 'us-east-1',
deletionPolicy: 'delete',
timeToLiveSpecification: undefined
};

class AwsDynamoDb extends Component {
Expand Down Expand Up @@ -88,6 +89,11 @@ class AwsDynamoDb extends Component {
await updateTable.call(this, { dynamodb, prevGlobalSecondaryIndexes, ...config });
}

if (config.timeToLiveSpecification) {
await updateTimeToLive({dynamodb, ...config})

}

log(`Table ${config.name} was successfully deployed to the ${config.region} region.`);

this.state.arn = config.arn;
Expand Down
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,25 @@ async function deleteTable({ dynamodb, name }) {
return !!res;
}

async function updateTimeToLive({dynamodb, name, timeToLiveSpecification= {}}) {
return await dynamodb.waitFor('tableExists', { TableName: name}, async function(err, data) {
if (err) throw err;
return await dynamodb
.updateTimeToLive({
TableName: name,
TimeToLiveSpecification: {
AttributeName: timeToLiveSpecification.AttributeName,
Enabled: timeToLiveSpecification.Enabled,
}
}).promise();
}).promise();
}

module.exports = {
log,
createTable,
describeTable,
updateTable,
deleteTable,
updateTimeToLive,
};
8 changes: 7 additions & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { sleep, generateId, getCredentials, getServerlessSdk, getTable } = require('./utils');
const { sleep, generateId, getCredentials, getServerlessSdk, getTable, getTableTimeToLive } = require('./utils');

// set enough timeout for deployment to finish
jest.setTimeout(30000);
Expand All @@ -16,6 +16,10 @@ const instanceYaml = {
stage: 'dev',
inputs: {
deletionPolicy: 'delete',
timeToLiveSpecification: {
AttributeName : 'expires',
Enabled: true
},
attributeDefinitions: [
{
AttributeName: 'attribute1',
Expand Down Expand Up @@ -75,6 +79,8 @@ it('should successfully deploy dynamodb table and local index', async () => {

const res = await getTable(credentials, name);

//const resTTL = await getTableTimeToLive(credentials, name);

expect(instance.outputs.name).toBeDefined();
expect(instance.outputs.arn).toBeDefined();
expect(res.Table.AttributeDefinitions.length).toEqual(2);
Expand Down
23 changes: 21 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getCredentials = () => {
};

if (!credentials.aws.accessKeyId || !credentials.aws.accessKeyId) {
throw new Error('Unable to run tests. AWS credentials not found in the envionrment');
throw new Error('Unable to run tests. AWS credentials not found in the environment');
}

return credentials;
Expand Down Expand Up @@ -67,4 +67,23 @@ const getTable = async (credentials, tableName) => {
.promise();
};

module.exports = { sleep, generateId, getCredentials, getServerlessSdk, getTable };
/*
* Fetches a DynamoDB timeToLive for a table from aws for validation
* @param ${object} credentials - the cross provider credentials object
* @param ${string} tableName - the name of the dynamodb table
*/
const getTableTimeToLive = async (credentials, tableName) => {
const config = {
credentials: credentials.aws,
region: 'us-east-1',
};
const dynamodb = new AWS.DynamoDB(config);

return dynamodb
.describeTimeToLive({
TableName: tableName,
})
.promise();
};

module.exports = { sleep, generateId, getCredentials, getServerlessSdk, getTable, getTableTimeToLive };

0 comments on commit 5c7fd98

Please sign in to comment.