feat: add router CLI

Closes #3331.
pull/24376/head
Marco Neumann 2021-12-08 11:52:23 +01:00
parent c17a6c10c1
commit b7d6865f87
4 changed files with 203 additions and 0 deletions

View File

@ -0,0 +1,101 @@
//! This module implements the `database` CLI command
use influxdb_iox_client::{
connection::Connection,
router::{self, generated_types::Router as RouterConfig},
};
use structopt::StructOpt;
use thiserror::Error;
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Error)]
pub enum Error {
#[error("Error formatting: {0}")]
FormattingError(#[from] influxdb_iox_client::format::Error),
#[error("Error querying: {0}")]
Query(#[from] influxdb_iox_client::flight::Error),
#[error("JSON Serialization error: {0}")]
Serde(#[from] serde_json::Error),
#[error("Client error: {0}")]
ClientError(#[from] influxdb_iox_client::error::Error),
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Manage IOx databases
#[derive(Debug, StructOpt)]
pub struct Config {
#[structopt(subcommand)]
command: Command,
}
/// Create a new router
#[derive(Debug, StructOpt)]
struct Update {
/// The name of the router
name: String,
}
/// Return configuration of specific router
#[derive(Debug, StructOpt)]
struct Get {
/// The name of the router
name: String,
}
/// Delete specific router
#[derive(Debug, StructOpt)]
struct Delete {
/// The name of the router
name: String,
}
/// All possible subcommands for database
#[derive(Debug, StructOpt)]
enum Command {
Update(Update),
List,
Get(Get),
Delete(Delete),
}
pub async fn command(connection: Connection, config: Config) -> Result<()> {
match config.command {
Command::Update(command) => {
let mut client = router::Client::new(connection);
let config = RouterConfig {
name: command.name.clone(),
..Default::default()
};
client.update_router(config).await?;
println!("Updated router {}", command.name);
}
Command::List => {
let mut client = router::Client::new(connection);
let routers = client.list_routers().await?;
for router in routers {
println!("{}", router.name);
}
}
Command::Get(get) => {
let Get { name } = get;
let mut client = router::Client::new(connection);
let router = client.get_router(&name).await?;
println!("{}", serde_json::to_string_pretty(&router)?);
}
Command::Delete(delete) => {
let Delete { name } = delete;
let mut client = router::Client::new(connection);
client.delete_router(&name).await?;
println!("Deleted router {}", name);
}
}
Ok(())
}

View File

@ -22,6 +22,7 @@ mod commands {
pub mod database;
pub mod debug;
pub mod operations;
pub mod router;
pub mod run;
pub mod server;
pub mod server_remote;
@ -147,6 +148,7 @@ enum Command {
Database(commands::database::Config),
// Clippy recommended boxing this variant because it's much larger than the others
Run(Box<commands::run::Config>),
Router(commands::router::Config),
Server(commands::server::Config),
Operation(commands::operations::Config),
Sql(commands::sql::Config),
@ -216,6 +218,14 @@ fn main() -> Result<(), std::io::Error> {
std::process::exit(ReturnCode::Failure as _)
}
}
Command::Router(config) => {
let _tracing_guard = handle_init_logs(init_simple_logs(log_verbose_count));
let connection = connection().await;
if let Err(e) = commands::router::command(connection, config).await {
eprintln!("{}", e);
std::process::exit(ReturnCode::Failure as _)
}
}
Command::Run(config) => {
let _tracing_guard =
handle_init_logs(init_logs_and_tracing(log_verbose_count, &config));

View File

@ -19,6 +19,7 @@ mod read_cli;
mod remote_api;
mod remote_cli;
mod router_api;
mod router_cli;
mod run_cli;
pub mod scenario;
mod sql_cli;

View File

@ -0,0 +1,91 @@
use crate::{
common::server_fixture::{ServerFixture, ServerType},
end_to_end_cases::scenario::rand_name,
};
use assert_cmd::Command;
use predicates::prelude::*;
#[tokio::test]
async fn test_router_crud() {
let server_fixture = ServerFixture::create_shared(ServerType::Router).await;
let addr = server_fixture.grpc_base();
let router_name = rand_name();
Command::cargo_bin("influxdb_iox")
.unwrap()
.arg("router")
.arg("get")
.arg(&router_name)
.arg("--host")
.arg(addr)
.assert()
.failure()
.stderr(predicate::str::contains(format!(
"Resource router/{} not found",
router_name,
)));
Command::cargo_bin("influxdb_iox")
.unwrap()
.arg("router")
.arg("update")
.arg(&router_name)
.arg("--host")
.arg(addr)
.assert()
.success()
.stdout(predicate::str::contains(format!(
"Updated router {}",
router_name
)));
Command::cargo_bin("influxdb_iox")
.unwrap()
.arg("router")
.arg("list")
.arg("--host")
.arg(addr)
.assert()
.success()
.stdout(predicate::str::contains(&router_name));
Command::cargo_bin("influxdb_iox")
.unwrap()
.arg("router")
.arg("get")
.arg(&router_name)
.arg("--host")
.arg(addr)
.assert()
.success()
.stdout(
predicate::str::contains(&router_name).and(predicate::str::contains(format!(
r#""name": "{}"#,
&router_name
))), // validate the defaults have been set reasonably
);
Command::cargo_bin("influxdb_iox")
.unwrap()
.arg("router")
.arg("delete")
.arg(&router_name)
.arg("--host")
.arg(addr)
.assert()
.success()
.stdout(predicate::str::contains(format!(
"Deleted router {}",
router_name
)));
Command::cargo_bin("influxdb_iox")
.unwrap()
.arg("router")
.arg("list")
.arg("--host")
.arg(addr)
.assert()
.success()
.stdout(predicate::str::contains(&router_name).not());
}