This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Add 'iml debugapi' to iml cli #2132
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c386725
Add 'iml debugapi' to iml cli
utopiabound bcb489d
Fix formatting
utopiabound 84edc25
Address review comments
utopiabound 89da5f5
Send json not strings
utopiabound da50f5d
Fix typo
utopiabound 98a79f7
Fix method names
utopiabound 8874617
Send data for DELETE also
utopiabound 55737af
Make delete body optional
utopiabound 76d0afd
Use map instead of if Some
utopiabound fc2c5a4
Add HEAD and PATCH methods
utopiabound 62c9da0
updates
jgrund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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``