From c06ac0ee82dbb03827d079a30207159e9906e8de Mon Sep 17 00:00:00 2001 From: Or Ouziel Date: Thu, 20 Jan 2022 16:40:49 +0200 Subject: [PATCH] register data view on plugin setup (#104) --- .../public/pages/findings/utils.tsx | 11 ++-------- .../cloud_security_posture/public/plugin.ts | 20 ++++++++++++++++++- .../cloud_security_posture/server/plugin.ts | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/cloud_security_posture/public/pages/findings/utils.tsx b/x-pack/plugins/cloud_security_posture/public/pages/findings/utils.tsx index d1fa4a5ed69224..a5d2c6082fc0ad 100644 --- a/x-pack/plugins/cloud_security_posture/public/pages/findings/utils.tsx +++ b/x-pack/plugins/cloud_security_posture/public/pages/findings/utils.tsx @@ -32,21 +32,14 @@ export const useKubebeatDataView = () => { data: { dataViews }, } = useKibana().services; - const createDataView = () => - dataViews.createAndSave({ - title: CSP_KUBEBEAT_INDEX_NAME, - allowNoIndex: false, - }); - // TODO: check if index exists // if not, no point in creating a data view // const check = () => http?.get(`/kubebeat`); + // TODO: use `dataViews.get(ID)` const findDataView = async () => (await dataViews.find(CSP_KUBEBEAT_INDEX_NAME))?.[0]; - const getKubebeatDataView = () => findDataView().then((v) => (v ? v : createDataView())); - - return useQuery(['kubebeat_dataview'], getKubebeatDataView); + return useQuery(['kubebeat_dataview'], findDataView); }; export const useEsClientMutation = ({ diff --git a/x-pack/plugins/cloud_security_posture/public/plugin.ts b/x-pack/plugins/cloud_security_posture/public/plugin.ts index 4aa89464998f1a..a9fccb5a1d8bba 100755 --- a/x-pack/plugins/cloud_security_posture/public/plugin.ts +++ b/x-pack/plugins/cloud_security_posture/public/plugin.ts @@ -15,6 +15,7 @@ import type { import { PLUGIN_NAME, PLUGIN_ID } from '../common'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; import { ENABLE_CSP } from '../common/constants'; +import { CSP_KUBEBEAT_INDEX_NAME } from '../common/constants'; export class CspPlugin implements @@ -27,7 +28,7 @@ export class CspPlugin { public setup( core: CoreSetup, - plugins: CspClientPluginSetup + plugins: CspClientPluginSetupDeps ): CspClientPluginSetup { // Register an application into the side navigation menu const cspEnabled: boolean = core.uiSettings.get(ENABLE_CSP); @@ -47,6 +48,8 @@ export class CspPlugin }, }); + registerKubebeatDataView(core); + // Return methods that should be available to other plugins return {}; } @@ -56,3 +59,18 @@ export class CspPlugin public stop() {} } + +async function registerKubebeatDataView( + core: CoreSetup +) { + try { + const [, depsStart] = await core.getStartServices(); + await depsStart.data.dataViews.createAndSave({ + title: CSP_KUBEBEAT_INDEX_NAME, + allowNoIndex: true, + }); + } catch (e) { + // eslint-disable-next-line no-console + console.error(e); + } +} diff --git a/x-pack/plugins/cloud_security_posture/server/plugin.ts b/x-pack/plugins/cloud_security_posture/server/plugin.ts index fb3f923bf3b2e5..df518cfed3f1fe 100755 --- a/x-pack/plugins/cloud_security_posture/server/plugin.ts +++ b/x-pack/plugins/cloud_security_posture/server/plugin.ts @@ -37,7 +37,7 @@ export class CspPlugin } public setup( - core: CoreSetup, + core: CoreSetup, plugins: CspServerPluginSetupDeps ): CspServerPluginSetup { this.logger.debug('csp: Setup');