diff options
author | Zach Blankenship <zach.blankenship@gmail.com> | 2017-01-10 23:29:19 -0500 |
---|---|---|
committer | Zach Blankenship <zach.blankenship@gmail.com> | 2017-01-14 07:43:24 -0500 |
commit | 2b378af17ad69df4c95094ebbf7b9cf8202a26b7 (patch) | |
tree | a3a1b8285864b93ef32a8d6a13cffba228289243 /src | |
parent | 66a0691a4d7370813932214f5e47e745a1257bcc (diff) |
Handle object arrays for ContainerOptions
Adding an additional parameter `params_hash` to `ContainerOptions`
to maintain params that are vectors of key value pairs.
This is needed for something like devices:
```json
{
...
"Devices":[{ "PathOnHost": "/dev/deviceName", "PathInContainer": "/dev/deviceName", "CgroupPermissions": "mrw"}]
...
}
```
Diffstat (limited to 'src')
-rw-r--r-- | src/builder.rs | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/src/builder.rs b/src/builder.rs index 024dcac..7c307f5 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,8 +1,11 @@ //! Interfaces for building various structures +use rustc_serialize::json::{self, Json, ToJson}; use self::super::Result; +use std::cmp::Eq; use std::collections::{BTreeMap, HashMap}; -use rustc_serialize::json::{self, Json, ToJson}; +use std::hash::Hash; +use std::iter::IntoIterator; use url::form_urlencoded; #[derive(Default)] @@ -252,6 +255,22 @@ impl ContainerListOptionsBuilder { pub struct ContainerOptions { params: HashMap<&'static str, String>, params_list: HashMap<&'static str, Vec<String>>, + params_hash: HashMap<String, Vec<HashMap<String, String>>>, +} + +impl ToJson for ContainerOptions { + fn to_json(&self) -> Json { + let mut body: BTreeMap<String, Json> = BTreeMap::new(); + let mut host_config: BTreeMap<String, Json> = BTreeMap::new(); + + self.parse_from(&self.params, &mut host_config, &mut body); + self.parse_from(&self.params_list, &mut host_config, &mut body); + self.parse_from(&self.params_hash, &mut host_config, &mut body); + + body.insert("HostConfig".to_string(), host_config.to_json()); + + body.to_json() + } } impl ContainerOptions { @@ -262,30 +281,29 @@ impl ContainerOptions { /// serialize options as a string. returns None if no options are defined pub fn serialize(&self) -> Result<String> { - let mut body = BTreeMap::new(); - let mut host_config = BTreeMap::new(); + Ok(try!(json::encode(&self.to_json()))) + } - for (k, v) in &self.params { - if k.starts_with("HostConfig.") { - let (_, s) = k.split_at(11); - host_config.insert(s.to_owned(), v.to_json()); - } else { - body.insert(k.to_string(), v.to_json()); - } - } + pub fn parse_from<'a, K, V>(&self, + params: &'a HashMap<K, V>, + host_config: &mut BTreeMap<String, Json>, + body: &mut BTreeMap<String, Json>) + where &'a HashMap<K, V>: IntoIterator, + K: ToString + Eq + Hash, + V: ToJson + { + for (k, v) in params.iter() { + let key = k.to_string(); + let value = v.to_json(); - for (k, v) in &self.params_list { - if k.starts_with("HostConfig.") { - let (_, s) = k.split_at(11); - host_config.insert(s.to_owned(), v.to_json()); + if key.starts_with("HostConfig.") { + let (_, s) = key.split_at(11); + + host_config.insert(s.to_string(), value); } else { - body.insert(k.to_string(), v.to_json()); + body.insert(key, value); } } - - body.insert("HostConfig".to_owned(), host_config.to_json()); - let json_obj: Json = body.to_json(); - Ok(try!(json::encode(&json_obj))) } } @@ -293,16 +311,20 @@ impl ContainerOptions { pub struct ContainerOptionsBuilder { params: HashMap<&'static str, String>, params_list: HashMap<&'static str, Vec<String>>, + params_hash: HashMap<String, Vec<HashMap<String, String>>>, } impl ContainerOptionsBuilder { pub fn new(image: &str) -> ContainerOptionsBuilder { let mut params = HashMap::new(); let params_list = HashMap::new(); + let params_hash = HashMap::new(); + params.insert("Image", image.to_owned()); ContainerOptionsBuilder { params: params, params_list: params_list, + params_hash: params_hash, } } @@ -380,11 +402,11 @@ impl ContainerOptionsBuilder { ContainerOptions { params: self.params.clone(), params_list: self.params_list.clone(), + params_hash: self.params_hash.clone(), } } } - /// Options for filtering streams of Docker events #[derive(Default)] pub struct EventsOptions { |