From fedfaa99c63750ec0b339b8bdf83e6c12319f880 Mon Sep 17 00:00:00 2001 From: Jen Huang Date: Thu, 19 Dec 2019 17:37:09 -0800 Subject: [PATCH] [Ingest] Return associated policy IDs in data source info (#53350) * Return number of policies from data source, surface in assign data source UI * Update snapshots --- .../components/assign_datasources.tsx | 14 ++ .../components/datasources_table.tsx | 151 ++++++++++-------- .../policy.contract.test.slap_snap | 130 +++++++-------- .../plugins/ingest/server/libs/policy.ts | 27 +++- .../server/rest_api/datasource/index.ts | 50 +++++- .../ingest/server/rest_api/policy/index.ts | 1 + 6 files changed, 231 insertions(+), 142 deletions(-) diff --git a/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/assign_datasources.tsx b/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/assign_datasources.tsx index f5e4d89a53c7..9f273ad82ee2 100644 --- a/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/assign_datasources.tsx +++ b/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/assign_datasources.tsx @@ -176,6 +176,7 @@ export const AssignDatasourcesFlyout: React.FC = ({ = ({ incremental: true, schema: true, }, + filters: [ + { + type: 'field_value_toggle', + field: 'policies', + value: 0, + name: ( + + ), + }, + ], }} selection={{ onSelectionChange: (selection: Datasource[]) => diff --git a/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/datasources_table.tsx b/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/datasources_table.tsx index d51fdcf2f2e7..0b5976434060 100644 --- a/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/datasources_table.tsx +++ b/x-pack/legacy/plugins/fleet/public/pages/policy_details/components/datasources_table.tsx @@ -6,18 +6,21 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; -import { EuiInMemoryTable, EuiInMemoryTableProps, EuiLink } from '@elastic/eui'; +import { EuiInMemoryTable, EuiInMemoryTableProps, EuiLink, EuiBadge } from '@elastic/eui'; import { Datasource } from '../../../../common/types/domain_data'; import { useLibs } from '../../../hooks'; interface Props extends EuiInMemoryTableProps { - datasources?: Datasource[]; + datasources?: Array; + withPoliciesCount?: boolean; } -export const DatasourcesTable: React.FC = ({ - datasources: originalDatasources, - ...rest -}) => { +export const DatasourcesTable: React.FC = ( + { datasources: originalDatasources, withPoliciesCount, ...rest } = { + datasources: [], + withPoliciesCount: false, + } +) => { const { framework } = useLibs(); // Flatten some values so that they can be searched via in-memory table search @@ -32,6 +35,7 @@ export const DatasourcesTable: React.FC = ({ version: packageVersion, description: packageDescription, }, + policies, }) => ({ id, name, @@ -40,73 +44,88 @@ export const DatasourcesTable: React.FC = ({ packageTitle, packageVersion, packageDescription, + policies: policies?.length || 0, }) ); + const columns: EuiInMemoryTableProps['columns'] = [ + { + field: 'name', + name: i18n.translate('xpack.fleet.policyDetails.datasourcesTable.nameColumnTitle', { + defaultMessage: 'Name', + }), + }, + { + field: 'packageTitle', + name: i18n.translate('xpack.fleet.policyDetails.datasourcesTable.packageNameColumnTitle', { + defaultMessage: 'Package', + }), + }, + { + field: 'packageVersion', + name: i18n.translate('xpack.fleet.policyDetails.datasourcesTable.packageVersionColumnTitle', { + defaultMessage: 'Version', + }), + }, + { + field: 'streams', + name: i18n.translate('xpack.fleet.policyDetails.datasourcesTable.streamsCountColumnTitle', { + defaultMessage: 'Streams', + }), + }, + { + name: i18n.translate('xpack.fleet.policyDetails.datasourcesTable.actionsColumnTitle', { + defaultMessage: 'Actions', + }), + actions: [ + { + render: ({ packageName, packageVersion }: any) => { + return ( + + + + ); + }, + }, + ], + width: '100px', + }, + ]; + + if (withPoliciesCount) { + columns.splice(columns.length - 1, 0, { + field: 'policies', + name: i18n.translate('xpack.fleet.policyDetails.datasourcesTable.policiesColumnTitle', { + defaultMessage: 'Policies', + }), + render: (policies: number) => { + return policies === 0 ? ( + + + + ) : ( + policies + ); + }, + }); + } + return ( { - return ( - - - - ); - }, - }, - ], - width: '100px', - }, - ]} + columns={columns} sorting={{ field: 'name', direction: 'asc', diff --git a/x-pack/legacy/plugins/ingest/server/libs/__memorize_snapshots__/policy.contract.test.slap_snap b/x-pack/legacy/plugins/ingest/server/libs/__memorize_snapshots__/policy.contract.test.slap_snap index f542932792bf..d9f702f76c9a 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/__memorize_snapshots__/policy.contract.test.slap_snap +++ b/x-pack/legacy/plugins/ingest/server/libs/__memorize_snapshots__/policy.contract.test.slap_snap @@ -7,7 +7,7 @@ exports['Policies Lib create should create a new policy - get info (1)'] = { "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -23,26 +23,26 @@ exports['Policies Lib create should create a new policy - get info (1)'] = { exports['Policies Lib create should create a new policy - create - {"name":"test","description":"test description"} (2)'] = { "results": { - "id": "23c08d20-1bdf-11ea-aa72-271a734b43b0", + "id": "25f8fbf0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:17.967Z", + "updated_on": "2019-12-19T23:41:58.955Z", "updated_by": "system (Fleet)" } } -exports['Policies Lib create should create a new policy - get - "23c08d20-1bdf-11ea-aa72-271a734b43b0" (3)'] = { +exports['Policies Lib create should create a new policy - get - "25f8fbf0-22b9-11ea-b637-7765ebe7310c" (3)'] = { "results": { - "id": "23c08d20-1bdf-11ea-aa72-271a734b43b0", + "id": "25f8fbf0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:17.967Z", + "updated_on": "2019-12-19T23:41:58.955Z", "updated_by": "system (Fleet)" } } @@ -63,7 +63,7 @@ exports['Policies Lib ensureDefaultPolicy should create a new default policy if "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -84,12 +84,12 @@ exports['Policies Lib ensureDefaultPolicy should create a new default policy if "description": "Default policy created by Kibana", "status": "active", "datasources": [], - "updated_on": "2019-12-11T06:26:19.740Z", + "updated_on": "2019-12-19T23:42:00.952Z", "updated_by": "Fleet (system action)" } } -exports['Policies Lib ensureDefaultPolicy should create a new default policy if none exists - list - {"username":"internal","options":{"page":1,"perPage":20}} (4)'] = { +exports['Policies Lib ensureDefaultPolicy should create a new default policy if none exists - list - {"username":"internal","options":{"page":1,"perPage":20,"withDatasources":true}} (4)'] = { "results": { "items": [ { @@ -98,7 +98,7 @@ exports['Policies Lib ensureDefaultPolicy should create a new default policy if "description": "Default policy created by Kibana", "status": "active", "datasources": [], - "updated_on": "2019-12-11T06:26:19.740Z", + "updated_on": "2019-12-19T23:42:00.952Z", "updated_by": "Fleet (system action)" } ], @@ -124,7 +124,7 @@ exports['Policies Lib ensureDefaultPolicy should not create the new default poli "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -145,7 +145,7 @@ exports['Policies Lib ensureDefaultPolicy should not create the new default poli "description": "Default policy created by Kibana", "status": "active", "datasources": [], - "updated_on": "2019-12-11T06:26:20.769Z", + "updated_on": "2019-12-19T23:42:02.011Z", "updated_by": "Fleet (system action)" } } @@ -157,12 +157,12 @@ exports['Policies Lib ensureDefaultPolicy should not create the new default poli "description": "Default policy created by Kibana", "status": "active", "datasources": [], - "updated_on": "2019-12-11T06:26:20.769Z", + "updated_on": "2019-12-19T23:42:02.011Z", "updated_by": "Fleet (system action)" } } -exports['Policies Lib ensureDefaultPolicy should not create the new default policy more than once - list - {"username":"internal","options":{"page":1,"perPage":20}} (5)'] = { +exports['Policies Lib ensureDefaultPolicy should not create the new default policy more than once - list - {"username":"internal","options":{"page":1,"perPage":20,"withDatasources":true}} (5)'] = { "results": { "items": [ { @@ -171,7 +171,7 @@ exports['Policies Lib ensureDefaultPolicy should not create the new default poli "description": "Default policy created by Kibana", "status": "active", "datasources": [], - "updated_on": "2019-12-11T06:26:20.769Z", + "updated_on": "2019-12-19T23:42:02.011Z", "updated_by": "Fleet (system action)" } ], @@ -193,7 +193,7 @@ exports['Policies Lib list should list all active policies - get info (1)'] = { "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -209,13 +209,13 @@ exports['Policies Lib list should list all active policies - get info (1)'] = { exports['Policies Lib list should list all active policies - create - {"name":"test","description":"test description"} (2)'] = { "results": { - "id": "260c2ad0-1bdf-11ea-aa72-271a734b43b0", + "id": "28660450-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:21.819Z", + "updated_on": "2019-12-19T23:42:03.027Z", "updated_by": "system (Fleet)" } } @@ -228,7 +228,7 @@ exports['Policies Lib list should list all active policies - get info (3)'] = { "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -244,13 +244,13 @@ exports['Policies Lib list should list all active policies - get info (3)'] = { exports['Policies Lib list should list all active policies - create - {"name":"test2","description":"test description"} (4)'] = { "results": { - "id": "26a07b90-1bdf-11ea-aa72-271a734b43b0", + "id": "28fc50e0-22b9-11ea-b637-7765ebe7310c", "name": "test2", "description": "test description", "status": "active", "datasources": [], "label": "test2", - "updated_on": "2019-12-11T06:26:22.791Z", + "updated_on": "2019-12-19T23:42:04.013Z", "updated_by": "system (Fleet)" } } @@ -263,7 +263,7 @@ exports['Policies Lib list should list all active policies - get info (5)'] = { "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -279,48 +279,48 @@ exports['Policies Lib list should list all active policies - get info (5)'] = { exports['Policies Lib list should list all active policies - create - {"name":"test3","description":"test description"} (6)'] = { "results": { - "id": "273ce2a0-1bdf-11ea-aa72-271a734b43b0", + "id": "2997a680-22b9-11ea-b637-7765ebe7310c", "name": "test3", "description": "test description", "status": "active", "datasources": [], "label": "test3", - "updated_on": "2019-12-11T06:26:23.816Z", + "updated_on": "2019-12-19T23:42:05.031Z", "updated_by": "system (Fleet)" } } -exports['Policies Lib list should list all active policies - list - {"username":"internal","options":{"page":1,"perPage":20}} (7)'] = { +exports['Policies Lib list should list all active policies - list - {"username":"internal","options":{"page":1,"perPage":20,"withDatasources":true}} (7)'] = { "results": { "items": [ { - "id": "260c2ad0-1bdf-11ea-aa72-271a734b43b0", + "id": "28660450-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:21.819Z", + "updated_on": "2019-12-19T23:42:03.027Z", "updated_by": "system (Fleet)" }, { - "id": "26a07b90-1bdf-11ea-aa72-271a734b43b0", + "id": "28fc50e0-22b9-11ea-b637-7765ebe7310c", "name": "test2", "description": "test description", "status": "active", "datasources": [], "label": "test2", - "updated_on": "2019-12-11T06:26:22.791Z", + "updated_on": "2019-12-19T23:42:04.013Z", "updated_by": "system (Fleet)" }, { - "id": "273ce2a0-1bdf-11ea-aa72-271a734b43b0", + "id": "2997a680-22b9-11ea-b637-7765ebe7310c", "name": "test3", "description": "test description", "status": "active", "datasources": [], "label": "test3", - "updated_on": "2019-12-11T06:26:23.816Z", + "updated_on": "2019-12-19T23:42:05.031Z", "updated_by": "system (Fleet)" } ], @@ -342,7 +342,7 @@ exports['Policies Lib update should update a policy and invalidate the original "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -358,52 +358,52 @@ exports['Policies Lib update should update a policy and invalidate the original exports['Policies Lib update should update a policy and invalidate the original - create - {"name":"test","description":"test description"} (2)'] = { "results": { - "id": "27de52c0-1bdf-11ea-aa72-271a734b43b0", + "id": "2a38a170-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:24.875Z", + "updated_on": "2019-12-19T23:42:06.084Z", "updated_by": "system (Fleet)" } } -exports['Policies Lib update should update a policy and invalidate the original - get - "27de52c0-1bdf-11ea-aa72-271a734b43b0" (3)'] = { +exports['Policies Lib update should update a policy and invalidate the original - get - "2a38a170-22b9-11ea-b637-7765ebe7310c" (3)'] = { "results": { - "id": "27de52c0-1bdf-11ea-aa72-271a734b43b0", + "id": "2a38a170-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:24.875Z", + "updated_on": "2019-12-19T23:42:06.084Z", "updated_by": "system (Fleet)" } } exports['Policies Lib update should update a policy and invalidate the original - update - {"name":"foo","description":"test description","datasources":[]} (4)'] = { "results": { - "id": "27de52c0-1bdf-11ea-aa72-271a734b43b0", + "id": "2a38a170-22b9-11ea-b637-7765ebe7310c", "name": "foo", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:25 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:07 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } -exports['Policies Lib update should update a policy and invalidate the original - get - "27de52c0-1bdf-11ea-aa72-271a734b43b0" (5)'] = { +exports['Policies Lib update should update a policy and invalidate the original - get - "2a38a170-22b9-11ea-b637-7765ebe7310c" (5)'] = { "results": { - "id": "27de52c0-1bdf-11ea-aa72-271a734b43b0", + "id": "2a38a170-22b9-11ea-b637-7765ebe7310c", "name": "foo", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:25 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:07 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } @@ -420,7 +420,7 @@ exports['Policies Lib update should assign and unassign data sources to policy - "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -436,33 +436,33 @@ exports['Policies Lib update should assign and unassign data sources to policy - exports['Policies Lib update should assign and unassign data sources to policy - create - {"name":"test","description":"test description"} (2)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:27.008Z", + "updated_on": "2019-12-19T23:42:08.156Z", "updated_by": "system (Fleet)" } } -exports['Policies Lib update should assign and unassign data sources to policy - get - "2923f220-1bdf-11ea-aa72-271a734b43b0" (3)'] = { +exports['Policies Lib update should assign and unassign data sources to policy - get - "2b747cd0-22b9-11ea-b637-7765ebe7310c" (3)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:27.008Z", + "updated_on": "2019-12-19T23:42:08.156Z", "updated_by": "system (Fleet)" } } exports['Policies Lib update should assign and unassign data sources to policy - update - {"name":"test","description":"test description","datasources":["foo","bar"]} (4)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", @@ -471,14 +471,14 @@ exports['Policies Lib update should assign and unassign data sources to policy - "bar" ], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:28 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:09 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } -exports['Policies Lib update should assign and unassign data sources to policy - get - "2923f220-1bdf-11ea-aa72-271a734b43b0" (5)'] = { +exports['Policies Lib update should assign and unassign data sources to policy - get - "2b747cd0-22b9-11ea-b637-7765ebe7310c" (5)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", @@ -487,14 +487,14 @@ exports['Policies Lib update should assign and unassign data sources to policy - "bar" ], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:28 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:09 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } exports['Policies Lib update should assign and unassign data sources to policy - update - {"name":"test","description":"test description","datasources":["foo","bar","test"]} (6)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", @@ -504,14 +504,14 @@ exports['Policies Lib update should assign and unassign data sources to policy - "test" ], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:29 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:10 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } -exports['Policies Lib update should assign and unassign data sources to policy - get - "2923f220-1bdf-11ea-aa72-271a734b43b0" (7)'] = { +exports['Policies Lib update should assign and unassign data sources to policy - get - "2b747cd0-22b9-11ea-b637-7765ebe7310c" (7)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", @@ -521,14 +521,14 @@ exports['Policies Lib update should assign and unassign data sources to policy - "test" ], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:29 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:10 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } exports['Policies Lib update should assign and unassign data sources to policy - update - {"name":"test","description":"test description","datasources":["bar"]} (8)'] = { "results": { - "id": "2923f220-1bdf-11ea-aa72-271a734b43b0", + "id": "2b747cd0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", @@ -536,7 +536,7 @@ exports['Policies Lib update should assign and unassign data sources to policy - "bar" ], "label": "test", - "updated_on": "Tue Dec 10 2019 22:26:30 GMT-0800 (Pacific Standard Time)", + "updated_on": "Thu Dec 19 2019 15:42:11 GMT-0800 (Pacific Standard Time)", "updated_by": "Fleet (system action)" } } @@ -549,7 +549,7 @@ exports['Policies Lib delete Should delete the by the ID - get info (1)'] = { "license": { "type": "trial", "expired": false, - "expiry_date_in_millis": 1578637547310 + "expiry_date_in_millis": 1579390787514 }, "security": { "enabled": true, @@ -565,20 +565,20 @@ exports['Policies Lib delete Should delete the by the ID - get info (1)'] = { exports['Policies Lib delete Should delete the by the ID - create - {"name":"test","description":"test description"} (2)'] = { "results": { - "id": "2b9ae590-1bdf-11ea-aa72-271a734b43b0", + "id": "2dea5ed0-22b9-11ea-b637-7765ebe7310c", "name": "test", "description": "test description", "status": "active", "datasources": [], "label": "test", - "updated_on": "2019-12-11T06:26:31.142Z", + "updated_on": "2019-12-19T23:42:12.282Z", "updated_by": "system (Fleet)" } } -exports['Policies Lib delete Should delete the by the ID - delete - 2b9ae590-1bdf-11ea-aa72-271a734b43b0'] = {} +exports['Policies Lib delete Should delete the by the ID - delete - 2dea5ed0-22b9-11ea-b637-7765ebe7310c'] = {} -exports['Policies Lib delete Should delete the by the ID - list - {"username":"internal","options":{"page":1,"perPage":20}} (3)'] = { +exports['Policies Lib delete Should delete the by the ID - list - {"username":"internal","options":{"page":1,"perPage":20,"withDatasources":true}} (3)'] = { "results": { "items": [], "total": 0, diff --git a/x-pack/legacy/plugins/ingest/server/libs/policy.ts b/x-pack/legacy/plugins/ingest/server/libs/policy.ts index 8cb693b7b6b2..a3eb9dd48228 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/policy.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/policy.ts @@ -82,25 +82,40 @@ export class PolicyLib { kuery?: string; page?: number; perPage?: number; + withDatasources?: boolean; } = { page: 1, perPage: 20, + withDatasources: true, } ): Promise<{ items: Policy[]; total: number; page: number; perPage: number }> { const response = await this.adapter.list(user, options); - const dataSourcesIds = unique(flatten(response.items.map(policy => policy.datasources || []))); - - const datasources: Datasource[] = await this.libs.datasources.getByIDs(user, dataSourcesIds); + if (options.withDatasources) { + const dataSourcesIds = unique( + flatten(response.items.map(policy => policy.datasources || [])) + ); + const datasources: Datasource[] = await this.libs.datasources.getByIDs(user, dataSourcesIds); + + return { + ...response, + items: response.items.map(policy => { + return { + ...policy, + datasources: (policy.datasources || []).map(id => { + return datasources.find(ds => ds.id === id); + }), + } as Policy; + }), + }; + } return { ...response, items: response.items.map(policy => { return { ...policy, - datasources: (policy.datasources || []).map(id => { - return datasources.find(ds => ds.id === id); - }), + datasources: undefined, } as Policy; }), }; diff --git a/x-pack/legacy/plugins/ingest/server/rest_api/datasource/index.ts b/x-pack/legacy/plugins/ingest/server/rest_api/datasource/index.ts index 709e571a5074..b1a73dbd89b3 100644 --- a/x-pack/legacy/plugins/ingest/server/rest_api/datasource/index.ts +++ b/x-pack/legacy/plugins/ingest/server/rest_api/datasource/index.ts @@ -35,10 +35,25 @@ export const createGETDatasourceRoute = (libs: ServerLibs) => ({ config: {}, handler: (async ( request: FrameworkRequest<{ params: { datasourceId: string } }> - ): Promise> => { + ): Promise> => { const datasource = await libs.datasources.get(request.user, request.params.datasourceId); - - return { item: datasource, success: true }; + if (datasource) { + const policies = await libs.policy.list(request.user, { + kuery: `policies.datasources:"${datasource.id}"`, + page: 1, + perPage: 10000, + withDatasources: false, + }); + return { + item: { + ...datasource, + policies: policies.items.map(p => p.id), + }, + success: true, + }; + } else { + return { item: null, success: false }; + } }) as FrameworkRouteHandler, }); @@ -56,7 +71,9 @@ export const createGETDatasourcesRoute = (libs: ServerLibs) => ({ }, }, }, - handler: async (request: FrameworkRequest): Promise> => { + handler: async ( + request: FrameworkRequest + ): Promise> => { // TODO fix for types that broke in TS 3.7 const query: { page: string; @@ -64,13 +81,36 @@ export const createGETDatasourcesRoute = (libs: ServerLibs) => ({ kuery: string; showInactive: string; } = request.query as any; + const { items, total, page, perPage } = await libs.datasources.list(request.user, { page: parseInt(query.page, 10), perPage: parseInt(query.perPage, 10), kuery: query.kuery, }); - return { list: items, success: true, total, page, perPage }; + const list: Array = []; + + // TODO: this could be optimized so that it is not sequentially blocking + for (const ds of items) { + const policies = await libs.policy.list(request.user, { + kuery: `policies.datasources:"${ds.id}"`, + page: 1, + perPage: 10000, + withDatasources: false, + }); + list.push({ + ...ds, + policies: policies.items.map(p => p.id), + }); + } + + return { + list, + success: true, + total, + page, + perPage, + }; }, }); diff --git a/x-pack/legacy/plugins/ingest/server/rest_api/policy/index.ts b/x-pack/legacy/plugins/ingest/server/rest_api/policy/index.ts index ab037cdde667..25db5147d80c 100644 --- a/x-pack/legacy/plugins/ingest/server/rest_api/policy/index.ts +++ b/x-pack/legacy/plugins/ingest/server/rest_api/policy/index.ts @@ -67,6 +67,7 @@ export const createGETPoliciesRoute = (libs: ServerLibs) => ({ page: parseInt(query.page, 10), perPage: parseInt(query.perPage, 10), kuery: query.kuery, + withDatasources: true, }); return { list: items, success: true, total, page, perPage };