-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
spaces_saved_objects_client.ts
308 lines (280 loc) · 9.19 KB
/
spaces_saved_objects_client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import {
SavedObjectsBaseOptions,
SavedObjectsBulkCreateObject,
SavedObjectsBulkGetObject,
SavedObjectsBulkUpdateObject,
SavedObjectsClientContract,
SavedObjectsCreateOptions,
SavedObjectsFindOptions,
SavedObjectsUpdateOptions,
SavedObjectsAddToNamespacesOptions,
SavedObjectsDeleteFromNamespacesOptions,
ISavedObjectTypeRegistry,
} from 'src/core/server';
import { SpacesServiceSetup } from '../spaces_service/spaces_service';
import { spaceIdToNamespace } from '../lib/utils/namespace';
import { SpacesClient } from '../lib/spaces_client';
interface SpacesSavedObjectsClientOptions {
baseClient: SavedObjectsClientContract;
request: any;
spacesService: SpacesServiceSetup;
typeRegistry: ISavedObjectTypeRegistry;
}
const coerceToArray = (param: string | string[]) => {
if (Array.isArray(param)) {
return param;
}
return [param];
};
const throwErrorIfNamespaceSpecified = (options: any) => {
if (options.namespace) {
throw new Error('Spaces currently determines the namespaces');
}
};
export class SpacesSavedObjectsClient implements SavedObjectsClientContract {
private readonly client: SavedObjectsClientContract;
private readonly spaceId: string;
private readonly types: string[];
private readonly getSpacesClient: Promise<SpacesClient>;
public readonly errors: SavedObjectsClientContract['errors'];
constructor(options: SpacesSavedObjectsClientOptions) {
const { baseClient, request, spacesService, typeRegistry } = options;
this.client = baseClient;
this.getSpacesClient = spacesService.scopedClient(request);
this.spaceId = spacesService.getSpaceId(request);
this.types = typeRegistry.getAllTypes().map((t) => t.name);
this.errors = baseClient.errors;
}
/**
* Persists an object
*
* @param {string} type
* @param {object} attributes
* @param {object} [options={}]
* @property {string} [options.id] - force id on creation, not recommended
* @property {boolean} [options.overwrite=false]
* @property {string} [options.namespace]
* @returns {promise} - { id, type, version, attributes }
*/
public async create<T = unknown>(
type: string,
attributes: T = {} as T,
options: SavedObjectsCreateOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.create<T>(type, attributes, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Creates multiple documents at once
*
* @param {array} objects - [{ type, id, attributes }]
* @param {object} [options={}]
* @property {boolean} [options.overwrite=false] - overwrites existing documents
* @property {string} [options.namespace]
* @returns {promise} - { saved_objects: [{ id, type, version, attributes, error: { message } }]}
*/
public async bulkCreate<T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options: SavedObjectsBaseOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.bulkCreate(objects, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Deletes an object
*
* @param {string} type
* @param {string} id
* @param {object} [options={}]
* @property {string} [options.namespace]
* @returns {promise}
*/
public async delete(type: string, id: string, options: SavedObjectsBaseOptions = {}) {
throwErrorIfNamespaceSpecified(options);
return await this.client.delete(type, id, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* @param {object} [options={}]
* @property {(string|Array<string>)} [options.type]
* @property {string} [options.search]
* @property {string} [options.defaultSearchOperator]
* @property {Array<string>} [options.searchFields] - see Elasticsearch Simple Query String
* Query field argument for more information
* @property {integer} [options.page=1]
* @property {integer} [options.perPage=20]
* @property {string} [options.sortField]
* @property {string} [options.sortOrder]
* @property {Array<string>} [options.fields]
* @property {string} [options.namespaces]
* @property {object} [options.hasReference] - { type, id }
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }], total, per_page, page }
*/
public async find<T = unknown>(options: SavedObjectsFindOptions) {
throwErrorIfNamespaceSpecified(options);
let namespaces = options.namespaces;
if (namespaces) {
const spacesClient = await this.getSpacesClient;
const availableSpaces = await spacesClient.getAll('findSavedObjects');
if (namespaces.includes('*')) {
namespaces = availableSpaces.map((space) => space.id);
} else {
namespaces = namespaces.filter((namespace) =>
availableSpaces.some((space) => space.id === namespace)
);
}
// This forbidden error allows this scenario to be consistent
// with the way the SpacesClient behaves when no spaces are authorized
// there.
if (namespaces.length === 0) {
throw this.errors.decorateForbiddenError(new Error());
}
} else {
namespaces = [this.spaceId];
}
return await this.client.find<T>({
...options,
type: (options.type ? coerceToArray(options.type) : this.types).filter(
(type) => type !== 'space'
),
namespaces,
});
}
/**
* Returns an array of objects by id
*
* @param {array} objects - an array ids, or an array of objects containing id and optionally type
* @param {object} [options={}]
* @property {string} [options.namespace]
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }] }
* @example
*
* bulkGet([
* { id: 'one', type: 'config' },
* { id: 'foo', type: 'index-pattern' }
* ])
*/
public async bulkGet<T = unknown>(
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.bulkGet<T>(objects, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Gets a single object
*
* @param {string} type
* @param {string} id
* @param {object} [options={}]
* @property {string} [options.namespace]
* @returns {promise} - { id, type, version, attributes }
*/
public async get<T = unknown>(type: string, id: string, options: SavedObjectsBaseOptions = {}) {
throwErrorIfNamespaceSpecified(options);
return await this.client.get<T>(type, id, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Updates an object
*
* @param {string} type
* @param {string} id
* @param {object} [options={}]
* @property {string} options.version - ensures version matches that of persisted object
* @property {string} [options.namespace]
* @returns {promise}
*/
public async update<T = unknown>(
type: string,
id: string,
attributes: Partial<T>,
options: SavedObjectsUpdateOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.update(type, id, attributes, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Adds namespaces to a SavedObject
*
* @param type
* @param id
* @param namespaces
* @param options
*/
public async addToNamespaces(
type: string,
id: string,
namespaces: string[],
options: SavedObjectsAddToNamespacesOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.addToNamespaces(type, id, namespaces, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Removes namespaces from a SavedObject
*
* @param type
* @param id
* @param namespaces
* @param options
*/
public async deleteFromNamespaces(
type: string,
id: string,
namespaces: string[],
options: SavedObjectsDeleteFromNamespacesOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.deleteFromNamespaces(type, id, namespaces, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
/**
* Updates an array of objects by id
*
* @param {array} objects - an array ids, or an array of objects containing id, type, attributes and optionally version, references and namespace
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }] }
* @example
*
* bulkUpdate([
* { id: 'one', type: 'config', attributes: { title: 'My new title'}, version: 'd7rhfk47d=' },
* { id: 'foo', type: 'index-pattern', attributes: {} }
* ])
*/
public async bulkUpdate<T = unknown>(
objects: Array<SavedObjectsBulkUpdateObject<T>> = [],
options: SavedObjectsBaseOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.bulkUpdate(objects, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
}