Skip to content

Commit

Permalink
[8.x] [ECO][Inventory v2] Ad hoc data view: Add get entities definiti…
Browse files Browse the repository at this point in the history
…on endpoint using sources (#203424) (#203861)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ECO][Inventory v2] Ad hoc data view: Add get entities definition
endpoint using sources
(#203424)](#203424)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"jennypavlova","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-11T17:44:57Z","message":"[ECO][Inventory
v2] Ad hoc data view: Add get entities definition endpoint using sources
(#203424)\n\nCloses #202298 \r\n⚠️ Depends on
https://github.com/elastic/kibana/pull/203246\r\n\r\n##
Summary\r\n\r\nThis PR changes the way we get the entity index patterns
to v2. It\r\ncreates an endpoint part of the inventory API which returns
the index\r\npatterns by entity type.\r\n\r\n## Testing\r\n\r\n⚠️
Currently in order to test we need to create the definition using
the\r\ndev tools manually before
this\r\n[PR](#203246) is
merged\r\n\r\n- Open Dev tools and add a definition for an existing
entity type\r\n(example with host)\r\n ```\r\nPOST
kbn:/internal/entities/v2/definitions/sources\r\n{\r\n \"source\": {\r\n
\"id\": \"host_id\",\r\n \"type_id\": \"host\",\r\n \"index_patterns\":
[\"metrics-*\", \"metricbeat-*\"],\r\n \"identity_fields\":
[\"host.name\"],\r\n \"metadata_fields\": [],\r\n \"filters\": [],\r\n
\"timestamp_field\": \"@timestamp\"\r\n }\r\n ```\r\n\r\n### Test the
endpoint: \r\n- Open Dev tools and add\r\n` GET
kbn:/internal/inventory/entity/definitions/sources?type=host `\r\n-
Response: \r\n<img width=\"408\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/1c7416b0-50a4-4e63-8081-928ec7856ff8\">\r\n\r\n###
Test in the UI\r\n- After the previous steps add some host data (oblt
cluster /\r\nmetricbeat) or use synthtrace (for example use `node
scripts/synthtrace\r\ninfra_hosts_with_apm_hosts
--scenarioOpts.numInstances=10`)\r\n- Go to Inventory and expand the
host group\r\n- Click on the actions button for any host and click on
the Discover\r\nlink\r\n- The correct dataview should be selected based
on the created\r\ndefinition\r\nThe same can be done for other entity
types\r\n\r\n\r\nhttps://github.com/user-attachments/assets/c9c3a7ae-daff-4d4b-b1b7-898c3f1603c6\r\n\r\n---------\r\n\r\nCo-authored-by:
Carlos Crespo
<[email protected]>","sha":"5bacf1fe50ae373f5951d227ef487a6714aeb541","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability","Team:obs-ux-infra_services"],"title":"[ECO][Inventory
v2] Ad hoc data view: Add get entities definition endpoint using
sources","number":203424,"url":"https://github.com/elastic/kibana/pull/203424","mergeCommit":{"message":"[ECO][Inventory
v2] Ad hoc data view: Add get entities definition endpoint using sources
(#203424)\n\nCloses #202298 \r\n⚠️ Depends on
https://github.com/elastic/kibana/pull/203246\r\n\r\n##
Summary\r\n\r\nThis PR changes the way we get the entity index patterns
to v2. It\r\ncreates an endpoint part of the inventory API which returns
the index\r\npatterns by entity type.\r\n\r\n## Testing\r\n\r\n⚠️
Currently in order to test we need to create the definition using
the\r\ndev tools manually before
this\r\n[PR](#203246) is
merged\r\n\r\n- Open Dev tools and add a definition for an existing
entity type\r\n(example with host)\r\n ```\r\nPOST
kbn:/internal/entities/v2/definitions/sources\r\n{\r\n \"source\": {\r\n
\"id\": \"host_id\",\r\n \"type_id\": \"host\",\r\n \"index_patterns\":
[\"metrics-*\", \"metricbeat-*\"],\r\n \"identity_fields\":
[\"host.name\"],\r\n \"metadata_fields\": [],\r\n \"filters\": [],\r\n
\"timestamp_field\": \"@timestamp\"\r\n }\r\n ```\r\n\r\n### Test the
endpoint: \r\n- Open Dev tools and add\r\n` GET
kbn:/internal/inventory/entity/definitions/sources?type=host `\r\n-
Response: \r\n<img width=\"408\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/1c7416b0-50a4-4e63-8081-928ec7856ff8\">\r\n\r\n###
Test in the UI\r\n- After the previous steps add some host data (oblt
cluster /\r\nmetricbeat) or use synthtrace (for example use `node
scripts/synthtrace\r\ninfra_hosts_with_apm_hosts
--scenarioOpts.numInstances=10`)\r\n- Go to Inventory and expand the
host group\r\n- Click on the actions button for any host and click on
the Discover\r\nlink\r\n- The correct dataview should be selected based
on the created\r\ndefinition\r\nThe same can be done for other entity
types\r\n\r\n\r\nhttps://github.com/user-attachments/assets/c9c3a7ae-daff-4d4b-b1b7-898c3f1603c6\r\n\r\n---------\r\n\r\nCo-authored-by:
Carlos Crespo
<[email protected]>","sha":"5bacf1fe50ae373f5951d227ef487a6714aeb541"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/203424","number":203424,"mergeCommit":{"message":"[ECO][Inventory
v2] Ad hoc data view: Add get entities definition endpoint using sources
(#203424)\n\nCloses #202298 \r\n⚠️ Depends on
https://github.com/elastic/kibana/pull/203246\r\n\r\n##
Summary\r\n\r\nThis PR changes the way we get the entity index patterns
to v2. It\r\ncreates an endpoint part of the inventory API which returns
the index\r\npatterns by entity type.\r\n\r\n## Testing\r\n\r\n⚠️
Currently in order to test we need to create the definition using
the\r\ndev tools manually before
this\r\n[PR](#203246) is
merged\r\n\r\n- Open Dev tools and add a definition for an existing
entity type\r\n(example with host)\r\n ```\r\nPOST
kbn:/internal/entities/v2/definitions/sources\r\n{\r\n \"source\": {\r\n
\"id\": \"host_id\",\r\n \"type_id\": \"host\",\r\n \"index_patterns\":
[\"metrics-*\", \"metricbeat-*\"],\r\n \"identity_fields\":
[\"host.name\"],\r\n \"metadata_fields\": [],\r\n \"filters\": [],\r\n
\"timestamp_field\": \"@timestamp\"\r\n }\r\n ```\r\n\r\n### Test the
endpoint: \r\n- Open Dev tools and add\r\n` GET
kbn:/internal/inventory/entity/definitions/sources?type=host `\r\n-
Response: \r\n<img width=\"408\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/1c7416b0-50a4-4e63-8081-928ec7856ff8\">\r\n\r\n###
Test in the UI\r\n- After the previous steps add some host data (oblt
cluster /\r\nmetricbeat) or use synthtrace (for example use `node
scripts/synthtrace\r\ninfra_hosts_with_apm_hosts
--scenarioOpts.numInstances=10`)\r\n- Go to Inventory and expand the
host group\r\n- Click on the actions button for any host and click on
the Discover\r\nlink\r\n- The correct dataview should be selected based
on the created\r\ndefinition\r\nThe same can be done for other entity
types\r\n\r\n\r\nhttps://github.com/user-attachments/assets/c9c3a7ae-daff-4d4b-b1b7-898c3f1603c6\r\n\r\n---------\r\n\r\nCo-authored-by:
Carlos Crespo
<[email protected]>","sha":"5bacf1fe50ae373f5951d227ef487a6714aeb541"}}]}]
BACKPORT-->

Co-authored-by: jennypavlova <[email protected]>
  • Loading branch information
kibanamachine and jennypavlova authored Dec 11, 2024
1 parent 4225373 commit 82d1f7a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 39 deletions.
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

0 comments on commit 82d1f7a

Please sign in to comment.