Skip to content

Commit

Permalink
register data view on plugin setup (elastic#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
orouz authored Jan 20, 2022
1 parent c3889b3 commit c06ac0e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,14 @@ export const useKubebeatDataView = () => {
data: { dataViews },
} = useKibana<CspClientPluginStartDeps>().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 = <T extends unknown>({
Expand Down
20 changes: 19 additions & 1 deletion x-pack/plugins/cloud_security_posture/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,7 +28,7 @@ export class CspPlugin
{
public setup(
core: CoreSetup<CspClientPluginStartDeps, CspClientPluginStart>,
plugins: CspClientPluginSetup
plugins: CspClientPluginSetupDeps
): CspClientPluginSetup {
// Register an application into the side navigation menu
const cspEnabled: boolean = core.uiSettings.get(ENABLE_CSP);
Expand All @@ -47,6 +48,8 @@ export class CspPlugin
},
});

registerKubebeatDataView(core);

// Return methods that should be available to other plugins
return {};
}
Expand All @@ -56,3 +59,18 @@ export class CspPlugin

public stop() {}
}

async function registerKubebeatDataView(
core: CoreSetup<CspClientPluginStartDeps, CspClientPluginStart>
) {
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);
}
}
2 changes: 1 addition & 1 deletion x-pack/plugins/cloud_security_posture/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class CspPlugin
}

public setup(
core: CoreSetup<CspServerPluginSetup>,
core: CoreSetup<CspServerPluginStartDeps, CspServerPluginStart>,
plugins: CspServerPluginSetupDeps
): CspServerPluginSetup {
this.logger.debug('csp: Setup');
Expand Down

0 comments on commit c06ac0e

Please sign in to comment.