Skip to content

Commit

Permalink
Merge pull request #17 from rruckley/feat-create-16
Browse files Browse the repository at this point in the history
feat: Add create handling
  • Loading branch information
rruckley authored Jan 22, 2025
2 parents af8b6b2 + 427fa6b commit 6923f29
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

use clap::{Parser,Subcommand};
use log::info;
use log::{info,error};
use tmf_client::TMFClient;
use tmf_client::common::tmf_error::TMFError;

Expand Down Expand Up @@ -132,7 +132,7 @@ fn main() -> Result<(),TMFError> {

let mut client = TMFClient::new(host);

match args.tmf {
let result = match args.tmf {
TMFModules::TMF620 { module } => {
handle_tmf620(&mut client, module, Some(opts),output)
},
Expand All @@ -154,5 +154,15 @@ fn main() -> Result<(),TMFError> {
TMFModules::TMF674 { module } => {
handle_tmf674(&mut client, module, Some(opts),output)
}
};
match result {
Ok(r) => {
info!("Successful operation");
Ok(r)
},
Err(e) => {
error!("Operation failed: {}",e.message);
Err(e)
},
}
}
7 changes: 5 additions & 2 deletions src/tmf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ pub enum TMFOperation {
Get {
id : String,
},
Create,
Create {
name : String,
desc : Option<String>,
},
Update,
Delete
}
Expand Down Expand Up @@ -71,6 +74,6 @@ pub fn display_opt(label : &str, field : &Option<String>) {
}

pub fn display_json<T : Serialize>(item : T) {
let json = serde_json::to_string(&item).expect("Could not create JSON");
let json = serde_json::to_string_pretty(&item).expect("Could not create JSON");
println!("{}",json);
}
32 changes: 27 additions & 5 deletions src/tmf/tmf620.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! TMF620 CLI Module
use clap::Subcommand;
use tmflib::tmf620::catalog::Catalog;
use tmflib::tmf620::category::Category;


use crate::Output;

use super::{
display_desc,
display_name,
display_json,
iterate_name,
TMFOperation
display_desc, display_json, display_name, display_opt, iterate_name, TMFOperation
};

use tmf_client::common::tmf_error::TMFError;
Expand Down Expand Up @@ -46,6 +45,14 @@ pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Opt
match module {
TMF620Modules::Catalog { op } => {
match op {
TMFOperation::Create { name, desc } => {
// Create a new object
let catalog = Catalog::new(name);
// .description(desc.unwrap_or_default());
let _new_catalog = client.tmf620().catalog().create(catalog)?;
// TODO: display_name(&new_catalog);
Ok(())
},
TMFOperation::List => {
let catalogs = client.tmf620().catalog().list(opts)?;
iterate_name(&catalogs,output);
Expand All @@ -67,11 +74,26 @@ pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Opt
},
TMF620Modules::Category { op } => {
match op {
TMFOperation::Create { name, desc } => {
let category = Category::new(name)
.description(desc.unwrap_or_default());
let new_cat = client.tmf620().category().create(category)?;
display_name(&new_cat);
display_opt("Desc", &new_cat.description);
Ok(())
}
TMFOperation::List => {
let categories = client.tmf620().category().list(opts)?;
iterate_name(&categories,output);
Ok(())
},
TMFOperation::Get { id } => {
let category = client.tmf620().category().get(id)?;
let the_first = category.first().unwrap();
display_name(the_first);
display_opt("Desc", &the_first.description);
Ok(())
}
_ => {
Err(TMFError::from("Not implemented"))
}
Expand Down
21 changes: 21 additions & 0 deletions src/tmf/tmf629.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! TMF629 CLI Module
use clap::Subcommand;
use tmflib::tmf629::customer::Customer;

use crate::Output;

Expand All @@ -26,6 +27,26 @@ pub fn handle_tmf629(client : &mut TMFClient, module : TMF629Modules, opts : Opt
match module {
TMF629Modules::Customer { op } => {
match op {
TMFOperation::Create { name, desc } => {
// Assumption is there is already an organization with the same name.
let org_name = match desc {
Some(d) => d,
None => name.clone(),
};
let filter = QueryOptions::default()
.name(&org_name);
let orgs = client.tmf632().organization().list(Some(filter))?;
if orgs.len() == 1 {
let org =orgs.first().unwrap();
let customer = Customer::new(org.clone());
let new_cust = client.tmf629().customer().create(customer)?;
display_name(&new_cust);
Ok(())
} else {
Err(TMFError::from(format!("Could not find matchig organization with the name: '{}'",org_name).as_str()))
}

}
TMFOperation::List => {
let customers = client.tmf629().customer().list(opts)?;
iterate_name(&customers,output);
Expand Down
15 changes: 15 additions & 0 deletions src/tmf/tmf632.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! TMF632 CLI Module
use clap::Subcommand;
use tmflib::tmf632::individual_v4::Individual;
use tmflib::tmf632::organization_v4::Organization;

use crate::Output;

Expand All @@ -27,6 +29,13 @@ pub fn handle_tmf632(client : &mut TMFClient, module : TMF632Modules, opts : Opt
match module {
TMF632Modules::Individual { op } => {
match op {
TMFOperation::Create { name, desc } => {
let individual = Individual::new(name)
.title(desc.unwrap_or_default());
let new_ind = client.tmf632().individual().create(individual)?;
display_name(&new_ind);
Ok(())
}
TMFOperation::List => {
let individuals = client.tmf632().individual().list(opts)?;
iterate_name(&individuals,output);
Expand All @@ -51,6 +60,12 @@ pub fn handle_tmf632(client : &mut TMFClient, module : TMF632Modules, opts : Opt
},
TMF632Modules::Organization { op } => {
match op {
TMFOperation::Create { name, desc } => {
let organization = Organization::new(name);
let new_org = client.tmf632().organization().create(organization)?;
display_name(&new_org);
Ok(())
}
TMFOperation::List => {
let organization = client.tmf632().organization().list(opts)?;
iterate_name(&organization,output);
Expand Down
34 changes: 33 additions & 1 deletion src/tmf/tmf633.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
//! TMF620 CLI Module
use clap::Subcommand;
use tmflib::tmf633::service_candidate::ServiceCandidate;
use tmflib::tmf633::service_catalog::ServiceCatalog;
use tmflib::tmf633::service_category::ServiceCategory;
use tmflib::tmf633::service_specification::ServiceSpecification;
use tmflib::HasDescription;

use crate::Output;

use super::{
display_desc, display_name, iterate_name, TMFOperation
display_desc, display_name, display_opt, iterate_name, TMFOperation
};

use tmf_client::common::tmf_error::TMFError;
Expand Down Expand Up @@ -39,6 +44,12 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
match module {
TMF633Modules::Catalog { op } => {
match op {
TMFOperation::Create { name, desc } => {
let catalog = ServiceCatalog::new(name);
let new_cat = client.tmf633().catalog().create(catalog)?;
display_name(&new_cat);
Ok(())
}
TMFOperation::List => {
let catalogs = client.tmf633().catalog().list(opts)?;
iterate_name(&catalogs,output);
Expand All @@ -57,6 +68,14 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
},
TMF633Modules::Category { op } => {
match op {
TMFOperation::Create { name, desc } => {
let category = ServiceCategory::new(name)
.description(desc.unwrap_or_default());
let new_cat = client.tmf633().category().create(category)?;
display_name(&new_cat);
display_desc(&new_cat);
Ok(())
}
TMFOperation::List => {
let categories = client.tmf633().category().list(opts)?;
iterate_name(&categories,output);
Expand All @@ -75,6 +94,11 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
},
TMF633Modules::Candidate { op } => {
match op {
TMFOperation::Create { name, desc } => {
// Need to use the desc field as the id of a specificaton to link to this candidate
// let candidate = ServiceCandidate::new(name, specification_ref)
Err(TMFError::from("Not implemented"))
}
TMFOperation::List => {
let candidates = client.tmf633().candidate().list(opts)?;
iterate_name(&candidates,output);
Expand All @@ -93,6 +117,14 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
},
TMF633Modules::Specification { op } => {
match op {
TMFOperation::Create { name, desc } => {
let specification = ServiceSpecification::new(name)
.description(desc.unwrap_or_default());
let new_spec = client.tmf633().specification().create(specification)?;
display_name(&new_spec);
display_opt("Desc", &new_spec.description);
Ok(())
},
TMFOperation::List => {
let specifications = client.tmf633().specification().list(opts)?;
iterate_name(&specifications,output);
Expand Down
21 changes: 21 additions & 0 deletions src/tmf/tmf648.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
//! TMF629 CLI Module
use clap::Subcommand;
use tmflib::tmf648::quote::Quote;
use tmflib::common::note::Note;
use tmflib::{
HasName,
HasNote
};

use crate::Output;

use super::{
display_id,
display_desc,
display_opt,
iterate_name,
Expand All @@ -28,6 +35,20 @@ pub fn handle_tmf648(client : &mut TMFClient, module : TMF648Modules, opts : Opt
match module {
TMF648Modules::Quote { op } => {
match op {
TMFOperation::Create { name, desc } => {
let mut quote = Quote::new();
quote.set_name(name);
if let Some(n) = desc {
// BUG: HasNote macro does not properly create the Vec<Note>
quote.note = Some(vec![Note::new(n)]);
// quote.add_note(Note::new(n));
};
// quote.description = desc.clone();
let new_quote = client.tmf648().quote().create(quote)?;
display_id(&new_quote);
display_opt("Desc", &new_quote.description);
Ok(())
}
TMFOperation::List => {
let quotes = client.tmf648().quote().list(opts)?;
iterate_name(&quotes,output);
Expand Down
11 changes: 11 additions & 0 deletions src/tmf/tmf674.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! TMF674 CLI Module
use clap::Subcommand;
use tmflib::tmf674::geographic_site_v4::GeographicSite;
use tmflib::HasDescription;

use crate::Output;

Expand All @@ -26,6 +28,15 @@ pub fn handle_tmf674(client : &mut TMFClient, module : TMF674Modules, opts : Opt
match module {
TMF674Modules::Site { op } => {
match op {
TMFOperation::Create { name, desc } => {
let site = GeographicSite::new(name)
.description(desc.unwrap_or_default());
let new_site = client.tmf674().site().create(site)?;
display_name(&new_site);
display_opt("Desc", &new_site.description);
display_opt("Code", &new_site.code);
Ok(())
}
TMFOperation::List => {
let sites = client.tmf674().site().list(opts)?;
iterate_name(&sites,output);
Expand Down

0 comments on commit 6923f29

Please sign in to comment.