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

Commit

Permalink
Add debugql command to talk to /graphql endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Clark <nclark@whamcloud.com>
  • Loading branch information
utopiabound committed Sep 15, 2020
1 parent 73186ad commit 6bd1f87
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions iml-graphql-queries/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::fmt;
/// to the graphql server
#[derive(serde::Serialize, Debug)]
pub struct Query<T: serde::Serialize> {
query: String,
variables: Option<T>,
pub query: String,
pub variables: Option<T>,
}

#[derive(Debug, Clone, serde::Deserialize)]
Expand Down
41 changes: 37 additions & 4 deletions iml-manager-cli/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use crate::error::ImlManagerCliError;
use console::Term;
use iml_graphql_queries::Query;
use iml_manager_client::Url;
use structopt::{clap::arg_enum, StructOpt};
use tokio::io::{stdin, AsyncReadExt};

Expand Down Expand Up @@ -31,12 +33,42 @@ pub struct ApiCommand {
body: Option<String>,
}

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

query: String,

variables: Option<String>,
}

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

api_command(command.method, uri, command.body).await
}

pub async fn graphql_cli(command: GraphQlCommand) -> Result<(), ImlManagerCliError> {
let uri = iml_manager_client::create_url("/graphql")?;

let query = Query {
query: command.query,
variables: command.variables,
};

api_command(command.method, uri, serde_json::to_string(&query).ok()).await
}

async fn api_command(
method: ApiMethod,
uri: Url,
body: Option<String>,
) -> 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 {
let req = match method {
ApiMethod::Delete => client.delete(uri),
ApiMethod::Get => client.get(uri),
ApiMethod::Head => client.head(uri),
Expand All @@ -45,16 +77,17 @@ pub async fn api_cli(command: ApiCommand) -> Result<(), ImlManagerCliError> {
ApiMethod::Put => client.put(uri),
};

let body: Option<serde_json::Value> = if command.body == Some("-".to_string()) {
let body: Option<serde_json::Value> = if body == Some("-".to_string()) {
let mut buf: Vec<u8> = Vec::new();
stdin().read_to_end(&mut buf).await?;
let s = String::from_utf8_lossy(&buf);
Some(serde_json::from_str(&s)?)
} else {
command.body.map(|s| serde_json::from_str(&s)).transpose()?
body.map(|s| serde_json::from_str(&s)).transpose()?
};

let req = if let Some(data) = body {
tracing::debug!("Requesting: {}", &data);
req.json(&data)
} else {
req
Expand Down
7 changes: 6 additions & 1 deletion iml-manager-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

use iml_manager_cli::{
api::{self, api_cli},
api::{self, api_cli, graphql_cli},
display_utils::display_error,
filesystem::{self, filesystem_cli},
server::{self, server_cli},
Expand Down Expand Up @@ -50,6 +50,10 @@ pub enum App {
#[structopt(name = "debugapi", setting = structopt::clap::AppSettings::Hidden)]
/// Direct API Access (for testing and debug)
DebugApi(api::ApiCommand),

#[structopt(name = "debugql", setting = structopt::clap::AppSettings::Hidden)]
/// Direct GraphQL Access (for testing and debug)
DebugQl(api::GraphQlCommand),
}

#[tokio::main]
Expand All @@ -64,6 +68,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let r = match matches {
App::DebugApi(command) => api_cli(command).await,
App::DebugQl(command) => graphql_cli(command).await,
App::Filesystem { command } => filesystem_cli(command).await,
App::Server { command } => server_cli(command).await,
App::Snapshot { command } => snapshot_cli(command).await,
Expand Down
11 changes: 11 additions & 0 deletions iml-manager-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ pub fn get_client() -> Result<Client, ImlManagerClientError> {
.map_err(ImlManagerClientError::Reqwest)
}

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

let url = Url::parse(&iml_manager_env::get_manager_url())?
.join("/")?
.join(path.trim_start_matches('/'))?;

Ok(url)
}

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

0 comments on commit 6bd1f87

Please sign in to comment.