Skip to content

Commit 8aee1ad

Browse files
abetomoDeviaVir
authored andcommitted
Fix to use authenticated aws object in main.js (#225)
1 parent 8cf047c commit 8aee1ad

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

lib/schedule_events.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict';
22

3-
const aws = require('aws-sdk');
4-
const lambda = new aws.Lambda({
5-
apiVersion: '2015-03-31'
6-
});
7-
const cloudwatchevents = new aws.CloudWatchEvents({
8-
apiVersion: '2015-10-07'
9-
});
3+
const ScheduleEvents = function(aws) {
4+
// Authenticated `aws` object in `lib/main.js`
5+
this.lambda = new aws.Lambda({
6+
apiVersion: '2015-03-31'
7+
});
8+
this.cloudwatchevents = new aws.CloudWatchEvents({
9+
apiVersion: '2015-10-07'
10+
});
11+
};
1012

11-
const ScheduleEvents = {
13+
ScheduleEvents.prototype = {
1214
_ruleDescription: (params) => {
1315
return `${params.ScheduleName} - ${params.ScheduleExpression}`;
1416
},
@@ -17,20 +19,21 @@ const ScheduleEvents = {
1719
return params.FunctionArnPrefix + params.FunctionName;
1820
},
1921

20-
_putRulePrams: (params) => {
22+
_putRulePrams: function(params) {
2123
return {
2224
Name: params.ScheduleName,
23-
Description: ScheduleEvents._ruleDescription(params),
25+
Description: this._ruleDescription(params),
2426
State: params.ScheduleState,
2527
ScheduleExpression: params.ScheduleExpression
2628
};
2729
},
2830

29-
_putRule: (params) => {
31+
_putRule: function(params) {
32+
const _this = this;
3033
// return RuleArn if created
3134
return new Promise((resolve) => {
32-
const _params = ScheduleEvents._putRulePrams(params);
33-
cloudwatchevents.putRule(_params, (err, rule) => {
35+
const _params = _this._putRulePrams(params);
36+
_this.cloudwatchevents.putRule(_params, (err, rule) => {
3437
if (err) throw err;
3538
resolve(rule);
3639
});
@@ -47,10 +50,11 @@ const ScheduleEvents = {
4750
};
4851
},
4952

50-
_addPermission: (params) => {
53+
_addPermission: function(params) {
54+
const _this = this;
5155
return new Promise((resolve) => {
52-
const _params = ScheduleEvents._addPermissionParams(params);
53-
lambda.addPermission(_params, (err, data) => {
56+
const _params = _this._addPermissionParams(params);
57+
_this.lambda.addPermission(_params, (err, data) => {
5458
if (err) {
5559
if (err.code != 'ResourceConflictException') throw err;
5660
// If it exists it will result in an error but there is no problem.
@@ -61,34 +65,36 @@ const ScheduleEvents = {
6165
});
6266
},
6367

64-
_putTargetsParams: (params) => {
68+
_putTargetsParams: function(params) {
6569
return {
6670
Rule: params.ScheduleName,
6771
Targets: [{
68-
Arn: ScheduleEvents._functionArn(params),
72+
Arn: this._functionArn(params),
6973
Id: params.FunctionName
7074
}]
7175
};
7276
},
7377

74-
_putTargets: (params) => {
78+
_putTargets: function(params) {
79+
const _this = this;
7580
return new Promise((resolve) => {
76-
const _params = ScheduleEvents._putTargetsParams(params);
77-
cloudwatchevents.putTargets(_params, (err, data) => {
81+
const _params = _this._putTargetsParams(params);
82+
_this.cloudwatchevents.putTargets(_params, (err, data) => {
7883
// even if it is already registered, it will not be an error.
7984
if (err) throw(err);
8085
resolve(data);
8186
});
8287
});
8388
},
8489

85-
add: (params) => {
90+
add: function(params) {
91+
const _this = this;
8692
return Promise.resolve().then(() => {
87-
return ScheduleEvents._putRule(params);
93+
return _this._putRule(params);
8894
}).then((rule) => {
89-
return ScheduleEvents._addPermission(Object.assign(params, rule));
95+
return _this._addPermission(Object.assign(params, rule));
9096
}).then((data) => {
91-
return ScheduleEvents._putTargets(params);
97+
return _this._putTargets(params);
9298
});
9399
},
94100
};

test/schedule_events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('chai').assert;
44
const path = require('path');
55
const aws = require('aws-sdk-mock');
66
aws.setSDK(path.resolve('node_modules/aws-sdk'));
7+
const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events'));
78

89
const params = {
910
FunctionName: 'node-lambda-test-function',
@@ -49,7 +50,7 @@ describe('schedule_events', () => {
4950
callback(null, mockResponse.addPermission);
5051
});
5152

52-
schedule = require(path.join('..', 'lib', 'schedule_events'));
53+
schedule = new ScheduleEvents(require('aws-sdk'));
5354
});
5455

5556
describe('_ruleDescription', () => {

0 commit comments

Comments
 (0)