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 864c4ec2df3b3..df1a115b83923 100644 --- a/src/plugins/data_views/common/data_views/data_views.ts +++ b/src/plugins/data_views/common/data_views/data_views.ts @@ -1011,6 +1011,7 @@ export class DataViewsService { body, { id: dataView.id, + initialNamespaces: dataView.namespaces.length > 0 ? dataView.namespaces : undefined, } )) as SavedObject; diff --git a/src/plugins/data_views/common/types.ts b/src/plugins/data_views/common/types.ts index 4e2d308692afe..d44f8fd34df47 100644 --- a/src/plugins/data_views/common/types.ts +++ b/src/plugins/data_views/common/types.ts @@ -296,7 +296,7 @@ export interface SavedObjectsClientCommon { create: ( type: string, attributes: DataViewAttributes, - options: SavedObjectsCreateOptions + options: SavedObjectsCreateOptions & { initialNamespaces?: string[] } ) => Promise; /** * Delete a saved object by id diff --git a/src/plugins/data_views/server/rest_api_routes/create_data_view.ts b/src/plugins/data_views/server/rest_api_routes/create_data_view.ts index 00897c364e085..58bc36ed1c869 100644 --- a/src/plugins/data_views/server/rest_api_routes/create_data_view.ts +++ b/src/plugins/data_views/server/rest_api_routes/create_data_view.ts @@ -70,6 +70,7 @@ const dataViewSpecSchema = schema.object({ allowNoIndex: schema.maybe(schema.boolean()), runtimeFieldMap: schema.maybe(schema.recordOf(schema.string(), runtimeFieldSchema)), name: schema.maybe(schema.string()), + namespaces: schema.maybe(schema.arrayOf(schema.string())), }); const registerCreateDataViewRouteFactory = @@ -124,7 +125,10 @@ const registerCreateDataViewRouteFactory = 'content-type': 'application/json', }, body: { - [serviceKey]: dataView.toSpec(), + [serviceKey]: { + ...dataView.toSpec(), + namespaces: dataView.namespaces, + }, }, }); }) diff --git a/test/api_integration/apis/data_views/data_views_crud/create_data_view/main.ts b/test/api_integration/apis/data_views/data_views_crud/create_data_view/main.ts index 6b6a84e539da3..f93ebea4bbe3f 100644 --- a/test/api_integration/apis/data_views/data_views_crud/create_data_view/main.ts +++ b/test/api_integration/apis/data_views/data_views_crud/create_data_view/main.ts @@ -8,7 +8,7 @@ import expect from '@kbn/expect'; import { FtrProviderContext } from '../../../../ftr_provider_context'; -import { configArray } from '../../constants'; +import { configArray, dataViewConfig } from '../../constants'; export default function ({ getService }: FtrProviderContext) { const supertest = getService('supertest'); @@ -290,5 +290,59 @@ export default function ({ getService }: FtrProviderContext) { }); }); }); + + describe('spaces', () => { + const kibanaServer = getService('kibanaServer'); + const fooNamespace = 'foo-namespace'; + + before(async () => { + await kibanaServer.spaces.create({ + id: fooNamespace, + name: fooNamespace, + }); + }); + + after(async () => { + await kibanaServer.spaces.delete(fooNamespace); + }); + + it('can specify optional namespaces array when creating a data view', async () => { + const title = `foo-${Date.now()}-${Math.random()}*`; + const namespaces = ['default', fooNamespace]; + const createResponse = await supertest.post(dataViewConfig.path).send({ + [dataViewConfig.serviceKey]: { + title, + namespaces, + }, + }); + + expect(createResponse.status).to.be(200); + expect(createResponse.body[dataViewConfig.serviceKey].namespaces).to.eql(namespaces); + + const getResponse = await supertest.get(dataViewConfig.basePath); + const dataView = getResponse.body.data_view.find((dv: any) => dv.title === title); + + expect(dataView.namespaces).to.eql(namespaces); + }); + + it('sets namespaces to the current space if namespaces array is not specified', async () => { + const title = `foo-${Date.now()}-${Math.random()}*`; + const createResponse = await supertest + .post(`/s/${fooNamespace}${dataViewConfig.path}`) + .send({ + [dataViewConfig.serviceKey]: { + title, + }, + }); + + expect(createResponse.status).to.be(200); + expect(createResponse.body[dataViewConfig.serviceKey].namespaces).to.eql([fooNamespace]); + + const getResponse = await supertest.get(`/s/${fooNamespace}${dataViewConfig.basePath}`); + const dataView = getResponse.body.data_view.find((dv: any) => dv.title === title); + + expect(dataView.namespaces).to.eql([fooNamespace]); + }); + }); }); } diff --git a/test/api_integration/apis/data_views/integration/integration.ts b/test/api_integration/apis/data_views/integration/integration.ts index 22f0755373323..5c6faa292d928 100644 --- a/test/api_integration/apis/data_views/integration/integration.ts +++ b/test/api_integration/apis/data_views/integration/integration.ts @@ -111,7 +111,7 @@ export default function ({ getService }: FtrProviderContext) { expect(response6.status).to.be(200); const recreatedIndexPattern = response6.body.index_pattern; - expect(_.omit(recreatedIndexPattern, 'version')).to.eql( + expect(_.omit(recreatedIndexPattern, 'version', 'namespaces')).to.eql( _.omit(resultIndexPattern, 'version') ); });