Skip to content

Commit

Permalink
[data views] Fix overwrite param for create (#160953)
Browse files Browse the repository at this point in the history
## Summary

Under some circumstances passing `override` to `POST
/api/data_views/data_view` would fail. Its now fixed.

To test - Try using the override param from the Kibana dev console. I
found it reproduced the problem before the fix and shows its resolved
after the fix. The problem did not appear in the integration tests.

I suspect the problem had to do with how quickly the delete was
performed - if it completed before the create command then everything
was fine. If it didn't then the error would appear. Passing the
overwrite param to the saved object client eliminates the possibility of
the delete failing to complete.

Closes #161016

(cherry picked from commit 803d139)
  • Loading branch information
mattkime committed Jul 4, 2023
1 parent 4af53fe commit 253db7d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DataViewContentType } from './constants';
interface DataViewCreateOptions {
id?: SavedObjectCreateOptions['id'];
initialNamespaces?: SavedObjectCreateOptions['initialNamespaces'];
overwrite?: SavedObjectCreateOptions['overwrite'];
}

interface DataViewUpdateOptions {
Expand Down
15 changes: 10 additions & 5 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export interface DataViewsServicePublicMethods {
*/
createSavedObject: (
indexPattern: DataView,
override?: boolean,
overwrite?: boolean,
displayErrors?: boolean
) => Promise<DataView>;
/**
Expand Down Expand Up @@ -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!;
}
Expand All @@ -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()}`);
Expand All @@ -1000,6 +1004,7 @@ export class DataViewsService {
const response: SavedObject<DataViewAttributes> = (await this.savedObjectsClient.create(body, {
id: dataView.id,
initialNamespaces: dataView.namespaces.length > 0 ? dataView.namespaces : undefined,
overwrite,
})) as SavedObject<DataViewAttributes>;

const createdIndexPattern = await this.initFromSavedObject(response, displayErrors);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data_views/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export interface SavedObjectsClientCommon {
create: (
attributes: DataViewAttributes,
// SavedObjectsCreateOptions
options: { id?: string; initialNamespaces?: string[] }
options: { id?: string; initialNamespaces?: string[]; overwrite?: boolean }
) => Promise<SavedObject>;
/**
* Delete a saved object by id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 253db7d

Please sign in to comment.