diff --git a/lib/cloudwatch_logs.js b/lib/cloudwatch_logs.js new file mode 100644 index 00000000..5fc0dd34 --- /dev/null +++ b/lib/cloudwatch_logs.js @@ -0,0 +1,54 @@ +'use strict' + +class CloudWatchLogs { + constructor (aws) { + // Authenticated `aws` object in `lib/main.js` + this.lambda = new aws.Lambda({ + apiVersion: '2015-03-31' + }) + this.cloudwatchlogs = new aws.CloudWatchLogs({ + apiVersion: '2014-03-28' + }) + } + + _logGroupName (params) { + return `/aws/lambda/${params.FunctionName}` + } + + _createLogGroup (params) { + return new Promise((resolve, reject) => { + this.cloudwatchlogs.createLogGroup({ + logGroupName: this._logGroupName(params) + }, (err, data) => { + if (err) { + if (err.code === 'ResourceAlreadyExistsException') { + // If it exists it will result in an error but there is no problem. + return resolve({}) + } + return reject(err) + } + + resolve(data) + }) + }) + } + + _putRetentionPolicy (params) { + return new Promise((resolve, reject) => { + this.cloudwatchlogs.putRetentionPolicy({ + logGroupName: this._logGroupName(params), + retentionInDays: params.retentionInDays + }, (err, data) => { + if (err) return reject(err) + resolve(data) + }) + }) + } + + setLogsRetentionPolicy (params) { + return this._createLogGroup(params) + .then(() => this._putRetentionPolicy(params)) + } +} + +module.exports = CloudWatchLogs diff --git a/test/cloudwatch_logs.js b/test/cloudwatch_logs.js new file mode 100644 index 00000000..0032591e --- /dev/null +++ b/test/cloudwatch_logs.js @@ -0,0 +1,73 @@ +'use strict' + +const assert = require('chai').assert +const path = require('path') +const aws = require('aws-sdk-mock') +aws.setSDK(path.resolve('node_modules/aws-sdk')) +const CloudWatchLogs = require(path.join('..', 'lib', 'cloudwatch_logs')) + +const mockResponse = { + createLogGroup: { + testCreateLogGroupResponse: 'An empty object is returned in the actual API' + }, + + putRetentionPolicy: { + testPutRetentionPolicyResponse: 'An empty object is returned in the actual API' + } +} + +const params = { + FunctionName: 'node-lambda-test-function', + retentionInDays: 14 +} + +var logs = null + +/* global before, after, describe, it */ +describe('lib/cloudwatch_logs', () => { + before(() => { + aws.mock('CloudWatchLogs', 'createLogGroup', (params, callback) => { + callback(null, mockResponse.createLogGroup) + }) + aws.mock('CloudWatchLogs', 'putRetentionPolicy', (params, callback) => { + callback(null, mockResponse.putRetentionPolicy) + }) + + logs = new CloudWatchLogs(require('aws-sdk')) + }) + + after(() => aws.restore('CloudWatchLogs')) + + describe('_logGroupName', () => { + it('correct value', () => { + assert.equal( + logs._logGroupName(params), + '/aws/lambda/node-lambda-test-function' + ) + }) + }) + + describe('_createLogGroup', () => { + it('using mock', () => { + return logs._createLogGroup(params).then((data) => { + assert.deepEqual(data, mockResponse.createLogGroup) + }) + }) + }) + + describe('_putRetentionPolicy', () => { + it('using mock', () => { + return logs._putRetentionPolicy(params).then((data) => { + assert.deepEqual(data, mockResponse.putRetentionPolicy) + }) + }) + }) + + describe('setLogsRetentionPolicy', () => { + it('using mock', () => { + return logs.setLogsRetentionPolicy(params).then((data) => { + assert.deepEqual(data, mockResponse.putRetentionPolicy) + }) + }) + }) +}) diff --git a/test/schedule_events.js b/test/schedule_events.js index d917852a..1003a4cd 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -54,6 +54,11 @@ describe('lib/schedule_events', () => { schedule = new ScheduleEvents(require('aws-sdk')) }) + after(() => { + aws.restore('CloudWatchEvents') + aws.restore('Lambda') + }) + describe('_ruleDescription (default)', () => { it('correct value', () => { assert.equal(