Adds get all REST API to data views (#131683)

* Adds get all API to data views

* Move size to query params

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

* Use ids/titles instead of entire object

* Add docs

* Add integration test

* Fix docs

* Update docs

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Lukas Olson 2022-06-02 10:00:19 -07:00 committed by GitHub
parent b17bbdcb90
commit e921693c50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 208 additions and 2 deletions

View file

@ -11,6 +11,7 @@ WARNING: Use the data views APIs for managing data views instead of lower-level
The following data views APIs are available:
* Data views
** <<data-views-api-get-all, Get all data views API>> to retrieve a list of data views
** <<data-views-api-get, Get data view API>> to retrieve a single data view
** <<data-views-api-create, Create data view API>> to create data view
** <<data-views-api-update, Update data view API>> to partially updated data view
@ -27,6 +28,7 @@ The following data views APIs are available:
** <<data-views-runtime-field-api-update, Update runtime field API>> to partially update an existing runtime field
** <<data-views-runtime-field-api-delete, Delete runtime field API>> to delete a runtime field
include::data-views/get-all.asciidoc[]
include::data-views/get.asciidoc[]
include::data-views/create.asciidoc[]
include::data-views/update.asciidoc[]

View file

@ -0,0 +1,60 @@
[[data-views-api-get-all]]
=== Get all data views API
++++
<titleabbrev>Get all data views</titleabbrev>
++++
experimental[] Retrieve a list of all data views.
[[data-views-api-get-all-request]]
==== Request
`GET <kibana host>:<port>/api/data_views`
`GET <kibana host>:<port>/s/<space_id>/api/data_views`
[[data-views-api-get-all-codes]]
==== Response code
`200`::
Indicates a successful call.
[[data-views-api-get-all-example]]
==== Example
Retrieve the list of data views:
[source,sh]
--------------------------------------------------
$ curl -X GET api/data_views
--------------------------------------------------
// KIBANA
The API returns a list of data views:
[source,sh]
--------------------------------------------------
{
"data_view": [
{
"id": "e9e024f0-d098-11ec-bbe9-c753adcb34bc",
"namespaces": [
"default"
],
"title": "tmp*",
"type": "rollup",
"typeMeta": {}
},
{
"id": "90943e30-9a47-11e8-b64d-95841ca0b247",
"namespaces": [
"default"
],
"title": "kibana_sample_data_logs"
}
]
}
--------------------------------------------------

View file

@ -753,7 +753,6 @@ export class DataViewsService {
* Get an index pattern by id, cache optimized.
* @param id
*/
get = async (id: string): Promise<DataView> => {
const indexPatternPromise =
this.dataViewCache.get(id) || this.dataViewCache.set(id, this.getSavedObjectAndInit(id));

View file

@ -28,4 +28,5 @@ export const dataViewsService = {
getDefaultId: jest.fn(),
updateSavedObject: jest.fn(),
refreshFields: jest.fn(),
getIdsWithTitle: jest.fn(),
} as unknown as jest.Mocked<DataViewsService>;

View file

@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { getDataViews } from './get_data_views';
import { dataViewsService } from '../mocks';
import { getUsageCollection } from './test_utils';
describe('get all data views', () => {
it('call usageCollection', () => {
const usageCollection = getUsageCollection();
getDataViews({
dataViewsService,
counterName: 'GET /path',
usageCollection,
});
expect(usageCollection.incrementCounter).toBeCalledTimes(1);
});
});

View file

@ -0,0 +1,77 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { IRouter, StartServicesAccessor } from '@kbn/core/server';
import { DataViewsService } from '../../common';
import { handleErrors } from './util/handle_errors';
import type { DataViewsServerPluginStartDependencies, DataViewsServerPluginStart } from '../types';
import { SERVICE_KEY, SERVICE_PATH } from '../constants';
interface GetDataViewsArgs {
dataViewsService: DataViewsService;
usageCollection?: UsageCounter;
counterName: string;
}
export const getDataViews = async ({
dataViewsService,
usageCollection,
counterName,
}: GetDataViewsArgs) => {
usageCollection?.incrementCounter({ counterName });
return dataViewsService.getIdsWithTitle();
};
const getDataViewsRouteFactory =
(path: string, serviceKey: string) =>
(
router: IRouter,
getStartServices: StartServicesAccessor<
DataViewsServerPluginStartDependencies,
DataViewsServerPluginStart
>,
usageCollection?: UsageCounter
) => {
router.get(
{
path,
validate: {},
},
router.handleLegacyErrors(
handleErrors(async (ctx, req, res) => {
const core = await ctx.core;
const savedObjectsClient = core.savedObjects.client;
const elasticsearchClient = core.elasticsearch.client.asCurrentUser;
const [, , { dataViewsServiceFactory }] = await getStartServices();
const dataViewsService = await dataViewsServiceFactory(
savedObjectsClient,
elasticsearchClient,
req
);
const dataViews = await getDataViews({
dataViewsService,
usageCollection,
counterName: `${req.route.method} ${path}`,
});
return res.ok({
headers: {
'content-type': 'application/json',
},
body: {
[serviceKey]: dataViews,
},
});
})
)
);
};
export const registerGetDataViewsRoute = getDataViewsRouteFactory(SERVICE_PATH, SERVICE_KEY);

View file

@ -14,6 +14,7 @@ import * as createRoutes from './create_data_view';
import * as defaultRoutes from './default_data_view';
import * as deleteRoutes from './delete_data_view';
import * as getRoutes from './get_data_view';
import * as getAllRoutes from './get_data_views';
import * as hasRoutes from './has_user_data_view';
import * as updateRoutes from './update_data_view';
@ -38,6 +39,7 @@ const routes = [
deleteRoutes.registerDeleteDataViewRouteLegacy,
getRoutes.registerGetDataViewRoute,
getRoutes.registerGetDataViewRouteLegacy,
getAllRoutes.registerGetDataViewsRoute,
hasRoutes.registerHasUserDataViewRoute,
hasRoutes.registerHasUserDataViewRouteLegacy,
updateRoutes.registerUpdateDataViewRoute,

View file

@ -22,7 +22,7 @@ const legacyConfig = {
serviceKey: SERVICE_KEY_LEGACY,
};
const dataViewConfig = {
export const dataViewConfig = {
name: 'data view api',
path: DATA_VIEW_PATH,
basePath: SERVICE_PATH,

View file

@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { FtrProviderContext } from '../../../../ftr_provider_context';
export default function ({ loadTestFile }: FtrProviderContext) {
describe('get_data_views', () => {
loadTestFile(require.resolve('./main'));
});
}

View file

@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../ftr_provider_context';
import { dataViewConfig } from '../../constants';
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
describe('main', () => {
describe('get data views api', () => {
it('returns list of data views', async () => {
const response = await supertest.get(dataViewConfig.basePath);
expect(response.status).to.be(200);
expect(response.body).to.have.property(dataViewConfig.serviceKey);
expect(response.body[dataViewConfig.serviceKey]).to.be.an('array');
});
});
});
}

View file

@ -14,5 +14,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./get_index_pattern'));
loadTestFile(require.resolve('./delete_index_pattern'));
loadTestFile(require.resolve('./update_index_pattern'));
loadTestFile(require.resolve('./get_data_views'));
});
}