Skip to main content

security_client_rs/
spaces.rs

1//! Kibana space administration. Space routes are global even on a scoped client.
2use serde::{Deserialize, Serialize};
3use serde_json::{Map, Value};
4
5use crate::{
6    Kibana, Scope,
7    http::{Empty, Method},
8    request::endpoint,
9};
10
11/// Space settings for administration. Selecting a client space with
12/// [`Kibana::space`] does not create or change these settings.
13#[derive(Clone, Debug, Serialize, Deserialize)]
14#[non_exhaustive]
15pub struct Space {
16    pub id: String,
17    pub name: String,
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub description: Option<String>,
20    #[serde(default, rename = "disabledFeatures")]
21    pub disabled_features: Vec<String>,
22    /// Fields such as `color`, `initials` and `solution`, kept for round trips.
23    #[serde(flatten)]
24    pub extra: Map<String, Value>,
25}
26
27impl Space {
28    pub fn new(id: impl Into<String>, name: impl Into<String>) -> Self {
29        Self {
30            id: id.into(),
31            name: name.into(),
32            description: None,
33            disabled_features: Vec::new(),
34            extra: Map::new(),
35        }
36    }
37}
38
39#[derive(Clone, Copy, Debug)]
40pub struct Spaces<'a>(pub(crate) &'a Kibana);
41
42impl<'a> Spaces<'a> {
43    pub fn list(&self) -> ListSpaces<'a> {
44        ListSpaces(
45            self.0
46                .request(Method::GET, Scope::Global, &["api", "spaces", "space"]),
47        )
48    }
49
50    pub fn get(&self, id: &str) -> GetSpace<'a> {
51        GetSpace(
52            self.0
53                .request(Method::GET, Scope::Global, &["api", "spaces", "space", id]),
54        )
55    }
56
57    /// Creates a space from a [`Space`] or any JSON object with `id` and `name`.
58    pub fn create<B: Serialize + ?Sized>(&self, space: &B) -> CreateSpace<'a> {
59        CreateSpace(
60            self.0
61                .request(Method::POST, Scope::Global, &["api", "spaces", "space"])
62                .json(space),
63        )
64    }
65
66    /// Replaces the space's definition. The JSON object body must contain a
67    /// string `id` matching the path argument. Missing, non-string or conflicting
68    /// IDs fail with [`crate::Error::InvalidRequest`] before HTTP.
69    pub fn update<B: Serialize + ?Sized>(&self, id: &str, space: &B) -> UpdateSpace<'a> {
70        UpdateSpace(
71            self.0
72                .request(Method::PUT, Scope::Global, &["api", "spaces", "space", id])
73                .json(space)
74                .check_body_id(id, true),
75        )
76    }
77
78    /// Deletes the space and every saved object inside it.
79    pub fn delete(&self, id: &str) -> DeleteSpace<'a> {
80        DeleteSpace(self.0.request(
81            Method::DELETE,
82            Scope::Global,
83            &["api", "spaces", "space", id],
84        ))
85    }
86}
87
88endpoint! {
89    /// `GET /api/spaces/space`
90    ListSpaces => Vec<Space>
91}
92
93endpoint! {
94    /// `GET /api/spaces/space/{id}`
95    GetSpace => Space
96}
97
98endpoint! {
99    /// `POST /api/spaces/space`
100    CreateSpace => Space
101}
102
103endpoint! {
104    /// `PUT /api/spaces/space/{id}`
105    UpdateSpace => Space
106}
107
108endpoint! {
109    /// `DELETE /api/spaces/space/{id}`
110    DeleteSpace => Empty
111}