Repository: motdotla/node-lambda
Pull request #359 (2017-07-20 01:24:58)

Add function for setting CloudWatchLogs RetentionPolicy
I would like to add it as it is convenient to set the RetentionPolicy of CloudWatch Log automatically.
I will do the addition to `lib/main.js` after this.

DeviaVir: Absolutely liking the syntax you're building up here (referring to the `lib/` path), great stuff.

Patch:
diff --git a/lib/cloudwatch_logs.js b/lib/cloudwatch_logs.js
new file mode 100644
index 0000000..5fc0dd3
--- /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


Test patch:
diff --git a/test/cloudwatch_logs.js b/test/cloudwatch_logs.js
new file mode 100644
index 0000000..0032591
--- /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 d917852..1003a4c 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(