diff options
author | softprops <d.tangren@gmail.com> | 2015-04-30 00:12:50 -0400 |
---|---|---|
committer | softprops <d.tangren@gmail.com> | 2015-04-30 00:12:50 -0400 |
commit | 91a8d8be6e7b6813fa5a39b250e547b81af3aebb (patch) | |
tree | 56ae1f9d67c8d833462bd4dbfcb00c992b8ddadb /src | |
parent | 136adb4ded7817f93c0c8945446d14688c8cddea (diff) |
start working on builder interface
Diffstat (limited to 'src')
-rw-r--r-- | src/builder.rs | 33 | ||||
-rw-r--r-- | src/lib.rs | 24 | ||||
-rw-r--r-- | src/transport.rs | 46 |
3 files changed, 78 insertions, 25 deletions
diff --git a/src/builder.rs b/src/builder.rs new file mode 100644 index 0000000..808c51c --- /dev/null +++ b/src/builder.rs @@ -0,0 +1,33 @@ +use self::super::Docker; +use std::io::Result; + +pub struct ContainerBuilder<'a, 'b> { + docker: &'a mut Docker, + image: &'b str, + hostname: Option<String>, + user: Option<String>, + memory: Option<u64> +} + +impl<'a, 'b> ContainerBuilder<'a, 'b> { + pub fn new(docker: &'a mut Docker, image: &'b str) -> ContainerBuilder<'a,'b> { + ContainerBuilder { + docker: docker, image: image, + hostname: None, user: None, + memory: None + } + } + pub fn hostname(mut self, h: &str) -> ContainerBuilder<'a,'b> { + self.hostname = Some(h.to_string()); + self + } + + pub fn user(mut self, u: &str) -> ContainerBuilder<'a, 'b> { + self.user = Some(u.to_string()); + self + } + + pub fn build(self) -> Result<String> { + self.docker.post("/containers/create") + } +} @@ -19,9 +19,11 @@ extern crate rustc_serialize; extern crate unix_socket; extern crate url; +pub mod builder; pub mod rep; pub mod transport; +use builder::ContainerBuilder; use hyper::{ Client, Url }; use hyper::method::Method; use openssl::x509::X509FileType; @@ -34,7 +36,7 @@ use rep::{ use std::env; use std::path::Path; use std::io::{ Read, Result }; -use transport::Transport; +use transport::{ Body, Transport }; use unix_socket::UnixStream; use url::{ Host, RelativeSchemeData, SchemeData }; @@ -94,16 +96,6 @@ impl<'a> Images<'a> { } } -pub struct ContainerBuilder<'a, 'b> { - docker: &'a mut Docker, - image: &'b str -} - -impl<'a, 'b> ContainerBuilder<'a, 'b> { - pub fn new(docker: &'a mut Docker, image: &'b str) -> ContainerBuilder<'a,'b> { - ContainerBuilder { docker: docker, image: image } - } -} pub struct Container<'a, 'b> { docker: &'a mut Docker, @@ -286,22 +278,22 @@ impl Docker { } fn get(&mut self, endpoint: &str) -> Result<String> { - (*self.transport).request(Method::Get, endpoint) + (*self.transport).request(Method::Get, endpoint, None) } fn post(&mut self, endpoint: &str) -> Result<String> { - (*self.transport).request(Method::Post, endpoint) + (*self.transport).request(Method::Post, endpoint, None) } fn delete(&mut self, endpoint: &str) -> Result<String> { - (*self.transport).request(Method::Delete, endpoint) + (*self.transport).request(Method::Delete, endpoint, None) } fn stream_post(&mut self, endpoint: &str) -> Result<Box<Read>> { - (*self.transport).stream(Method::Post, endpoint) + (*self.transport).stream(Method::Post, endpoint, None) } fn stream_get(&mut self, endpoint: &str) -> Result<Box<Read>> { - (*self.transport).stream(Method::Get, endpoint) + (*self.transport).stream(Method::Get, endpoint, None) } } diff --git a/src/transport.rs b/src/transport.rs index 8eb7509..1cef470 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,18 +1,38 @@ extern crate hyper; +extern crate mime; extern crate unix_socket; +use std::ops::DerefMut; use hyper::Client; +use hyper::client; +use self::hyper::header::{ ContentType, UserAgent, qitem }; use hyper::method::Method; +use self::mime::{ Attr, Mime, Value }; +use self::mime::TopLevel::Application; +use self::mime::SubLevel::Json; use std::io::{ self, Read, Result, Write }; use unix_socket::UnixStream; +#[doc(hidden)] +pub struct Body<'a> { + read: &'a mut Box<&'a mut Read>, + size: u64 +} + +impl<'a> Body<'a> { + /// Create a new body instance + pub fn new(read: &'a mut Box<&'a mut Read>, size: u64) -> Body<'a> { + Body { read: read, size: size } + } +} + pub trait Transport { - fn request(&mut self, method: Method, endpoint: &str) -> Result<String>; - fn stream(&mut self, method: Method, endpoint: &str) -> Result<Box<Read>>; + fn request(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<String>; + fn stream(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<Box<Read>>; } impl Transport for UnixStream { - fn request(&mut self, method: Method, endpoint: &str) -> Result<String> { + fn request(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<String> { let method_str = match method { Method::Put => "PUT", Method::Post => "POST", @@ -25,14 +45,14 @@ impl Transport for UnixStream { self.read_to_string(&mut result).map(|_| result) } - fn stream(&mut self, method: Method, endpoint: &str) -> Result<Box<Read>> { + fn stream(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<Box<Read>> { Err(io::Error::new(io::ErrorKind::InvalidInput, "Not yet implemented")) } } impl Transport for (Client, String) { - fn request(&mut self, method: Method, endpoint: &str) -> Result<String> { - let mut res = match self.stream(method, endpoint) { + fn request(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<String> { + let mut res = match self.stream(method, endpoint, body) { Ok(r) => r, Err(e) => panic!("failed request {:?}", e) }; @@ -41,15 +61,23 @@ impl Transport for (Client, String) { res.read_to_string(&mut body).map(|_| body) } - fn stream(&mut self, method: Method, endpoint: &str) -> Result<Box<Read>> { + fn stream(&mut self, method: Method, endpoint: &str, body: Option<Body>) -> Result<Box<Read>> { let uri = format!("{}{}", self.1, endpoint); - let req = match method { + let mut req = match method { Method::Put => self.0.put(&uri[..]), Method::Post => self.0.post(&uri[..]), Method::Delete => self.0.delete(&uri[..]), _ => self.0.get(&uri[..]) }; - let mut res = match req.send() { + let embodied = match body { + Some(Body { read: r, size: l }) => { + let reader: &mut Read = *r.deref_mut(); + let content_type: Mime = Mime(Application, Json, vec![(Attr::Charset, Value::Utf8)]); + req.header(ContentType(content_type)).body(client::Body::SizedBody(reader, l)) + }, + _ => req + }; + let mut res = match embodied.send() { Ok(r) => r, Err(e) => panic!("failed request {:?}", e) }; |