Skip to content

Commit

Permalink
Add copy saved objects among workspaces functionality
Browse files Browse the repository at this point in the history
Signed-off-by: gaobinlong <[email protected]>

Signed-off-by: gaobinlong <[email protected]>
  • Loading branch information
gaobinlong committed Jul 19, 2023
1 parent 124896c commit ffa51fc
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock';
export { savedObjectsServiceMock } from './saved_objects/saved_objects_service.mock';
export { scopedHistoryMock } from './application/scoped_history.mock';
export { applicationServiceMock } from './application/application_service.mock';
export { workspacesServiceMock } from './fatal_errors/fatal_errors_service.mock';

function createCoreSetupMock({
basePath = '',
Expand Down
82 changes: 82 additions & 0 deletions src/core/server/saved_objects/routes/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { schema } from '@osd/config-schema';
import { IRouter } from '../../http';
import { SavedObjectConfig } from '../saved_objects_config';
import { exportSavedObjectsToStream } from '../export';
import { validateObjects } from './utils';
import { importSavedObjectsFromStream } from '../import';

export const registerCopyRoute = (router: IRouter, config: SavedObjectConfig) => {
const { maxImportExportSize } = config;

router.post(
{
path: '/_copy',
validate: {
body: schema.object({
objects: schema.maybe(
schema.arrayOf(
schema.object({
type: schema.string(),
id: schema.string(),
}),
{ maxSize: maxImportExportSize }
)
),
includeReferencesDeep: schema.boolean({ defaultValue: false }),
targetWorkspace: schema.string(),
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const savedObjectsClient = context.core.savedObjects.client;
const { objects, includeReferencesDeep, targetWorkspace } = req.body;

// need to access the registry for type validation, can't use the schema for this
const supportedTypes = context.core.savedObjects.typeRegistry
.getImportableAndExportableTypes()
.map((t) => t.name);

if (objects) {
const validationError = validateObjects(objects, supportedTypes);
if (validationError) {
return res.badRequest({
body: {
message: validationError,
},
});
}
}

const objectsListStream = await exportSavedObjectsToStream({
savedObjectsClient,
objects,
exportSizeLimit: maxImportExportSize,
includeReferencesDeep,
excludeExportDetails: true,
});

const result = await importSavedObjectsFromStream({
savedObjectsClient: context.core.savedObjects.client,
typeRegistry: context.core.savedObjects.typeRegistry,
readStream: objectsListStream,
objectLimit: maxImportExportSize,
overwrite: true,
createNewCopies: true,
workspaces: [targetWorkspace],
});

return res.ok({ body: result });
})
);
};
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { registerExportRoute } from './export';
import { registerImportRoute } from './import';
import { registerResolveImportErrorsRoute } from './resolve_import_errors';
import { registerMigrateRoute } from './migrate';
import { registerCopyRoute } from './copy';

export function registerRoutes({
http,
Expand All @@ -70,6 +71,7 @@ export function registerRoutes({
registerLogLegacyImportRoute(router, logger);
registerExportRoute(router, config);
registerImportRoute(router, config);
registerCopyRoute(router, config);
registerResolveImportErrorsRoute(router, config);

const internalRouter = http.createRouter('/internal/saved_objects/');
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/saved_objects_management/public/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export const SAVED_QUERIES_WORDINGS = i18n.translate(
defaultMessage: 'Saved filters',
}
);

export const SAVED_OBJECT_TYPE_WORKSAPCE = 'workspace';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { HttpStart } from 'src/core/public';

export async function copySavedObjects(
http: HttpStart,
objects: any[],
includeReferencesDeep: boolean = true,
targetWorkspace: string
) {
return await http.post('/api/saved_objects/_copy', {
body: JSON.stringify({
objects,
includeReferencesDeep,
targetWorkspace,
}),
});
}
1 change: 1 addition & 0 deletions src/plugins/saved_objects_management/public/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ export { extractExportDetails, SavedObjectsExportResultDetails } from './extract
export { createFieldList } from './create_field_list';
export { getAllowedTypes } from './get_allowed_types';
export { filterQuery } from './filter_query';
export { copySavedObjects } from './copy_saved_objects';
Loading

0 comments on commit ffa51fc

Please sign in to comment.