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

Uses SavedObjectsClient for UI Settings #12747

Merged
merged 3 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
17 changes: 11 additions & 6 deletions src/server/saved_objects/saved_objects_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
});
Expand Down
41 changes: 0 additions & 41 deletions src/ui/ui_settings/__tests__/lib/call_cluster_stub.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/ui/ui_settings/__tests__/lib/create_objects_client_stub.js
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/ui/ui_settings/__tests__/lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { createCallClusterStub } from './call_cluster_stub';
export { createObjectsClientStub } from './create_objects_client_stub';
20 changes: 9 additions & 11 deletions src/ui/ui_settings/__tests__/ui_settings_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
};
}

Expand Down Expand Up @@ -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 {
Expand All @@ -216,9 +214,9 @@ describe('ui settings', () => {
const expectedUnexpectedError = new Error('unexpected');

const { uiSettings } = setup({
async callCluster() {
savedObjectsClient: { async get() {
throw expectedUnexpectedError;
}
} }
});

try {
Expand Down
29 changes: 5 additions & 24 deletions src/ui/ui_settings/ui_settings_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = {}) {
Expand All @@ -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 {};
Expand Down
5 changes: 2 additions & 3 deletions src/ui/ui_settings/ui_settings_service_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
5 changes: 1 addition & 4 deletions src/ui/ui_settings/ui_settings_service_for_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down