Skip to content

Commit

Permalink
Add copy saved objects among workspaces functionality (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#53)

* Add copy saved objects among workspaces functionality

Signed-off-by: gaobinlong <gbinlong@amazon.com>

Signed-off-by: gaobinlong <gbinlong@amazon.com>

* Fix bug

Signed-off-by: gaobinlong <gbinlong@amazon.com>

* Fix bug

Signed-off-by: gaobinlong <gbinlong@amazon.com>

---------

Signed-off-by: gaobinlong <gbinlong@amazon.com>
  • Loading branch information
gaobinlong authored Jul 21, 2023
1 parent ad29881 commit feef1d5
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 2 deletions.
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: false,
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 feef1d5

Please sign in to comment.