From de1a5650776bf8763bc63511584562457af51f86 Mon Sep 17 00:00:00 2001 From: Jose Quijada Date: Mon, 19 Jul 2021 21:48:07 -0400 Subject: [PATCH 1/2] introduce serverless v2 schema validation --- add-log-retention.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/add-log-retention.js b/add-log-retention.js index 7e90c2b..b521cf7 100644 --- a/add-log-retention.js +++ b/add-log-retention.js @@ -15,6 +15,25 @@ class AwsAddLogRetention { this.serverless = serverless; this.options = options; this.provider = this.serverless.getProvider('aws'); + + // Add schema based validation of service config + const propertyDefinitions = { + properties: { + logRetentionInDays: { type: 'number' } + }, + additionalProperties: false + }; + if(this.serverless.configSchemaHandler.defineFunctionProperties) { + this.serverless.configSchemaHandler.defineFunctionProperties('aws', propertyDefinitions); + } + + if(this.serverless.configSchemaHandler.defineCustomProperties) { + this.serverless.configSchemaHandler.defineCustomProperties({ + type: 'object', + ...propertyDefinitions + }); + } + this.hooks = { 'package:createDeploymentArtifacts': this.beforeDeploy.bind(this), }; From 83859f8569ca8092a8575d504db70badb6cea611 Mon Sep 17 00:00:00 2001 From: Jose Quijada Date: Tue, 27 Jul 2021 16:42:17 -0400 Subject: [PATCH 2/2] introduce serverless v2 schema validation make corrections to custom section properties validation and update docs --- README.md | 3 ++- add-log-retention.js | 15 ++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8b5ce25..3136a50 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ provider: name: aws custom: - logRetentionInDays: 30 # used to set a global value for all functions + serverless-plugin-log-retention: + logRetentionInDays: 30 # used to set a global value for all functions functions: function1: diff --git a/add-log-retention.js b/add-log-retention.js index b521cf7..4bbf45d 100644 --- a/add-log-retention.js +++ b/add-log-retention.js @@ -2,6 +2,7 @@ const nco = require('nco'); const semver = require('semver'); +const PLUGIN_NAME = 'serverless-plugin-log-retention'; //values from http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutRetentionPolicy.html const validRetentionInDays = [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653]; @@ -20,8 +21,7 @@ class AwsAddLogRetention { const propertyDefinitions = { properties: { logRetentionInDays: { type: 'number' } - }, - additionalProperties: false + } }; if(this.serverless.configSchemaHandler.defineFunctionProperties) { this.serverless.configSchemaHandler.defineFunctionProperties('aws', propertyDefinitions); @@ -30,7 +30,12 @@ class AwsAddLogRetention { if(this.serverless.configSchemaHandler.defineCustomProperties) { this.serverless.configSchemaHandler.defineCustomProperties({ type: 'object', - ...propertyDefinitions + properties: { + [PLUGIN_NAME] : { + type: 'object', + ...propertyDefinitions + } + } }); } @@ -77,8 +82,8 @@ class AwsAddLogRetention { beforeDeploy() { const service = this.serverless.service; - const globalLogRetentionInDays = service.custom && service.custom.logRetentionInDays - ? this.sanitizeRetentionValue(service.custom.logRetentionInDays) + const globalLogRetentionInDays = service.custom && service.custom[PLUGIN_NAME] && service.custom[PLUGIN_NAME].logRetentionInDays + ? this.sanitizeRetentionValue(service.custom[PLUGIN_NAME].logRetentionInDays) : null; this.addLogRetentionForFunctions(globalLogRetentionInDays); }