Expose 'getSavedObjectsRepository' from Saved Objects Service (#19677)

* Expose 'getSavedObjectsRepository' from Saved Objects Service

* remove superfluous config from SavedObjectsClientProvider
This commit is contained in:
Larry Gregory 2018-06-14 10:24:02 -04:00 committed by GitHub
parent 06bd2d463e
commit 27439900be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 128 additions and 39 deletions

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { SavedObjectsRepository, ScopedSavedObjectsClientProvider } from './lib';
import { SavedObjectsRepository, ScopedSavedObjectsClientProvider, SavedObjectsRepositoryProvider } from './lib';
import { SavedObjectsClient } from './saved_objects_client';
export function createSavedObjectsService(server) {
@ -58,25 +58,23 @@ export function createSavedObjectsService(server) {
}
};
const repositoryProvider = new SavedObjectsRepositoryProvider({
index: server.config().get('kibana.index'),
mappings: server.getKibanaIndexMappingsDsl(),
onBeforeWrite,
});
const scopedClientProvider = new ScopedSavedObjectsClientProvider({
index: server.config().get('kibana.index'),
mappings: server.getKibanaIndexMappingsDsl(),
onBeforeWrite,
defaultClientFactory({
request,
index,
mappings,
onBeforeWrite
}) {
const { callWithRequest } = server.plugins.elasticsearch.getCluster('admin');
const callCluster = (...args) => callWithRequest(request, ...args);
const repository = new SavedObjectsRepository({
index,
mappings,
onBeforeWrite,
callCluster
});
const repository = repositoryProvider.getRepository(callCluster);
return new SavedObjectsClient(repository);
}
@ -85,6 +83,8 @@ export function createSavedObjectsService(server) {
return {
SavedObjectsClient,
SavedObjectsRepository,
getSavedObjectsRepository: (...args) =>
repositoryProvider.getRepository(...args),
getScopedSavedObjectsClient: (...args) =>
scopedClientProvider.getClient(...args),
setScopedSavedObjectsClientFactory: (...args) =>

View file

@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`requires "callCluster" to be provided 1`] = `"Repository requires a \\"callCluster\\" function to be provided."`;

View file

@ -19,6 +19,7 @@
export { SavedObjectsRepository } from './repository';
export { ScopedSavedObjectsClientProvider } from './scoped_client_provider';
export { SavedObjectsRepositoryProvider } from './repository_provider';
import * as errors from './errors';
export { errors };

View file

@ -0,0 +1,50 @@
/*
* 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 { SavedObjectsRepository } from './repository';
/**
* Provider for the Saved Object Reppository.
*/
export class SavedObjectsRepositoryProvider {
constructor({
index,
mappings,
onBeforeWrite
}) {
this._index = index;
this._mappings = mappings;
this._onBeforeWrite = onBeforeWrite;
}
getRepository(callCluster) {
if (typeof callCluster !== 'function') {
throw new TypeError('Repository requires a "callCluster" function to be provided.');
}
return new SavedObjectsRepository({
index: this._index,
mappings: this._mappings,
onBeforeWrite: this._onBeforeWrite,
callCluster
});
}
}

View file

@ -0,0 +1,62 @@
/*
* 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 { SavedObjectsRepositoryProvider } from './repository_provider';
test('requires "callCluster" to be provided', () => {
const provider = new SavedObjectsRepositoryProvider({
index: 'idx',
mappings: {
foo: {}
},
onBeforeWrite: jest.fn()
});
expect(() => provider.getRepository({})).toThrowErrorMatchingSnapshot();
});
test('creates a valid Repository', async () => {
const properties = {
index: 'default-index',
mappings: {
foo: {
properties: {
field: { type: 'string' }
}
}
},
onBeforeWrite: jest.fn()
};
const provider = new SavedObjectsRepositoryProvider(properties);
const callCluster = jest.fn().mockReturnValue({
_id: 'new'
});
const repository = provider.getRepository(callCluster);
await repository.create('foo', {});
expect(callCluster).toHaveBeenCalledTimes(1);
expect(properties.onBeforeWrite).toHaveBeenCalledTimes(1);
expect(callCluster).toHaveBeenCalledWith('index', expect.objectContaining({
index: properties.index
}));
});

View file

@ -25,14 +25,8 @@ export class ScopedSavedObjectsClientProvider {
_wrapperFactories = [];
constructor({
index,
mappings,
onBeforeWrite,
defaultClientFactory
}) {
this._index = index;
this._mappings = mappings;
this._onBeforeWrite = onBeforeWrite;
this._originalClientFactory = this._clientFactory = defaultClientFactory;
}
@ -59,9 +53,6 @@ export class ScopedSavedObjectsClientProvider {
getClient(request) {
const client = this._clientFactory({
request,
index: this._index,
mappings: this._mappings,
onBeforeWrite: this._onBeforeWrite,
});
return this._wrapperFactories.reduce((clientToWrap, wrapperFactory) => {

View file

@ -22,15 +22,9 @@ import { ScopedSavedObjectsClientProvider } from './scoped_client_provider';
test(`uses default client factory when one isn't set`, () => {
const returnValue = Symbol();
const defaultClientFactoryMock = jest.fn().mockReturnValue(returnValue);
const index = Symbol();
const mappings = Symbol();
const onBeforeWrite = () => {};
const request = Symbol();
const clientProvider = new ScopedSavedObjectsClientProvider({
index,
mappings,
onBeforeWrite,
defaultClientFactory: defaultClientFactoryMock
});
const result = clientProvider.getClient(request);
@ -39,25 +33,16 @@ test(`uses default client factory when one isn't set`, () => {
expect(defaultClientFactoryMock).toHaveBeenCalledTimes(1);
expect(defaultClientFactoryMock).toHaveBeenCalledWith({
request,
index,
mappings,
onBeforeWrite,
});
});
test(`uses custom client factory when one is set`, () => {
const defaultClientFactoryMock = jest.fn();
const index = Symbol();
const mappings = Symbol();
const onBeforeWrite = () => {};
const request = Symbol();
const returnValue = Symbol();
const customClientFactoryMock = jest.fn().mockReturnValue(returnValue);
const clientProvider = new ScopedSavedObjectsClientProvider({
index,
mappings,
onBeforeWrite,
defaultClientFactory: defaultClientFactoryMock
});
clientProvider.setClientFactory(customClientFactoryMock);
@ -68,17 +53,14 @@ test(`uses custom client factory when one is set`, () => {
expect(customClientFactoryMock).toHaveBeenCalledTimes(1);
expect(customClientFactoryMock).toHaveBeenCalledWith({
request,
index,
mappings,
onBeforeWrite,
});
});
test(`throws error when more than one scoped saved objects client factory is set`, () => {
const clientProvider = new ScopedSavedObjectsClientProvider({});
clientProvider.setClientFactory(() => {});
clientProvider.setClientFactory(() => { });
expect(() => {
clientProvider.setClientFactory(() => {});
clientProvider.setClientFactory(() => { });
}).toThrowErrorMatchingSnapshot();
});