From 6b0636ef0c10747d5f8965ef7eed565f026ca5b2 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Thu, 29 Jun 2023 20:03:15 -0500 Subject: [PATCH 1/5] pass overwrite to saved object client --- .../data_views/common/content_management/v1/types.ts | 1 + src/plugins/data_views/common/data_views/data_views.ts | 9 +++------ src/plugins/data_views/common/types.ts | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/plugins/data_views/common/content_management/v1/types.ts b/src/plugins/data_views/common/content_management/v1/types.ts index 1bada30b8dd94..19637bb17e8c5 100644 --- a/src/plugins/data_views/common/content_management/v1/types.ts +++ b/src/plugins/data_views/common/content_management/v1/types.ts @@ -18,6 +18,7 @@ import { DataViewContentType } from './constants'; interface DataViewCreateOptions { id?: SavedObjectCreateOptions['id']; initialNamespaces?: SavedObjectCreateOptions['initialNamespaces']; + overwrite?: SavedObjectCreateOptions['overwrite']; } interface DataViewUpdateOptions { diff --git a/src/plugins/data_views/common/data_views/data_views.ts b/src/plugins/data_views/common/data_views/data_views.ts index 947e8884bc7cc..5618ad88abf4d 100644 --- a/src/plugins/data_views/common/data_views/data_views.ts +++ b/src/plugins/data_views/common/data_views/data_views.ts @@ -987,12 +987,8 @@ export class DataViewsService { } const dupe = await findByName(this.savedObjectsClient, dataView.getName()); - if (dupe) { - if (override) { - await this.delete(dupe.id); - } else { - throw new DuplicateDataViewError(`Duplicate data view: ${dataView.getName()}`); - } + if (dupe && !override) { + throw new DuplicateDataViewError(`Duplicate data view: ${dataView.getName()}`); } const body = dataView.getAsSavedObjectBody(); @@ -1000,6 +996,7 @@ export class DataViewsService { const response: SavedObject = (await this.savedObjectsClient.create(body, { id: dataView.id, initialNamespaces: dataView.namespaces.length > 0 ? dataView.namespaces : undefined, + overwrite: override, })) as SavedObject; const createdIndexPattern = await this.initFromSavedObject(response, displayErrors); diff --git a/src/plugins/data_views/common/types.ts b/src/plugins/data_views/common/types.ts index 6c42664c90ffb..aa157d5336685 100644 --- a/src/plugins/data_views/common/types.ts +++ b/src/plugins/data_views/common/types.ts @@ -297,7 +297,7 @@ export interface SavedObjectsClientCommon { create: ( attributes: DataViewAttributes, // SavedObjectsCreateOptions - options: { id?: string; initialNamespaces?: string[] } + options: { id?: string; initialNamespaces?: string[]; overwrite?: boolean } ) => Promise; /** * Delete a saved object by id From 820a6cdb6b593290e60fff76cbd2f34d19f5d828 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Thu, 29 Jun 2023 21:26:20 -0500 Subject: [PATCH 2/5] add schema for overwrite --- .../data_views/common/content_management/v1/cm_services.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/data_views/common/content_management/v1/cm_services.ts b/src/plugins/data_views/common/content_management/v1/cm_services.ts index 7ff2d135a9d90..e425a40cce433 100644 --- a/src/plugins/data_views/common/content_management/v1/cm_services.ts +++ b/src/plugins/data_views/common/content_management/v1/cm_services.ts @@ -54,6 +54,7 @@ const dataViewSavedObjectSchema = savedObjectSchema(dataViewAttributesSchema); const dataViewCreateOptionsSchema = schema.object({ id: createOptionsSchemas.id, initialNamespaces: createOptionsSchemas.initialNamespaces, + overwrite: schema.maybe(createOptionsSchemas.overwrite), }); const dataViewSearchOptionsSchema = schema.object({ From 3fc8c3b29ef8a5fb8234a821a1f527027d4dc94f Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Thu, 29 Jun 2023 23:00:05 -0500 Subject: [PATCH 3/5] supply override --- .../apis/data_views/fields_api/update_fields/main.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/api_integration/apis/data_views/fields_api/update_fields/main.ts b/test/api_integration/apis/data_views/fields_api/update_fields/main.ts index d48dd90396e16..40c03bbec46c1 100644 --- a/test/api_integration/apis/data_views/fields_api/update_fields/main.ts +++ b/test/api_integration/apis/data_views/fields_api/update_fields/main.ts @@ -437,6 +437,7 @@ export default function ({ getService }: FtrProviderContext) { const title = indexPattern.title; await supertest.delete(`${config.path}/${indexPattern.id}`); const response1 = await supertest.post(config.path).send({ + override: true, [config.serviceKey]: { title, fields: { From 2123867ae979c9edf65a84e8ba11da3063d74aeb Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Fri, 30 Jun 2023 10:29:05 -0500 Subject: [PATCH 4/5] restore delete on dupe name --- src/plugins/data_views/common/data_views/data_views.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/plugins/data_views/common/data_views/data_views.ts b/src/plugins/data_views/common/data_views/data_views.ts index 5618ad88abf4d..31ca2497a6690 100644 --- a/src/plugins/data_views/common/data_views/data_views.ts +++ b/src/plugins/data_views/common/data_views/data_views.ts @@ -987,8 +987,12 @@ export class DataViewsService { } const dupe = await findByName(this.savedObjectsClient, dataView.getName()); - if (dupe && !override) { - throw new DuplicateDataViewError(`Duplicate data view: ${dataView.getName()}`); + if (dupe) { + if (override) { + await this.delete(dupe.id); + } else { + throw new DuplicateDataViewError(`Duplicate data view: ${dataView.getName()}`); + } } const body = dataView.getAsSavedObjectBody(); From 425377b8b5861a602dfefd13a271b9de163b8a13 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 3 Jul 2023 11:24:20 -0500 Subject: [PATCH 5/5] overwrite instead of override --- .../data_views/common/data_views/data_views.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/plugins/data_views/common/data_views/data_views.ts b/src/plugins/data_views/common/data_views/data_views.ts index 31ca2497a6690..f51189f2409ea 100644 --- a/src/plugins/data_views/common/data_views/data_views.ts +++ b/src/plugins/data_views/common/data_views/data_views.ts @@ -167,7 +167,7 @@ export interface DataViewsServicePublicMethods { */ createSavedObject: ( indexPattern: DataView, - override?: boolean, + overwrite?: boolean, displayErrors?: boolean ) => Promise; /** @@ -964,12 +964,16 @@ export class DataViewsService { async createAndSave( spec: DataViewSpec, - override = false, + overwrite = false, skipFetchFields = false, displayErrors = true ) { const indexPattern = await this.createFromSpec(spec, skipFetchFields, displayErrors); - const createdIndexPattern = await this.createSavedObject(indexPattern, override, displayErrors); + const createdIndexPattern = await this.createSavedObject( + indexPattern, + overwrite, + displayErrors + ); await this.setDefault(createdIndexPattern.id!); return createdIndexPattern!; } @@ -981,14 +985,14 @@ export class DataViewsService { * @param displayErrors - If set false, API consumer is responsible for displaying and handling errors. */ - async createSavedObject(dataView: DataView, override = false, displayErrors = true) { + async createSavedObject(dataView: DataView, overwrite = false, displayErrors = true) { if (!(await this.getCanSave())) { throw new DataViewInsufficientAccessError(); } const dupe = await findByName(this.savedObjectsClient, dataView.getName()); if (dupe) { - if (override) { + if (overwrite) { await this.delete(dupe.id); } else { throw new DuplicateDataViewError(`Duplicate data view: ${dataView.getName()}`); @@ -1000,7 +1004,7 @@ export class DataViewsService { const response: SavedObject = (await this.savedObjectsClient.create(body, { id: dataView.id, initialNamespaces: dataView.namespaces.length > 0 ? dataView.namespaces : undefined, - overwrite: override, + overwrite, })) as SavedObject; const createdIndexPattern = await this.initFromSavedObject(response, displayErrors);