Skip to content
/ kibana Public
forked from elastic/kibana

Commit

Permalink
Honor current search criteria when exporting saved objects (elastic#4…
Browse files Browse the repository at this point in the history
…7223)

* honor current search criteria when exporting saved objects

* adding core docs
  • Loading branch information
legrego authored Oct 7, 2019
1 parent f5c6853 commit dba0946
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## SavedObjectsExportOptions.exportSizeLimit property

the maximum number of objects to export.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## SavedObjectsExportOptions.includeReferencesDeep property

flag to also include all related saved objects in the export response.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export interface SavedObjectsExportOptions

| Property | Type | Description |
| --- | --- | --- |
| [exportSizeLimit](./kibana-plugin-server.savedobjectsexportoptions.exportsizelimit.md) | <code>number</code> | |
| [includeReferencesDeep](./kibana-plugin-server.savedobjectsexportoptions.includereferencesdeep.md) | <code>boolean</code> | |
| [namespace](./kibana-plugin-server.savedobjectsexportoptions.namespace.md) | <code>string</code> | |
| [objects](./kibana-plugin-server.savedobjectsexportoptions.objects.md) | <code>Array&lt;{</code><br/><code> id: string;</code><br/><code> type: string;</code><br/><code> }&gt;</code> | |
| [savedObjectsClient](./kibana-plugin-server.savedobjectsexportoptions.savedobjectsclient.md) | <code>SavedObjectsClientContract</code> | |
| [types](./kibana-plugin-server.savedobjectsexportoptions.types.md) | <code>string[]</code> | |
| [exportSizeLimit](./kibana-plugin-server.savedobjectsexportoptions.exportsizelimit.md) | <code>number</code> | the maximum number of objects to export. |
| [includeReferencesDeep](./kibana-plugin-server.savedobjectsexportoptions.includereferencesdeep.md) | <code>boolean</code> | flag to also include all related saved objects in the export response. |
| [namespace](./kibana-plugin-server.savedobjectsexportoptions.namespace.md) | <code>string</code> | optional namespace to override the namespace used by the savedObjectsClient. |
| [objects](./kibana-plugin-server.savedobjectsexportoptions.objects.md) | <code>Array&lt;{</code><br/><code> id: string;</code><br/><code> type: string;</code><br/><code> }&gt;</code> | optional array of objects to export. |
| [savedObjectsClient](./kibana-plugin-server.savedobjectsexportoptions.savedobjectsclient.md) | <code>SavedObjectsClientContract</code> | an instance of the SavedObjectsClient. |
| [search](./kibana-plugin-server.savedobjectsexportoptions.search.md) | <code>string</code> | optional query string to filter exported objects. |
| [types](./kibana-plugin-server.savedobjectsexportoptions.types.md) | <code>string[]</code> | optional array of saved object types. |

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## SavedObjectsExportOptions.namespace property

optional namespace to override the namespace used by the savedObjectsClient.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## SavedObjectsExportOptions.objects property

optional array of objects to export.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## SavedObjectsExportOptions.savedObjectsClient property

an instance of the SavedObjectsClient.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [SavedObjectsExportOptions](./kibana-plugin-server.savedobjectsexportoptions.md) &gt; [search](./kibana-plugin-server.savedobjectsexportoptions.search.md)

## SavedObjectsExportOptions.search property

optional query string to filter exported objects.

<b>Signature:</b>

```typescript
search?: string;
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## SavedObjectsExportOptions.types property

optional array of saved object types.

<b>Signature:</b>

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,114 @@ describe('getSortedObjectsForExport()', () => {
]
`);
expect(savedObjectsClient.find).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
[MockFunction] {
"calls": Array [
Array [
Object {
"namespace": undefined,
"perPage": 500,
"search": undefined,
"sortField": "_id",
"sortOrder": "asc",
"type": Array [
"index-pattern",
"search",
],
},
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
`);
});

test('exports selected types with search string when present', async () => {
savedObjectsClient.find.mockResolvedValueOnce({
total: 2,
saved_objects: [
{
id: '2',
type: 'search',
attributes: {},
references: [
{
name: 'name',
type: 'index-pattern',
id: '1',
},
],
},
{
id: '1',
type: 'index-pattern',
attributes: {},
references: [],
},
],
per_page: 1,
page: 0,
});
const exportStream = await getSortedObjectsForExport({
savedObjectsClient,
exportSizeLimit: 500,
types: ['index-pattern', 'search'],
search: 'foo',
});

const response = await readStreamToCompletion(exportStream);

expect(response).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"namespace": undefined,
"perPage": 500,
"sortField": "_id",
"sortOrder": "asc",
"type": Array [
"index-pattern",
"search",
],
"id": "1",
"name": "name",
"type": "index-pattern",
},
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
"type": "search",
},
]
`);
expect(savedObjectsClient.find).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"namespace": undefined,
"perPage": 500,
"search": "foo",
"sortField": "_id",
"sortOrder": "asc",
"type": Array [
"index-pattern",
"search",
],
},
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
`);
});

test('exports from the provided namespace when present', async () => {
Expand Down Expand Up @@ -179,29 +264,30 @@ describe('getSortedObjectsForExport()', () => {
]
`);
expect(savedObjectsClient.find).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"namespace": "foo",
"perPage": 500,
"sortField": "_id",
"sortOrder": "asc",
"type": Array [
"index-pattern",
"search",
],
},
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
[MockFunction] {
"calls": Array [
Array [
Object {
"namespace": "foo",
"perPage": 500,
"search": undefined,
"sortField": "_id",
"sortOrder": "asc",
"type": Array [
"index-pattern",
"search",
],
}
`);
},
],
],
"results": Array [
Object {
"type": "return",
"value": Promise {},
},
],
}
`);
});

