Skip to content

Commit

Permalink
removing dependency on legacy saved objects service
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Mar 4, 2020
1 parent 0bb63a1 commit d3f86ab
Show file tree
Hide file tree
Showing 42 changed files with 683 additions and 532 deletions.
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; [SavedObjectsServiceStart](./kibana-plugin-server.savedobjectsservicestart.md) &gt; [getImportExportObjectLimit](./kibana-plugin-server.savedobjectsservicestart.getimportexportobjectlimit.md)

## SavedObjectsServiceStart.getImportExportObjectLimit property

Returns the maximum number of objects allowed for import or export operations.

<b>Signature:</b>

```typescript
getImportExportObjectLimit: () => number;
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface SavedObjectsServiceStart
| [createInternalRepository](./kibana-plugin-server.savedobjectsservicestart.createinternalrepository.md) | <code>(extraTypes?: string[]) =&gt; ISavedObjectsRepository</code> | Creates a [Saved Objects repository](./kibana-plugin-server.isavedobjectsrepository.md) that uses the internal Kibana user for authenticating with Elasticsearch. |
| [createScopedRepository](./kibana-plugin-server.savedobjectsservicestart.createscopedrepository.md) | <code>(req: KibanaRequest, extraTypes?: string[]) =&gt; ISavedObjectsRepository</code> | Creates a [Saved Objects repository](./kibana-plugin-server.isavedobjectsrepository.md) that uses the credentials from the passed in request to authenticate with Elasticsearch. |
| [createSerializer](./kibana-plugin-server.savedobjectsservicestart.createserializer.md) | <code>() =&gt; SavedObjectsSerializer</code> | Creates a [serializer](./kibana-plugin-server.savedobjectsserializer.md) that is aware of all registered types. |
| [getImportExportObjectLimit](./kibana-plugin-server.savedobjectsservicestart.getimportexportobjectlimit.md) | <code>() =&gt; number</code> | Returns the maximum number of objects allowed for import or export operations. |
| [getScopedClient](./kibana-plugin-server.savedobjectsservicestart.getscopedclient.md) | <code>(req: KibanaRequest, options?: SavedObjectsClientProviderOptions) =&gt; SavedObjectsClientContract</code> | Creates a [Saved Objects client](./kibana-plugin-server.savedobjectsclientcontract.md) that uses the credentials from the passed in request to authenticate with Elasticsearch. If other plugins have registered Saved Objects client wrappers, these will be applied to extend the functionality of the client.<!-- -->A client that is already scoped to the incoming request is also exposed from the route handler context see [RequestHandlerContext](./kibana-plugin-server.requesthandlercontext.md)<!-- -->. |
| [getTypeRegistry](./kibana-plugin-server.savedobjectsservicestart.gettyperegistry.md) | <code>() =&gt; ISavedObjectTypeRegistry</code> | Returns the [registry](./kibana-plugin-server.isavedobjecttyperegistry.md) containing all registered [saved object types](./kibana-plugin-server.savedobjectstype.md) |

1 change: 1 addition & 0 deletions src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class LegacyService implements CoreService {
createInternalRepository: startDeps.core.savedObjects.createInternalRepository,
createSerializer: startDeps.core.savedObjects.createSerializer,
getTypeRegistry: startDeps.core.savedObjects.getTypeRegistry,
getImportExportObjectLimit: startDeps.core.savedObjects.getImportExportObjectLimit,
},
uiSettings: { asScopedToClient: startDeps.core.uiSettings.asScopedToClient },
};
Expand Down
1 change: 1 addition & 0 deletions src/core/server/plugins/plugin_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export function createPluginStartContext<TPlugin, TPluginDependencies>(
createScopedRepository: deps.savedObjects.createScopedRepository,
createSerializer: deps.savedObjects.createSerializer,
getTypeRegistry: deps.savedObjects.getTypeRegistry,
getImportExportObjectLimit: deps.savedObjects.getImportExportObjectLimit,
},
uiSettings: {
asScopedToClient: deps.uiSettings.asScopedToClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const createStartContractMock = () => {
createScopedRepository: jest.fn(),
createSerializer: jest.fn(),
getTypeRegistry: jest.fn(),
getImportExportObjectLimit: jest.fn(),
};

startContrat.getScopedClient.mockReturnValue(savedObjectsClientMock.create());
Expand Down
5 changes: 5 additions & 0 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export interface SavedObjectsServiceStart {
* {@link SavedObjectsType | saved object types}
*/
getTypeRegistry: () => ISavedObjectTypeRegistry;
/**
* Returns the maximum number of objects allowed for import or export operations.
*/
getImportExportObjectLimit: () => number;
}

export interface InternalSavedObjectsServiceStart extends SavedObjectsServiceStart {
Expand Down Expand Up @@ -452,6 +456,7 @@ export class SavedObjectsService
createInternalRepository: repositoryFactory.createInternalRepository,
createSerializer: () => new SavedObjectsSerializer(this.typeRegistry),
getTypeRegistry: () => this.typeRegistry,
getImportExportObjectLimit: () => this.config!.maxImportExportSize,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,7 @@ export interface SavedObjectsServiceStart {
createInternalRepository: (extraTypes?: string[]) => ISavedObjectsRepository;
createScopedRepository: (req: KibanaRequest, extraTypes?: string[]) => ISavedObjectsRepository;
createSerializer: () => SavedObjectsSerializer;
getImportExportObjectLimit: () => number;
getScopedClient: (req: KibanaRequest, options?: SavedObjectsClientProviderOptions) => SavedObjectsClientContract;
getTypeRegistry: () => ISavedObjectTypeRegistry;
}
Expand Down
1 change: 0 additions & 1 deletion x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export const spaces = (kibana: Record<string, any>) =>
const { registerLegacyAPI, createDefaultSpace } = spacesPlugin.__legacyCompat;

registerLegacyAPI({
savedObjects: server.savedObjects,
auditLogger: {
create: (pluginId: string) =>
new AuditLogger(server, pluginId, server.config(), server.plugins.xpack_main.info),
Expand Down

This file was deleted.

182 changes: 120 additions & 62 deletions x-pack/plugins/spaces/server/lib/copy_to_spaces/copy_to_spaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
* you may not use this file except in compliance with the Elastic License.
*/
import {
SavedObjectsSchema,
SavedObjectsLegacyService,
SavedObjectsClientContract,
SavedObjectsImportResponse,
SavedObjectsImportOptions,
SavedObjectsExportOptions,
} from 'src/core/server';
import { copySavedObjectsToSpacesFactory } from './copy_to_spaces';
import { Readable } from 'stream';
import { coreMock, savedObjectsTypeRegistryMock, httpServerMock } from 'src/core/server/mocks';

jest.mock('src/core/server', () => {
return {
exportSavedObjectsToStream: jest.fn(),
importSavedObjectsFromStream: jest.fn(),
};
});
import { exportSavedObjectsToStream, importSavedObjectsFromStream } from 'src/core/server';

interface SetupOpts {
objects: Array<{ type: string; id: string; attributes: Record<string, any> }>;
getSortedObjectsForExportImpl?: (opts: SavedObjectsExportOptions) => Promise<Readable>;
importSavedObjectsImpl?: (opts: SavedObjectsImportOptions) => Promise<SavedObjectsImportResponse>;
exportSavedObjectsToStreamImpl?: (opts: SavedObjectsExportOptions) => Promise<Readable>;
importSavedObjectsFromStreamImpl?: (
opts: SavedObjectsImportOptions
) => Promise<SavedObjectsImportResponse>;
}

const expectStreamToContainObjects = async (
Expand All @@ -40,49 +48,73 @@ const expectStreamToContainObjects = async (

describe('copySavedObjectsToSpaces', () => {
const setup = (setupOpts: SetupOpts) => {
const savedObjectsClient = (null as unknown) as SavedObjectsClientContract;
const coreStart = coreMock.createStart();

const savedObjectsService: SavedObjectsLegacyService = ({
importExport: {
objectLimit: 1000,
getSortedObjectsForExport:
setupOpts.getSortedObjectsForExportImpl ||
jest.fn().mockResolvedValue(
new Readable({
objectMode: true,
read() {
setupOpts.objects.forEach(o => this.push(o));

this.push(null);
},
})
),
importSavedObjects:
setupOpts.importSavedObjectsImpl ||
jest.fn().mockImplementation(async (importOpts: SavedObjectsImportOptions) => {
await expectStreamToContainObjects(importOpts.readStream, setupOpts.objects);
const response: SavedObjectsImportResponse = {
success: true,
successCount: setupOpts.objects.length,
};
coreStart.savedObjects.getImportExportObjectLimit.mockReturnValue(1000);

return Promise.resolve(response);
}),
const typeRegistry = savedObjectsTypeRegistryMock.create();
typeRegistry.getAllTypes.mockReturnValue([
{
name: 'dashboard',
namespaceAgnostic: false,
hidden: false,
mappings: { properties: {} },
},
{
name: 'visualization',
namespaceAgnostic: false,
hidden: false,
mappings: { properties: {} },
},
{
name: 'globaltype',
namespaceAgnostic: true,
hidden: false,
mappings: { properties: {} },
},
types: ['dashboard', 'visualization', 'globalType'],
schema: new SavedObjectsSchema({
globalType: { isNamespaceAgnostic: true },
}),
} as unknown) as SavedObjectsLegacyService;
]);

coreStart.savedObjects.getTypeRegistry.mockReturnValue(typeRegistry);

(exportSavedObjectsToStream as jest.Mock).mockImplementation(
async (opts: SavedObjectsExportOptions) => {
return (
setupOpts.exportSavedObjectsToStreamImpl?.(opts) ??
new Readable({
objectMode: true,
read() {
setupOpts.objects.forEach(o => this.push(o));

this.push(null);
},
})
);
}
);

(importSavedObjectsFromStream as jest.Mock).mockImplementation(
async (opts: SavedObjectsImportOptions) => {
const defaultImpl = async () => {
await expectStreamToContainObjects(opts.readStream, setupOpts.objects);
const response: SavedObjectsImportResponse = {
success: true,
successCount: setupOpts.objects.length,
};

return Promise.resolve(response);
};

return setupOpts.importSavedObjectsFromStreamImpl?.(opts) ?? defaultImpl();
}
);

return {
savedObjectsClient,
savedObjectsService,
coreStart,
};
};

it('uses the Saved Objects Service to perform an export followed by a series of imports', async () => {
const { savedObjectsClient, savedObjectsService } = setup({
const { coreStart } = setup({
objects: [
{
type: 'dashboard',
Expand All @@ -102,10 +134,9 @@ describe('copySavedObjectsToSpaces', () => {
],
});

const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
savedObjectsClient,
savedObjectsService
);
const request = httpServerMock.createKibanaRequest();

const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(coreStart, request);

const result = await copySavedObjectsToSpaces('sourceSpace', ['destination1', 'destination2'], {
includeReferences: true,
Expand Down Expand Up @@ -133,8 +164,7 @@ describe('copySavedObjectsToSpaces', () => {
}
`);

