From f1d22938a70b6de1e8595f3d661e81678c187df7 Mon Sep 17 00:00:00 2001 From: iblancof Date: Wed, 9 Oct 2024 11:46:53 +0200 Subject: [PATCH 1/7] Check space configuration before registering the Inventory plugin --- .../inventory/public/plugin.ts | 67 +++++++++++-------- .../inventory/public/types.ts | 2 + .../inventory/tsconfig.json | 3 +- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/x-pack/plugins/observability_solution/inventory/public/plugin.ts b/x-pack/plugins/observability_solution/inventory/public/plugin.ts index c02a57b45f691..3ec22680c6750 100644 --- a/x-pack/plugins/observability_solution/inventory/public/plugin.ts +++ b/x-pack/plugins/observability_solution/inventory/public/plugin.ts @@ -17,7 +17,7 @@ import { import { INVENTORY_APP_ID } from '@kbn/deeplinks-observability/constants'; import { i18n } from '@kbn/i18n'; import type { Logger } from '@kbn/logging'; -import { from, map } from 'rxjs'; +import { from, map, mergeMap } from 'rxjs'; import { createCallInventoryAPI } from './api'; import { TelemetryService } from './services/telemetry/telemetry_service'; import { InventoryServices } from './services/types'; @@ -54,34 +54,45 @@ export class InventoryPlugin 'observability:entityCentricExperience', true ); + const getStartServices = coreSetup.getStartServices(); - if (isEntityCentricExperienceSettingEnabled) { - pluginsSetup.observabilityShared.navigation.registerSections( - from(coreSetup.getStartServices()).pipe( - map(([coreStart, pluginsStart]) => { - return [ - { - label: '', - sortKey: 300, - entries: [ - { - label: i18n.translate('xpack.inventory.inventoryLinkTitle', { - defaultMessage: 'Inventory', - }), - app: INVENTORY_APP_ID, - path: '/', - matchPath(currentPath: string) { - return ['/', ''].some((testPath) => currentPath.startsWith(testPath)); - }, - isTechnicalPreview: true, - }, - ], - }, - ]; - }) + const hideInventory$ = from(getStartServices).pipe( + mergeMap(([coreStart, pluginsStart]) => + from(pluginsStart.spaces.getActiveSpace()).pipe( + map((space) => space.disabledFeatures.includes('inventory')) ) - ); - } + ) + ); + + const sections$ = hideInventory$.pipe( + map((hideInventory) => { + if (isEntityCentricExperienceSettingEnabled && !hideInventory) { + return [ + { + label: '', + sortKey: 300, + entries: [ + { + label: i18n.translate('xpack.inventory.inventoryLinkTitle', { + defaultMessage: 'Inventory', + }), + app: INVENTORY_APP_ID, + path: '/', + matchPath(currentPath: string) { + return ['/', ''].some((testPath) => currentPath.startsWith(testPath)); + }, + isTechnicalPreview: true, + }, + ], + }, + ]; + } + return []; + }) + ); + + pluginsSetup.observabilityShared.navigation.registerSections(sections$); + this.telemetry.setup({ analytics: coreSetup.analytics }); const telemetry = this.telemetry.start(); @@ -102,7 +113,7 @@ export class InventoryPlugin // Load application bundle and Get start services const [{ renderApp }, [coreStart, pluginsStart]] = await Promise.all([ import('./application'), - coreSetup.getStartServices(), + getStartServices, ]); const services: InventoryServices = { diff --git a/x-pack/plugins/observability_solution/inventory/public/types.ts b/x-pack/plugins/observability_solution/inventory/public/types.ts index 2393b1b55e2b6..68bed576fbfd0 100644 --- a/x-pack/plugins/observability_solution/inventory/public/types.ts +++ b/x-pack/plugins/observability_solution/inventory/public/types.ts @@ -17,6 +17,7 @@ import type { SharePluginSetup, SharePluginStart } from '@kbn/share-plugin/publi import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public'; import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; import type { DataPublicPluginSetup, DataPublicPluginStart } from '@kbn/data-plugin/public'; +import { SpacesPluginSetup } from '@kbn/spaces-plugin/public'; /* eslint-disable @typescript-eslint/no-empty-interface*/ @@ -28,6 +29,7 @@ export interface InventorySetupDependencies { share: SharePluginSetup; data: DataPublicPluginSetup; entityManager: EntityManagerPublicPluginSetup; + spaces: SpacesPluginSetup; } export interface InventoryStartDependencies { diff --git a/x-pack/plugins/observability_solution/inventory/tsconfig.json b/x-pack/plugins/observability_solution/inventory/tsconfig.json index 54fcfe7e3a11f..20b5e2e37232a 100644 --- a/x-pack/plugins/observability_solution/inventory/tsconfig.json +++ b/x-pack/plugins/observability_solution/inventory/tsconfig.json @@ -45,6 +45,7 @@ "@kbn/config-schema", "@kbn/elastic-agent-utils", "@kbn/custom-icons", - "@kbn/ui-theme" + "@kbn/ui-theme", + "@kbn/spaces-plugin" ] } From 5d5b82fbe8cbbc77967d570a7a0e3d47549a234d Mon Sep 17 00:00:00 2001 From: iblancof Date: Wed, 9 Oct 2024 11:52:55 +0200 Subject: [PATCH 2/7] Remove unnecessary spaces setup dependency --- .../plugins/observability_solution/inventory/public/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/observability_solution/inventory/public/types.ts b/x-pack/plugins/observability_solution/inventory/public/types.ts index 68bed576fbfd0..72c42797ca80d 100644 --- a/x-pack/plugins/observability_solution/inventory/public/types.ts +++ b/x-pack/plugins/observability_solution/inventory/public/types.ts @@ -17,7 +17,7 @@ import type { SharePluginSetup, SharePluginStart } from '@kbn/share-plugin/publi import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public'; import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; import type { DataPublicPluginSetup, DataPublicPluginStart } from '@kbn/data-plugin/public'; -import { SpacesPluginSetup } from '@kbn/spaces-plugin/public'; +import type { SpacesPluginStart } from '@kbn/spaces-plugin/public'; /* eslint-disable @typescript-eslint/no-empty-interface*/ @@ -29,7 +29,6 @@ export interface InventorySetupDependencies { share: SharePluginSetup; data: DataPublicPluginSetup; entityManager: EntityManagerPublicPluginSetup; - spaces: SpacesPluginSetup; } export interface InventoryStartDependencies { @@ -40,6 +39,7 @@ export interface InventoryStartDependencies { data: DataPublicPluginStart; entityManager: EntityManagerPublicPluginStart; share: SharePluginStart; + spaces: SpacesPluginStart; } export interface InventoryPublicSetup {} From c7b8fee47aed7224eff1c201493138ba21441832 Mon Sep 17 00:00:00 2001 From: iblancof Date: Wed, 9 Oct 2024 11:53:22 +0200 Subject: [PATCH 3/7] Add missing required plugin dependency --- x-pack/plugins/observability_solution/inventory/kibana.jsonc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/inventory/kibana.jsonc b/x-pack/plugins/observability_solution/inventory/kibana.jsonc index f60cf36183b24..b421cd60ab7ea 100644 --- a/x-pack/plugins/observability_solution/inventory/kibana.jsonc +++ b/x-pack/plugins/observability_solution/inventory/kibana.jsonc @@ -16,7 +16,8 @@ "features", "unifiedSearch", "data", - "share" + "share", + "spaces" ], "requiredBundles": ["kibanaReact"], "optionalPlugins": [], From 3b1d39becde980a55ebde1f007e5dde28195e9e6 Mon Sep 17 00:00:00 2001 From: iblancof Date: Wed, 9 Oct 2024 12:53:25 +0200 Subject: [PATCH 4/7] Check app capabilities before registering the Inventory plugin --- .../observability_solution/inventory/public/plugin.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/inventory/public/plugin.ts b/x-pack/plugins/observability_solution/inventory/public/plugin.ts index 3ec22680c6750..390ad767ccc7d 100644 --- a/x-pack/plugins/observability_solution/inventory/public/plugin.ts +++ b/x-pack/plugins/observability_solution/inventory/public/plugin.ts @@ -59,7 +59,11 @@ export class InventoryPlugin const hideInventory$ = from(getStartServices).pipe( mergeMap(([coreStart, pluginsStart]) => from(pluginsStart.spaces.getActiveSpace()).pipe( - map((space) => space.disabledFeatures.includes('inventory')) + map( + (space) => + space.disabledFeatures.includes('inventory') || + !coreStart.application.capabilities.inventory.show + ) ) ) ); From 12278fe78ff7814d5c87c1514010bc229cf2457a Mon Sep 17 00:00:00 2001 From: iblancof Date: Wed, 9 Oct 2024 17:24:22 +0200 Subject: [PATCH 5/7] Add missing spaces mock to getMockInventoryContext --- .../inventory/.storybook/get_mock_inventory_context.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/x-pack/plugins/observability_solution/inventory/.storybook/get_mock_inventory_context.tsx b/x-pack/plugins/observability_solution/inventory/.storybook/get_mock_inventory_context.tsx index 51aaeebc655f2..d90ce08aab1c6 100644 --- a/x-pack/plugins/observability_solution/inventory/.storybook/get_mock_inventory_context.tsx +++ b/x-pack/plugins/observability_solution/inventory/.storybook/get_mock_inventory_context.tsx @@ -13,6 +13,7 @@ import type { InferencePublicStart } from '@kbn/inference-plugin/public'; import type { ObservabilitySharedPluginStart } from '@kbn/observability-shared-plugin/public'; import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public'; import type { SharePluginStart } from '@kbn/share-plugin/public'; +import type { SpacesPluginStart } from '@kbn/spaces-plugin/public'; import type { InventoryKibanaContext } from '../public/hooks/use_kibana'; import type { ITelemetryClient } from '../public/services/telemetry/types'; @@ -33,5 +34,6 @@ export function getMockInventoryContext(): InventoryKibanaContext { fetch: jest.fn(), stream: jest.fn(), }, + spaces: {} as unknown as SpacesPluginStart, }; } From d880fb6804a66ac77d421055c806f2c30ae70d5a Mon Sep 17 00:00:00 2001 From: iblancof Date: Thu, 10 Oct 2024 10:13:46 +0200 Subject: [PATCH 6/7] Use INVENTORY_APP_ID const --- .../plugins/observability_solution/inventory/public/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/inventory/public/plugin.ts b/x-pack/plugins/observability_solution/inventory/public/plugin.ts index 390ad767ccc7d..5173e317e425c 100644 --- a/x-pack/plugins/observability_solution/inventory/public/plugin.ts +++ b/x-pack/plugins/observability_solution/inventory/public/plugin.ts @@ -61,7 +61,7 @@ export class InventoryPlugin from(pluginsStart.spaces.getActiveSpace()).pipe( map( (space) => - space.disabledFeatures.includes('inventory') || + space.disabledFeatures.includes(INVENTORY_APP_ID) || !coreStart.application.capabilities.inventory.show ) ) From a2239e00c0e1ad493bbf52c2948f5d471c4038f6 Mon Sep 17 00:00:00 2001 From: iblancof Date: Thu, 10 Oct 2024 10:19:59 +0200 Subject: [PATCH 7/7] Make spaces dependency optional --- .../inventory/kibana.jsonc | 5 ++-- .../inventory/public/plugin.ts | 24 +++++++++++-------- .../inventory/public/types.ts | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/observability_solution/inventory/kibana.jsonc b/x-pack/plugins/observability_solution/inventory/kibana.jsonc index b421cd60ab7ea..28556c7bcc583 100644 --- a/x-pack/plugins/observability_solution/inventory/kibana.jsonc +++ b/x-pack/plugins/observability_solution/inventory/kibana.jsonc @@ -16,11 +16,10 @@ "features", "unifiedSearch", "data", - "share", - "spaces" + "share" ], "requiredBundles": ["kibanaReact"], - "optionalPlugins": [], + "optionalPlugins": ["spaces"], "extraPublicDirs": [] } } diff --git a/x-pack/plugins/observability_solution/inventory/public/plugin.ts b/x-pack/plugins/observability_solution/inventory/public/plugin.ts index 5173e317e425c..30e3a1eed3681 100644 --- a/x-pack/plugins/observability_solution/inventory/public/plugin.ts +++ b/x-pack/plugins/observability_solution/inventory/public/plugin.ts @@ -17,7 +17,7 @@ import { import { INVENTORY_APP_ID } from '@kbn/deeplinks-observability/constants'; import { i18n } from '@kbn/i18n'; import type { Logger } from '@kbn/logging'; -import { from, map, mergeMap } from 'rxjs'; +import { from, map, mergeMap, of } from 'rxjs'; import { createCallInventoryAPI } from './api'; import { TelemetryService } from './services/telemetry/telemetry_service'; import { InventoryServices } from './services/types'; @@ -57,15 +57,19 @@ export class InventoryPlugin const getStartServices = coreSetup.getStartServices(); const hideInventory$ = from(getStartServices).pipe( - mergeMap(([coreStart, pluginsStart]) => - from(pluginsStart.spaces.getActiveSpace()).pipe( - map( - (space) => - space.disabledFeatures.includes(INVENTORY_APP_ID) || - !coreStart.application.capabilities.inventory.show - ) - ) - ) + mergeMap(([coreStart, pluginsStart]) => { + if (pluginsStart.spaces) { + return from(pluginsStart.spaces.getActiveSpace()).pipe( + map( + (space) => + space.disabledFeatures.includes(INVENTORY_APP_ID) || + !coreStart.application.capabilities.inventory.show + ) + ); + } + + return of(!coreStart.application.capabilities.inventory.show); + }) ); const sections$ = hideInventory$.pipe( diff --git a/x-pack/plugins/observability_solution/inventory/public/types.ts b/x-pack/plugins/observability_solution/inventory/public/types.ts index 72c42797ca80d..48fe7e7eed1c7 100644 --- a/x-pack/plugins/observability_solution/inventory/public/types.ts +++ b/x-pack/plugins/observability_solution/inventory/public/types.ts @@ -39,7 +39,7 @@ export interface InventoryStartDependencies { data: DataPublicPluginStart; entityManager: EntityManagerPublicPluginStart; share: SharePluginStart; - spaces: SpacesPluginStart; + spaces?: SpacesPluginStart; } export interface InventoryPublicSetup {}