kibana/docs/api/spaces-management/update_objects_spaces.asciidoc
Jeramy Soucy 1589547b6f
Handles non-existing objects in _copy_saved_objects API call (#158036)
Closes #156791

## Summary

This PR implements catching the error thrown by the
saved_objects_exporter when an object is not found, and responding with
a detailed 404 ("Not Found") rather than a generic 500 ("Internal Server
Error") response message.

### Example Response:
```
{
  "statusCode": 404,
  "error": "Not Found",
  "message": "Saved objects not found",
  "attributes": {
    "objects": [
      {
        "id": "7adfa750-4c81-11e8-b3d7-01146121b73d",
        "type": "dashboard"
      },
      {
        "id": "571aaf70-4c88-11e8-b3d7-01146121b73d",
        "type": "search"
      },
      {
        "id": "d3d7af60-4c81-11e8-b3d7-01146121b73d",
        "type": "index-pattern"
      }
    ]
  }
}
```

### Testing

1. Install sample flight data, find the id of the flights dashboard SO
2. Create an additional space 'b'
3. Issue a request to copy saved objects to space b
```
POST kbn:/api/spaces/_copy_saved_objects
{
  "spaces": [
    "b"
  ],
  "objects": [
    {
      "type": "dashboard",
      "id": "7adfa750-4c81-11e8-b3d7-01146121b73e"
    },
    {
      "type": "dashboard",
      "id": "7adfa750-4c81-11e8-b3d7-01146121b73f"
    },
    {
      "type": "dashboard",
      "id": "7adfa750-4c81-11e8-b3d7-01146121b73g"
    }
  ]
}
```
4. Verify response
```
{
  "statusCode": 404,
  "error": "Not Found",
  "message": "Saved objects not found",
  "attributes": {
    "objects": [
      {
        "type": "dashboard",
        "id": "7adfa750-4c81-11e8-b3d7-01146121b73e"
      },
      {
        "type": "dashboard",
        "id": "7adfa750-4c81-11e8-b3d7-01146121b73f"
      },
      {
        "type": "dashboard",
        "id": "7adfa750-4c81-11e8-b3d7-01146121b73g"
      }
    ]
  }
}
```
5. Issue a request to copy the flights dashboard SO
6. Verify the usual response (200, missing references)
7. Issue a malformed request to copy an SO
8. Verify status 400 response with details
2023-05-24 12:20:46 -04:00

151 lines
3.7 KiB
Text

[role="xpack"]
[[spaces-api-update-objects-spaces]]
=== Update saved objects spaces API
++++
<titleabbrev>Update saved objects spaces</titleabbrev>
++++
experimental[] Update saved objects spaces.
Updates one or more saved objects to add and/or remove them from specified spaces.
[[spaces-api-update-objects-spaces-request]]
==== {api-request-title}
`POST <kibana host>:<port>/api/spaces/_update_objects_spaces`
[[spaces-api-update-objects-spaces-request-body]]
==== {api-request-body-title}
`objects`::
(Required, object array) The saved objects to update.
+
.Properties of `objects`
[%collapsible%open]
=====
`type`:::
(Required, string) The saved object type.
`id`:::
(Required, string) The saved object ID.
=====
`spacesToAdd`::
(Required, string array) The IDs of the spaces the specified objects should be added to.
`spacesToRemove`::
(Required, string array) The IDs of the spaces the specified objects should be removed from.
[[spaces-api-update-objects-spaces-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-update-objects-spaces-response-body]]
==== {api-response-body-title}
`objects`::
(object array) The saved objects that have been updated.
+
.Properties of `objects`
[%collapsible%open]
=====
`type`:::
(string) The saved object type.
`id`:::
(string) The saved object ID.
`spaces`:::
(string array) The space(s) that the referenced saved object exists in.
`errors`:::
(string) Included if there was an error updating this object's spaces.
=====
[[spaces-api-update-objects-spaces-example]]
==== {api-examples-title}
[[spaces-api-update-objects-spaces-example-1]]
===== Sharing saved objects
To share a saved object to a space programmatically follow these steps:
1. Collect reference graph and spaces context for each saved object that you want to share using <<spaces-api-get-shareable-references, Get shareable references API>>:
+
[source,sh]
----
$ curl -X POST /api/spaces/_get_shareable_references
{
"objects": [
{
"type": "index-pattern",
"id": "90943e30-9a47-11e8-b64d-95841ca0b247"
}
]
}
----
+
The API returns the following:
+
[source,json]
----
{
"objects": [
{
"type": "index-pattern",
"id": "90943e30-9a47-11e8-b64d-95841ca0b247",
"spaces": ["default"],
"inboundReferences": [],
"spacesWithMatchingOrigins": ["default"]
}
]
}
----
2. Check each saved object for `spacesWithMatchingOrigins` conflicts.
+
Objects should not be shared to spaces with matching origins or you will create URL conflicts (causing the same URL to point to different saved objects).
3. Check each saved object for `spacesWithMatchingAliases` conflicts.
+
If these match the space(s) that these saved objects will be shared to you should disable legacy URL aliases for them using <<spaces-api-disable-legacy-url-aliases, Disable legacy URL aliases API>>.
+
When sharing to all spaces (`*`) all entries in `spacesWithMatchingAliases` should be checked.
4. Update spaces of each saved object and all its references:
+
[source,sh]
----
$ curl -X POST /api/spaces/_update_objects_spaces
{
"objects": [
{
"type": "index-pattern",
"id": "90943e30-9a47-11e8-b64d-95841ca0b247"
}
],
"spacesToAdd": ["test"],
"spacesToRemove": []
}
----
+
The API returns the following:
+
[source,json]
----
{
"objects": [
{
"type": "index-pattern",
"id": "90943e30-9a47-11e8-b64d-95841ca0b247",
"spaces": ["default", "test"]
}
]
}
----