diff --git a/rust/agama-cli/src/logs.rs b/rust/agama-cli/src/logs.rs index 1f8d881ce3..cc9a12da07 100644 --- a/rust/agama-cli/src/logs.rs +++ b/rust/agama-cli/src/logs.rs @@ -19,8 +19,8 @@ // find current contact information at www.suse.com. use agama_lib::base_http_client::BaseHTTPClient; -use agama_lib::logs::http_client::HTTPClient; use agama_lib::logs::set_archive_permissions; +use agama_lib::manager::http_client::ManagerHTTPClient as HTTPClient; use clap::Subcommand; use std::io; use std::path::PathBuf; @@ -41,7 +41,7 @@ pub enum LogsCommands { /// Main entry point called from agama CLI main loop pub async fn run(client: BaseHTTPClient, subcommand: LogsCommands) -> anyhow::Result<()> { - let client = HTTPClient::new(client)?; + let client = HTTPClient::new(client); match subcommand { LogsCommands::Store { destination } => { diff --git a/rust/agama-lib/src/logs.rs b/rust/agama-lib/src/logs.rs index 8f64d62684..7d9d7c2405 100644 --- a/rust/agama-lib/src/logs.rs +++ b/rust/agama-lib/src/logs.rs @@ -32,8 +32,6 @@ use std::process::Command; use tempfile::TempDir; use utoipa::ToSchema; -pub mod http_client; - const DEFAULT_COMMANDS: [(&str, &str); 3] = [ // (, ) ("journalctl -u agama", "agama"), diff --git a/rust/agama-lib/src/logs/http_client.rs b/rust/agama-lib/src/logs/http_client.rs deleted file mode 100644 index e0824dbf92..0000000000 --- a/rust/agama-lib/src/logs/http_client.rs +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) [2024] SUSE LLC -// -// All Rights Reserved. -// -// This program is free software; you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the Free -// Software Foundation; either version 2 of the License, or (at your option) -// any later version. -// -// This program is distributed in the hope that it will be useful, but WITHOUT -// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -// more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, contact SUSE LLC. -// -// To contact SUSE LLC about this file by physical or electronic mail, you may -// find current contact information at www.suse.com. - -use crate::logs::LogsLists; -use crate::{base_http_client::BaseHTTPClient, error::ServiceError}; -use reqwest::header::CONTENT_ENCODING; -use std::io::Cursor; -use std::path::{Path, PathBuf}; - -pub struct HTTPClient { - client: BaseHTTPClient, -} - -impl HTTPClient { - pub fn new(client: BaseHTTPClient) -> Result { - Ok(Self { client }) - } - - /// Downloads package of logs from the backend - /// - /// For now the path is path to a destination file without an extension. Extension - /// will be added according to the compression type found in the response - /// - /// Returns path to logs - pub async fn store(&self, path: &Path) -> Result { - // 1) response with logs - let response = self.client.get_raw("/manager/logs/store").await?; - - // 2) find out the destination file name - let ext = - &response - .headers() - .get(CONTENT_ENCODING) - .ok_or(ServiceError::CannotGenerateLogs(String::from( - "Invalid response", - )))?; - let mut destination = path.to_path_buf(); - - destination.set_extension( - ext.to_str() - .map_err(|_| ServiceError::CannotGenerateLogs(String::from("Invalid response")))?, - ); - - // 3) store response's binary content (logs) in a file - let mut file = std::fs::File::create(destination.as_path()).map_err(|_| { - ServiceError::CannotGenerateLogs(String::from("Cannot store received response")) - })?; - let mut content = Cursor::new(response.bytes().await?); - - std::io::copy(&mut content, &mut file).map_err(|_| { - ServiceError::CannotGenerateLogs(String::from("Cannot store received response")) - })?; - - Ok(destination) - } - - /// Asks backend for lists of log files and commands used for creating logs archive returned by - /// store (/logs/store) backed HTTP API command - pub async fn list(&self) -> Result { - Ok(self.client.get("/manager/logs/list").await?) - } -} diff --git a/rust/agama-lib/src/manager/http_client.rs b/rust/agama-lib/src/manager/http_client.rs index 7cfd373fda..30245bc954 100644 --- a/rust/agama-lib/src/manager/http_client.rs +++ b/rust/agama-lib/src/manager/http_client.rs @@ -18,7 +18,11 @@ // To contact SUSE LLC about this file by physical or electronic mail, you may // find current contact information at www.suse.com. +use crate::logs::LogsLists; use crate::{base_http_client::BaseHTTPClient, error::ServiceError}; +use reqwest::header::CONTENT_ENCODING; +use std::io::Cursor; +use std::path::{Path, PathBuf}; pub struct ManagerHTTPClient { client: BaseHTTPClient, @@ -34,4 +38,48 @@ impl ManagerHTTPClient { // so we pass () which is rendered as `null` self.client.post_void("/manager/probe_sync", &()).await } + + /// Downloads package of logs from the backend + /// + /// For now the path is path to a destination file without an extension. Extension + /// will be added according to the compression type found in the response + /// + /// Returns path to logs + pub async fn store(&self, path: &Path) -> Result { + // 1) response with logs + let response = self.client.get_raw("/manager/logs/store").await?; + + // 2) find out the destination file name + let ext = + &response + .headers() + .get(CONTENT_ENCODING) + .ok_or(ServiceError::CannotGenerateLogs(String::from( + "Invalid response", + )))?; + let mut destination = path.to_path_buf(); + + destination.set_extension( + ext.to_str() + .map_err(|_| ServiceError::CannotGenerateLogs(String::from("Invalid response")))?, + ); + + // 3) store response's binary content (logs) in a file + let mut file = std::fs::File::create(destination.as_path()).map_err(|_| { + ServiceError::CannotGenerateLogs(String::from("Cannot store received response")) + })?; + let mut content = Cursor::new(response.bytes().await?); + + std::io::copy(&mut content, &mut file).map_err(|_| { + ServiceError::CannotGenerateLogs(String::from("Cannot store received response")) + })?; + + Ok(destination) + } + + /// Asks backend for lists of log files and commands used for creating logs archive returned by + /// store (/logs/store) backed HTTP API command + pub async fn list(&self) -> Result { + Ok(self.client.get("/manager/logs/list").await?) + } }