Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch ScheduleEvents to class syntax #360

Merged
Merged
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
76 changes: 36 additions & 40 deletions lib/schedule_events.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
'use strict'

const ScheduleEvents = function (aws) {
// Authenticated `aws` object in `lib/main.js`
this.lambda = new aws.Lambda({
apiVersion: '2015-03-31'
})
this.cloudwatchevents = new aws.CloudWatchEvents({
apiVersion: '2015-10-07'
})
}
class ScheduleEvents {
constructor (aws) {
// Authenticated `aws` object in `lib/main.js`
this.lambda = new aws.Lambda({
apiVersion: '2015-03-31'
})
this.cloudwatchevents = new aws.CloudWatchEvents({
apiVersion: '2015-10-07'
})
}

ScheduleEvents.prototype = {
_ruleDescription: (params) => {
_ruleDescription (params) {
if ('ScheduleDescription' in params && params.ScheduleDescription != null) {
return `${params.ScheduleDescription}`
}
return `${params.ScheduleName} - ${params.ScheduleExpression}`
},
}

_functionName: (params) => {
_functionName (params) {
return params.FunctionArn.split(':').pop()
},
}

_putRulePrams: function (params) {
_putRulePrams (params) {
return {
Name: params.ScheduleName,
Description: this._ruleDescription(params),
State: params.ScheduleState,
ScheduleExpression: params.ScheduleExpression
}
},
}

_putRule: function (params) {
const _this = this
_putRule (params) {
// return RuleArn if created
return new Promise((resolve) => {
const _params = _this._putRulePrams(params)
_this.cloudwatchevents.putRule(_params, (err, rule) => {
const _params = this._putRulePrams(params)
this.cloudwatchevents.putRule(_params, (err, rule) => {
if (err) throw err
resolve(rule)
})
})
},
}

_addPermissionParams: function (params) {
_addPermissionParams (params) {
return {
Action: 'lambda:InvokeFunction',
FunctionName: this._functionName(params),
Principal: 'events.amazonaws.com',
SourceArn: params.RuleArn,
StatementId: params.ScheduleName
}
},
}

_addPermission: function (params) {
const _this = this
_addPermission (params) {
return new Promise((resolve) => {
const _params = _this._addPermissionParams(params)
_this.lambda.addPermission(_params, (err, data) => {
const _params = this._addPermissionParams(params)
this.lambda.addPermission(_params, (err, data) => {
if (err) {
if (err.code !== 'ResourceConflictException') throw err
// If it exists it will result in an error but there is no problem.
Expand All @@ -66,38 +64,36 @@ ScheduleEvents.prototype = {
resolve(data)
})
})
},
}

_putTargetsParams: function (params) {
_putTargetsParams (params) {
return {
Rule: params.ScheduleName,
Targets: [{
Arn: params.FunctionArn,
Id: this._functionName(params)
}]
}
},
}

_putTargets: function (params) {
const _this = this
_putTargets (params) {
return new Promise((resolve) => {
const _params = _this._putTargetsParams(params)
_this.cloudwatchevents.putTargets(_params, (err, data) => {
const _params = this._putTargetsParams(params)
this.cloudwatchevents.putTargets(_params, (err, data) => {
// even if it is already registered, it will not be an error.
if (err) throw (err)
resolve(data)
})
})
},
}

add: function (params) {
const _this = this
add (params) {
return Promise.resolve().then(() => {
return _this._putRule(params)
return this._putRule(params)
}).then((rule) => {
return _this._addPermission(Object.assign(params, rule))
return this._addPermission(Object.assign(params, rule))
}).then((data) => {
return _this._putTargets(params)
return this._putTargets(params)
})
}
}
Expand Down