Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
imp: implements droplets neighbors command
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Jun 5, 2015
1 parent 5aeee79 commit 6f1d64c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 46 deletions.
32 changes: 31 additions & 1 deletion src/request/builder/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl response::NamedResponse for String {
"".into()
}
}

impl<'t> DoRequest<response::ResponseStringArray> for RequestBuilder<'t, response::ResponseStringArray> {
#[allow(unused_variables)]
fn retrieve_obj(&self, obj: String) -> Result<response::ResponseStringArray, String> {
Expand All @@ -37,4 +38,33 @@ impl<'t> DoRequest<response::ResponseStringArray> for RequestBuilder<'t, respons
}
}
}
}
}

impl<'t> DoRequest<response::Neighbors> for RequestBuilder<'t, response::Neighbors> {}
// impl<'t> DoRequest<response::Neighbors> for RequestBuilder<'t, response::Neighbors> {
// #[allow(unused_variables)]
// fn retrieve_obj(&self, obj: String) -> Result<response::Neighbors, String> {
// debug!("Inside retrieve_obj() of ResponseStringArray");
// debug!("Retrieveing JSON");
// match self.retrieve_json() {
// Ok(ref s) => {
// debug!("Success");
// debug!("Retrieving Value");
// match json::from_str::<response::Neighbors>(s) {
// Ok(ob) => {
// debug!("Success");
// Ok(ob)
// },
// Err(e) => {
// debug!("Failed");
// Err(e.to_string())
// }
// }
// },
// Err(e) => {
// debug!("Failed");
// Err(e.to_string())
// }
// }
// }
// }
103 changes: 58 additions & 45 deletions src/response/neighbors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,51 @@
use std::fmt;
use std::borrow::Cow;

use response::{self, Kernel, Region, Backup, Network, Image, Size, NamedResponse};

pub type Neighbors = Vec<Neighbor>;
use response::{Kernel, Region, Backup, Networks, Image, Size, NamedResponse};

// Have to duplicate Droplet because of lack of negative trait bounds
#[derive(Deserialize, Debug)]
pub struct Neighbor {
pub struct DropletNeighbor {
id: f64,
name: String,
memory: f64,
vcpus: f64,
disk: f64,
locked: bool,
created_at: String,
status: String,
backup_ids: Vec<String>,
snapshot_ids: Vec<String>,
kernel: Option<Kernel>,
created_at: String,
features: Vec<String>,
region: Region,
backup_ids: Vec<Option<String>>,
next_backup_window: Option<Backup>,
snapshot_ids: Vec<Option<String>>,
image: Image,
region: Region,
size: Size,
size_slug: String,
networks: Network,
kernel: Option<Kernel>,
next_backup_window: Option<Backup>
networks: Networks,
}

impl response::NotArray for Neighbor {}

impl fmt::Display for Neighbor {
impl fmt::Display for DropletNeighbor {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Droplet:\n\
ID: {:.0}\n\
Name: {}\n\
Memory: {} MB\n\
Virtual CPUs: {:.0}\n\
Disk: {} GB\n\
Locked: {}\n\
Created At: {}\n\
Status: {}\n\
Backup IDs: {}\n\
Snapshot IDs: {}\n\
Features: {}\n\
Droplet: \n{}\n\
Image: \n{}\n\
Size: \n{}\n\
Size Slug: {}\n\
Network: \n{}\n\
Kernel: {}\
Next Backup Window: {}",
write!(f, "ID: {:.0}\n\
Name: {}\n\
Memory: {} MB\n\
Virtual CPUs: {:.0}\n\
Disk: {} GB\n\
Locked: {}\n\
Created At: {}\n\
Status: {}\n\
Backup IDs: {}\n\
Snapshot IDs: {}\n\
Features: {}\n\
Region: \n\t{}\n\
Image: \n\t{}\n\
Size: \n\t{}\n\
Size Slug: {}\n\
Network: \n\t{}\n\
Kernel: \n\t{}\n\
Next Backup Window: {}\n",
self.id,
self.name,
self.memory,
Expand All @@ -59,30 +55,47 @@ impl fmt::Display for Neighbor {
self.locked,
self.created_at,
self.status,
self.backup_ids.iter().fold(String::new(), |acc, s| acc + &format!(" {},", s)[..]),
self.snapshot_ids.iter().fold(String::new(), |acc, s| acc + &format!(" {},", s)[..]),
self.features.iter().fold(String::new(), |acc, s| acc + &format!(" {},", s)[..]),
self.region,
self.image,
self.size,
self.backup_ids.iter()
.filter_map(|n| if n.is_some() {
Some(n.clone().unwrap().to_string())
}else{
None
})
.fold(String::new(), |acc, s| acc + &format!(" {},", s)[..]),
self.snapshot_ids.iter()
.filter_map(|n| if n.is_some() {
Some(n.clone().unwrap().to_string())
}else{
None
})
.fold(String::new(), |acc, s| acc + &format!(" {},", s)[..]),
self.features.iter()
.fold(String::new(), |acc, s| acc + &format!(" {},", s)[..]),
&self.region.to_string()[..].replace("\n","\n\t"),
&self.image.to_string()[..].replace("\n","\n\t"),
&self.size.to_string()[..].replace("\n","\n\t"),
self.size_slug,
self.networks,
&self.networks.to_string()[..].replace("\n","\n\t"),
if let Some(ref k) = self.kernel {
format!("\n{}\n", k)
format!("{}", &k.to_string()[..].replace("\n","\n\t"))
} else {
"None\n".to_owned()
"None".to_owned()
},
if let Some(ref k) = self.next_backup_window {
format!("\n{}\n", k)
format!("{}", &k.to_string()[..].replace("\n","\n\t"))
} else {
"None\n".to_owned()
"None".to_owned()
})

}
}

pub type Neighbor = Vec<DropletNeighbor>;

impl NamedResponse for Neighbor {
fn name<'a>() -> Cow<'a, str> {
"neighbor".into()
}
}
}

pub type Neighbors = Vec<Neighbor>;

0 comments on commit 6f1d64c

Please sign in to comment.