Skip to content

Commit

Permalink
PLAT-916 - changes for inclusion by default in sls frmwk! (#111)
Browse files Browse the repository at this point in the history
 PLAT-916 - changes for inclusion by default in sls frmwk!
  • Loading branch information
dschep authored May 3, 2019
2 parents 29ef429 + 980a7bf commit f93502b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@serverless/enterprise-plugin",
"version": "0.5.0",
"engines": {
"node": ">=6.0"
},
"description": "The Serverless Enterprise plugin",
"main": "dist/index.js",
"scripts": {
Expand Down
68 changes: 46 additions & 22 deletions src/lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,70 @@ class ServerlessEnterprisePlugin {
const user = getLoggedInUser()
const currentCommand = sls.processedInput.commands[0]

// Skip everything if user is not logged in and not trying to log in or out...
if (
!user &&
(currentCommand !== 'login' &&
currentCommand !== 'logout' &&
!process.env.SERVERLESS_ACCESS_KEY)
) {
const errorMessage = `You are not currently logged in. To log in, use: $ serverless login`
console.log('') // eslint-disable-line
sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line
throw new Error(errorMessage) // eslint-disable-line
// default hook, only applies if user isn't using SFE. gets overridden if they are
this.hooks = {
'after:aws:deploy:finalize:cleanup': () =>
sls.cli.log(
'Run `serverless login` and deploy again to explore, monitor, secure your serverless project for free.',
'Serverless Enterprise'
)
}

// Defaults
this.sls = sls
this.state = {} // Useful for storing data across hooks
this.state.secretsUsed = new Set()
this.provider = this.sls.getProvider('aws')

// Check if Enterprise is configured
const missing = []
if (!this.sls.service.tenant) {
if (!sls.service.tenant) {
missing.push('tenant')
}
if (!this.sls.service.app) {
if (!sls.service.app) {
missing.push('app')
}
if (!this.sls.service.service) {
if (!sls.service.service) {
missing.push('service')
}
if (missing.length > 0) {
this.sls.cli.log(

// Skip everything if user is not logged in and not trying to log in or out...
if (
!user &&
(currentCommand !== 'login' &&
currentCommand !== 'logout' &&
!process.env.SERVERLESS_ACCESS_KEY)
) {
if (missing.includes('tenant') && missing.includes('app')) {
return // user isn't trying to use SFE
}
const errorMessage = `You are not currently logged in. To log in, use: $ serverless login`
console.log('') // eslint-disable-line
sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line
throw new Error(errorMessage) // eslint-disable-line
}
if (currentCommand !== 'login' && currentCommand !== 'logout' && missing.length > 0) {
sls.cli.log(
`Warning: The Enterprise Plugin requires a ${missing
.map((opt) => `"${opt}"`)
.join(', ')} property in your "serverless.yml" and will not work without it.`,
'Serverless Enterprise'
)
// replace the default hook with a message about configuring sls enterprise
this.hooks = {
'after:aws:deploy:finalize:cleanup': () =>
sls.cli.log(
`Update your "serverless.yml" with ${missing
.map((opt) => `"${opt}"`)
.join(
', '
)} properties and deploy again to explore, monitor, secure your serverless project for free.`,
'Serverless Enterprise'
)
}
return
}

// Defaults
this.sls = sls
this.state = {} // Useful for storing data across hooks
this.state.secretsUsed = new Set()
this.provider = this.sls.getProvider('aws')

// Add commands
this.commands = {
login: {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { saveDeployment } from './deployment'
import { hookIntoVariableGetter } from './variables'
import { generate } from './generateEvent'
import injectLogsIamRole from './injectLogsIamRole'
import _ from 'lodash'

afterAll(() => jest.restoreAllMocks())

Expand Down Expand Up @@ -97,7 +98,7 @@ describe('plugin', () => {
})

it('construct requires tenant', () => {
const slsClone = Object.assign({}, sls)
const slsClone = _.cloneDeep(sls)
delete slsClone.service.tenant
const instance = new ServerlessEnterprisePlugin(slsClone) // eslint-disable-line
expect(slsClone.getProvider).toBeCalledWith('aws')
Expand Down

0 comments on commit f93502b

Please sign in to comment.