diff --git a/x-pack/plugins/observability_solution/inventory/public/components/entity_actions/index.tsx b/x-pack/plugins/observability_solution/inventory/public/components/entity_actions/index.tsx index 691ba4388ac63..6b31f877f10af 100644 --- a/x-pack/plugins/observability_solution/inventory/public/components/entity_actions/index.tsx +++ b/x-pack/plugins/observability_solution/inventory/public/components/entity_actions/index.tsx @@ -24,17 +24,18 @@ export const EntityActions = ({ entity, setShowActions }: Props) => { ? `inventoryEntityActionsButton-${entity.entityDisplayName}` : 'inventoryEntityActionsButton'; - const { getDiscoverEntitiesRedirectUrl, isEntityDefinitionLoading } = useDiscoverRedirect(entity); + const { getDiscoverEntitiesRedirectUrl, isEntityDefinitionIndexPatternsLoading } = + useDiscoverRedirect(entity); const discoverUrl = getDiscoverEntitiesRedirectUrl(); const actions: React.ReactElement[] = []; - if (!discoverUrl && !isEntityDefinitionLoading) { + if (!discoverUrl && !isEntityDefinitionIndexPatternsLoading) { setShowActions(false); return null; } - if (!isEntityDefinitionLoading) { + if (!isEntityDefinitionIndexPatternsLoading) { actions.push( { iconType="boxesHorizontal" color="text" onClick={togglePopover} - isLoading={isEntityDefinitionLoading} + isLoading={isEntityDefinitionIndexPatternsLoading} /> } closePopover={closePopover} diff --git a/x-pack/plugins/observability_solution/inventory/public/hooks/use_discover_redirect.ts b/x-pack/plugins/observability_solution/inventory/public/hooks/use_discover_redirect.ts index dc9f5bf4a4740..2855df8e4dd3f 100644 --- a/x-pack/plugins/observability_solution/inventory/public/hooks/use_discover_redirect.ts +++ b/x-pack/plugins/observability_solution/inventory/public/hooks/use_discover_redirect.ts @@ -7,23 +7,22 @@ import { useCallback, useMemo } from 'react'; import type { InventoryEntity } from '../../common/entities'; import { useAdHocDataView } from './use_adhoc_data_view'; -import { useFetchEntityDefinition } from './use_fetch_entity_definition'; +import { useFetchEntityDefinitionIndexPattern } from './use_fetch_entity_definition_index_patterns'; import { useKibana } from './use_kibana'; export const useDiscoverRedirect = (entity: InventoryEntity) => { const { services: { share, application, entityManager }, } = useKibana(); - const { entityDefinitions, isEntityDefinitionLoading } = useFetchEntityDefinition( - entity.entityDefinitionId - ); + const { entityDefinitionIndexPatterns, isEntityDefinitionIndexPatternsLoading } = + useFetchEntityDefinitionIndexPattern(entity.entityType); const title = useMemo( () => - !isEntityDefinitionLoading && entityDefinitions && entityDefinitions?.length > 0 - ? entityDefinitions[0]?.indexPatterns?.join(',') + !isEntityDefinitionIndexPatternsLoading && (entityDefinitionIndexPatterns ?? []).length > 0 + ? entityDefinitionIndexPatterns[0].join() : '', - [entityDefinitions, isEntityDefinitionLoading] + [entityDefinitionIndexPatterns, isEntityDefinitionIndexPatternsLoading] ); const { dataView } = useAdHocDataView(title); @@ -54,5 +53,5 @@ export const useDiscoverRedirect = (entity: InventoryEntity) => { entityManager.entityClient, ]); - return { getDiscoverEntitiesRedirectUrl, isEntityDefinitionLoading }; + return { getDiscoverEntitiesRedirectUrl, isEntityDefinitionIndexPatternsLoading }; }; diff --git a/x-pack/plugins/observability_solution/inventory/public/hooks/use_fetch_entity_definition.ts b/x-pack/plugins/observability_solution/inventory/public/hooks/use_fetch_entity_definition.ts deleted file mode 100644 index 9f6a0232891b2..0000000000000 --- a/x-pack/plugins/observability_solution/inventory/public/hooks/use_fetch_entity_definition.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { useAbortableAsync } from '@kbn/observability-utils-browser/hooks/use_abortable_async'; -import { useKibana } from './use_kibana'; - -export const useFetchEntityDefinition = (id: string) => { - const { - services: { entityManager }, - } = useKibana(); - - const { value, loading } = useAbortableAsync( - ({ signal }) => { - return entityManager.entityClient.getEntityDefinition(id); - }, - [entityManager.entityClient, id] - ); - - return { - entityDefinitions: value?.definitions, - isEntityDefinitionLoading: loading, - }; -}; diff --git a/x-pack/plugins/observability_solution/inventory/public/hooks/use_fetch_entity_definition_index_patterns.ts b/x-pack/plugins/observability_solution/inventory/public/hooks/use_fetch_entity_definition_index_patterns.ts new file mode 100644 index 0000000000000..127c95b532749 --- /dev/null +++ b/x-pack/plugins/observability_solution/inventory/public/hooks/use_fetch_entity_definition_index_patterns.ts @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { useInventoryAbortableAsync } from './use_inventory_abortable_async'; +import { useKibana } from './use_kibana'; + +export const useFetchEntityDefinitionIndexPattern = (type: string) => { + const { + services: { inventoryAPIClient }, + } = useKibana(); + + const { value = { definitionIndexPatterns: [] }, loading } = useInventoryAbortableAsync( + ({ signal }) => { + return inventoryAPIClient.fetch('GET /internal/inventory/entity/definitions/sources', { + params: { + query: { + type, + }, + }, + signal, + }); + }, + [inventoryAPIClient] + ); + + return { + entityDefinitionIndexPatterns: value?.definitionIndexPatterns, + isEntityDefinitionIndexPatternsLoading: loading, + }; +}; diff --git a/x-pack/plugins/observability_solution/inventory/server/routes/entity_definition/get_entity_definitions.ts b/x-pack/plugins/observability_solution/inventory/server/routes/entity_definition/get_entity_definitions.ts new file mode 100644 index 0000000000000..7db85de7c460b --- /dev/null +++ b/x-pack/plugins/observability_solution/inventory/server/routes/entity_definition/get_entity_definitions.ts @@ -0,0 +1,41 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import * as t from 'io-ts'; +import { createInventoryServerRoute } from '../create_inventory_server_route'; + +export const getEntityDefinitionSourceIndexPatternsByType = createInventoryServerRoute({ + endpoint: 'GET /internal/inventory/entity/definitions/sources', + params: t.type({ + query: t.type({ + type: t.string, + }), + }), + options: { + tags: ['access:inventory'], + }, + async handler({ context, params, request, plugins }) { + const [_coreContext, entityManagerStart] = await Promise.all([ + context.core, + plugins.entityManager.start(), + ]); + const { type } = params.query; + const entityManagerClient = await entityManagerStart.getScopedClient({ request }); + + const entityDefinitionsSource = await entityManagerClient.v2.readSourceDefinitions({ type }); + + return { + definitionIndexPatterns: entityDefinitionsSource.map( + (definition) => definition.index_patterns, + [] + ), + }; + }, +}); + +export const entityDefinitionsRoutes = { + ...getEntityDefinitionSourceIndexPatternsByType, +}; diff --git a/x-pack/plugins/observability_solution/inventory/server/routes/get_global_inventory_route_repository.ts b/x-pack/plugins/observability_solution/inventory/server/routes/get_global_inventory_route_repository.ts index 598b69db90e5a..5a808ea9cc2cb 100644 --- a/x-pack/plugins/observability_solution/inventory/server/routes/get_global_inventory_route_repository.ts +++ b/x-pack/plugins/observability_solution/inventory/server/routes/get_global_inventory_route_repository.ts @@ -6,11 +6,13 @@ */ import { entitiesRoutes } from './entities/route'; +import { entityDefinitionsRoutes } from './entity_definition/get_entity_definitions'; import { hasDataRoutes } from './has_data/route'; export function getGlobalInventoryServerRouteRepository() { return { ...entitiesRoutes, + ...entityDefinitionsRoutes, ...hasDataRoutes, }; }