use std::fmt;
use serde_json::Value;
use crate::{
Error, Request, Result,
cases::Cases,
exceptions::Exceptions,
fleet::Fleet,
http::{Method, Transport},
request::endpoint,
roles::Roles,
security::Security,
spaces::Spaces,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Scope {
Global,
Space,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SortOrder {
Asc,
Desc,
}
impl SortOrder {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Asc => "asc",
Self::Desc => "desc",
}
}
}
#[derive(Clone)]
pub struct Kibana {
transport: Transport,
space: Option<String>,
}
impl Kibana {
pub fn new(transport: Transport) -> Self {
Self {
transport,
space: None,
}
}
pub fn transport(&self) -> &Transport {
&self.transport
}
pub fn space(&self, id: impl Into<String>) -> Result<Self> {
let id = id.into();
if id.is_empty() || id == "." || id == ".." || id.contains('/') {
return Err(Error::Configuration("invalid space ID".into()));
}
Ok(Self {
transport: self.transport.clone(),
space: Some(id),
})
}
pub fn default_space(&self) -> Self {
Self {
transport: self.transport.clone(),
space: None,
}
}
pub fn space_id(&self) -> Option<&str> {
self.space.as_deref()
}
pub fn security(&self) -> Security<'_> {
Security(self)
}
pub fn exceptions(&self) -> Exceptions<'_> {
Exceptions(self)
}
pub fn cases(&self) -> Cases<'_> {
Cases(self)
}
pub fn fleet(&self) -> Fleet<'_> {
Fleet(self)
}
pub fn spaces(&self) -> Spaces<'_> {
Spaces(self)
}
pub fn roles(&self) -> Roles<'_> {
Roles(self)
}
pub fn status(&self) -> Status<'_> {
Status(self.request(Method::GET, Scope::Global, &["api", "status"]))
}
pub fn request(&self, method: Method, scope: Scope, segments: &[&str]) -> Request<'_> {
Request::new(self, method, scope, segments)
}
}
impl fmt::Debug for Kibana {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Kibana")
.field("transport", &self.transport)
.field("space", &self.space)
.finish()
}
}
endpoint! {
Status => Value
}