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

migrate savedObjects routes to core #56734

Merged
merged 41 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
d69bfaa
migrate `get` route
pgayvallet Feb 4, 2020
97137df
migrate `create` route
pgayvallet Feb 4, 2020
50ae2b8
migrate `delete` route
pgayvallet Feb 4, 2020
7760136
migrate `find` route
pgayvallet Feb 5, 2020
3636265
migrate `update` route
pgayvallet Feb 5, 2020
ac30dad
migrate `bulk_get` route
pgayvallet Feb 5, 2020
77d1e8a
migrate `bulk_create` route
pgayvallet Feb 5, 2020
e16802a
remove route-related mixin tests
pgayvallet Feb 5, 2020
2a699c4
migrate `bulk_update` route
pgayvallet Feb 5, 2020
22f8ba7
fix expectTypeRequired assertion
pgayvallet Feb 5, 2020
e617d7e
migrate `log_legacy_imports` route
pgayvallet Feb 5, 2020
b05883c
migrate `export` route
pgayvallet Feb 5, 2020
9b18fdd
fix karma tests
pgayvallet Feb 5, 2020
38e25a0
array is better than object in some situations.
pgayvallet Feb 5, 2020
cac5e14
remove prototype pollution tests
pgayvallet Feb 5, 2020
3f9992f
adapt ftr assertions
pgayvallet Feb 5, 2020
03bb316
adapt ftr assertions
pgayvallet Feb 5, 2020
27bd062
adapt yet more ftr assertions
pgayvallet Feb 5, 2020
d517045
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 6, 2020
5671fc6
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 6, 2020
ad0fcd1
migrate `import` route
pgayvallet Feb 6, 2020
3ac2bc1
fix test tests
pgayvallet Feb 7, 2020
4d72c14
fix getSortedObjectsForExport usages
pgayvallet Feb 7, 2020
234c1d7
fix snapshots
pgayvallet Feb 7, 2020
6d25398
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 10, 2020
e2134b0
fix so ui exports usages due to merge
pgayvallet Feb 10, 2020
6d57bf9
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 11, 2020
ad6233b
create router with prefix
pgayvallet Feb 11, 2020
ca1a90c
creates `savedObjects` namespace config in addition to `migrations`
pgayvallet Feb 11, 2020
db4d79f
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 11, 2020
cfbde19
migrate `resolve_import_errors` route
pgayvallet Feb 11, 2020
459334d
remove old types file
pgayvallet Feb 11, 2020
d3c019e
fix FTR assertion
pgayvallet Feb 11, 2020
84ad5ce
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 13, 2020
4e97843
remove types parameter from copy_to_space
pgayvallet Feb 13, 2020
16a7599
Merge remote-tracking branch 'upstream/master' into kbn-50321-migrate…
pgayvallet Feb 14, 2020
178ea49
move route tests to integration_tests
pgayvallet Feb 14, 2020
56fc21f
use byteSize instead of number
pgayvallet Feb 14, 2020
d25b471
fix unit tests
pgayvallet Feb 14, 2020
6ff7d55
Merge branch 'master' into pr/56734
mshustov Feb 18, 2020
aaae92d
add has_reference query parameter
mshustov Feb 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/core/server/http/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ const defaultContext: CoreContext = {
configService,
};

export const createCoreContext = (overrides: Partial<CoreContext> = {}): CoreContext => ({
...defaultContext,
...overrides,
});

