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 7 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
52 changes: 52 additions & 0 deletions iml-manager-cli/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 ApiType {
utopiabound marked this conversation as resolved.
Show resolved Hide resolved
Delete,
Get,
Post,
Put,
}
}

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

path: String,

/// JSON formatted body to send
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 body: serde_json::Value = serde_json::from_str(&command.body.unwrap_or("{}".to_string()))?;

if let Some(resp) = match command.call {
ApiType::Delete => Some(client.delete(uri).json(&body).send().await?),
utopiabound marked this conversation as resolved.
Show resolved Hide resolved
ApiType::Get => {
let resp = client.get(uri).send().await?;
let data: serde_json::Value = resp.json().await?;
term.write_line(&format!("{}", data))?;
None
}
ApiType::Post => Some(client.post(uri).json(&body).send().await?),
ApiType::Put => Some(client.put(uri).json(&body).send().await?),
} {
term.write_line(&format!("{:?}", resp))?;
term.write_line(&format!("{}", resp.text().await?))?;
}

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