[SavedObjects] use constructor options (#14200)

This commit is contained in:
Spencer 2017-09-27 14:57:47 -07:00 committed by GitHub
parent 46e5919262
commit 4fed3725ae
4 changed files with 50 additions and 26 deletions

View file

@ -75,7 +75,13 @@ describe('SavedObjectsClient', () => {
beforeEach(() => {
callAdminCluster = sandbox.stub();
savedObjectsClient = new SavedObjectsClient('.kibana-test', mappings, callAdminCluster);
savedObjectsClient = new SavedObjectsClient({
index: '.kibana-test',
mappings,
callCluster: callAdminCluster,
});
sandbox.stub(savedObjectsClient, '_getCurrentTime').returns(mockTimestamp);
sandbox.stub(getSearchDslNS, 'getSearchDsl').returns({});
});

View file

@ -12,11 +12,17 @@ import {
} from './lib';
export class SavedObjectsClient {
constructor(kibanaIndex, mappings, callAdminCluster) {
this._kibanaIndex = kibanaIndex;
constructor(options) {
const {
index,
mappings,
callCluster,
} = options;
this._index = index;
this._mappings = mappings;
this._type = getRootType(this._mappings);
this._callAdminCluster = callAdminCluster;
this._unwrappedCallCluster = callCluster;
}
static errors = errors
@ -40,9 +46,10 @@ export class SavedObjectsClient {
const method = id && !overwrite ? 'create' : 'index';
const time = this._getCurrentTime();
const response = await this._withKibanaIndex(method, {
const response = await this._callCluster(method, {
id: this._generateEsId(type, id),
type: this._type,
index: this._index,
refresh: 'wait_for',
body: {
type,
@ -91,7 +98,8 @@ export class SavedObjectsClient {
];
};
const { items } = await this._withKibanaIndex('bulk', {
const { items } = await this._callCluster('bulk', {
index: this._index,
refresh: 'wait_for',
body: objects.reduce((acc, object) => ([
...acc,
@ -140,9 +148,10 @@ export class SavedObjectsClient {
* @returns {promise}
*/
async delete(type, id) {
const response = await this._withKibanaIndex('delete', {
const response = await this._callCluster('delete', {
id: this._generateEsId(type, id),
type: this._type,
index: this._index,
refresh: 'wait_for',
});
@ -185,6 +194,7 @@ export class SavedObjectsClient {
}
const esOptions = {
index: this._index,
size: perPage,
from: perPage * (page - 1),
_source: includedFields(type, fields),
@ -200,7 +210,7 @@ export class SavedObjectsClient {
}
};
const response = await this._withKibanaIndex('search', esOptions);
const response = await this._callCluster('search', esOptions);
return {
page,
@ -236,7 +246,8 @@ export class SavedObjectsClient {
return { saved_objects: [] };
}
const response = await this._withKibanaIndex('mget', {
const response = await this._callCluster('mget', {
index: this._index,
body: {
docs: objects.map(object => ({
_id: this._generateEsId(object.type, object.id),
@ -276,9 +287,10 @@ export class SavedObjectsClient {
* @returns {promise} - { id, type, version, attributes }
*/
async get(type, id) {
const response = await this._withKibanaIndex('get', {
const response = await this._callCluster('get', {
id: this._generateEsId(type, id),
type: this._type,
index: this._index,
});
const { updated_at: updatedAt } = response._source;
@ -302,9 +314,10 @@ export class SavedObjectsClient {
*/
async update(type, id, attributes, options = {}) {
const time = this._getCurrentTime();
const response = await this._withKibanaIndex('update', {
const response = await this._callCluster('update', {
id: this._generateEsId(type, id),
type: this._type,
index: this._index,
version: options.version,
refresh: 'wait_for',
body: {
@ -324,12 +337,9 @@ export class SavedObjectsClient {
};
}
async _withKibanaIndex(method, params) {
async _callCluster(method, params) {
try {
return await this._callAdminCluster(method, {
...params,
index: this._kibanaIndex,
});
return await this._unwrappedCallCluster(method, params);
} catch (err) {
throw decorateEsError(err);
}

View file

@ -27,11 +27,11 @@ export function savedObjectsMixin(kbnServer, server) {
server.route(createUpdateRoute(prereqs));
server.decorate('server', 'savedObjectsClientFactory', ({ callCluster }) => {
return new SavedObjectsClient(
server.config().get('kibana.index'),
server.getKibanaIndexMappingsDsl(),
callCluster
);
return new SavedObjectsClient({
index: server.config().get('kibana.index'),
mappings: server.getKibanaIndexMappingsDsl(),
callCluster,
});
});
const savedObjectsClientCache = new WeakMap();

View file

@ -5,11 +5,19 @@ export class KibanaServerUiSettings {
constructor(log, es, kibanaIndex, kibanaVersion) {
this._log = log;
this._kibanaVersion = kibanaVersion;
this._savedObjectsClient = new SavedObjectsClient(
kibanaIndex.getName(),
kibanaIndex.getMappingsDsl(),
createCallCluster(es)
);
this._savedObjectsClient = new SavedObjectsClient({
index: kibanaIndex.getName(),
mappings: kibanaIndex.getMappingsDsl(),
callCluster: createCallCluster(es),
async onBeforeWrite() {
await es.cluster.health({
timeout: '5s',
index: kibanaIndex.getName(),
waitForStatus: 'yellow',
});
}
});
}
async _id() {