Skip to content

Commit

Permalink
Minor refactor the JSON output of a node's available resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ccicconetti committed Oct 18, 2024
1 parent 15a5027 commit cef4e2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
27 changes: 6 additions & 21 deletions edgeless_node/src/bin/edgeless_node_d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use edgeless_node::resources::http_ingress::HttpIngressResourceSpec;
use edgeless_node::resources::kafka_egress::KafkaEgressResourceSpec;
use edgeless_node::resources::ollama::OllamasResourceSpec;
use edgeless_node::resources::redis::RedisResourceSpec;
use edgeless_node::resources::resource_provider_specs::ResourceProviderSpecOutput;
use edgeless_node::resources::resource_provider_specs::ResourceProviderSpecs;

#[derive(Debug, clap::Parser)]
Expand Down Expand Up @@ -47,27 +48,11 @@ fn main() -> anyhow::Result<()> {
specs.push(Box::new(KafkaEgressResourceSpec {}));

if args.output_json {
let mut json_output = String::from("[");
for (i, spec) in specs.iter().enumerate() {
let resource_json = format!(
r#"{{"class_type": "{}", "version": "{}", "outputs": [{}], "configurations": [{}]}}"#,
spec.class_type(),
spec.version(),
spec.outputs().iter().map(|o| format!(r#""{}""#, o)).collect::<Vec<String>>().join(", "),
spec.configurations()
.iter()
.map(|(field, desc)| format!(r#"{{"field": "{}", "desc": "{}"}}"#, field, desc))
.collect::<Vec<String>>()
.join(", ")
);

json_output.push_str(&resource_json);
if i < specs.len() - 1 {
json_output.push_str(", ");
}
}
json_output.push_str("]");
println!("{}", json_output);
println!(
"{}",
serde_json::to_string(&specs.iter().map(|x| x.to_output()).collect::<Vec<ResourceProviderSpecOutput>>())
.expect("could not serialize available resources to JSON")
);
} else {
for spec in specs {
println!("class_type: {}", spec.class_type());
Expand Down
19 changes: 19 additions & 0 deletions edgeless_node/src/resources/resource_provider_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@ pub trait ResourceProviderSpecs {
fn configurations(&self) -> std::collections::HashMap<String, String>;
fn version(&self) -> String;
}

#[derive(serde::Serialize)]
pub struct ResourceProviderSpecOutput {
class_type: String,
version: String,
outputs: Vec<String>,
configurations: std::collections::HashMap<String, String>,
}

impl dyn ResourceProviderSpecs {
pub fn to_output(&self) -> ResourceProviderSpecOutput {
ResourceProviderSpecOutput {
class_type: self.class_type(),
version: self.version(),
outputs: self.outputs(),
configurations: self.configurations(),
}
}
}

0 comments on commit cef4e2f

Please sign in to comment.