forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add copy saved objects among workspaces functionality (opensearch-pro…
…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
1 parent
ad29881
commit feef1d5
Showing
10 changed files
with
514 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/plugins/saved_objects_management/public/lib/copy_saved_objects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.