From 5006a5f23995143afe70e09e2f64c88ffa5880d8 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 2 Dec 2020 12:50:50 -0500 Subject: [PATCH 1/4] init --- x-pack/plugins/maps/server/plugin.ts | 26 +++++++++--------- x-pack/plugins/maps/server/routes.js | 40 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/x-pack/plugins/maps/server/plugin.ts b/x-pack/plugins/maps/server/plugin.ts index a79e5353048c8..05f2d76a93fc7 100644 --- a/x-pack/plugins/maps/server/plugin.ts +++ b/x-pack/plugins/maps/server/plugin.ts @@ -147,27 +147,25 @@ export class MapsPlugin implements Plugin { return; } - let routesInitialized = false; let isEnterprisePlus = false; + let lastLicenseId: string; const emsSettings = new EMSSettings(mapsLegacyConfig, () => isEnterprisePlus); licensing.license$.subscribe((license: ILicense) => { - const basic = license.check(APP_ID, 'basic'); - const enterprise = license.check(APP_ID, 'enterprise'); isEnterprisePlus = enterprise.state === 'valid'; - - if (basic.state === 'valid' && !routesInitialized) { - routesInitialized = true; - initRoutes( - core.http.createRouter(), - license.uid, - emsSettings, - this.kibanaVersion, - this._logger - ); - } + lastLicenseId = license.uid; }); + initRoutes( + core.http.createRouter(), + () => { + return lastLicenseId; + }, + emsSettings, + this.kibanaVersion, + this._logger + ); + this._initHomeData(home, core.http.basePath.prepend, emsSettings); features.registerKibanaFeature({ diff --git a/x-pack/plugins/maps/server/routes.js b/x-pack/plugins/maps/server/routes.js index 49d646f9a4e6d..e29aa97c8708f 100644 --- a/x-pack/plugins/maps/server/routes.js +++ b/x-pack/plugins/maps/server/routes.js @@ -52,25 +52,31 @@ const EMPTY_EMS_CLIENT = { addQueryParams() {}, }; -export function initRoutes(router, licenseUid, emsSettings, kbnVersion, logger) { +export function initRoutes(router, getLicenseId, emsSettings, kbnVersion, logger) { let emsClient; - - if (emsSettings.isIncludeElasticMapsService()) { - emsClient = new EMSClient({ - language: i18n.getLocale(), - appVersion: kbnVersion, - appName: EMS_APP_NAME, - fileApiUrl: emsSettings.getEMSFileApiUrl(), - tileApiUrl: emsSettings.getEMSTileApiUrl(), - landingPageUrl: emsSettings.getEMSLandingPageUrl(), - fetchFunction: fetch, - }); - emsClient.addQueryParams({ license: licenseUid }); - } else { - emsClient = EMPTY_EMS_CLIENT; - } + let lastLicenseId; function getEMSClient() { + const currentLicenseId = getLicenseId(); + if (emsClient && lastLicenseId === currentLicenseId) { + return emsClient; + } + + lastLicenseId = currentLicenseId; + if (emsSettings.isIncludeElasticMapsService()) { + emsClient = new EMSClient({ + language: i18n.getLocale(), + appVersion: kbnVersion, + appName: EMS_APP_NAME, + fileApiUrl: emsSettings.getEMSFileApiUrl(), + tileApiUrl: emsSettings.getEMSTileApiUrl(), + landingPageUrl: emsSettings.getEMSLandingPageUrl(), + fetchFunction: fetch, + }); + emsClient.addQueryParams({ license: currentLicenseId }); + } else { + emsClient = EMPTY_EMS_CLIENT; + } return emsSettings.isEMSEnabled() ? emsClient : EMPTY_EMS_CLIENT; } @@ -547,6 +553,8 @@ export function initRoutes(router, licenseUid, emsSettings, kbnVersion, logger) }, }, async (context, request, response) => { + console.log('Load index settings'); + const { query } = request; if (!query.indexPatternTitle) { From f02cacb57785b894de65210a9aed91cf4a4bdf6c Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 2 Dec 2020 13:06:01 -0500 Subject: [PATCH 2/4] remove log --- x-pack/plugins/maps/server/routes.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/x-pack/plugins/maps/server/routes.js b/x-pack/plugins/maps/server/routes.js index e29aa97c8708f..796f1fbc212b1 100644 --- a/x-pack/plugins/maps/server/routes.js +++ b/x-pack/plugins/maps/server/routes.js @@ -553,8 +553,6 @@ export function initRoutes(router, getLicenseId, emsSettings, kbnVersion, logger }, }, async (context, request, response) => { - console.log('Load index settings'); - const { query } = request; if (!query.indexPatternTitle) { From 2a93e26061858292f203bab1dbc3da707880398e Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 2 Dec 2020 13:28:39 -0500 Subject: [PATCH 3/4] type fix --- x-pack/plugins/maps/server/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/maps/server/plugin.ts b/x-pack/plugins/maps/server/plugin.ts index 05f2d76a93fc7..2a14473006320 100644 --- a/x-pack/plugins/maps/server/plugin.ts +++ b/x-pack/plugins/maps/server/plugin.ts @@ -148,7 +148,7 @@ export class MapsPlugin implements Plugin { } let isEnterprisePlus = false; - let lastLicenseId: string; + let lastLicenseId: string | undefined; const emsSettings = new EMSSettings(mapsLegacyConfig, () => isEnterprisePlus); licensing.license$.subscribe((license: ILicense) => { const enterprise = license.check(APP_ID, 'enterprise'); From 53d409c034aaf7f4c8ab4d6401d0a60a614fcf26 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Wed, 2 Dec 2020 16:56:42 -0500 Subject: [PATCH 4/4] feedback --- x-pack/plugins/maps/server/plugin.ts | 4 +--- x-pack/plugins/maps/server/routes.js | 6 +++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/maps/server/plugin.ts b/x-pack/plugins/maps/server/plugin.ts index 2a14473006320..f3241b79759a1 100644 --- a/x-pack/plugins/maps/server/plugin.ts +++ b/x-pack/plugins/maps/server/plugin.ts @@ -158,9 +158,7 @@ export class MapsPlugin implements Plugin { initRoutes( core.http.createRouter(), - () => { - return lastLicenseId; - }, + () => lastLicenseId, emsSettings, this.kibanaVersion, this._logger diff --git a/x-pack/plugins/maps/server/routes.js b/x-pack/plugins/maps/server/routes.js index 796f1fbc212b1..d98259540f5e4 100644 --- a/x-pack/plugins/maps/server/routes.js +++ b/x-pack/plugins/maps/server/routes.js @@ -58,7 +58,7 @@ export function initRoutes(router, getLicenseId, emsSettings, kbnVersion, logger function getEMSClient() { const currentLicenseId = getLicenseId(); - if (emsClient && lastLicenseId === currentLicenseId) { + if (emsClient && emsSettings.isEMSEnabled() && lastLicenseId === currentLicenseId) { return emsClient; } @@ -74,10 +74,10 @@ export function initRoutes(router, getLicenseId, emsSettings, kbnVersion, logger fetchFunction: fetch, }); emsClient.addQueryParams({ license: currentLicenseId }); + return emsClient; } else { - emsClient = EMPTY_EMS_CLIENT; + return EMPTY_EMS_CLIENT; } - return emsSettings.isEMSEnabled() ? emsClient : EMPTY_EMS_CLIENT; } router.get(