From fbf5fce22e8024586b06e0569b17e6382b27c654 Mon Sep 17 00:00:00 2001 From: Kristen Tian <105667444+kristenTian@users.noreply.github.com> Date: Wed, 10 Aug 2022 11:53:15 -0700 Subject: [PATCH] Link datasource to indexpattern (#2118) Signed-off-by: Kristen Tian Signed-off-by: Kristen Tian --- .../index_patterns/index_pattern.ts | 22 ++++++++++++++++++- .../index_patterns/index_patterns.ts | 3 +++ .../data/common/index_patterns/types.ts | 6 +++++ .../create_index_pattern_wizard.tsx | 3 ++- .../mount_management_section.tsx | 2 +- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts index ebe893479cf3..930d21f02992 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts @@ -43,7 +43,13 @@ import { IndexPatternField, IIndexPatternFieldList, fieldList } from '../fields' import { formatHitProvider } from './format_hit'; import { flattenHitWrapper } from './flatten_hit'; import { FieldFormatsStartCommon, FieldFormat } from '../../field_formats'; -import { IndexPatternSpec, TypeMeta, SourceFilter, IndexPatternFieldMap } from '../types'; +import { + IndexPatternSpec, + TypeMeta, + SourceFilter, + IndexPatternFieldMap, + SavedObjectReference, +} from '../types'; import { SerializedFieldFormat } from '../../../../expressions/common'; interface IndexPatternDeps { @@ -67,6 +73,7 @@ interface SavedObjectBody { type FormatFieldFn = (hit: Record, fieldName: string) => any; +const DATA_SOURCE_REFERNECE_NAME = 'dataSource'; export class IndexPattern implements IIndexPattern { public id?: string; public title: string = ''; @@ -86,6 +93,7 @@ export class IndexPattern implements IIndexPattern { // savedObject version public version: string | undefined; public sourceFilters?: SourceFilter[]; + public dataSourceRef?: SavedObjectReference; private originalSavedObjectBody: SavedObjectBody = {}; private shortDotsEnable: boolean = false; private fieldFormats: FieldFormatsStartCommon; @@ -128,6 +136,7 @@ export class IndexPattern implements IIndexPattern { this.fieldFormatMap = _.mapValues(fieldFormatMap, (mapping) => { return this.deserializeFieldFormatMap(mapping); }); + this.dataSourceRef = spec.dataSourceRef; } /** @@ -357,6 +366,17 @@ export class IndexPattern implements IIndexPattern { }; } + getSaveObjectReference() { + return this.dataSourceRef + ? [ + { + ...this.dataSourceRef, + name: DATA_SOURCE_REFERNECE_NAME, + }, + ] + : []; + } + /** * Provide a field, get its formatter * @param field diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index f3d605472b96..4ec31928257e 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -562,8 +562,11 @@ export class IndexPatternsService { } const body = indexPattern.getAsSavedObjectBody(); + const references = indexPattern.getSaveObjectReference(); + const response = await this.savedObjectsClient.create(savedObjectType, body, { id: indexPattern.id, + references, }); indexPattern.id = response.id; indexPatternCache.set(indexPattern.id, indexPattern); diff --git a/src/plugins/data/common/index_patterns/types.ts b/src/plugins/data/common/index_patterns/types.ts index 01ab9e41bab1..2200fb85d8b1 100644 --- a/src/plugins/data/common/index_patterns/types.ts +++ b/src/plugins/data/common/index_patterns/types.ts @@ -184,6 +184,11 @@ export interface FieldSpec { export type IndexPatternFieldMap = Record; +export interface SavedObjectReference { + name?: string; + id: string; + type: string; +} export interface IndexPatternSpec { id?: string; version?: string; @@ -194,6 +199,7 @@ export interface IndexPatternSpec { fields?: IndexPatternFieldMap; typeMeta?: TypeMeta; type?: string; + dataSourceRef?: SavedObjectReference; } export interface SourceFilter { diff --git a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/create_index_pattern_wizard.tsx b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/create_index_pattern_wizard.tsx index c10deeed93ef..7d9dad054887 100644 --- a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/create_index_pattern_wizard.tsx +++ b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/create_index_pattern_wizard.tsx @@ -181,13 +181,14 @@ export class CreateIndexPatternWizard extends Component< createIndexPattern = async (timeFieldName: string | undefined, indexPatternId: string) => { let emptyPattern: IndexPattern; const { history } = this.props; - const { indexPattern } = this.state; + const { indexPattern, dataSourceRef } = this.state; try { emptyPattern = await this.context.services.data.indexPatterns.createAndSave({ id: indexPatternId, title: indexPattern, timeFieldName, + dataSourceRef, ...this.state.indexPatternCreationType.getIndexPatternMappings(), }); } catch (err) { diff --git a/src/plugins/index_pattern_management/public/management_app/mount_management_section.tsx b/src/plugins/index_pattern_management/public/management_app/mount_management_section.tsx index 3be80bdfa14e..6e03f9f5e1dc 100644 --- a/src/plugins/index_pattern_management/public/management_app/mount_management_section.tsx +++ b/src/plugins/index_pattern_management/public/management_app/mount_management_section.tsx @@ -70,7 +70,7 @@ export async function mountManagementSection( ] = await getStartServices(); const canSave = Boolean(application.capabilities.indexPatterns.save); const dataSourceEnabled = false; - // todo: Boolean(application.capabilities.indexPatterns.dataSourceEnabled) or get from plugin constructor; + // todo: Boolean(application.capabilities.indexPatterns.dataSourceEnabled) or get from plugin constructor; #2111 if (!canSave) { chrome.setBadge(readOnlyBadge);