diff --git a/src/platform/plugins/shared/data_view_editor/kibana.jsonc b/src/platform/plugins/shared/data_view_editor/kibana.jsonc index 04d543bdd47ec..be7a296475a7a 100644 --- a/src/platform/plugins/shared/data_view_editor/kibana.jsonc +++ b/src/platform/plugins/shared/data_view_editor/kibana.jsonc @@ -15,6 +15,9 @@ "data", "dataViews" ], + "optionalPlugins": [ + "cps" + ], "requiredBundles": [ "kibanaReact", "kibanaUtils", diff --git a/src/platform/plugins/shared/data_view_editor/moon.yml b/src/platform/plugins/shared/data_view_editor/moon.yml index 6b7501f549cee..260c400fd5834 100644 --- a/src/platform/plugins/shared/data_view_editor/moon.yml +++ b/src/platform/plugins/shared/data_view_editor/moon.yml @@ -33,6 +33,8 @@ dependsOn: - '@kbn/code-editor' - '@kbn/rollup' - '@kbn/css-utils' + - '@kbn/cps-utils' + - '@kbn/cps' tags: - plugin - prod diff --git a/src/platform/plugins/shared/data_view_editor/public/components/data_view_flyout_content_container.tsx b/src/platform/plugins/shared/data_view_editor/public/components/data_view_flyout_content_container.tsx index 097b32a50e168..bcee7b9b3c0d7 100644 --- a/src/platform/plugins/shared/data_view_editor/public/components/data_view_flyout_content_container.tsx +++ b/src/platform/plugins/shared/data_view_editor/public/components/data_view_flyout_content_container.tsx @@ -30,13 +30,13 @@ const DataViewFlyoutContentContainer = ({ getDataViewHelpText, }: DataViewEditorProps) => { const { - services: { dataViews, notifications, http }, + services: { dataViews, notifications, http, cps }, } = useKibana(); const [dataViewEditorService] = useState( () => new DataViewEditorService({ - services: { http, dataViews }, + services: { http, dataViews, cpsManager: cps?.cpsManager }, initialValues: { name: editData?.name, type: editData?.type as INDEX_PATTERN_TYPE, diff --git a/src/platform/plugins/shared/data_view_editor/public/data_view_editor_service.ts b/src/platform/plugins/shared/data_view_editor/public/data_view_editor_service.ts index d8db9d554ec12..0e443d7369780 100644 --- a/src/platform/plugins/shared/data_view_editor/public/data_view_editor_service.ts +++ b/src/platform/plugins/shared/data_view_editor/public/data_view_editor_service.ts @@ -25,6 +25,7 @@ import type { DataViewField, } from '@kbn/data-views-plugin/public'; import { INDEX_PATTERN_TYPE } from '@kbn/data-views-plugin/public'; +import type { ICPSManager } from '@kbn/cps-utils'; import type { RollupIndicesCapsResponse, @@ -52,6 +53,7 @@ export interface DataViewEditorServiceConstructorArgs { services: { http: HttpSetup; dataViews: DataViewsServicePublic; + cpsManager?: ICPSManager; }; /** * Whether service requires requireTimestampField @@ -93,7 +95,7 @@ export const stateSelectorFactory = export class DataViewEditorService { constructor({ - services: { http, dataViews }, + services: { http, dataViews, cpsManager }, initialValues: { type: initialType = INDEX_PATTERN_TYPE.DEFAULT, indexPattern: initialIndexPattern = '', @@ -106,6 +108,7 @@ export class DataViewEditorService { this.requireTimestampField = requireTimestampField; this.type = initialType; this.indexPattern = removeSpaces(initialIndexPattern); + this.cpsManager = cpsManager; // fire off a couple of requests that we know we'll need this.rollupCapsResponse = this.getRollupIndexCaps(); @@ -140,12 +143,20 @@ export class DataViewEditorService { this.rollupIndexForProvider$.next(rollupIndex); this.rollupIndexForProvider$.next(undefined); }); + + // Subscribe to CPS project routing changes + if (cpsManager) { + this.cpsProjectRoutingSub = cpsManager.getProjectRouting$().subscribe(() => { + this.loadIndices(); + }); + } } private http: HttpSetup; private dataViews: DataViewsServicePublic; // config private requireTimestampField: boolean; + private cpsManager?: ICPSManager; private type = INDEX_PATTERN_TYPE.DEFAULT; private indexPattern = ''; @@ -157,6 +168,7 @@ export class DataViewEditorService { private loadTimestampFieldsSub: Subscription; private matchedIndicesForProviderSub: Subscription; private rollupIndexForProviderSub: Subscription; + private cpsProjectRoutingSub?: Subscription; private state$: BehaviorSubject; @@ -316,7 +328,8 @@ export class DataViewEditorService { pattern: string; showAllIndices?: boolean | undefined; }) => { - const key = JSON.stringify(props); + const projectRouting = this.cpsManager?.getProjectRouting(); + const key = JSON.stringify({ ...props, projectRouting }); this.getIndicesMemory[key] = this.getIndicesMemory[key] || @@ -348,7 +361,8 @@ export class DataViewEditorService { getFieldsOptions: GetFieldsOptions, requireTimestampField: boolean ) => { - const key = JSON.stringify(getFieldsOptions) + requireTimestampField; + const projectRouting = this.cpsManager?.getProjectRouting(); + const key = JSON.stringify({ getFieldsOptions, requireTimestampField, projectRouting }); const getTimestampOptionsPromise = this.getTimestampOptionsForWildcard( getFieldsOptions, @@ -423,5 +437,6 @@ export class DataViewEditorService { this.loadTimestampFieldsSub.unsubscribe(); this.matchedIndicesForProviderSub.unsubscribe(); this.rollupIndexForProviderSub.unsubscribe(); + this.cpsProjectRoutingSub?.unsubscribe(); }; } diff --git a/src/platform/plugins/shared/data_view_editor/public/open_editor.tsx b/src/platform/plugins/shared/data_view_editor/public/open_editor.tsx index 579b07681c081..f01fe40d0e8bb 100644 --- a/src/platform/plugins/shared/data_view_editor/public/open_editor.tsx +++ b/src/platform/plugins/shared/data_view_editor/public/open_editor.tsx @@ -10,6 +10,7 @@ import React from 'react'; import type { CoreStart, OverlayRef } from '@kbn/core/public'; import type { DataView, DataViewsServicePublic } from '@kbn/data-views-plugin/public'; +import type { CPSPluginStart } from '@kbn/cps/public'; import { toMountPoint } from '@kbn/react-kibana-mount'; import type { DataPublicPluginStart } from './shared_imports'; @@ -22,10 +23,11 @@ interface Dependencies { core: CoreStart; searchClient: DataPublicPluginStart['search']['search']; dataViews: DataViewsServicePublic; + cps?: CPSPluginStart; } export const getEditorOpener = - ({ core, searchClient, dataViews }: Dependencies) => + ({ core, searchClient, dataViews, cps }: Dependencies) => (options: DataViewEditorProps): CloseEditor => { const { uiSettings, overlays, docLinks, notifications, http, application } = core; const { Provider: KibanaReactContextProvider } = @@ -38,6 +40,7 @@ export const getEditorOpener = dataViews, overlays, searchClient, + cps, }); let overlayRef: OverlayRef | null = null; diff --git a/src/platform/plugins/shared/data_view_editor/public/plugin.tsx b/src/platform/plugins/shared/data_view_editor/public/plugin.tsx index 87e5db7f9ebe5..44f5717cf4898 100644 --- a/src/platform/plugins/shared/data_view_editor/public/plugin.tsx +++ b/src/platform/plugins/shared/data_view_editor/public/plugin.tsx @@ -29,7 +29,7 @@ export class DataViewEditorPlugin public start(core: CoreStart, plugins: StartPlugins) { const { application, uiSettings, docLinks, http, notifications, overlays } = core; - const { data, dataViews } = plugins; + const { data, dataViews, cps } = plugins; return { /** @@ -41,6 +41,7 @@ export class DataViewEditorPlugin core, dataViews, searchClient: data.search.search, + cps, }), /** * Data view editor flyout via react component @@ -58,6 +59,7 @@ export class DataViewEditorPlugin overlays, dataViews, searchClient: data.search.search, + cps, }} {...props} /> diff --git a/src/platform/plugins/shared/data_view_editor/public/types.ts b/src/platform/plugins/shared/data_view_editor/public/types.ts index af96f240393e6..e9771b33c6b06 100644 --- a/src/platform/plugins/shared/data_view_editor/public/types.ts +++ b/src/platform/plugins/shared/data_view_editor/public/types.ts @@ -25,6 +25,7 @@ import type { INDEX_PATTERN_TYPE, MatchedItem, } from '@kbn/data-views-plugin/public'; +import type { CPSPluginStart } from '@kbn/cps/public'; import type { DataViewEditorService } from './data_view_editor_service'; import type { DataPublicPluginStart, IndexPatternAggRestrictions } from './shared_imports'; @@ -37,6 +38,7 @@ export interface DataViewEditorContext { overlays: OverlayStart; dataViews: DataViewsServicePublic; searchClient: DataPublicPluginStart['search']['search']; + cps?: CPSPluginStart; } /** @public */ @@ -110,6 +112,7 @@ export interface SetupPlugins {} export interface StartPlugins { data: DataPublicPluginStart; dataViews: DataViewsServicePublic; + cps?: CPSPluginStart; } export type CloseEditor = () => void; diff --git a/src/platform/plugins/shared/data_view_editor/tsconfig.json b/src/platform/plugins/shared/data_view_editor/tsconfig.json index 8e546a411bdc4..fa0ae529a48ac 100644 --- a/src/platform/plugins/shared/data_view_editor/tsconfig.json +++ b/src/platform/plugins/shared/data_view_editor/tsconfig.json @@ -22,6 +22,8 @@ "@kbn/code-editor", "@kbn/rollup", "@kbn/css-utils", + "@kbn/cps-utils", + "@kbn/cps", ], "exclude": [ "target/**/*", diff --git a/src/platform/plugins/shared/data_view_management/server/routes/resolve_index.ts b/src/platform/plugins/shared/data_view_management/server/routes/resolve_index.ts index f59c635e65980..b6a763dda4978 100644 --- a/src/platform/plugins/shared/data_view_management/server/routes/resolve_index.ts +++ b/src/platform/plugins/shared/data_view_management/server/routes/resolve_index.ts @@ -35,16 +35,26 @@ export function registerResolveIndexRoute(router: IRouter): void { schema.literal('none'), ]) ), + project_routing: schema.maybe(schema.string()), }), }, }, async (context, req, res) => { const esClient = (await context.core).elasticsearch.client; try { - const body = await esClient.asCurrentUser.indices.resolveIndex({ + const params = { name: req.params.query, expand_wildcards: req.query.expand_wildcards || 'open', - }); + // TODO: we should be sending this param here and esClient should pass it to body but it is not yet supported in esClient + // ...(req.query.project_routing ? { project_routing: req.query.project_routing } : {}), + // so we do this for now: + ...(req.query.project_routing + ? { body: { project_routing: req.query.project_routing } } + : {}), + }; + + // @ts-ignore because the types for resolveIndex do not yet include body param + const body = await esClient.asCurrentUser.indices.resolveIndex(params); return res.ok({ body }); } catch (e) { // 403: no_such_remote_cluster_exception diff --git a/src/platform/plugins/shared/data_views/kibana.jsonc b/src/platform/plugins/shared/data_views/kibana.jsonc index 00df1941eaa37..496defd0538d2 100644 --- a/src/platform/plugins/shared/data_views/kibana.jsonc +++ b/src/platform/plugins/shared/data_views/kibana.jsonc @@ -17,7 +17,8 @@ "contentManagement" ], "optionalPlugins": [ - "usageCollection" + "usageCollection", + "cps" ], "requiredBundles": [ "kibanaUtils" diff --git a/src/platform/plugins/shared/data_views/moon.yml b/src/platform/plugins/shared/data_views/moon.yml index f4e679fbc6fc0..051ec79905e1c 100644 --- a/src/platform/plugins/shared/data_views/moon.yml +++ b/src/platform/plugins/shared/data_views/moon.yml @@ -45,6 +45,7 @@ dependsOn: - '@kbn/logging-mocks' - '@kbn/doc-links' - '@kbn/core-deprecations-common' + - '@kbn/cps' tags: - plugin - prod diff --git a/src/platform/plugins/shared/data_views/public/data_views_service_public.ts b/src/platform/plugins/shared/data_views/public/data_views_service_public.ts index b09e6f51bd022..20cbcb6ab9455 100644 --- a/src/platform/plugins/shared/data_views/public/data_views_service_public.ts +++ b/src/platform/plugins/shared/data_views/public/data_views_service_public.ts @@ -34,6 +34,7 @@ export interface DataViewsServicePublicDeps extends DataViewsServiceDeps { pattern: string; showAllIndices?: boolean; isRollupIndex: (indexName: string) => boolean; + projectRouting?: string; }) => Promise; getRollupsEnabled: () => boolean; @@ -52,6 +53,7 @@ export class DataViewsServicePublic extends DataViewsService { pattern: string; showAllIndices?: boolean; isRollupIndex: (indexName: string) => boolean; + projectRouting?: string; }) => Promise; public hasData: HasDataService; private rollupsEnabled: boolean = false; diff --git a/src/platform/plugins/shared/data_views/public/plugin.ts b/src/platform/plugins/shared/data_views/public/plugin.ts index 72748ce8238bb..1c30bb0e77add 100644 --- a/src/platform/plugins/shared/data_views/public/plugin.ts +++ b/src/platform/plugins/shared/data_views/public/plugin.ts @@ -71,7 +71,7 @@ export class DataViewsPublicPlugin public start( core: CoreStart, - { fieldFormats, contentManagement }: DataViewsPublicStartDependencies + { fieldFormats, contentManagement, cps }: DataViewsPublicStartDependencies ): DataViewsPublicPluginStart { const { uiSettings, http, notifications, application } = core; @@ -106,7 +106,13 @@ export class DataViewsPublicPlugin getCanSaveSync: () => application.capabilities.indexPatterns.save === true, getCanSaveAdvancedSettings: () => Promise.resolve(application.capabilities.advancedSettings.save === true), - getIndices: (props) => getIndices({ ...props, http: core.http }), + getIndices: (props) => + getIndices({ + ...props, + http: core.http, + projectRouting: + 'projectRouting' in props ? props.projectRouting : cps?.cpsManager?.getProjectRouting(), + }), getRollupsEnabled: () => this.rollupsEnabled, scriptedFieldsEnabled: config.scriptedFieldsEnabled === false ? false : true, // accounting for null value }); diff --git a/src/platform/plugins/shared/data_views/public/services/get_indices.ts b/src/platform/plugins/shared/data_views/public/services/get_indices.ts index 545fbb07e5710..2a427736d3fe0 100644 --- a/src/platform/plugins/shared/data_views/public/services/get_indices.ts +++ b/src/platform/plugins/shared/data_views/public/services/get_indices.ts @@ -10,6 +10,7 @@ import { sortBy } from 'lodash'; import type { HttpStart } from '@kbn/core/public'; import { i18n } from '@kbn/i18n'; +import { sanitizeProjectRoutingForES } from '@kbn/es-query'; import type { Tag } from '../types'; import { INDEX_PATTERN_TYPE } from '../types'; import type { MatchedItem, ResolveIndexResponse } from '../types'; @@ -48,18 +49,27 @@ export const getIndicesViaResolve = async ({ pattern, showAllIndices, isRollupIndex, + projectRouting, }: { http: HttpStart; pattern: string; showAllIndices: boolean; isRollupIndex: (indexName: string) => boolean; + projectRouting?: string; }) => { const encodedPattern = encodeURIComponent(pattern); + const query: Record = {}; + if (showAllIndices) { + query.expand_wildcards = 'all'; + } + if (projectRouting) { + query.project_routing = projectRouting; + } return http .get( `/internal/index-pattern-management/resolve_index/${encodedPattern}`, { - query: showAllIndices ? { expand_wildcards: 'all' } : undefined, + query: Object.keys(query).length > 0 ? query : undefined, } ) .then((response) => { @@ -76,11 +86,13 @@ export async function getIndices({ pattern: rawPattern = '', showAllIndices = false, isRollupIndex, + projectRouting, }: { http: HttpStart; pattern: string; showAllIndices?: boolean; isRollupIndex: (indexName: string) => boolean; + projectRouting?: string; }): Promise { const pattern = rawPattern.trim(); @@ -107,6 +119,7 @@ export async function getIndices({ pattern, showAllIndices, isRollupIndex, + projectRouting: sanitizeProjectRoutingForES(projectRouting), }).catch(() => []); } diff --git a/src/platform/plugins/shared/data_views/public/services/index.ts b/src/platform/plugins/shared/data_views/public/services/index.ts index 390b92f81e2a8..b31f76d88a825 100644 --- a/src/platform/plugins/shared/data_views/public/services/index.ts +++ b/src/platform/plugins/shared/data_views/public/services/index.ts @@ -17,6 +17,7 @@ export async function getIndices(props: { pattern: string; showAllIndices?: boolean; isRollupIndex: (indexName: string) => boolean; + projectRouting?: string; }): Promise { const { getIndices: getIndicesLazy } = await import('./get_indices'); return getIndicesLazy(props); diff --git a/src/platform/plugins/shared/data_views/public/types.ts b/src/platform/plugins/shared/data_views/public/types.ts index 7a048b7054016..a5df830cb2aaf 100644 --- a/src/platform/plugins/shared/data_views/public/types.ts +++ b/src/platform/plugins/shared/data_views/public/types.ts @@ -13,6 +13,7 @@ import type { ContentManagementPublicSetup, ContentManagementPublicStart, } from '@kbn/content-management-plugin/public'; +import type { CPSPluginStart } from '@kbn/cps/public'; import type { DataViewsServicePublicMethods } from './data_views'; import type { HasDataService } from '../common'; @@ -105,6 +106,10 @@ export interface DataViewsPublicStartDependencies { * Content management */ contentManagement: ContentManagementPublicStart; + /** + * CPS plugin (optional) + */ + cps?: CPSPluginStart; } /** @@ -121,6 +126,7 @@ export interface DataViewsServicePublic extends DataViewsServicePublicMethods { pattern: string; showAllIndices?: boolean; isRollupIndex: (indexName: string) => boolean; + projectRouting?: string; }) => Promise; getRollupsEnabled: () => boolean; scriptedFieldsEnabled: boolean; diff --git a/src/platform/plugins/shared/data_views/tsconfig.json b/src/platform/plugins/shared/data_views/tsconfig.json index c3f558324bd83..fff46686bb1ab 100644 --- a/src/platform/plugins/shared/data_views/tsconfig.json +++ b/src/platform/plugins/shared/data_views/tsconfig.json @@ -40,6 +40,7 @@ "@kbn/logging-mocks", "@kbn/doc-links", "@kbn/core-deprecations-common", + "@kbn/cps", ], "exclude": [ "target/**/*",