diff --git a/deploy/lib/constants.js b/deploy/lib/constants.js index 374eef64..902df6b0 100644 --- a/deploy/lib/constants.js +++ b/deploy/lib/constants.js @@ -3,17 +3,8 @@ const path = require('path'); const ROOT_PROJECT_DIRECTORY = path.resolve(__dirname, '../../../../'); const SERVERLESS_DIRECTORY = path.resolve(ROOT_PROJECT_DIRECTORY, '.serverless'); -// Function Configuration -const DEFAULT_MEMORY_LIMIT = 128; -const DEFAULT_CPU_LIMIT = 1; -const DEFAULT_MIN_SCALE = 0; -const DEFAULT_MAX_SCALE = 20; module.exports = { ROOT_PROJECT_DIRECTORY, SERVERLESS_DIRECTORY, - DEFAULT_MEMORY_LIMIT, - DEFAULT_CPU_LIMIT, - DEFAULT_MIN_SCALE, - DEFAULT_MAX_SCALE, }; diff --git a/deploy/lib/createFunctions.js b/deploy/lib/createFunctions.js index 57163ed5..650ff32c 100644 --- a/deploy/lib/createFunctions.js +++ b/deploy/lib/createFunctions.js @@ -14,7 +14,10 @@ module.exports = { getFunctions() { const functionsUrl = `namespaces/${this.namespace.id}/functions`; return this.provider.apiManager.get(functionsUrl) - .then(response => response.data.functions); + .then(response => response.data.functions) + .catch((err) => { + throw new Error(err.response.data.message) + }) }, createOrUpdateFunctions(foundFunctions) { @@ -36,45 +39,43 @@ module.exports = { createSingleFunction(func) { const params = { name: func.name, - environment_variables: func.env || {}, + environment_variables: func.env, namespace_id: this.namespace.id, - memory_limit: func.memoryLimit || constants.DEFAULT_MEMORY_LIMIT, - cpu_limit: func.cpuLimit || constants.DEFAULT_CPU_LIMIT, - min_scale: func.minScale || constants.DEFAULT_MIN_SCALE, - max_scale: func.maxScale || constants.DEFAULT_MAX_SCALE, + memory_limit: func.memoryLimit, + min_scale: func.minScale, + max_scale: func.maxScale, runtime: this.runtime, + timeout: func.timeout, + handler: func.handler }; - if (func.timeout) { - params.timeout = func.timeout; - } - if (func.handler) { - params.handler = func.handler; - } + this.serverless.cli.log(`Creating function ${func.name}...`); return this.provider.apiManager.post('functions', params) - .then(response => Object.assign(response.data, { handler: func.handler })); + .then(response => Object.assign(response.data, { handler: func.handler })) + .catch((err) => { + throw new Error(err.response.data.message) + }) }, updateFunction(func, foundFunc) { - const params = { - environment_variables: func.env || {}, - min_scale: func.minScale || foundFunc.min_scale, - max_scale: func.maxScale || foundFunc.max_scale, - memory_limit: func.memoryLimit || foundFunc.memory_limit, - cpu_limit: func.cpuLimit || foundFunc.cpu_limit, - runtime: this.runtime, - redeploy: false, - }; - if (func.timeout) { - params.timeout = func.timeout; - } - if (func.handler) { - params.handler = func.handler; - } + + const params = {}; + + params.redeploy = false; + params.environment_variables = func.env; + params.memory_limit = func.memoryLimit; + params.min_scale = func.minScale; + params.max_scale = func.maxScale; + params.timeout = func.timeout; + params.handler = func.handler; + const updateUrl = `functions/${foundFunc.id}`; this.serverless.cli.log(`Updating function ${func.name}...`); - return this.provider.apiManager.put(updateUrl, params) - .then(response => Object.assign(response.data, { handler: func.handler })); + return this.provider.apiManager.patch(updateUrl, params) + .then(response => Object.assign(response.data, { handler: func.handler })) + .catch((err) => { + throw new Error(err.response.data.message) + }) }, }; diff --git a/deploy/lib/createNamespace.js b/deploy/lib/createNamespace.js index 1917e56c..38bcfc32 100644 --- a/deploy/lib/createNamespace.js +++ b/deploy/lib/createNamespace.js @@ -15,14 +15,21 @@ module.exports = { createIfNotExists(foundNamespace) { // If Space already exists -> Do not create + + if (foundNamespace && foundNamespace.status === 'error') { + throw new Error(foundNamespace.error_message) + } + const isReady = foundNamespace && foundNamespace.status === 'ready'; if (isReady) { this.saveNamespaceToProvider(foundNamespace); + this.updateNamespace(foundNamespace); return BbPromise.resolve(); } if (foundNamespace && !isReady) { this.serverless.cli.log('Waiting for Namespace to become ready...'); + this.updateNamespace(foundNamespace); return this.waitNamespaceIsReady(); } @@ -35,12 +42,18 @@ module.exports = { return this.provider.apiManager.post('namespaces', params) .then(response => this.saveNamespaceToProvider(response.data)) + .catch((err) => { + throw new Error(err.response.data.message) + }) .then(() => this.waitNamespaceIsReady()); }, waitNamespaceIsReady() { return this.provider.apiManager.get(`namespaces/${this.namespace.id}`) .then((response) => { + if (response.data.status == 'error') { + throw new Error(response.data.error_message) + } if (response.data.status !== 'ready') { return new Promise((resolve) => { setTimeout(() => resolve(this.waitNamespaceIsReady()), 1000); @@ -49,4 +62,17 @@ module.exports = { return true; }); }, + + updateNamespace(foundNamespace) { + if (this.namespaceVariables) { + + const params = {}; + params.environment_variables = this.namespaceVariables + + return this.provider.apiManager.patch(`namespaces/${foundNamespace.id}`, params) + .catch((err) => { + throw new Error(err.response.data.message) + }) + } + } }; diff --git a/deploy/lib/deployFunctions.js b/deploy/lib/deployFunctions.js index 2d7712f5..e4a7de17 100644 --- a/deploy/lib/deployFunctions.js +++ b/deploy/lib/deployFunctions.js @@ -8,12 +8,19 @@ module.exports = { return BbPromise.bind(this) .then(this.deployEachFunction) .then(() => this.serverless.cli.log('Waiting for function deployments, this may take multiple minutes...')) + .catch((err) => { + throw new Error(err.response.data.message) + }) .then(this.waitFunctionsAreDeployed); }, deployEachFunction() { const promises = this.functions.map( - func => this.provider.apiManager.post(`functions/${func.id}/deploy`, {}).then(response => response.data), + func => this.provider.apiManager.post(`functions/${func.id}/deploy`, {}) + .then(response => response.data) + .catch((err) => { + throw new Error(err.response.data.message) + }), ); return Promise.all(promises); @@ -26,6 +33,9 @@ module.exports = { let functionsAreReady = true; for (let i = 0; i < functions.length; i += 1) { const func = response.data.functions[i]; + if (func.status === 'error') { + throw new Error(func.error_message) + } if (func.status !== 'ready') { functionsAreReady = false; break; diff --git a/package.json b/package.json index 05206525..6674decd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-scaleway-functions", - "version": "0.1.3", + "version": "0.1.4", "description": "Provider plugin for the Serverless Framework v1.x which adds support for Scaleway Functions.", "main": "index.js", "author": "scaleway.com",