From 45ae13b3983958b66e5671c107ccce843adb3e5a Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Mon, 24 Jun 2019 18:20:41 +0100 Subject: [PATCH 1/3] Add environment variable to disable resource creation --- extension/docs/DevelopmentAndDeploymentGuide.md | 1 + extension/src/config/config.js | 3 ++- extension/src/config/init/ensure-resources.js | 6 ++++-- notification/docs/DevelopmentGuide.md | 1 + notification/src/config/config.js | 3 ++- .../config/init/ensure-interface-interaction-custom-type.js | 2 ++ 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/extension/docs/DevelopmentAndDeploymentGuide.md b/extension/docs/DevelopmentAndDeploymentGuide.md index 1d50d3590..85b4b3dac 100644 --- a/extension/docs/DevelopmentAndDeploymentGuide.md +++ b/extension/docs/DevelopmentAndDeploymentGuide.md @@ -36,6 +36,7 @@ Environment variables to configure the notification module: |`API_EXTENSION_BASE_URL` | URL of the Extension module server. In case of any payment changes, [CTP API extension](https://docs.commercetools.com/http-api-projects-api-extensions) will call this URL and pass the payment object in body. | YES | | |`LOG_LEVEL` | bunyan log level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`)| NO | `info` |`PORT` | port on which the application will run | NO | 8080 +|`DISABLE_ENSURE_RESOURCES` | disables the creation of resources in commercetools (e.g. custom types) | NO | `false` After setting all environmental variables, execute command `npm run extension` to run the module. diff --git a/extension/src/config/config.js b/extension/src/config/config.js index 574dd167d..fa9df83bd 100644 --- a/extension/src/config/config.js +++ b/extension/src/config/config.js @@ -6,7 +6,8 @@ function getEnvConfig () { logLevel: process.env.LOG_LEVEL, apiExtensionBaseUrl: process.env.API_EXTENSION_BASE_URL, keepAliveTimeout: !Number.isNaN(process.env.KEEP_ALIVE_TIMEOUT) ? parseFloat(process.env.KEEP_ALIVE_TIMEOUT, 10) - : undefined + : undefined, + disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === "true" || false } } diff --git a/extension/src/config/init/ensure-resources.js b/extension/src/config/init/ensure-resources.js index 66aac1df7..46a72e563 100644 --- a/extension/src/config/init/ensure-resources.js +++ b/extension/src/config/init/ensure-resources.js @@ -2,8 +2,9 @@ const { ensurePaymentCustomType } = require('./ensure-payment-custom-type') const { ensureInterfaceInteractionCustomType } = require('./ensure-interface-interaction-custom-type') const { ensureApiExtensions } = require('./ensure-api-extensions') const ctpClientBuilder = require('../../ctp/ctp-client') -const config = require('../../config/config') +const configLoader = require('../../config/config') +const config = configLoader.load() const ctpClient = ctpClientBuilder.get() function ensureCustomTypes () { @@ -14,9 +15,10 @@ function ensureCustomTypes () { } function ensureResources () { + if(config.disableEnsureResources) return Promise.resolve() return Promise.all([ ensureCustomTypes(), - ensureApiExtensions(ctpClient, config.load().apiExtensionBaseUrl) + ensureApiExtensions(ctpClient, config.apiExtensionBaseUrl) ]) } diff --git a/notification/docs/DevelopmentGuide.md b/notification/docs/DevelopmentGuide.md index 8733f834a..c28fe4b0d 100644 --- a/notification/docs/DevelopmentGuide.md +++ b/notification/docs/DevelopmentGuide.md @@ -26,6 +26,7 @@ CTP_CLIENT_SECRET | commercetools client secret (you can get in the [commercetoo LOG_LEVEL | bunyan log level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`)| NO | `info` PORT | port on which the application will run | NO | 443 KEEP_ALIVE_TIMEOUT | milliseconds to keep a socket alive after the last response ([Node.js docs](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_server_keepalivetimeout)) | NO | Node.js default +DISABLE_ENSURE_RESOURCES | disables the creation of resources in commercetools (e.g. custom types) | NO | `false` Don't forget to provide all required environment variables After setting all variables, execute command `npm run start` to run the module. diff --git a/notification/src/config/config.js b/notification/src/config/config.js index 90e4211f6..a4795553d 100644 --- a/notification/src/config/config.js +++ b/notification/src/config/config.js @@ -8,7 +8,8 @@ function getEnvConfig () { return { logLevel: process.env.LOG_LEVEL, keepAliveTimeout: !Number.isNaN(process.env.KEEP_ALIVE_TIMEOUT) ? parseFloat(process.env.KEEP_ALIVE_TIMEOUT, 10) - : undefined + : undefined, + disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === "true" || false } } diff --git a/notification/src/config/init/ensure-interface-interaction-custom-type.js b/notification/src/config/init/ensure-interface-interaction-custom-type.js index 366ce281b..62727eec7 100644 --- a/notification/src/config/init/ensure-interface-interaction-custom-type.js +++ b/notification/src/config/init/ensure-interface-interaction-custom-type.js @@ -1,9 +1,11 @@ const logger = require('../../utils/logger').getLogger() const interfaceInteractionType = require('../../../resources/payment-interface-interaction-type.json') +const config = require('../../config/config')() async function ensureInterfaceInteractionCustomType (ctpClient) { try { + if(config.disableEnsureResources) return logger.debug('Ensuring interfaceInteraction') const { body } = await ctpClient.fetch(ctpClient.builder.types.where(`key="${interfaceInteractionType.key}"`)) if (body.results.length === 0) { From bcba7f77cc305427f00d3a9db8cb0dadc810020f Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Tue, 25 Jun 2019 09:35:28 +0100 Subject: [PATCH 2/3] Fix minor formatting issues flagged by eslint --- extension/src/config/config.js | 2 +- extension/src/config/init/ensure-resources.js | 2 +- notification/src/config/config.js | 2 +- .../src/config/init/ensure-interface-interaction-custom-type.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extension/src/config/config.js b/extension/src/config/config.js index fa9df83bd..70f20d536 100644 --- a/extension/src/config/config.js +++ b/extension/src/config/config.js @@ -7,7 +7,7 @@ function getEnvConfig () { apiExtensionBaseUrl: process.env.API_EXTENSION_BASE_URL, keepAliveTimeout: !Number.isNaN(process.env.KEEP_ALIVE_TIMEOUT) ? parseFloat(process.env.KEEP_ALIVE_TIMEOUT, 10) : undefined, - disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === "true" || false + disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === 'true' || false } } diff --git a/extension/src/config/init/ensure-resources.js b/extension/src/config/init/ensure-resources.js index 46a72e563..f260a21f2 100644 --- a/extension/src/config/init/ensure-resources.js +++ b/extension/src/config/init/ensure-resources.js @@ -15,7 +15,7 @@ function ensureCustomTypes () { } function ensureResources () { - if(config.disableEnsureResources) return Promise.resolve() + if (config.disableEnsureResources) return Promise.resolve() return Promise.all([ ensureCustomTypes(), ensureApiExtensions(ctpClient, config.apiExtensionBaseUrl) diff --git a/notification/src/config/config.js b/notification/src/config/config.js index a4795553d..5f7cc69b4 100644 --- a/notification/src/config/config.js +++ b/notification/src/config/config.js @@ -9,7 +9,7 @@ function getEnvConfig () { logLevel: process.env.LOG_LEVEL, keepAliveTimeout: !Number.isNaN(process.env.KEEP_ALIVE_TIMEOUT) ? parseFloat(process.env.KEEP_ALIVE_TIMEOUT, 10) : undefined, - disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === "true" || false + disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === 'true' || false } } diff --git a/notification/src/config/init/ensure-interface-interaction-custom-type.js b/notification/src/config/init/ensure-interface-interaction-custom-type.js index 62727eec7..962a1e863 100644 --- a/notification/src/config/init/ensure-interface-interaction-custom-type.js +++ b/notification/src/config/init/ensure-interface-interaction-custom-type.js @@ -5,7 +5,7 @@ const config = require('../../config/config')() async function ensureInterfaceInteractionCustomType (ctpClient) { try { - if(config.disableEnsureResources) return + if (config.disableEnsureResources) return logger.debug('Ensuring interfaceInteraction') const { body } = await ctpClient.fetch(ctpClient.builder.types.where(`key="${interfaceInteractionType.key}"`)) if (body.results.length === 0) { From 8e3b3010d9c1e3d06adffcad4a9a0597755a89d4 Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Wed, 3 Jul 2019 10:06:01 +0100 Subject: [PATCH 3/3] Use ENSURE_RESOURCES instead of DISABLE_ENSURE_RESOURCES --- extension/docs/DevelopmentAndDeploymentGuide.md | 2 +- extension/src/config/config.js | 2 +- extension/src/config/init/ensure-resources.js | 4 ++-- notification/docs/DevelopmentGuide.md | 2 +- notification/src/config/config.js | 2 +- .../config/init/ensure-interface-interaction-custom-type.js | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/extension/docs/DevelopmentAndDeploymentGuide.md b/extension/docs/DevelopmentAndDeploymentGuide.md index 85b4b3dac..e2f84b87c 100644 --- a/extension/docs/DevelopmentAndDeploymentGuide.md +++ b/extension/docs/DevelopmentAndDeploymentGuide.md @@ -36,7 +36,7 @@ Environment variables to configure the notification module: |`API_EXTENSION_BASE_URL` | URL of the Extension module server. In case of any payment changes, [CTP API extension](https://docs.commercetools.com/http-api-projects-api-extensions) will call this URL and pass the payment object in body. | YES | | |`LOG_LEVEL` | bunyan log level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`)| NO | `info` |`PORT` | port on which the application will run | NO | 8080 -|`DISABLE_ENSURE_RESOURCES` | disables the creation of resources in commercetools (e.g. custom types) | NO | `false` +|`ENSURE_RESOURCES` | Set to `false` to disable the creation of resources in commercetools (e.g. custom types) | NO | `true` After setting all environmental variables, execute command `npm run extension` to run the module. diff --git a/extension/src/config/config.js b/extension/src/config/config.js index 70f20d536..e9cfd2242 100644 --- a/extension/src/config/config.js +++ b/extension/src/config/config.js @@ -7,7 +7,7 @@ function getEnvConfig () { apiExtensionBaseUrl: process.env.API_EXTENSION_BASE_URL, keepAliveTimeout: !Number.isNaN(process.env.KEEP_ALIVE_TIMEOUT) ? parseFloat(process.env.KEEP_ALIVE_TIMEOUT, 10) : undefined, - disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === 'true' || false + ensureResources: process.env.ENSURE_RESOURCES !== 'false' } } diff --git a/extension/src/config/init/ensure-resources.js b/extension/src/config/init/ensure-resources.js index f260a21f2..8f46c1adb 100644 --- a/extension/src/config/init/ensure-resources.js +++ b/extension/src/config/init/ensure-resources.js @@ -4,7 +4,6 @@ const { ensureApiExtensions } = require('./ensure-api-extensions') const ctpClientBuilder = require('../../ctp/ctp-client') const configLoader = require('../../config/config') -const config = configLoader.load() const ctpClient = ctpClientBuilder.get() function ensureCustomTypes () { @@ -15,7 +14,8 @@ function ensureCustomTypes () { } function ensureResources () { - if (config.disableEnsureResources) return Promise.resolve() + let config = configLoader.load() + if (!config.ensureResources) return Promise.resolve() return Promise.all([ ensureCustomTypes(), ensureApiExtensions(ctpClient, config.apiExtensionBaseUrl) diff --git a/notification/docs/DevelopmentGuide.md b/notification/docs/DevelopmentGuide.md index c28fe4b0d..6e8c6e1f8 100644 --- a/notification/docs/DevelopmentGuide.md +++ b/notification/docs/DevelopmentGuide.md @@ -26,7 +26,7 @@ CTP_CLIENT_SECRET | commercetools client secret (you can get in the [commercetoo LOG_LEVEL | bunyan log level (`trace`, `debug`, `info`, `warn`, `error`, `fatal`)| NO | `info` PORT | port on which the application will run | NO | 443 KEEP_ALIVE_TIMEOUT | milliseconds to keep a socket alive after the last response ([Node.js docs](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_server_keepalivetimeout)) | NO | Node.js default -DISABLE_ENSURE_RESOURCES | disables the creation of resources in commercetools (e.g. custom types) | NO | `false` +ENSURE_RESOURCES | Set to `false` to disable the creation of resources in commercetools (e.g. custom types) | NO | `true` Don't forget to provide all required environment variables After setting all variables, execute command `npm run start` to run the module. diff --git a/notification/src/config/config.js b/notification/src/config/config.js index 5f7cc69b4..e1a372cc4 100644 --- a/notification/src/config/config.js +++ b/notification/src/config/config.js @@ -9,7 +9,7 @@ function getEnvConfig () { logLevel: process.env.LOG_LEVEL, keepAliveTimeout: !Number.isNaN(process.env.KEEP_ALIVE_TIMEOUT) ? parseFloat(process.env.KEEP_ALIVE_TIMEOUT, 10) : undefined, - disableEnsureResources: process.env.DISABLE_ENSURE_RESOURCES === 'true' || false + ensureResources: process.env.ENSURE_RESOURCES !== 'false' } } diff --git a/notification/src/config/init/ensure-interface-interaction-custom-type.js b/notification/src/config/init/ensure-interface-interaction-custom-type.js index 962a1e863..8e124f939 100644 --- a/notification/src/config/init/ensure-interface-interaction-custom-type.js +++ b/notification/src/config/init/ensure-interface-interaction-custom-type.js @@ -5,7 +5,7 @@ const config = require('../../config/config')() async function ensureInterfaceInteractionCustomType (ctpClient) { try { - if (config.disableEnsureResources) return + if (!config.ensureResources) return logger.debug('Ensuring interfaceInteraction') const { body } = await ctpClient.fetch(ctpClient.builder.types.where(`key="${interfaceInteractionType.key}"`)) if (body.results.length === 0) {