Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [ECO][Inventory v2] Ad hoc data view: Add get entities definition endpoint using sources (#203424) #203861

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<EuiContextMenuItem
data-test-subj="inventoryEntityActionExploreInDiscover"
Expand Down Expand Up @@ -65,7 +66,7 @@ export const EntityActions = ({ entity, setShowActions }: Props) => {
iconType="boxesHorizontal"
color="text"
onClick={togglePopover}
isLoading={isEntityDefinitionLoading}
isLoading={isEntityDefinitionIndexPatternsLoading}
/>
}
closePopover={closePopover}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -54,5 +53,5 @@ export const useDiscoverRedirect = (entity: InventoryEntity) => {
entityManager.entityClient,
]);

return { getDiscoverEntitiesRedirectUrl, isEntityDefinitionLoading };
return { getDiscoverEntitiesRedirectUrl, isEntityDefinitionIndexPatternsLoading };
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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,
};
};
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down
Loading