Skip to content

Commit

Permalink
Updated functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindiamond committed Nov 20, 2017
1 parent f85aaac commit 03e5508
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ class SplunkPlugin {
this.stage = (this.options.stage && (this.options.stage.length > 0)) ? this.options.stage : service.provider.stage

this.hooks = {
'before:package:initialize': this.update.bind(this),
'before:package:compileEvents': this.add.bind(this)
'before:package:initialize': this.start.bind(this),
'after:deploy:deploy': this.cleanup.bind(this)
}
}

/**
* Start bindings
*/
start () {
this.update()
this.addFunction()
}

/**
* Add Splunk function to this package
*/
add () {
addFunction () {
const service = this.serverless.service
const stage = this.stage

Expand All @@ -35,10 +43,11 @@ class SplunkPlugin {
service.provider.environment.SPLUNK_HEC_URL = service.custom.splunk.url
service.provider.environment.SPLUNK_HEC_TOKEN = service.custom.splunk.token

const functionPath = path.relative(this.serverless.config.servicePath, path.resolve(__dirname, 'splunk/splunk-cloudwatch-logs-processor'))
let splunkFile = fs.readFileSync(path.resolve(__dirname, 'splunk/splunk-cloudwatch-logs-processor/index.js'))
fs.writeFileSync(path.join(this.serverless.config.servicePath, 'splunk.js'), splunkFile)

service.functions.splunk = {
handler: `${functionPath}/index.handler`,
handler: `splunk.handler`,
events: []
}

Expand Down Expand Up @@ -88,7 +97,7 @@ class SplunkPlugin {
} else {
destination = {
'Fn::GetAtt': [
`${serviceName}-splunk`,
`SplunkLambdaFunction`,
'Arn'
]
}
Expand All @@ -111,7 +120,7 @@ class SplunkPlugin {
_.extend(resource, permission)

service.getAllFunctions().forEach((functionName) => {
if (functionName !== `${serviceName}-splunk`) {
if (functionName !== 'splunk') {
const logicalName = this.provider.naming.getLogGroupLogicalId(functionName)
const logName = logicalName + 'Splunk'

Expand All @@ -133,9 +142,21 @@ class SplunkPlugin {
}
})

console.log(resource)
return resource
}

cleanup () {
this.serverless.cli.log('Removing temporary Splunk function file')
let splunkFile = path.join(this.serverless.config.servicePath, 'splunk.js')

try {
if (fs.existsSync(splunkFile)) {
fs.unlinkSync(splunkFile)
}
} catch (err) {
throw new Error(err)
}
}
}

module.exports = SplunkPlugin

0 comments on commit 03e5508

Please sign in to comment.