Skip to content

Commit

Permalink
Uses SavedObjectsClient for UI Settings
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Smalley <[email protected]>
  • Loading branch information
Tyler Smalley committed Jul 11, 2017
1 parent fbb074a commit ebdb4d9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 84 deletions.
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

0 comments on commit ebdb4d9

Please sign in to comment.