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

[MD][IP]Link datasource to index pattern #2118

Merged
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 @@ -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 {
Expand All @@ -67,6 +73,7 @@ interface SavedObjectBody {

type FormatFieldFn = (hit: Record<string, any>, fieldName: string) => any;

const DATA_SOURCE_REFERNECE_NAME = 'dataSource';
export class IndexPattern implements IIndexPattern {
public id?: string;
public title: string = '';
Expand All @@ -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;
Expand Down Expand Up @@ -128,6 +136,7 @@ export class IndexPattern implements IIndexPattern {
this.fieldFormatMap = _.mapValues(fieldFormatMap, (mapping) => {
return this.deserializeFieldFormatMap(mapping);
});
this.dataSourceRef = spec.dataSourceRef;
}

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export interface FieldSpec {

export type IndexPatternFieldMap = Record<string, FieldSpec>;

export interface SavedObjectReference {
name?: string;
id: string;
type: string;
}
export interface IndexPatternSpec {
id?: string;
version?: string;
Expand All @@ -194,6 +199,7 @@ export interface IndexPatternSpec {
fields?: IndexPatternFieldMap;
typeMeta?: TypeMeta;
type?: string;
dataSourceRef?: SavedObjectReference;
}

export interface SourceFilter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down