Skip to content

Commit

Permalink
Add schema
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Nov 27, 2020
1 parent 785cc88 commit 4002236
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/index.js
Expand Up @@ -28,13 +28,146 @@ class WarmUp {

this.provider = this.serverless.getProvider('aws');

this.extendServerlessSchema();

this.hooks = {
'after:package:initialize': this.afterPackageInitialize.bind(this),
'after:package:createDeploymentArtifacts': this.afterCreateDeploymentArtifacts.bind(this),
'after:deploy:deploy': this.afterDeployFunctions.bind(this),
};
}

/**
* @description Define the additions to the serverless schema by this plugin.
* */
extendServerlessSchema() {
// Most of these are taken from
// https://github.com/serverless/serverless/blob/master/lib/configSchema.js
// https://github.com/serverless/serverless/blob/master/lib/plugins/aws/provider/awsProvider.js
// https://github.com/serverless/serverless/blob/master/lib/plugins/aws/package/compile/events/schedule/index.js

const rateSyntax = '^rate\\((?:1 (?:minute|hour|day)|(?:1\\d+|[2-9]\\d*) (?:minute|hour|day)s)\\)$';
const cronSyntax = '^cron\\(\\S+ \\S+ \\S+ \\S+ \\S+ \\S+\\)$';
const scheduleSyntax = `${rateSyntax}|${cronSyntax}`;

const globalConfigSchemaProperties = {
folderName: { type: 'string' },
cleanFolder: { type: 'boolean' },
name: { type: 'string' },
role: { $ref: '#/definitions/awsLambdaRole' },
tags: { $ref: '#/definitions/awsResourceTags' },
vpc: { $ref: '#/definitions/awsLambdaVpcConfig' },
events: {
type: 'array',
items: {
type: 'object',
anyOf: [
{ type: 'string', pattern: scheduleSyntax },
{
type: 'object',
properties: {
rate: { type: 'string', pattern: scheduleSyntax },
enabled: { type: 'boolean' },
name: {
type: 'string', minLength: 1, maxLength: 64, pattern: '[\\.\\-_A-Za-z0-9]+',
},
description: { type: 'string', maxLength: 512 },
// input: {
// anyOf: [
// { type: 'string', maxLength: 8192 },
// {
// type: 'object',
// oneOf: [
// {
// properties: {
// body: { type: 'string', maxLength: 8192 },
// },
// required: ['body'],
// additionalProperties: false,
// },
// {
// not: {
// required: ['body'],
// },
// },
// ],
// },
// ],
// },
// inputPath: { type: 'string', maxLength: 256 },
// inputTransformer: {
// type: 'object',
// properties: {
// inputTemplate: {
// type: 'string',
// minLength: 1,
// maxLength: 8192,
// },
// inputPathsMap: { type: 'object' },
// },
// required: ['inputTemplate'],
// additionalProperties: false,
// },
},
required: ['rate'],
additionalProperties: false,
},
],
},
},
package: {
type: 'object',
properties: {
artifact: { type: 'string' },
exclude: { type: 'array', items: { type: 'string' } },
include: { type: 'array', items: { type: 'string' } },
individually: { type: 'boolean' },
},
additionalProperties: false,
},
memorySize: { $ref: '#/definitions/awsLambdaMemorySize' },
timeout: { $ref: '#/definitions/awsLambdaTimeout' },
environment: { $ref: '#/definitions/awsLambdaEnvironment' },
prewarm: { type: 'boolean' },
};

const functionConfigSchemaProperties = {
enabled: {
anyOf: [
{ type: 'boolean' },
{ type: 'string' },
{ type: 'array', items: { type: 'string' } },
],
},
clientContext: { type: 'object' }, // any
payload: { type: 'object' }, // any
payloadRaw: { type: 'boolean' },
concurrency: { type: 'integer' },
};

this.serverless.configSchemaHandler.defineCustomProperties({
type: 'object',
properties: {
warmup: {
type: 'object',
properties: { ...globalConfigSchemaProperties, ...functionConfigSchemaProperties },
additionalProperties: false,
},
},
});

this.serverless.configSchemaHandler.defineFunctionProperties('aws', {
type: 'object',
properties: {
warmup: {
type: 'object',
properties: { functionConfigSchemaProperties },
additionalProperties: false,
},
},
});
}

/**
* @description After package initialize hook. Create warmer function and add it to the service.
*
Expand Down
4 changes: 4 additions & 0 deletions test/utils/configUtils.js
@@ -1,6 +1,10 @@
function getServerlessConfig(serverless = {}) {
return {
getProvider: serverless.getProvider || (() => {}),
configSchemaHandler: {
defineCustomProperties() {},
defineFunctionProperties() {},
},
config: {
servicePath: (serverless.config && serverless.config.servicePath) ? serverless.config.servicePath : 'testPath',
},
Expand Down

0 comments on commit 4002236

Please sign in to comment.