Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/cloudwatch_logs.js
Original file line number Diff line number Diff line change
@@ -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
73 changes: 73 additions & 0 deletions test/cloudwatch_logs.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
5 changes: 5 additions & 0 deletions test/schedule_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down