Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Add 'iml debugapi' to iml cli #2132

Merged
merged 11 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions iml-manager-cli/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2020 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

use crate::error::ImlManagerCliError;
use console::Term;
use structopt::{clap::arg_enum, StructOpt};

arg_enum! {
#[derive(PartialEq, Debug)]
pub enum ApiMethod {
Delete,
Get,
Head,
Patch,
Post,
Put,
}
}

#[derive(Debug, StructOpt)]
pub struct ApiCommand {
#[structopt(possible_values = &ApiMethod::variants(), case_insensitive = true)]
method: ApiMethod,

path: String,

/// JSON formatted body to send
#[structopt(required_ifs(&[("method", "patch"),("method", "post"),("method", "put")]))]
body: Option<String>,
}

pub async fn api_cli(command: ApiCommand) -> Result<(), ImlManagerCliError> {
let term = Term::stdout();
let client = iml_manager_client::get_client()?;
let uri = iml_manager_client::create_api_url(command.path)?;

let req = match command.method {
ApiMethod::Delete => client.delete(uri),
ApiMethod::Get => client.get(uri),
ApiMethod::Head => client.head(uri),
ApiMethod::Patch => client.patch(uri),
ApiMethod::Post => client.post(uri),
ApiMethod::Put => client.put(uri),
Comment on lines +39 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Headers and bodies are not supported, is it intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bodies are supported here: https://github.com/whamcloud/integrated-manager-for-lustre/pull/2132/files#diff-f33d40dcab0b4085abfc004fe3408e29R50-R54

Headers generally aren't needed (currently) when using the ```iml_manager_client``

};

let body: Option<serde_json::Value> =
command.body.map(|s| serde_json::from_str(&s)).transpose()?;

let req = if let Some(data) = body {
req.json(&data)
} else {
req
};

let resp = req.send().await?;
let resp_txt = format!("{:?}", resp);
let body = resp.text().await?;

match serde_json::from_str::<serde_json::Value>(&body) {
Ok(json) => term.write_line(&format!("{}", json)),
Err(_) => {
term.write_line(&resp_txt)?;
term.write_line(&format!("{}", body))?;

Ok(())
}
}?;

Ok(())
}
1 change: 1 addition & 0 deletions iml-manager-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

pub mod api;
pub mod api_utils;
pub mod display_utils;
pub mod error;
Expand Down
8 changes: 7 additions & 1 deletion iml-manager-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

use iml_manager_cli::{
api::{self, api_cli},
display_utils::display_error,
filesystem::{self, filesystem_cli},
server::{self, server_cli},
Expand Down Expand Up @@ -35,8 +36,12 @@ pub enum App {
command: filesystem::FilesystemCommand,
},
#[structopt(name = "update_repo")]
/// Update Agent repo files
/// Update Agent repo files
UpdateRepoFile(update_repo_file::UpdateRepoFileHosts),

#[structopt(name = "debugapi", setting = structopt::clap::AppSettings::Hidden)]
utopiabound marked this conversation as resolved.
Show resolved Hide resolved
/// Direct API Access (for testing and debug)
DebugApi(api::ApiCommand),
}

#[tokio::main]
Expand All @@ -54,6 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
App::Server { command } => server_cli(command).await,
App::UpdateRepoFile(config) => update_repo_file_cli(config).await,
App::Filesystem { command } => filesystem_cli(command).await,
App::DebugApi(command) => api_cli(command).await,
};

if let Err(e) = r {
Expand Down
2 changes: 1 addition & 1 deletion iml-manager-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn get_client() -> Result<Client, ImlManagerClientError> {
}

/// Given a path, constructs a full API url
fn create_api_url(path: impl ToString) -> Result<Url, ImlManagerClientError> {
pub fn create_api_url(path: impl ToString) -> Result<Url, ImlManagerClientError> {
let mut path = path.to_string();

if !path.ends_with('/') {
Expand Down