/**
* Creates a concrete HttpServer with a mocked context.
*/
export const createHttpServer = (overrides: Partial<CoreContext> = {}): HttpService => {
const context = {
...defaultContext,
...overrides,
};
return new HttpService(context);
return new HttpService(createCoreContext(overrides));
};
23 changes: 22 additions & 1 deletion src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.
import { httpServiceMock } from './http/http_service.mock';
import { contextServiceMock } from './context/context_service.mock';
import { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock';
import { savedObjectsClientMock } from './saved_objects/service/saved_objects_client.mock';
import { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
import { SharedGlobalConfig } from './plugins';
import { InternalCoreSetup, InternalCoreStart } from './internal_types';
Expand All @@ -36,7 +37,6 @@ export { configServiceMock } from './config/config_service.mock';
export { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
export { httpServiceMock } from './http/http_service.mock';
export { loggingServiceMock } from './logging/logging_service.mock';
export { savedObjectsClientMock } from './saved_objects/service/saved_objects_client.mock';
export { savedObjectsRepositoryMock } from './saved_objects/service/lib/repository.mock';
export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
import { uuidServiceMock } from './uuid/uuid_service.mock';
Expand Down Expand Up @@ -160,10 +160,31 @@ function createInternalCoreStartMock() {
return startDeps;
}

function createCoreRequestHandlerContextMock() {
return {
rendering: {
render: jest.fn(),
},
savedObjects: {
client: savedObjectsClientMock.create(),
},
elasticsearch: {
adminClient: elasticsearchServiceMock.createScopedClusterClient(),
dataClient: elasticsearchServiceMock.createScopedClusterClient(),
},
uiSettings: {
client: uiSettingsServiceMock.createClient(),
},
};
}

export const coreMock = {
createSetup: createCoreSetupMock,
createStart: createCoreStartMock,
createInternalSetup: createInternalCoreSetupMock,
createInternalStart: createInternalCoreStartMock,
createPluginInitializerContext: pluginInitializerContextMock,
createRequestHandlerContext: createCoreRequestHandlerContextMock,
};

export { savedObjectsClientMock };
92 changes: 92 additions & 0 deletions src/core/server/saved_objects/routes/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import supertest from 'supertest';
import { registerGetRoute } from './get';
import { ContextService } from '../../context';
import { savedObjectsClientMock } from '../service/saved_objects_client.mock';
import { HttpService, InternalHttpServiceSetup } from '../../http';
import { createHttpServer, createCoreContext } from '../../http/test_utils';
import { coreMock } from '../../mocks';

const coreId = Symbol('core');

describe('GET /api/saved_objects/{type}/{id}', () => {
let server: HttpService;
let httpSetup: InternalHttpServiceSetup;
let handlerContext: ReturnType<typeof coreMock.createRequestHandlerContext>;
let savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;

beforeEach(async () => {
const coreContext = createCoreContext({ coreId });
server = createHttpServer(coreContext);

const contextService = new ContextService(coreContext);
httpSetup = await server.setup({
context: contextService.setup({ pluginDependencies: new Map() }),
});

handlerContext = coreMock.createRequestHandlerContext();
savedObjectsClient = handlerContext.savedObjects.client;

httpSetup.registerRouteHandlerContext(coreId, 'core', async (ctx, req, res) => {
return handlerContext;
});

const router = httpSetup.createRouter('');
registerGetRoute(router);

await server.start();
});
pgayvallet marked this conversation as resolved.
Show resolved Hide resolved

afterEach(async () => {
await server.stop();
});

it('formats successful response', async () => {
const clientResponse = {
id: 'logstash-*',
title: 'logstash-*',
type: 'logstash-type',
attributes: {},
timeFieldName: '@timestamp',
notExpandable: true,
references: [],
};

savedObjectsClient.get.mockResolvedValue(clientResponse);

const result = await supertest(httpSetup.server.listener)
.get('/api/saved_objects/index-pattern/logstash-*')
.expect(200);

expect(result.body).toEqual(clientResponse);
});

it('calls upon savedObjectClient.get', async () => {
await supertest(httpSetup.server.listener)
.get('/api/saved_objects/index-pattern/logstash-*')
.expect(200);

expect(savedObjectsClient.get).toHaveBeenCalled();

const args = savedObjectsClient.get.mock.calls[0];
expect(args).toEqual(['index-pattern', 'logstash-*']);
});
});
40 changes: 40 additions & 0 deletions src/core/server/saved_objects/routes/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { schema } from '@kbn/config-schema';
import { IRouter } from '../../http';

export const registerGetRoute = (router: IRouter) => {
router.get(
{
path: '/api/saved_objects/{type}/{id}',
validate: {
params: schema.object({
type: schema.string(),
id: schema.string(),
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { type, id } = req.params;
const savedObject = await context.core.savedObjects.client.get(type, id);
return res.ok({ body: savedObject });
})
);
};
26 changes: 26 additions & 0 deletions src/core/server/saved_objects/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { InternalHttpServiceSetup } from '../../http';
import { registerGetRoute } from './get';

export function registerRoutes(http: InternalHttpServiceSetup) {
const router = http.createRouter('');
registerGetRoute(router);
}
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/saved_objects_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Env } from '../config';
import { configServiceMock } from '../mocks';
import { elasticsearchServiceMock } from '../elasticsearch/elasticsearch_service.mock';
import { legacyServiceMock } from '../legacy/legacy_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { SavedObjectsClientFactoryProvider } from './service/lib';
import { BehaviorSubject } from 'rxjs';
import { NodesVersionCompatibility } from '../elasticsearch/version_check/ensure_es_version';
Expand All @@ -38,6 +39,7 @@ describe('SavedObjectsService', () => {
const createSetupDeps = () => {
const elasticsearchMock = elasticsearchServiceMock.createInternalSetup();
return {
http: httpServiceMock.createSetupContract(),
elasticsearch: elasticsearchMock,
legacyPlugins: legacyServiceMock.createDiscoverPlugins(),
};
Expand Down
6 changes: 5 additions & 1 deletion src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { InternalElasticsearchServiceSetup, APICaller } from '../elasticsearch';
import { KibanaConfigType } from '../kibana_config';
import { migrationsRetryCallCluster } from '../elasticsearch/retry_call_cluster';
import { SavedObjectsConfigType } from './saved_objects_config';
import { KibanaRequest } from '../http';
import { InternalHttpServiceSetup, KibanaRequest } from '../http';
import { SavedObjectsClientContract } from './types';
import { ISavedObjectsRepository, SavedObjectsRepository } from './service/lib/repository';
import {
Expand All @@ -45,6 +45,7 @@ import { SavedObjectsMapping } from './mappings';
import { MigrationDefinition } from './migrations/core/document_migrator';
import { SavedObjectsSchemaDefinition } from './schema';
import { PropertyValidators } from './validation';
import { registerRoutes } from './routes';

/**
* Saved Objects is Kibana's data persistence mechanism allowing plugins to
Expand Down Expand Up @@ -187,6 +188,7 @@ export interface SavedObjectsRepositoryFactory {
export interface SavedObjectsSetupDeps {
legacyPlugins: LegacyServiceDiscoverPlugins;
elasticsearch: InternalElasticsearchServiceSetup;
http: InternalHttpServiceSetup;
}

interface WrappedClientFactoryWrapper {
Expand Down Expand Up @@ -232,6 +234,8 @@ export class SavedObjectsService
this.schemas = savedObjectsSchemasDefinition;
this.validations = savedObjectValidations;

registerRoutes(setupDeps.http);

return {
setClientFactoryProvider: provider => {
if (this.clientFactoryProvider) {
Expand Down
1 change: 1 addition & 0 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class Server {
});

const savedObjectsSetup = await this.savedObjects.setup({
http: httpSetup,
elasticsearch: elasticsearchServiceSetup,
legacyPlugins,
});
Expand Down
95 changes: 0 additions & 95 deletions src/legacy/server/saved_objects/routes/get.test.ts

This file was deleted.

Loading