Skip to content

Commit

Permalink
[7.17] Handles non-existing objects in _copy_saved_objects API call (#…
Browse files Browse the repository at this point in the history
…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)](#158036)

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

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)
  • Loading branch information
jeramysoucy authored May 24, 2023
1 parent 38d9cb7 commit 4946ce0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
9 changes: 9 additions & 0 deletions docs/api/spaces-management/copy_saved_objects.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
40 changes: 28 additions & 12 deletions x-pack/plugins/spaces/server/routes/api/external/copy_to_space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
})
);

Expand Down

0 comments on commit 4946ce0

Please sign in to comment.