Skip to content

Commit b657ac8

Browse files
committed
Change can* signature to be the same as their equivalent function, apply PR feedback
1 parent 6287a8c commit b657ac8

15 files changed

Lines changed: 408 additions & 128 deletions

src/legacy/server/saved_objects/export/get_sorted_objects_for_export.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ describe('getSortedObjectsForExport()', () => {
3636

3737
beforeEach(() => {
3838
jest.resetAllMocks();
39-
savedObjectsClient.canBulkCreate.mockImplementation((types: string[]) =>
40-
types.map(type => ({ type, can: true }))
39+
savedObjectsClient.canBulkCreate.mockImplementation((objects: any, options: any) =>
40+
objects.map((obj: any) => ({ type: obj.type, can: true }))
4141
);
42-
savedObjectsClient.canBulkGet.mockImplementation((types: string[]) =>
43-
types.map(type => ({ type, can: true }))
44-
);
45-
savedObjectsClient.canFind.mockImplementation((types: string[]) =>
46-
types.map(type => ({ type, can: true }))
42+
savedObjectsClient.canBulkGet.mockImplementation((objects: any, options: any) =>
43+
objects.map((obj: any) => ({ type: obj.type, can: true }))
4744
);
45+
savedObjectsClient.canFind.mockImplementation((options: any) => {
46+
const types = Array.isArray(options.type) ? options.type : [options.type];
47+
return types.map((type: string) => ({ type, can: true }));
48+
});
4849
});
4950

5051
test('exports selected types and sorts them', async () => {

src/legacy/server/saved_objects/export/get_sorted_objects_for_export.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async function fetchObjectsToExport({
7474
'bulk_get',
7575
objectTypes,
7676
supportedTypes,
77-
await savedObjectsClient.canBulkGet(objectTypes)
77+
await savedObjectsClient.canBulkGet(objects)
7878
);
7979
if (objects.length > exportSizeLimit) {
8080
throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`);
@@ -95,7 +95,12 @@ async function fetchObjectsToExport({
9595
'find',
9696
types || [],
9797
supportedTypes,
98-
await savedObjectsClient.canFind(types || [])
98+
await savedObjectsClient.canFind({
99+
type: types,
100+
sortField: '_id',
101+
sortOrder: 'asc',
102+
perPage: exportSizeLimit,
103+
})
99104
);
100105
const findResponse = await savedObjectsClient.find({
101106
type: types,

src/legacy/server/saved_objects/import/import_saved_objects.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ describe('importSavedObjects()', () => {
7272

7373
beforeEach(() => {
7474
jest.resetAllMocks();
75-
savedObjectsClient.canBulkCreate.mockImplementation((types: string[]) =>
76-
types.map(type => ({ type, can: true }))
75+
savedObjectsClient.canBulkCreate.mockImplementation((objects: any, options: any) =>
76+
objects.map((obj: any) => ({ type: obj.type, can: true }))
7777
);
78-
savedObjectsClient.canBulkGet.mockImplementation((types: string[]) =>
79-
types.map(type => ({ type, can: true }))
80-
);
81-
savedObjectsClient.canFind.mockImplementation((types: string[]) =>
82-
types.map(type => ({ type, can: true }))
78+
savedObjectsClient.canBulkGet.mockImplementation((objects: any, options: any) =>
79+
objects.map((obj: any) => ({ type: obj.type, can: true }))
8380
);
81+
savedObjectsClient.canFind.mockImplementation((options: any) => {
82+
const types = Array.isArray(options.type) ? options.type : [options.type];
83+
return types.map((type: string) => ({ type, can: true }));
84+
});
8485
});
8586

8687
test('returns early when no objects exist', async () => {

src/legacy/server/saved_objects/import/import_saved_objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function importSavedObjects({
5252
const objectsFromStream = await collectSavedObjects({ readStream, objectLimit });
5353

5454
const objectTypes = [...new Set(objectsFromStream.map(obj => obj.type))];
55-
const authorizedTypes = await savedObjectsClient.canBulkCreate(objectTypes);
55+
const authorizedTypes = await savedObjectsClient.canBulkCreate(objectsFromStream, { overwrite });
5656
const invalidTypes = [
5757
...new Set([
5858
...objectTypes.filter(type => !supportedTypes.includes(type)),

src/legacy/server/saved_objects/import/resolve_import_errors.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,16 @@ describe('resolveImportErrors()', () => {
7878

7979
beforeEach(() => {
8080
jest.resetAllMocks();
81-
savedObjectsClient.canBulkCreate.mockImplementation((types: string[]) =>
82-
types.map(type => ({ type, can: true }))
81+
savedObjectsClient.canBulkCreate.mockImplementation((objects: any, options: any) =>
82+
objects.map((obj: any) => ({ type: obj.type, can: true }))
8383
);
84-
savedObjectsClient.canBulkGet.mockImplementation((types: string[]) =>
85-
types.map(type => ({ type, can: true }))
86-
);
87-
savedObjectsClient.canFind.mockImplementation((types: string[]) =>
88-
types.map(type => ({ type, can: true }))
84+
savedObjectsClient.canBulkGet.mockImplementation((objects: any, options: any) =>
85+
objects.map((obj: any) => ({ type: obj.type, can: true }))
8986
);
87+
savedObjectsClient.canFind.mockImplementation((options: any) => {
88+
const types = Array.isArray(options.type) ? options.type : [options.type];
89+
return types.map((type: string) => ({ type, can: true }));
90+
});
9091
});
9192

9293
test('works with empty parameters', async () => {

src/legacy/server/saved_objects/import/resolve_import_errors.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import Boom from 'boom';
2121
import { Readable } from 'stream';
22-
import { SavedObjectsClient } from '../service';
22+
import { SavedObjectsClient, SavedObject } from '../service';
2323
import { collectSavedObjects } from './collect_saved_objects';
2424
import { createObjectsFilter } from './create_objects_filter';
2525
import { extractErrors } from './extract_errors';
@@ -41,6 +41,36 @@ interface ImportResponse {
4141
errors?: ImportError[];
4242
}
4343

44+
async function getAuthorizedTypes(
45+
savedObjectsClient: SavedObjectsClient,
46+
objects: SavedObject[],
47+
retries: Retry[]
48+
) {
49+
// Call canBulkCreate twice since the parameters have to be the same as if bulkCreate is called
50+
const { objectsToOverwrite, objectsToNotOverwrite } = splitOverwrites(objects, retries);
51+
const resultForObjectsToOverwrite = await savedObjectsClient.canBulkCreate(objectsToOverwrite, {
52+
overwrite: true,
53+
});
54+
const resultForObjectsToNotOverwrite = await savedObjectsClient.canBulkCreate(
55+
objectsToNotOverwrite
56+
);
57+
58+
// Extract which types can be created in bulk
59+
const resultMap: { [key: string]: boolean } = {};
60+
for (const { type, can } of resultForObjectsToNotOverwrite) {
61+
resultMap[type] = can;
62+
}
63+
for (const { type, can } of resultForObjectsToOverwrite) {
64+
const canPreviously = resultMap[type] !== false;
65+
resultMap[type] = canPreviously && can;
66+
}
67+
68+
return Object.keys(resultMap).map(type => ({
69+
type,
70+
can: resultMap[type],
71+
}));
72+
}
73+
4474
export async function resolveImportErrors({
4575
readStream,
4676
objectLimit,
@@ -60,7 +90,7 @@ export async function resolveImportErrors({
6090
});
6191

6292
const objectTypes = [...new Set(objectsToResolve.map(obj => obj.type))];
63-
const authorizedTypes = await savedObjectsClient.canBulkCreate(objectTypes);
93+
const authorizedTypes = await getAuthorizedTypes(savedObjectsClient, objectsToResolve, retries);
6494
const invalidTypes = [
6595
...new Set([
6696
...objectTypes.filter(type => !supportedTypes.includes(type)),

src/legacy/server/saved_objects/routes/import.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ describe('POST /api/saved_objects/_import', () => {
4040
beforeEach(() => {
4141
server = createMockServer();
4242
jest.resetAllMocks();
43-
savedObjectsClient.canBulkCreate.mockImplementation((types: string[]) =>
44-
types.map(type => ({ type, can: true }))
43+
savedObjectsClient.canBulkCreate.mockImplementation((objects: any, options: any) =>
44+
objects.map((obj: any) => ({ type: obj.type, can: true }))
4545
);
46-
savedObjectsClient.canBulkGet.mockImplementation((types: string[]) =>
47-
types.map(type => ({ type, can: true }))
48-
);
49-
savedObjectsClient.canFind.mockImplementation((types: string[]) =>
50-
types.map(type => ({ type, can: true }))
46+
savedObjectsClient.canBulkGet.mockImplementation((objects: any, options: any) =>
47+
objects.map((obj: any) => ({ type: obj.type, can: true }))
5148
);
49+
savedObjectsClient.canFind.mockImplementation((options: any) => {
50+
const types = Array.isArray(options.type) ? options.type : [options.type];
51+
return types.map((type: string) => ({ type, can: true }));
52+
});
5253

5354
const prereqs = {
5455
getSavedObjectsClient: {

src/legacy/server/saved_objects/routes/resolve_import_errors.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ describe('POST /api/saved_objects/_resolve_import_errors', () => {
4040
beforeEach(() => {
4141
server = createMockServer();
4242
jest.resetAllMocks();
43-
savedObjectsClient.canBulkCreate.mockImplementation((types: string[]) =>
44-
types.map(type => ({ type, can: true }))
43+
savedObjectsClient.canBulkCreate.mockImplementation((objects: any, options: any) =>
44+
objects.map((obj: any) => ({ type: obj.type, can: true }))
4545
);
46-
savedObjectsClient.canBulkGet.mockImplementation((types: string[]) =>
47-
types.map(type => ({ type, can: true }))
48-
);
49-
savedObjectsClient.canFind.mockImplementation((types: string[]) =>
50-
types.map(type => ({ type, can: true }))
46+
savedObjectsClient.canBulkGet.mockImplementation((objects: any, options: any) =>
47+
objects.map((obj: any) => ({ type: obj.type, can: true }))
5148
);
49+
savedObjectsClient.canFind.mockImplementation((options: any) => {
50+
const types = Array.isArray(options.type) ? options.type : [options.type];
51+
return types.map((type: string) => ({ type, can: true }));
52+
});
5253

5354
const prereqs = {
5455
getSavedObjectsClient: {

src/legacy/server/saved_objects/service/saved_objects_client.d.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ export declare class SavedObjectsClient {
127127
objects: Array<BulkCreateObject<T>>,
128128
options?: CreateOptions
129129
): Promise<BulkCreateResponse<T>>;
130-
public canBulkCreate(
131-
types: string[]
130+
public canBulkCreate<T extends SavedObjectAttributes = any>(
131+
objects: Array<BulkCreateObject<T>>,
132+
options?: CreateOptions
132133
): Promise<
133134
Array<{
134135
type: string;
@@ -139,12 +140,17 @@ export declare class SavedObjectsClient {
139140
public find<T extends SavedObjectAttributes = any>(
140141
options: FindOptions
141142
): Promise<FindResponse<T>>;
142-
public canFind(types: string[]): Promise<Array<{ type: string; can: boolean }>>;
143+
public canFind<T extends SavedObjectAttributes = any>(
144+
options: FindOptions
145+
): Promise<Array<{ type: string; can: boolean }>>;
143146
public bulkGet<T extends SavedObjectAttributes = any>(
144147
objects: BulkGetObjects,
145148
options?: BaseOptions
146149
): Promise<BulkGetResponse<T>>;
147-
public canBulkGet(types: string[]): Promise<Array<{ type: string; can: boolean }>>;
150+
public canBulkGet<T extends SavedObjectAttributes = any>(
151+
objects: BulkGetObjects,
152+
options?: BaseOptions
153+
): Promise<Array<{ type: string; can: boolean }>>;
148154
public get<T extends SavedObjectAttributes = any>(
149155
type: string,
150156
id: string,

src/legacy/server/saved_objects/service/saved_objects_client.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,15 @@ export class SavedObjectsClient {
127127
*
128128
* This should only be used by import / export / resolve import errors.
129129
*
130-
* @param {Array<string>} types Types of saved objects
130+
* @param {array} objects - [{ type, id, attributes }]
131+
* @param {object} [options={}]
132+
* @property {boolean} [options.overwrite=false] - overwrites existing documents
133+
* @property {string} [options.namespace]
131134
* @return [{ type, can }]
132135
*/
133-
async canBulkCreate(types) {
134-
return types.map(type => ({ type, can: true }));
136+
async canBulkCreate(objects) {
137+
const types = new Set(objects.map(obj => obj.type));
138+
return [...types].map(type => ({ type, can: true }));
135139
}
136140

137141
/**
@@ -172,17 +176,30 @@ export class SavedObjectsClient {
172176
*
173177
* This should only be used by import / export / resolve import errors.
174178
*
175-
* @param {Array<string>} types Types of saved objects
179+
* @param {object} [options={}]
180+
* @property {(string|Array<string>)} [options.type]
181+
* @property {string} [options.search]
182+
* @property {string} [options.defaultSearchOperator]
183+
* @property {Array<string>} [options.searchFields] - see Elasticsearch Simple Query String
184+
* Query field argument for more information
185+
* @property {integer} [options.page=1]
186+
* @property {integer} [options.perPage=20]
187+
* @property {string} [options.sortField]
188+
* @property {string} [options.sortOrder]
189+
* @property {Array<string>} [options.fields]
190+
* @property {string} [options.namespace]
191+
* @property {object} [options.hasReference] - { type, id }
176192
* @return [{ type, can }]
177193
*/
178-
async canFind(types) {
194+
async canFind(options = {}) {
195+
const types = Array.isArray(options.type) ? options.type : [options.type];
179196
return types.map(type => ({ type, can: true }));
180197
}
181198

182199
/**
183200
* Returns an array of objects by id
184201
*
185-
* @param {array} objects - an array ids, or an array of objects containing id and optionally type
202+
* @param {array} objects - an array of objects containing id and type
186203
* @param {object} [options={}]
187204
* @property {string} [options.namespace]
188205
* @returns {promise} - { saved_objects: [{ id, type, version, attributes }] }
@@ -202,11 +219,14 @@ export class SavedObjectsClient {
202219
*
203220
* This should only be used by import / export / resolve import errors.
204221
*
205-
* @param {Array<string>} types Types of saved objects
222+
* @param {array} objects - an array of objects containing id and type
223+
* @param {object} [options={}]
224+
* @property {string} [options.namespace]
206225
* @return [{ type, can }]
207226
*/
208-
async canBulkGet(types) {
209-
return types.map(type => ({ type, can: true }));
227+
async canBulkGet(objects = []) {
228+
const types = new Set(objects.map(obj => obj.type));
229+
return [...types].map(type => ({ type, can: true }));
210230
}
211231

212232
/**

0 commit comments

Comments
 (0)