test('export selected types throws error when exceeding exportSizeLimit', async () => {
Expand Down Expand Up @@ -464,4 +550,17 @@ describe('getSortedObjectsForExport()', () => {
`"Either \`type\` or \`objects\` are required."`
);
});

test('rejects when both objects and search are passed in', () => {
const exportOpts = {
exportSizeLimit: 1,
savedObjectsClient,
objects: [{ type: 'index-pattern', id: '1' }],
search: 'foo',
};

expect(getSortedObjectsForExport(exportOpts)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Can't specify both \\"search\\" and \\"objects\\" properties when exporting"`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,38 @@ import { sortObjects } from './sort_objects';
* @public
*/
export interface SavedObjectsExportOptions {
/** optional array of saved object types. */
types?: string[];
/** optional array of objects to export. */
objects?: Array<{
/** the saved object id. */
id: string;
/** the saved object type. */
type: string;
}>;
/** optional query string to filter exported objects. */
search?: string;
/** an instance of the SavedObjectsClient. */
savedObjectsClient: SavedObjectsClientContract;
/** the maximum number of objects to export. */
exportSizeLimit: number;
/** flag to also include all related saved objects in the export response. */
includeReferencesDeep?: boolean;
/** optional namespace to override the namespace used by the savedObjectsClient. */
namespace?: string;
}

async function fetchObjectsToExport({
objects,
types,
search,
exportSizeLimit,
savedObjectsClient,
namespace,
}: {
objects?: SavedObjectsExportOptions['objects'];
types?: string[];
search?: string;
exportSizeLimit: number;
savedObjectsClient: SavedObjectsClientContract;
namespace?: string;
Expand All @@ -56,6 +68,9 @@ async function fetchObjectsToExport({
if (objects.length > exportSizeLimit) {
throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`);
}
if (typeof search === 'string') {
throw Boom.badRequest(`Can't specify both "search" and "objects" properties when exporting`);
}
const bulkGetResult = await savedObjectsClient.bulkGet(objects, { namespace });
const erroredObjects = bulkGetResult.saved_objects.filter(obj => !!obj.error);
if (erroredObjects.length) {
Expand All @@ -69,6 +84,7 @@ async function fetchObjectsToExport({
} else if (types && types.length > 0) {
const findResponse = await savedObjectsClient.find({
type: types,
search,
sortField: '_id',
sortOrder: 'asc',
perPage: exportSizeLimit,
Expand All @@ -86,6 +102,7 @@ async function fetchObjectsToExport({
export async function getSortedObjectsForExport({
types,
objects,
search,
savedObjectsClient,
exportSizeLimit,
includeReferencesDeep = false,
Expand All @@ -94,6 +111,7 @@ export async function getSortedObjectsForExport({
const objectsToExport = await fetchObjectsToExport({
types,
objects,
search,
savedObjectsClient,
exportSizeLimit,
namespace,
Expand Down
7 changes: 1 addition & 6 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,20 +820,15 @@ export class SavedObjectsErrorHelpers {

// @public
export interface SavedObjectsExportOptions {
// (undocumented)
exportSizeLimit: number;
// (undocumented)
includeReferencesDeep?: boolean;
// (undocumented)
namespace?: string;
// (undocumented)
objects?: Array<{
id: string;
type: string;
}>;
// (undocumented)
savedObjectsClient: SavedObjectsClientContract;
// (undocumented)
search?: string;
types?: string[];
}

Expand Down
Loading

0 comments on commit dba0946

Please sign in to comment.