2023-12-17 02:14:12 +00:00
|
|
|
use crate::api::APIErrorResponse;
|
|
|
|
use crate::{NetworkCommands, TableStyle};
|
2023-09-30 21:48:29 +00:00
|
|
|
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
|
|
|
|
use comfy_table::presets::UTF8_FULL;
|
|
|
|
use comfy_table::Table;
|
2023-06-16 00:49:06 +00:00
|
|
|
use serde::Deserialize;
|
2023-12-17 02:14:12 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fs;
|
2023-06-16 00:49:06 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
pub async fn network_main(command: NetworkCommands, server: Url) -> Result<(), Box<dyn Error>> {
|
|
|
|
match command {
|
2023-09-30 21:48:29 +00:00
|
|
|
NetworkCommands::List { table_style } => list_networks(server, table_style).await,
|
2023-12-17 02:14:12 +00:00
|
|
|
NetworkCommands::Lookup { id } => get_network(id, server).await,
|
2023-06-16 00:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct NetworkListResp {
|
2023-11-23 20:23:52 +00:00
|
|
|
pub data: Vec<Network>,
|
2023-06-16 00:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Network {
|
|
|
|
pub id: String,
|
|
|
|
pub cidr: String,
|
|
|
|
#[serde(rename = "organizationID")]
|
|
|
|
pub organization_id: String,
|
|
|
|
#[serde(rename = "signingCAID")]
|
|
|
|
pub signing_ca_id: String,
|
|
|
|
#[serde(rename = "createdAt")]
|
|
|
|
pub created_at: String,
|
|
|
|
#[serde(rename = "lighthousesAsRelays")]
|
|
|
|
pub lighthouses_as_relays: bool,
|
2023-11-23 20:23:52 +00:00
|
|
|
pub name: String,
|
2023-06-16 00:49:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 21:48:29 +00:00
|
|
|
pub async fn list_networks(server: Url, table_style: TableStyle) -> Result<(), Box<dyn Error>> {
|
2023-06-16 00:49:06 +00:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
// load session token
|
|
|
|
let sess_token_store = dirs::config_dir().unwrap().join("tfcli-session.token");
|
|
|
|
let session_token = fs::read_to_string(&sess_token_store)?;
|
|
|
|
let auth_token_store = dirs::config_dir().unwrap().join("tfcli-auth.token");
|
|
|
|
let auth_token = fs::read_to_string(&auth_token_store)?;
|
|
|
|
|
|
|
|
let token = format!("{} {}", session_token, auth_token);
|
|
|
|
|
2023-11-23 20:23:52 +00:00
|
|
|
let res = client
|
|
|
|
.get(server.join("/v1/networks")?)
|
|
|
|
.bearer_auth(token)
|
|
|
|
.send()
|
|
|
|
.await?;
|
2023-06-16 00:49:06 +00:00
|
|
|
|
|
|
|
if res.status().is_success() {
|
|
|
|
let resp: NetworkListResp = res.json().await?;
|
|
|
|
|
|
|
|
if resp.data.is_empty() {
|
|
|
|
println!("No networks found");
|
2023-09-30 21:48:29 +00:00
|
|
|
return Ok(());
|
2023-06-16 00:49:06 +00:00
|
|
|
}
|
2023-09-30 21:48:29 +00:00
|
|
|
|
|
|
|
if matches!(table_style, TableStyle::List) {
|
|
|
|
for network in &resp.data {
|
|
|
|
println!(" Network: {}", network.id);
|
|
|
|
println!(" CIDR: {}", network.cidr);
|
|
|
|
println!(" Organization: {}", network.organization_id);
|
|
|
|
println!(" Signing CA: {}", network.signing_ca_id);
|
|
|
|
println!("Dedicated Relays: {}", !network.lighthouses_as_relays);
|
|
|
|
println!(" Name: {}", network.name);
|
|
|
|
println!(" Created At: {}", network.created_at);
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
return Ok(());
|
2023-06-16 00:49:06 +00:00
|
|
|
}
|
2023-09-30 21:48:29 +00:00
|
|
|
|
|
|
|
let mut table = Table::new();
|
|
|
|
match table_style {
|
|
|
|
TableStyle::List => unreachable!(),
|
|
|
|
TableStyle::Basic => (),
|
2023-12-17 02:14:12 +00:00
|
|
|
TableStyle::Pretty => {
|
|
|
|
table
|
|
|
|
.load_preset(UTF8_FULL)
|
|
|
|
.apply_modifier(UTF8_ROUND_CORNERS);
|
|
|
|
}
|
2023-09-30 21:48:29 +00:00
|
|
|
};
|
|
|
|
|
2023-12-17 02:14:12 +00:00
|
|
|
table.set_header(vec![
|
|
|
|
"ID",
|
|
|
|
"Name",
|
|
|
|
"CIDR",
|
|
|
|
"Organization ID",
|
|
|
|
"Signing CA ID",
|
|
|
|
"Dedicated Relays",
|
|
|
|
"Created At",
|
|
|
|
]);
|
2023-09-30 21:48:29 +00:00
|
|
|
|
|
|
|
for network in &resp.data {
|
2023-12-17 02:14:12 +00:00
|
|
|
table.add_row(vec![
|
|
|
|
&network.id,
|
|
|
|
&network.name,
|
|
|
|
&network.cidr,
|
|
|
|
&network.organization_id,
|
|
|
|
&network.signing_ca_id,
|
|
|
|
(!network.lighthouses_as_relays).to_string().as_str(),
|
|
|
|
&network.created_at,
|
|
|
|
]);
|
2023-09-30 21:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("{table}");
|
2023-06-16 00:49:06 +00:00
|
|
|
} else {
|
|
|
|
let resp: APIErrorResponse = res.json().await?;
|
|
|
|
|
2023-11-23 20:23:52 +00:00
|
|
|
eprintln!(
|
|
|
|
"[error] Error listing networks: {} {}",
|
|
|
|
resp.errors[0].code, resp.errors[0].message
|
|
|
|
);
|
2023-06-16 00:49:06 +00:00
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct NetworkGetResponse {
|
2023-11-23 20:23:52 +00:00
|
|
|
pub data: Network,
|
2023-06-16 00:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_network(id: String, server: Url) -> Result<(), Box<dyn Error>> {
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
|
|
|
// load session token
|
|
|
|
let sess_token_store = dirs::config_dir().unwrap().join("tfcli-session.token");
|
|
|
|
let session_token = fs::read_to_string(&sess_token_store)?;
|
|
|
|
let auth_token_store = dirs::config_dir().unwrap().join("tfcli-auth.token");
|
|
|
|
let auth_token = fs::read_to_string(&auth_token_store)?;
|
|
|
|
|
|
|
|
let token = format!("{} {}", session_token, auth_token);
|
|
|
|
|
2023-11-23 20:23:52 +00:00
|
|
|
let res = client
|
|
|
|
.get(server.join(&format!("/v1/networks/{}", id))?)
|
|
|
|
.bearer_auth(token)
|
|
|
|
.send()
|
|
|
|
.await?;
|
2023-06-16 00:49:06 +00:00
|
|
|
|
|
|
|
if res.status().is_success() {
|
|
|
|
let network: Network = res.json::<NetworkGetResponse>().await?.data;
|
|
|
|
|
|
|
|
println!(" Network: {}", network.id);
|
|
|
|
println!(" CIDR: {}", network.cidr);
|
|
|
|
println!(" Organization: {}", network.organization_id);
|
|
|
|
println!(" Signing CA: {}", network.signing_ca_id);
|
|
|
|
println!("Dedicated Relays: {}", !network.lighthouses_as_relays);
|
|
|
|
println!(" Name: {}", network.name);
|
|
|
|
println!(" Created At: {}", network.created_at);
|
|
|
|
} else {
|
|
|
|
let resp: APIErrorResponse = res.json().await?;
|
|
|
|
|
2023-11-23 20:23:52 +00:00
|
|
|
eprintln!(
|
|
|
|
"[error] Error listing networks: {} {}",
|
|
|
|
resp.errors[0].code, resp.errors[0].message
|
|
|
|
);
|
2023-06-16 00:49:06 +00:00
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|