From 145130481eac30793c6c08caa4d208ddddfc30e8 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 30 Jan 2025 10:44:08 +0100 Subject: wip: proxy admin api requests through admin rpc, prepare new cli --- src/garage/cli_v2/mod.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/garage/cli_v2/mod.rs (limited to 'src/garage/cli_v2/mod.rs') diff --git a/src/garage/cli_v2/mod.rs b/src/garage/cli_v2/mod.rs new file mode 100644 index 00000000..6cf068c6 --- /dev/null +++ b/src/garage/cli_v2/mod.rs @@ -0,0 +1,63 @@ +use std::collections::{HashMap, HashSet}; +use std::convert::TryFrom; +use std::sync::Arc; +use std::time::Duration; + +use format_table::format_table; +use garage_util::error::*; + +use garage_rpc::layout::*; +use garage_rpc::system::*; +use garage_rpc::*; + +use garage_api::admin::api::*; +use garage_api::admin::EndpointHandler as AdminApiEndpoint; + +use crate::admin::*; +use crate::cli::*; + +pub struct Cli { + pub system_rpc_endpoint: Arc>, + pub admin_rpc_endpoint: Arc>, + pub rpc_host: NodeID, +} + +impl Cli { + pub async fn handle(&self, cmd: Command) -> Result<(), Error> { + println!("{:?}", self.api_request(GetClusterStatusRequest).await?); + Ok(()) + /* + match cmd { + _ => todo!(), + } + */ + } + + pub async fn api_request(&self, req: T) -> Result<::Response, Error> + where + T: AdminApiEndpoint, + AdminApiRequest: From, + ::Response: TryFrom, + { + let req = AdminApiRequest::from(req); + let req_name = req.name(); + match self + .admin_rpc_endpoint + .call(&self.rpc_host, AdminRpc::ApiRequest(req), PRIO_NORMAL) + .await? + .ok_or_message("xoxo")? + { + AdminRpc::ApiOkResponse(resp) => ::Response::try_from(resp) + .map_err(|_| Error::Message(format!("{} returned unexpected response", req_name))), + AdminRpc::ApiErrorResponse { + http_code, + error_code, + message, + } => Err(Error::Message(format!( + "{} returned {} ({}): {}", + req_name, error_code, http_code, message + ))), + m => Err(Error::unexpected_rpc_message(m)), + } + } +} -- cgit v1.2.3