From 42ce75493a8538e863badac46b584370ff4e6589 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 19 Jul 2017 11:53:22 +0900 Subject: [PATCH 1/3] Add aws-mock.restore to after in test/schedule_events.js --- test/schedule_events.js | 5 +++++ 1 file changed, 5 insertions(+) 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( From 1e619e2dced3759d2a88d49c4266034903c8bcd1 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 19 Jul 2017 12:25:51 +0900 Subject: [PATCH 2/3] Add function for setting CloudWatchLogs RetentionPolicy --- lib/cloudwatch_logs.js | 54 ++++++++++++++++++++++++++++++ test/cloudwatch_logs.js | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 lib/cloudwatch_logs.js create mode 100644 test/cloudwatch_logs.js diff --git a/lib/cloudwatch_logs.js b/lib/cloudwatch_logs.js new file mode 100644 index 00000000..ff73c676 --- /dev/null +++ b/lib/cloudwatch_logs.js @@ -0,0 +1,54 @@ +'use strict' + +const CloudWatchLogs = function (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' + }) +} + +CloudWatchLogs.prototype = { + _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) + }) + }) + }) +}) From 5c25eeb095892acd7b93e1cf268dbb6d0638828b Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 19 Jul 2017 12:44:10 +0900 Subject: [PATCH 3/3] Use class syntax --- lib/cloudwatch_logs.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/cloudwatch_logs.js b/lib/cloudwatch_logs.js index ff73c676..5fc0dd34 100644 --- a/lib/cloudwatch_logs.js +++ b/lib/cloudwatch_logs.js @@ -1,19 +1,19 @@ 'use strict' -const CloudWatchLogs = function (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' - }) -} +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' + }) + } -CloudWatchLogs.prototype = { _logGroupName (params) { return `/aws/lambda/${params.FunctionName}` - }, + } _createLogGroup (params) { return new Promise((resolve, reject) => { @@ -31,7 +31,7 @@ CloudWatchLogs.prototype = { resolve(data) }) }) - }, + } _putRetentionPolicy (params) { return new Promise((resolve, reject) => { @@ -43,7 +43,7 @@ CloudWatchLogs.prototype = { resolve(data) }) }) - }, + } setLogsRetentionPolicy (params) { return this._createLogGroup(params)