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

Adds get all REST API to data views #131683

Merged
merged 12 commits into from
Jun 2, 2022
2 changes: 2 additions & 0 deletions docs/api/data-views.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[]
Expand Down
59 changes: 59 additions & 0 deletions docs/api/data-views/get-all.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[[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_views": [
{
"id": "e9e024f0-d098-11ec-bbe9-c753adcb34bc",
"namespaces": [
"default"
],
"title": "tmp*",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also return the type if not default. add "type": "rollup" to make this a rollup data view example

"typeMeta": {}
},
{
"id": "90943e30-9a47-11e8-b64d-95841ca0b247",
"namespaces": [
"default"
],
"title": "kibana_sample_data_logs"
}
]
}
--------------------------------------------------
1 change: 0 additions & 1 deletion src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,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));
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_views/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SPECIFIC_SCRIPTED_FIELD_PATH_LEGACY = `${SCRIPTED_FIELD_PATH_LEGACY

export const SERVICE_KEY = 'data_view';
export const SERVICE_KEY_LEGACY = 'index_pattern';
export const SERVICE_KEY_MULTIPLE = 'data_views';
export type SERVICE_KEY_TYPE = typeof SERVICE_KEY | typeof SERVICE_KEY_LEGACY;

export const CREATE_DATA_VIEW_COUNTER_NAME = `POST ${DATA_VIEW_PATH}`;
1 change: 1 addition & 0 deletions src/plugins/data_views/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export const dataViewsService = {
getDefaultId: jest.fn(),
updateSavedObject: jest.fn(),
refreshFields: jest.fn(),
getIdsWithTitle: jest.fn(),
} as unknown as jest.Mocked<DataViewsService>;
Original file line number Diff line number Diff line change
@@ -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);
});
});
80 changes: 80 additions & 0 deletions src/plugins/data_views/server/rest_api_routes/get_data_views.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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_PATH, SERVICE_KEY_MULTIPLE } 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_MULTIPLE
);
2 changes: 2 additions & 0 deletions src/plugins/data_views/server/rest_api_routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -38,6 +39,7 @@ const routes = [
deleteRoutes.registerDeleteDataViewRouteLegacy,
getRoutes.registerGetDataViewRoute,
getRoutes.registerGetDataViewRouteLegacy,
getAllRoutes.registerGetDataViewsRoute,
hasRoutes.registerHasUserDataViewRoute,
hasRoutes.registerHasUserDataViewRouteLegacy,
updateRoutes.registerUpdateDataViewRoute,
Expand Down