Skip to content

Commit

Permalink
feat(kudoctl): get instances command
Browse files Browse the repository at this point in the history
Signed-off-by: Nils Ponsard <[email protected]>
  • Loading branch information
nponsard authored and sameo committed Aug 24, 2022
1 parent ee76073 commit 1ff2523
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
39 changes: 38 additions & 1 deletion kudoctl/src/client/instance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Context;
use log::debug;
use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::client::types::IdResponse;
use crate::{client::types::IdResponse, resource::workload};

use super::request::Client;

Expand All @@ -21,3 +22,39 @@ pub async fn create(client: &Client, workload_id: &String) -> anyhow::Result<Str
debug!("Instance {} created", response.id);
Ok(response.id)
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Instance {
pub id: String,
pub name: String,
pub r#type: String,
pub uri: String,
pub ports: Vec<String>,
pub env: Vec<String>,
pub resources: workload::Resources,
pub status: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct GetInstancesResponse {
pub count: u64,
pub instances: Vec<Instance>,

/// used for formatting in the Display impl
#[serde(skip)]
pub show_header: bool,
}

/// List the instances in the cluster.
pub async fn list(client: &Client) -> anyhow::Result<GetInstancesResponse> {
let response: GetInstancesResponse = (*client)
.send_json_request::<GetInstancesResponse, ()>("/instance", Method::GET, None)
.await
.context("Error getting instances")?;
debug!(
"{} total instances, {} instances received ",
response.count,
response.instances.len()
);
Ok(response)
}
33 changes: 33 additions & 0 deletions kudoctl/src/subcommands/get/instances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::output::{self, OutputFormat};
use crate::{
client::{self, instance::GetInstancesResponse, request::Client},
config,
};
use anyhow::{Context, Result};
use std::fmt::Display;

/// get instances subcommand execution
/// Does the request, then formats the output.
pub async fn execute(
conf: &config::Config,
format: OutputFormat,
show_header: bool,
) -> Result<String> {
let client = Client::new(conf).context("Error creating client")?;
let mut result = client::instance::list(&client).await?;
result.show_header = show_header;
output::format_output(result, format)
}

impl Display for GetInstancesResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.show_header {
writeln!(f, "ID\tSTATUS")?;
}

for inst in &self.instances {
writeln!(f, "{}\t{}\n", inst.id, inst.status)?;
}
Ok(())
}
}
5 changes: 3 additions & 2 deletions kudoctl/src/subcommands/get/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod instances;
mod output;
mod resources;

use self::output::OutputFormat;
use crate::config;
use anyhow::{bail, Result};
Expand Down Expand Up @@ -47,7 +47,8 @@ pub async fn execute(args: GetSubcommand, conf: &config::Config) -> Result<Strin

match args.subject {
GetSubjects::Resources => resources::execute(conf, format, show_header).await,
GetSubjects::Resource | GetSubjects::Instances | GetSubjects::Instance => {
GetSubjects::Instances => instances::execute(conf, format, show_header).await,
GetSubjects::Resource | GetSubjects::Instance => {
bail!(format!("{:?} not implemented yet", args.subject))
}
}
Expand Down
4 changes: 0 additions & 4 deletions kudoctl/src/subcommands/get/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ use crate::{
config, resource,
};
use anyhow::{Context, Result};
use clap::Args;
use std::fmt::Display;

use super::output::{self, OutputFormat}; // import without risk of name clashing

#[derive(Debug, Args)]
pub struct GetResources {}

/// get resources subcommand execution
pub async fn execute(
conf: &config::Config,
Expand Down

0 comments on commit 1ff2523

Please sign in to comment.