[7.17] Handles non-existing objects in _copy_saved_objects API call (#158036) (#158415)

# Backport

This will backport the following commits from `main` to `7.17`:
- [Handles non-existing objects in _copy_saved_objects API call
(#158036)](https://github.com/elastic/kibana/pull/158036)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)
This commit is contained in:
Jeramy Soucy 2023-05-24 18:34:11 -04:00 committed by GitHub
parent 38d9cb7263
commit 4946ce0bb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 12 deletions

View file

@ -63,6 +63,15 @@ NOTE: This cannot be used with the `overwrite` option.
+
NOTE: This cannot be used with the `createNewCopies` option.
[[spaces-api-copy-saved-objects-response-codes]]
==== Response codes
`200`::
Indicates a successful call.
`404`::
Indicates that the request failed because one or more of the objects specified could not be found. A list of the unresolved objects are included in the 404 response attributes.
[role="child_attributes"]
[[spaces-api-copy-saved-objects-response-body]]
==== {api-response-body-title}

View file

@ -94,18 +94,34 @@ export function initCopyToSpacesApi(deps: ExternalRouteDeps) {
usageStatsClient.incrementCopySavedObjects({ headers, createNewCopies, overwrite })
);
const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
startServices.savedObjects,
request
);
const sourceSpaceId = getSpacesService().getSpaceId(request);
const copyResponse = await copySavedObjectsToSpaces(sourceSpaceId, destinationSpaceIds, {
objects,
includeReferences,
overwrite,
createNewCopies,
});
return response.ok({ body: copyResponse });
try {
const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
startServices.savedObjects,
request
);
const sourceSpaceId = getSpacesService().getSpaceId(request);
const copyResponse = await copySavedObjectsToSpaces(sourceSpaceId, destinationSpaceIds, {
objects,
includeReferences,
overwrite,
createNewCopies,
});
return response.ok({ body: copyResponse });
} catch (e: any) {
if (e.type === 'object-fetch-error' && e.attributes?.objects) {
return response.notFound({
body: {
message: 'Saved objects not found',
attributes: {
objects: e.attributes?.objects.map((obj: SavedObjectIdentifier) => ({
id: obj.id,
type: obj.type,
})),
},
},
});
} else throw e;
}
})
);