mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
Uses SavedObjectsClient for UI Settings (#12747)
This commit is contained in:
parent
42fe210f20
commit
44dedd6091
9 changed files with 59 additions and 92 deletions
|
@ -26,6 +26,14 @@ export function savedObjectsMixin(kbnServer, server) {
|
|||
server.route(createGetRoute(prereqs));
|
||||
server.route(createUpdateRoute(prereqs));
|
||||
|
||||
server.decorate('server', 'savedObjectsClientFactory', ({ callCluster }) => {
|
||||
return new SavedObjectsClient(
|
||||
server.config().get('kibana.index'),
|
||||
kbnServer.uiExports.mappings.getCombined(),
|
||||
callCluster
|
||||
);
|
||||
});
|
||||
|
||||
const savedObjectsClientCache = new WeakMap();
|
||||
server.decorate('request', 'getSavedObjectsClient', function () {
|
||||
const request = this;
|
||||
|
@ -35,12 +43,9 @@ export function savedObjectsMixin(kbnServer, server) {
|
|||
}
|
||||
|
||||
const { callWithRequest } = server.plugins.elasticsearch.getCluster('admin');
|
||||
const callAdminCluster = (...args) => callWithRequest(request, ...args);
|
||||
const savedObjectsClient = new SavedObjectsClient(
|
||||
server.config().get('kibana.index'),
|
||||
kbnServer.uiExports.mappings.getCombined(),
|
||||
callAdminCluster
|
||||
);
|
||||
const callCluster = (...args) => callWithRequest(request, ...args);
|
||||
const savedObjectsClient = server.savedObjectsClientFactory({ callCluster });
|
||||
|
||||
savedObjectsClientCache.set(request, savedObjectsClient);
|
||||
return savedObjectsClient;
|
||||
});
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
import sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
|
||||
export function createCallClusterStub(index, type, id, esDocSource) {
|
||||
const callCluster = sinon.spy(async (method, params) => {
|
||||
expect(params)
|
||||
.to.have.property('index', index)
|
||||
.and.to.have.property('type', type)
|
||||
.and.to.have.property('id', id);
|
||||
|
||||
switch (method) {
|
||||
case 'get':
|
||||
return { _source: { ...esDocSource } };
|
||||
|
||||
case 'update':
|
||||
expect(params).to.have.property('body');
|
||||
expect(params.body).to.have.property('doc');
|
||||
return {};
|
||||
|
||||
default:
|
||||
throw new Error(`unexpected es method ${method}`);
|
||||
}
|
||||
});
|
||||
|
||||
callCluster.assertGetQuery = () => {
|
||||
sinon.assert.calledOnce(callCluster);
|
||||
sinon.assert.calledWith(callCluster, 'get');
|
||||
};
|
||||
|
||||
callCluster.assertUpdateQuery = doc => {
|
||||
sinon.assert.calledOnce(callCluster);
|
||||
sinon.assert.calledWithExactly(callCluster, 'update', {
|
||||
index,
|
||||
type,
|
||||
id,
|
||||
body: { doc }
|
||||
});
|
||||
};
|
||||
|
||||
return callCluster;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
import sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
|
||||
export function createObjectsClientStub(type, id, esDocSource = {}) {
|
||||
const savedObjectsClient = {
|
||||
update: sinon.stub().returns(Promise.resolve()),
|
||||
get: sinon.stub().returns({ attributes: esDocSource })
|
||||
};
|
||||
|
||||
savedObjectsClient.assertGetQuery = () => {
|
||||
sinon.assert.calledOnce(savedObjectsClient.get);
|
||||
|
||||
const { args } = savedObjectsClient.get.getCall(0);
|
||||
expect(args[0]).to.be(type);
|
||||
expect(args[1]).to.eql(id);
|
||||
};
|
||||
|
||||
savedObjectsClient.assertUpdateQuery = (expectedChanges) => {
|
||||
sinon.assert.calledOnce(savedObjectsClient.update);
|
||||
|
||||
const { args } = savedObjectsClient.update.getCall(0);
|
||||
expect(args[0]).to.be(type);
|
||||
expect(args[1]).to.eql(id);
|
||||
expect(args[2]).to.eql(expectedChanges);
|
||||
};
|
||||
|
||||
return savedObjectsClient;
|
||||
}
|
|
@ -1 +1 @@
|
|||
export { createCallClusterStub } from './call_cluster_stub';
|
||||
export { createObjectsClientStub } from './create_objects_client_stub';
|
||||
|
|
|
@ -5,9 +5,8 @@ import Chance from 'chance';
|
|||
|
||||
import { UiSettingsService } from '../ui_settings_service';
|
||||
|
||||
import { createCallClusterStub } from './lib';
|
||||
import { createObjectsClientStub } from './lib';
|
||||
|
||||
const INDEX = '.kibana';
|
||||
const TYPE = 'config';
|
||||
const ID = 'kibana-version';
|
||||
const chance = new Chance();
|
||||
|
@ -18,22 +17,21 @@ function setup(options = {}) {
|
|||
getDefaults,
|
||||
defaults = {},
|
||||
esDocSource = {},
|
||||
callCluster = createCallClusterStub(INDEX, TYPE, ID, esDocSource)
|
||||
savedObjectsClient = createObjectsClientStub(TYPE, ID, esDocSource)
|
||||
} = options;
|
||||
|
||||
const uiSettings = new UiSettingsService({
|
||||
index: INDEX,
|
||||
type: TYPE,
|
||||
id: ID,
|
||||
getDefaults: getDefaults || (() => defaults),
|
||||
readInterceptor,
|
||||
callCluster,
|
||||
savedObjectsClient,
|
||||
});
|
||||
|
||||
return {
|
||||
uiSettings,
|
||||
assertGetQuery: callCluster.assertGetQuery,
|
||||
assertUpdateQuery: callCluster.assertUpdateQuery,
|
||||
assertGetQuery: savedObjectsClient.assertGetQuery,
|
||||
assertUpdateQuery: savedObjectsClient.assertUpdateQuery,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -199,9 +197,9 @@ describe('ui settings', () => {
|
|||
|
||||
it('throws 401 errors', async () => {
|
||||
const { uiSettings } = setup({
|
||||
async callCluster() {
|
||||
savedObjectsClient: { async get() {
|
||||
throw new esErrors[401]();
|
||||
}
|
||||
} }
|
||||
});
|
||||
|
||||
try {
|
||||
|
@ -216,9 +214,9 @@ describe('ui settings', () => {
|
|||
const expectedUnexpectedError = new Error('unexpected');
|
||||
|
||||
const { uiSettings } = setup({
|
||||
async callCluster() {
|
||||
savedObjectsClient: { async get() {
|
||||
throw expectedUnexpectedError;
|
||||
}
|
||||
} }
|
||||
});
|
||||
|
||||
try {
|
||||
|
|
|
@ -25,20 +25,18 @@ function hydrateUserSettings(userSettings) {
|
|||
export class UiSettingsService {
|
||||
constructor(options) {
|
||||
const {
|
||||
index,
|
||||
type,
|
||||
id,
|
||||
callCluster,
|
||||
savedObjectsClient,
|
||||
readInterceptor = noop,
|
||||
// we use a function for getDefaults() so that defaults can be different in
|
||||
// different scenarios, and so they can change over time
|
||||
getDefaults = () => ({}),
|
||||
} = options;
|
||||
|
||||
this._callCluster = callCluster;
|
||||
this._savedObjectsClient = savedObjectsClient;
|
||||
this._getDefaults = getDefaults;
|
||||
this._readInterceptor = readInterceptor;
|
||||
this._index = index;
|
||||
this._type = type;
|
||||
this._id = id;
|
||||
}
|
||||
|
@ -95,14 +93,7 @@ export class UiSettingsService {
|
|||
}
|
||||
|
||||
async _write(changes) {
|
||||
await this._callCluster('update', {
|
||||
index: this._index,
|
||||
type: this._type,
|
||||
id: this._id,
|
||||
body: {
|
||||
doc: changes
|
||||
}
|
||||
});
|
||||
await this._savedObjectsClient.update(this._type, this._id, changes);
|
||||
}
|
||||
|
||||
async _read(options = {}) {
|
||||
|
@ -123,18 +114,8 @@ export class UiSettingsService {
|
|||
);
|
||||
|
||||
try {
|
||||
const clientParams = {
|
||||
index: this._index,
|
||||
type: this._type,
|
||||
id: this._id,
|
||||
};
|
||||
|
||||
const callOptions = {
|
||||
wrap401Errors: !ignore401Errors
|
||||
};
|
||||
|
||||
const resp = await this._callCluster('get', clientParams, callOptions);
|
||||
return resp._source;
|
||||
const resp = await this._savedObjectsClient.get(this._type, this._id);
|
||||
return resp.attributes;
|
||||
} catch (error) {
|
||||
if (isIgnorableError(error)) {
|
||||
return {};
|
||||
|
|
|
@ -19,16 +19,15 @@ export function uiSettingsServiceFactory(server, options) {
|
|||
const config = server.config();
|
||||
|
||||
const {
|
||||
callCluster,
|
||||
savedObjectsClient,
|
||||
readInterceptor,
|
||||
getDefaults,
|
||||
} = options;
|
||||
|
||||
return new UiSettingsService({
|
||||
index: config.get('kibana.index'),
|
||||
type: 'config',
|
||||
id: config.get('pkg.version'),
|
||||
callCluster,
|
||||
savedObjectsClient,
|
||||
readInterceptor,
|
||||
getDefaults,
|
||||
});
|
||||
|
|
|
@ -28,13 +28,10 @@ export function getUiSettingsServiceForRequest(server, request, options = {}) {
|
|||
getDefaults
|
||||
} = options;
|
||||
|
||||
const adminCluster = server.plugins.elasticsearch.getCluster('admin');
|
||||
const uiSettingsService = uiSettingsServiceFactory(server, {
|
||||
readInterceptor,
|
||||
getDefaults,
|
||||
callCluster(...args) {
|
||||
return adminCluster.callWithRequest(request, ...args);
|
||||
}
|
||||
savedObjectsClient: request.getSavedObjectsClient()
|
||||
});
|
||||
|
||||
BY_REQUEST_CACHE.set(request, uiSettingsService);
|
||||
|
|
|
@ -7,8 +7,8 @@ export default function ({ getService, getPageObjects }) {
|
|||
const PageObjects = getPageObjects(['common']);
|
||||
|
||||
describe('status page', function () {
|
||||
before(function () {
|
||||
return PageObjects.common.navigateToApp('status_page');
|
||||
beforeEach(async () => {
|
||||
await PageObjects.common.navigateToApp('status_page');
|
||||
});
|
||||
|
||||
it('should show the kibana plugin as ready', function () {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue