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

Commit

Permalink
Add 'iml debugapi' to iml cli (#2132)
Browse files Browse the repository at this point in the history
* Add 'iml debugapi' to iml cli

debugapi does not show up in help as it's for test/debug

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Fix formatting

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Address review comments

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Send json not strings

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Fix typo

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Fix method names

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Send data for DELETE also

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Make delete body optional

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Use map instead of if Some

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* Add HEAD and PATCH methods

This rounds out the methods that reqwest supports normally.

Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>

* updates

Signed-off-by: Joe Grund <jgrund@whamcloud.io>

Co-authored-by: Joe Grund <jgrund@whamcloud.io>
  • Loading branch information
utopiabound and jgrund authored Aug 7, 2020
1 parent d8297a4 commit 2f5c61b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
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),
};

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)]
/// 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

0 comments on commit 2f5c61b

Please sign in to comment.