expect((savedObjectsService.importExport.getSortedObjectsForExport as jest.Mock).mock.calls)
.toMatchInlineSnapshot(`
expect((exportSavedObjectsToStream as jest.Mock).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
Expand All @@ -148,14 +178,23 @@ describe('copySavedObjectsToSpaces', () => {
"type": "dashboard",
},
],
"savedObjectsClient": null,
"savedObjectsClient": Object {
"bulkCreate": [MockFunction],
"bulkGet": [MockFunction],
"bulkUpdate": [MockFunction],
"create": [MockFunction],
"delete": [MockFunction],
"errors": [Function],
"find": [MockFunction],
"get": [MockFunction],
"update": [MockFunction],
},
},
],
]
`);

expect((savedObjectsService.importExport.importSavedObjects as jest.Mock).mock.calls)
.toMatchInlineSnapshot(`
expect((importSavedObjectsFromStream as jest.Mock).mock.calls).toMatchInlineSnapshot(`
Array [
Array [
Object {
Expand Down Expand Up @@ -203,7 +242,17 @@ describe('copySavedObjectsToSpaces', () => {
},
"readable": false,
},
"savedObjectsClient": null,
"savedObjectsClient": Object {
"bulkCreate": [MockFunction],
"bulkGet": [MockFunction],
"bulkUpdate": [MockFunction],
"create": [MockFunction],
"delete": [MockFunction],
"errors": [Function],
"find": [MockFunction],
"get": [MockFunction],
"update": [MockFunction],
},
"supportedTypes": Array [
"dashboard",
"visualization",
Expand Down Expand Up @@ -256,7 +305,17 @@ describe('copySavedObjectsToSpaces', () => {
},
"readable": false,
},
"savedObjectsClient": null,
"savedObjectsClient": Object {
"bulkCreate": [MockFunction],
"bulkGet": [MockFunction],
"bulkUpdate": [MockFunction],
"create": [MockFunction],
"delete": [MockFunction],
"errors": [Function],
"find": [MockFunction],
"get": [MockFunction],
"update": [MockFunction],
},
"supportedTypes": Array [
"dashboard",
"visualization",
Expand Down Expand Up @@ -285,9 +344,10 @@ describe('copySavedObjectsToSpaces', () => {
attributes: {},
},
];
const { savedObjectsClient, savedObjectsService } = setup({

const { coreStart } = setup({
objects,
importSavedObjectsImpl: async opts => {
importSavedObjectsFromStreamImpl: async opts => {
if (opts.namespace === 'failure-space') {
throw new Error(`Some error occurred!`);
}
Expand All @@ -299,10 +359,9 @@ describe('copySavedObjectsToSpaces', () => {
},
});

const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
savedObjectsClient,
savedObjectsService
);
const request = httpServerMock.createKibanaRequest();

const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(coreStart, request);

const result = await copySavedObjectsToSpaces(
'sourceSpace',
Expand Down Expand Up @@ -343,7 +402,7 @@ describe('copySavedObjectsToSpaces', () => {
});

it(`handles stream read errors`, async () => {
const { savedObjectsClient, savedObjectsService } = setup({
const { coreStart } = setup({
objects: [
{
type: 'dashboard',
Expand All @@ -361,7 +420,7 @@ describe('copySavedObjectsToSpaces', () => {
attributes: {},
},
],
getSortedObjectsForExportImpl: opts => {
exportSavedObjectsToStreamImpl: opts => {
return Promise.resolve(
new Readable({
objectMode: true,
Expand All @@ -373,10 +432,9 @@ describe('copySavedObjectsToSpaces', () => {
},
});

const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
savedObjectsClient,
savedObjectsService
);
const request = httpServerMock.createKibanaRequest();

const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(coreStart, request);

await expect(
copySavedObjectsToSpaces(
Expand Down
Loading

0 comments on commit d3f86ab

Please sign in to comment.