Skip to content

Commit

Permalink
Fixes #15883: Refector api code in relayd
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Oct 7, 2019
1 parent bafb7d1 commit 0a7551f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 53 deletions.
6 changes: 3 additions & 3 deletions relay/sources/relayd/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 24 additions & 35 deletions relay/sources/relayd/src/api.rs
Expand Up @@ -28,13 +28,20 @@
// You should have received a copy of the GNU General Public License
// along with Rudder. If not, see <http://www.gnu.org/licenses/>.

mod remote_run;
mod shared_files;
mod shared_folder;
pub mod system;

use crate::{
api::{
remote_run::{RemoteRun, RemoteRunTarget},
shared_files::{SharedFilesHeadParams, SharedFilesPutParams},
shared_folder::SharedFolderParams,
system::{Info, Status},
},
error::Error,
remote_run::{RemoteRun, RemoteRunTarget},
shared_files::{self, SharedFilesHeadParams, SharedFilesPutParams},
shared_folder::{self, SharedFolderParams},
stats::Stats,
status::Status,
JobConfig,
};
use futures::Future;
Expand All @@ -46,7 +53,6 @@ use std::{
path::Path,
sync::{Arc, RwLock},
};
use structopt::clap::crate_version;
use tracing::info;
use warp::{
body::{self, FullBody},
Expand All @@ -57,26 +63,6 @@ use warp::{
reply, Filter, Reply,
};

#[derive(Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct Info {
pub major_version: String,
pub full_version: String,
}

impl Info {
fn new() -> Self {
Info {
major_version: format!(
"{}.{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR")
),
full_version: crate_version!().to_string(),
}
}
}

#[derive(Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ApiResult {
Expand All @@ -97,16 +83,19 @@ struct ApiResponse<T: Serialize> {

impl<T: Serialize> ApiResponse<T> {
fn new<E: Display>(action: &'static str, data: Result<T, E>) -> Self {
let (data, result, error_details) = match data {
Ok(d) => (Some(d), ApiResult::Success, None),
Err(e) => (None, ApiResult::Error, Some(e.to_string())),
};

Self {
data,
result,
action,
error_details,
match data {
Ok(d) => ApiResponse {
data: Some(d),
result: ApiResult::Success,
action,
error_details: None,
},
Err(e) => ApiResponse {
data: None,
result: ApiResult::Error,
action,
error_details: Some(e.to_string()),
},
}
}

Expand Down
File renamed without changes.
Expand Up @@ -119,12 +119,6 @@ pub fn validate_signature(
verifier.verify(digest)
}

pub fn metadata_parser(buf: &mut FullBody) -> Result<Metadata, Error> {
let mut metadata: Vec<u8> = Vec::new();
let _ = buf.reader().read_to_end(&mut metadata)?;
Metadata::from_str(str::from_utf8(&metadata)?)
}

fn get_pubkey(pubkey: &str) -> Result<PKey<Public>, ErrorStack> {
PKey::from_rsa(Rsa::public_key_from_pem_pkcs1(
format!(
Expand Down
File renamed without changes.
Expand Up @@ -31,7 +31,27 @@
use crate::{api::ApiResult, check_configuration, output::database::ping, Error, JobConfig};
use serde::Serialize;
use std::sync::Arc;
use structopt::clap::crate_version;

#[derive(Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct Info {
pub major_version: String,
pub full_version: String,
}

impl Info {
pub fn new() -> Self {
Info {
major_version: format!(
"{}.{}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR")
),
full_version: crate_version!().to_string(),
}
}
}
#[derive(Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub struct State {
Expand Down
2 changes: 1 addition & 1 deletion relay/sources/relayd/src/data/node.rs
Expand Up @@ -28,7 +28,7 @@
// You should have received a copy of the GNU General Public License
// along with Rudder. If not, see <http://www.gnu.org/licenses/>.

use crate::{error::Error, status::NodeCounts};
use crate::{error::Error, api::system::NodeCounts};
use openssl::{stack::Stack, x509::X509};
use serde::Deserialize;
use serde_json;
Expand Down
4 changes: 0 additions & 4 deletions relay/sources/relayd/src/lib.rs
Expand Up @@ -41,11 +41,7 @@ pub mod hashing;
pub mod input;
pub mod output;
pub mod processing;
pub mod remote_run;
pub mod shared_files;
pub mod shared_folder;
pub mod stats;
pub mod status;

use crate::{
api::api,
Expand Down
8 changes: 4 additions & 4 deletions relay/sources/relayd/src/relayd.rs
Expand Up @@ -36,7 +36,7 @@ use structopt::StructOpt;
use tracing::error;

/// Sets exit code based on error type
fn error_code(e: Error) -> i32 {
fn error_code(e: &Error) -> i32 {
match e {
Error::ConfigurationParsing(_) => 2,
_ => 1,
Expand All @@ -47,21 +47,21 @@ fn error_code(e: Error) -> i32 {
fn main() {
let cli_cfg = CliConfiguration::from_args();
if cli_cfg.check_configuration {
if let Err(e) = check_configuration(&cli_cfg.configuration_dir) {
if let Err(ref e) = check_configuration(&cli_cfg.configuration_dir) {
println!("{}", e);
exit(error_code(e));
}
println!("Syntax: OK");
} else {
let reload_handle = match init_logger() {
Ok(handle) => handle,
Err(e) => {
Err(ref e) => {
println!("{}", e);
exit(error_code(e));
}
};

if let Err(e) = start(cli_cfg, reload_handle) {
if let Err(ref e) = start(cli_cfg, reload_handle) {
error!("{}", e);
exit(error_code(e));
}
Expand Down

0 comments on commit 0a7551f

Please sign in to comment.