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

[data views] Fix overwrite param for create #160953

Merged
merged 12 commits into from
Jul 4, 2023
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 @@ -297,7 +297,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,
kertal marked this conversation as resolved.
Show resolved Hide resolved
[config.serviceKey]: {
title,
fields: {
Expand Down