garage/src/api/admin/router.rs

87 lines
2.1 KiB
Rust
Raw Normal View History

2022-05-11 08:27:40 +00:00
use std::borrow::Cow;
use hyper::{Method, Request};
2022-05-11 08:27:40 +00:00
use crate::error::*;
use crate::router_macros::*;
pub enum Authorization {
MetricsToken,
AdminToken,
}
router_match! {@func
/// List of all Admin API endpoints.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint {
Options,
2022-05-05 11:40:31 +00:00
Metrics,
GetClusterStatus,
GetClusterLayout,
UpdateClusterLayout,
ApplyClusterLayout,
RevertClusterLayout,
2022-05-11 08:27:40 +00:00
ListKeys,
CreateKey,
GetKeyInfo {
id: Option<String>,
search: Option<String>,
2022-05-11 08:27:40 +00:00
},
DeleteKey {
id: String,
},
UpdateKey {
id: String,
},
}}
impl Endpoint {
/// Determine which S3 endpoint a request is for using the request, and a bucket which was
/// possibly extracted from the Host header.
/// Returns Self plus bucket name, if endpoint is not Endpoint::ListBuckets
pub fn from_request<T>(req: &Request<T>) -> Result<Self, Error> {
2022-05-11 08:27:40 +00:00
let uri = req.uri();
let path = uri.path();
let query = uri.query();
let mut query = QueryParameters::from_query(query.unwrap_or_default())?;
let res = router_match!(@gen_path_parser (req.method(), path, query) [
OPTIONS _ => Options,
GET "/metrics" => Metrics,
GET "/status" => GetClusterStatus,
// Layout endpoints
GET "/layout" => GetClusterLayout,
POST "/layout" => UpdateClusterLayout,
POST "/layout/apply" => ApplyClusterLayout,
POST "/layout/revert" => RevertClusterLayout,
// API key endpoints
GET "/key" if id => GetKeyInfo (query_opt::id, query_opt::search),
GET "/key" if search => GetKeyInfo (query_opt::id, query_opt::search),
2022-05-11 08:27:40 +00:00
POST "/key" if id => UpdateKey (query::id),
POST "/key" => CreateKey,
DELETE "/key" if id => DeleteKey (query::id),
GET "/key" => ListKeys,
]);
if let Some(message) = query.nonempty_message() {
debug!("Unused query parameter: {}", message)
}
Ok(res)
}
/// Get the kind of authorization which is required to perform the operation.
pub fn authorization_type(&self) -> Authorization {
match self {
Self::Metrics => Authorization::MetricsToken,
_ => Authorization::AdminToken,
}
}
}
2022-05-11 08:27:40 +00:00
generateQueryParameters! {
"id" => id,
"search" => search
2022-05-11 08:27:40 +00:00
}