serverless-leo
Advanced tools
Comparing version 1.3.2 to 1.3.3
174
lib/leo.js
@@ -8,94 +8,94 @@ 'use strict' | ||
module.exports = { | ||
compileLeo () { | ||
const allFunctions = this.serverless.service.getAllFunctions() | ||
const stage = this.serverless.service.provider.stage | ||
const custom = this.serverless.service.custom[stage] ? this.serverless.service.custom[stage] : this.serverless.service.custom | ||
const cloudformation = this.serverless.service.provider.compiledCloudFormationTemplate | ||
const customInstall = { | ||
Type: 'Custom::Install', | ||
Properties: {} | ||
} | ||
compileLeo () { | ||
const allFunctions = this.serverless.service.getAllFunctions() | ||
const stage = this.serverless.service.provider.stage | ||
const custom = this.serverless.service.custom[stage] ? this.serverless.service.custom[stage] : this.serverless.service.custom | ||
const cloudformation = this.serverless.service.provider.compiledCloudFormationTemplate | ||
const customInstall = { | ||
Type: 'Custom::Install', | ||
Properties: {} | ||
} | ||
if (custom.leoRegister) { | ||
customInstall.Properties.ServiceToken = custom.leoRegister | ||
} else { | ||
customInstall.Properties.ServiceToken = { | ||
'Fn::ImportValue': { | ||
'Fn::Sub': `${custom.leoStack}-Register` | ||
} | ||
} | ||
} | ||
if (custom.leoRegister) { | ||
customInstall.Properties.ServiceToken = custom.leoRegister | ||
} else { | ||
customInstall.Properties.ServiceToken = { | ||
'Fn::ImportValue': { | ||
'Fn::Sub': `${custom.leoStack}-Register` | ||
} | ||
} | ||
} | ||
const registrations = [] | ||
const botIds = [] | ||
const registrations = [] | ||
const botIds = [] | ||
function addInstallProperty (logicalId, installProperty) { | ||
if (botIds.includes(installProperty.id)) { | ||
throw new Error(`Bot IDs must be unique. ${installProperty.id} has already been added to the cloudformation.`) | ||
} | ||
botIds.push(installProperty.id) | ||
if (registrations.length === 0) { | ||
registrations.push(cloneDeep(customInstall)) | ||
} | ||
let currentRegister = registrations[registrations.length - 1] | ||
if (Object.keys(currentRegister.Properties).length > 100) { | ||
currentRegister = cloneDeep(customInstall) | ||
registrations.push(currentRegister) | ||
} | ||
currentRegister.Properties[logicalId] = installProperty | ||
} | ||
function addInstallProperty (logicalId, installProperty) { | ||
if (botIds.includes(installProperty.id)) { | ||
throw new Error(`Bot IDs must be unique. ${installProperty.id} has already been added to the cloudformation.`) | ||
} | ||
botIds.push(installProperty.id) | ||
if (registrations.length === 0) { | ||
registrations.push(cloneDeep(customInstall)) | ||
} | ||
let currentRegister = registrations[registrations.length - 1] | ||
if (Object.keys(currentRegister.Properties).length > 100) { | ||
currentRegister = cloneDeep(customInstall) | ||
registrations.push(currentRegister) | ||
} | ||
currentRegister.Properties[logicalId] = installProperty | ||
} | ||
return BbPromise.each( | ||
allFunctions, | ||
ymlFunctionName => { | ||
const functionObj = this.serverless.service.getFunction(ymlFunctionName) | ||
const leoEvents = functionObj.events ? functionObj.events.filter(event => event.leo) : [] | ||
const logicalId = this.provider.naming.getLambdaLogicalId(ymlFunctionName) | ||
return BbPromise.each( | ||
allFunctions, | ||
ymlFunctionName => { | ||
const functionObj = this.serverless.service.getFunction(ymlFunctionName) | ||
const leoEvents = functionObj.events ? functionObj.events.filter(event => event.leo) : [] | ||
const logicalId = this.provider.naming.getLambdaLogicalId(ymlFunctionName) | ||
if (leoEvents.length > 0) { | ||
leoEvents.forEach(leoEvent => { | ||
const config = leoEvent.leo instanceof Object ? leoEvent.leo : false | ||
const sourceQueue = config ? config.queue : leoEvent.leo | ||
const botNumbers = times((config && config.botCount) || 1, Number) | ||
botNumbers.forEach(botNumber => { | ||
const botSuffix = botNumber > 0 ? '_' + botNumber : '' | ||
let botId = `${this.serverless.service.service}-${stage}-${ymlFunctionName}${botSuffix}` | ||
const installProperty = { | ||
id: botId, | ||
type: 'cron', | ||
settings: { | ||
botNumber | ||
}, | ||
lambdaName: { | ||
Ref: logicalId | ||
} | ||
} | ||
if (config && config.cron) { | ||
installProperty.time = config.cron | ||
} | ||
if (sourceQueue) { | ||
botId = `${this.serverless.service.service}-${stage}-${ymlFunctionName}_${sourceQueue}${botSuffix}` | ||
installProperty.id = botId | ||
installProperty.settings.source = sourceQueue | ||
} | ||
if (config && config.name) { | ||
installProperty.name = config.name + botSuffix | ||
} else { | ||
installProperty.name = functionObj.botName ? functionObj.botName + botSuffix : botId.replace(`${this.serverless.service.service}-${stage}-`, '') | ||
} | ||
if (config && config.codeOverrides) { | ||
installProperty.settings.codeOverrides = config.codeOverrides | ||
} | ||
if (sourceQueue || (config && (config.cron || config.register))) { | ||
addInstallProperty(botId, installProperty) | ||
} | ||
}) | ||
}) | ||
} | ||
}).then(() => { | ||
registrations.forEach((leoRegister, index) => { | ||
cloudformation.Resources[`LeoRegister${index}`] = leoRegister | ||
}) | ||
}) | ||
} | ||
if (leoEvents.length > 0) { | ||
leoEvents.forEach(leoEvent => { | ||
const config = leoEvent.leo instanceof Object ? leoEvent.leo : false | ||
const sourceQueue = config ? config.queue : leoEvent.leo | ||
const botNumbers = times((config && config.botCount) || 1, Number) | ||
botNumbers.forEach(botNumber => { | ||
const botSuffix = botNumber > 0 ? '_' + botNumber : '' | ||
let botId = `${stage}-${this.serverless.service.service}-${ymlFunctionName}${botSuffix}` | ||
const installProperty = { | ||
id: botId, | ||
type: 'cron', | ||
settings: { | ||
botNumber | ||
}, | ||
lambdaName: { | ||
Ref: logicalId | ||
} | ||
} | ||
if (config && config.cron) { | ||
installProperty.time = config.cron | ||
} | ||
if (sourceQueue) { | ||
botId = `${stage}-${this.serverless.service.service}-${ymlFunctionName}_${sourceQueue}${botSuffix}` | ||
installProperty.id = botId | ||
installProperty.settings.source = sourceQueue | ||
} | ||
if (config && config.name) { | ||
installProperty.name = config.name + botSuffix | ||
} else { | ||
installProperty.name = functionObj.botName ? functionObj.botName + botSuffix : botId.replace(`${stage}-${this.serverless.service.service}-`, '') | ||
} | ||
if (config && config.codeOverrides) { | ||
installProperty.settings.codeOverrides = config.codeOverrides | ||
} | ||
if (sourceQueue || (config && (config.cron || config.register))) { | ||
addInstallProperty(botId, installProperty) | ||
} | ||
}) | ||
}) | ||
} | ||
}).then(() => { | ||
registrations.forEach((leoRegister, index) => { | ||
cloudformation.Resources[`LeoRegister${index}`] = leoRegister | ||
}) | ||
}) | ||
} | ||
} |
{ | ||
"name": "serverless-leo", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Serverless plugin for leo microservices", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,4 +48,4 @@ # serverless-leo | ||
events: | ||
- leo | ||
cron: 0 0 1 * * * # Trigger Lambda from a Leo Cron (down to minute) | ||
- leo | ||
cron: 0 0 1 * * * # Trigger Lambda from a Leo Cron (down to minute) | ||
``` | ||
@@ -91,3 +91,3 @@ | ||
- leo: | ||
cron: 0 0 1 * * * | ||
cron: 0 0 1 * * * | ||
``` | ||
@@ -103,3 +103,3 @@ The bot will be named the same as the lambda. | ||
- leo: | ||
queue: helloWorldTestQueue | ||
queue: helloWorldTestQueue | ||
botCount: 4 | ||
@@ -116,3 +116,3 @@ ``` | ||
- leo: | ||
register: true | ||
register: true | ||
``` | ||
@@ -119,0 +119,0 @@ |
@@ -11,3 +11,3 @@ 'use strict' | ||
'timeout': 10, | ||
'name': 'hello-serverless-leo-world-dev-helloNodeWorld', | ||
'name': 'dev-hello-serverless-leo-world-helloNodeWorld', | ||
'package': { | ||
@@ -44,8 +44,8 @@ 'artifact': '.serverless\\helloNodeWorld.zip' | ||
lambda.events = [ | ||
{ | ||
leo: { | ||
register: true | ||
} | ||
} | ||
] | ||
{ | ||
leo: { | ||
register: true | ||
} | ||
} | ||
] | ||
sls.serverless.service.functions.helloNodeWorld = lambda | ||
@@ -59,8 +59,8 @@ await sls.compileLeo() | ||
lambda.events = [ | ||
{ | ||
leo: { | ||
cron: '* * * * * *' | ||
} | ||
} | ||
] | ||
{ | ||
leo: { | ||
cron: '* * * * * *' | ||
} | ||
} | ||
] | ||
sls.serverless.service.functions.helloNodeWorld = lambda | ||
@@ -74,12 +74,12 @@ await sls.compileLeo() | ||
lambda.events = [ | ||
{ | ||
'leo': { | ||
botCount: 5, | ||
name: 'test_hello', | ||
queue: 'something' | ||
} | ||
} | ||
{ | ||
'leo': { | ||
botCount: 5, | ||
name: 'test_hello', | ||
queue: 'something' | ||
} | ||
} | ||
] | ||
sls.serverless.service.functions.helloNodeWorld = lambda | ||
await sls.compileLeo() | ||
await sls.compileLeo() | ||
expect(Object.keys(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'].Properties).length).to.equal(6) | ||
@@ -106,14 +106,14 @@ }) | ||
lambda.events = [ | ||
{ | ||
'leo': { | ||
queue: 'test_hello', | ||
botCount: 5 | ||
} | ||
}, | ||
{ | ||
'leo': { | ||
queue: 'test_hello2', | ||
botCount: 5 | ||
} | ||
} | ||
{ | ||
'leo': { | ||
queue: 'test_hello', | ||
botCount: 5 | ||
} | ||
}, | ||
{ | ||
'leo': { | ||
queue: 'test_hello2', | ||
botCount: 5 | ||
} | ||
} | ||
] | ||
@@ -128,14 +128,14 @@ sls.serverless.service.functions.helloNodeWorld = lambda | ||
lambda.events = [ | ||
{ | ||
'leo': { | ||
queue: 'test_hello', | ||
botCount: 2 | ||
} | ||
}, | ||
{ | ||
'leo': { | ||
queue: 'test_hello2', | ||
botCount: 2 | ||
} | ||
} | ||
{ | ||
'leo': { | ||
queue: 'test_hello', | ||
botCount: 2 | ||
} | ||
}, | ||
{ | ||
'leo': { | ||
queue: 'test_hello2', | ||
botCount: 2 | ||
} | ||
} | ||
] | ||
@@ -145,9 +145,9 @@ sls.serverless.service.functions.helloNodeWorld = lambda | ||
expect(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'] | ||
.Properties['hello-serverless-leo-world-dev-helloNodeWorld_test_hello'].name).to.equal('helloNodeWorld_test_hello') | ||
.Properties['dev-hello-serverless-leo-world-helloNodeWorld_test_hello'].name).to.equal('helloNodeWorld_test_hello') | ||
expect(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'] | ||
.Properties['hello-serverless-leo-world-dev-helloNodeWorld_test_hello2'].name).to.equal('helloNodeWorld_test_hello2') | ||
.Properties['dev-hello-serverless-leo-world-helloNodeWorld_test_hello2'].name).to.equal('helloNodeWorld_test_hello2') | ||
expect(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'] | ||
.Properties['hello-serverless-leo-world-dev-helloNodeWorld_test_hello_1'].name).to.equal('helloNodeWorld_test_hello_1') | ||
.Properties['dev-hello-serverless-leo-world-helloNodeWorld_test_hello_1'].name).to.equal('helloNodeWorld_test_hello_1') | ||
expect(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'] | ||
.Properties['hello-serverless-leo-world-dev-helloNodeWorld_test_hello2_1'].name).to.equal('helloNodeWorld_test_hello2_1') | ||
.Properties['dev-hello-serverless-leo-world-helloNodeWorld_test_hello2_1'].name).to.equal('helloNodeWorld_test_hello2_1') | ||
}) | ||
@@ -166,3 +166,3 @@ it('names the bot according to the config', async () => { | ||
expect(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'] | ||
.Properties['hello-serverless-leo-world-dev-helloNodeWorld_test_hello'].name).to.equal('bot1') | ||
.Properties['dev-hello-serverless-leo-world-helloNodeWorld_test_hello'].name).to.equal('bot1') | ||
}) | ||
@@ -183,4 +183,4 @@ it('names the bot according to the config in events', async () => { | ||
expect(sls.serverless.service.provider.compiledCloudFormationTemplate.Resources['LeoRegister0'] | ||
.Properties['hello-serverless-leo-world-dev-helloNodeWorld_test_hello'].name).to.equal('bot2') | ||
.Properties['dev-hello-serverless-leo-world-helloNodeWorld_test_hello'].name).to.equal('bot2') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39536