Skip to content

Commit

Permalink
moving indexPattern.delete() to indexPatterns.delete(indexPattern) (e…
Browse files Browse the repository at this point in the history
…lastic#70430)

# Conflicts:
#	src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts
  • Loading branch information
ppisljar committed Jul 7, 2020
1 parent b6aaf8a commit a49e83b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 51 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export declare class IndexPattern implements IIndexPattern
| [\_fetchFields()](./kibana-plugin-plugins-data-public.indexpattern._fetchfields.md) | | |
| [addScriptedField(name, script, fieldType, lang)](./kibana-plugin-plugins-data-public.indexpattern.addscriptedfield.md) | | |
| [create(allowOverride)](./kibana-plugin-plugins-data-public.indexpattern.create.md) | | |
| [destroy()](./kibana-plugin-plugins-data-public.indexpattern.destroy.md) | | |
| [getAggregationRestrictions()](./kibana-plugin-plugins-data-public.indexpattern.getaggregationrestrictions.md) | | |
| [getComputedFields()](./kibana-plugin-plugins-data-public.indexpattern.getcomputedfields.md) | | |
| [getFieldByName(name)](./kibana-plugin-plugins-data-public.indexpattern.getfieldbyname.md) | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class IndexPattern implements IIndexPattern {
this.sourceFilters = spec.sourceFilters;

// ignoring this because the same thing happens elsewhere but via _.assign
// @ts-ignore
// @ts-expect-error
this.fields = spec.fields || [];
this.typeMeta = spec.typeMeta;
this.fieldFormatMap = _.mapValues(fieldFormatMap, (mapping) => {
Expand Down Expand Up @@ -531,22 +531,8 @@ export class IndexPattern implements IIndexPattern {
async create(allowOverride: boolean = false) {
const _create = async (duplicateId?: string) => {
if (duplicateId) {
const duplicatePattern = new IndexPattern(duplicateId, {
getConfig: this.getConfig,
savedObjectsClient: this.savedObjectsClient,
apiClient: this.apiClient,
patternCache: this.patternCache,
fieldFormats: this.fieldFormats,
onNotification: this.onNotification,
onError: this.onError,
onUnsupportedTimePattern: this.onUnsupportedTimePattern,
uiSettingsValues: {
shortDotsEnable: this.shortDotsEnable,
metaFields: this.metaFields,
},
});

await duplicatePattern.destroy();
this.patternCache.clear(duplicateId);
await this.savedObjectsClient.delete(savedObjectType, duplicateId);
}

const body = this.prepBody();
Expand Down Expand Up @@ -694,11 +680,4 @@ export class IndexPattern implements IIndexPattern {
toString() {
return '' + this.toJSON();
}

destroy() {
if (this.id) {
this.patternCache.clear(this.id);
return this.savedObjectsClient.delete(savedObjectType, this.id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('IndexPatterns', () => {
Array<SavedObject<any>>
>
);
savedObjectsClient.delete = jest.fn(() => Promise.resolve({}) as Promise<any>);

indexPatterns = new IndexPatternsService({
uiSettings: ({
Expand Down Expand Up @@ -99,4 +100,13 @@ describe('IndexPatterns', () => {
await indexPatterns.getFields(['id', 'title'], true);
expect(savedObjectsClient.find).toHaveBeenCalledTimes(3);
});

test('deletes the index pattern', async () => {
const id = '1';
const indexPattern = await indexPatterns.get(id);

expect(indexPattern).toBeDefined();
await indexPatterns.delete(id);
expect(indexPattern).not.toBe(await indexPatterns.get(id));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ export class IndexPatternsService {

return indexPattern.init();
}

/**
* Deletes an index pattern from .kibana index
* @param indexPatternId: Id of kibana Index Pattern to delete
*/
async delete(indexPatternId: string) {
indexPatternCache.clear(indexPatternId);
return this.savedObjectsClient.delete('index-pattern', indexPatternId);
}
}

export type IndexPatternsContract = PublicMethodsOf<IndexPatternsService>;
2 changes: 0 additions & 2 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,6 @@ export class IndexPattern implements IIndexPattern {
// (undocumented)
create(allowOverride?: boolean): Promise<string | false>;
// (undocumented)
destroy(): Promise<{}> | undefined;
// (undocumented)
_fetchFields(): Promise<void>;
// (undocumented)
fieldFormatMap: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ const confirmModalOptionsDelete = {

export const EditIndexPattern = withRouter(
({ indexPattern, history, location }: EditIndexPatternProps) => {
const { uiSettings, indexPatternManagementStart, overlays, savedObjects, chrome } = useKibana<
IndexPatternManagmentContext
>().services;
const {
uiSettings,
indexPatternManagementStart,
overlays,
savedObjects,
chrome,
data,
} = useKibana<IndexPatternManagmentContext>().services;
const [fields, setFields] = useState<IndexPatternField[]>(indexPattern.getNonScriptedFields());
const [conflictedFields, setConflictedFields] = useState<IndexPatternField[]>(
indexPattern.fields.filter((field) => field.type === 'conflict')
Expand Down Expand Up @@ -138,10 +143,11 @@ export const EditIndexPattern = withRouter(
uiSettings.set('defaultIndex', otherPatterns[0].id);
}
}

Promise.resolve(indexPattern.destroy()).then(function () {
history.push('');
});
if (indexPattern.id) {
Promise.resolve(data.indexPatterns.delete(indexPattern.id)).then(function () {
history.push('');
});
}
}

overlays.openConfirm('', confirmModalOptionsDelete).then((isConfirmed) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export class IndexPatternsTestPlugin
const [, { data }] = await core.getStartServices();
const id = (req.params as Record<string, string>).id;
const service = await data.indexPatterns.indexPatternsServiceFactory(req);
const ip = await service.get(id);
await ip.destroy();
await service.delete(id);
return res.ok();
}
);
Expand Down

0 comments on commit a49e83b

Please sign in to comment.