summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsoftprops <d.tangren@gmail.com>2016-01-03 22:24:00 -0500
committersoftprops <d.tangren@gmail.com>2016-01-03 22:24:00 -0500
commit50966067658108d08ad79deefb59d471f9c96fa1 (patch)
tree4edcceee11805749be4a500395508228cfce84e6
parent49b5b4fd76cb2641caed877fc95a73368451b3af (diff)
container create should following the same pattern as other methods that take options
-rw-r--r--examples/containercreate.rs15
-rw-r--r--src/builder.rs84
-rw-r--r--src/errors.rs2
-rw-r--r--src/lib.rs16
4 files changed, 68 insertions, 49 deletions
diff --git a/examples/containercreate.rs b/examples/containercreate.rs
new file mode 100644
index 0000000..8d51055
--- /dev/null
+++ b/examples/containercreate.rs
@@ -0,0 +1,15 @@
+extern crate shiplift;
+
+use shiplift::{ContainerOptions, Docker};
+use std::env;
+
+fn main() {
+ let docker = Docker::new();
+ let containers = docker.containers();
+ if let Some(image) = env::args().nth(1) {
+ let info = containers.create(
+ &ContainerOptions::builder(image.as_ref()).build()
+ ).unwrap();
+ println!("{:?}", info);
+ }
+}
diff --git a/src/builder.rs b/src/builder.rs
index 4753b71..77265fc 100644
--- a/src/builder.rs
+++ b/src/builder.rs
@@ -1,16 +1,11 @@
-//! Interfaces for building [docker](https://www.docker.com/) containers
+//! Interfaces for building various structures
-extern crate rustc_serialize;
-extern crate jed;
-extern crate url;
-
-use self::super::{Docker, Result};
-use self::super::transport::Body;
-use self::super::rep::ContainerCreateInfo;
+use self::super::Result;
use std::collections::{BTreeMap, HashMap};
use rustc_serialize::json::{self, Json, ToJson};
use url::form_urlencoded;
+/// Options for filtering container list results
#[derive(Default)]
pub struct ContainerListOptions {
params: HashMap<&'static str, String>,
@@ -40,7 +35,7 @@ pub enum ContainerFilter {
Label(String, String),
}
-/// Interface for building container list request
+/// Builder interface for `ContainerListOptions`
#[derive(Default)]
pub struct ContainerListOptionsBuilder {
params: HashMap<&'static str, String>,
@@ -89,54 +84,55 @@ impl ContainerListOptionsBuilder {
}
pub fn build(&self) -> ContainerListOptions {
- // Result<Vec<ContainerRep>> {
ContainerListOptions { params: self.params.clone() }
}
}
/// Interface for building a new docker container from an existing image
-pub struct ContainerBuilder<'a, 'b> {
- docker: &'a Docker,
- image: &'b str,
- hostname: Option<String>,
- user: Option<String>,
- memory: Option<u64>,
+pub struct ContainerOptions {
+ params: HashMap<&'static str, String>
}
-impl<'a, 'b> ContainerBuilder<'a, 'b> {
- pub fn new(docker: &'a Docker, image: &'b str) -> ContainerBuilder<'a, 'b> {
- ContainerBuilder {
- docker: docker,
- image: image,
- hostname: None,
- user: None,
- memory: None,
- }
+impl ContainerOptions {
+ /// return a new instance of a builder for options
+ pub fn builder(name: &str) -> ContainerOptionsBuilder {
+ ContainerOptionsBuilder::new(name)
}
- pub fn hostname(&mut self, h: &str) -> &mut ContainerBuilder<'a, 'b> {
- self.hostname = Some(h.to_owned());
- self
+ /// serialize options as a string. returns None if no options are defined
+ pub fn serialize(&self) -> Result<String> {
+ let mut body = BTreeMap::new();
+ if let Some(image) = self.params.get("Image") {
+ body.insert("Image".to_owned(), image.to_json());
+ }
+ let json_obj: Json = body.to_json();
+ Ok(try!(json::encode(&json_obj)))
}
+}
- pub fn user(&mut self, u: &str) -> &mut ContainerBuilder<'a, 'b> {
- self.user = Some(u.to_owned());
- self
+#[derive(Default)]
+pub struct ContainerOptionsBuilder {
+ params: HashMap<&'static str, String>
+}
+
+impl ContainerOptionsBuilder {
+ pub fn new(name: &str) -> ContainerOptionsBuilder {
+ let mut params = HashMap::new();
+ params.insert("Image", name.to_owned());
+ ContainerOptionsBuilder {
+ params: params
+ }
}
- pub fn build(&self) -> Result<ContainerCreateInfo> {
- let mut body = BTreeMap::new();
- body.insert("Image".to_owned(), self.image.to_json());
- let json_obj: Json = body.to_json();
- let data = try!(json::encode(&json_obj));
- let mut bytes = data.as_bytes();
- let raw = try!(self.docker.post("/containers/create",
- Some(Body::new(&mut Box::new(&mut bytes),
- bytes.len() as u64))));
- Ok(try!(json::decode::<ContainerCreateInfo>(&raw)))
+ pub fn build(&self) -> ContainerOptions {
+ ContainerOptions {
+ params: self.params.clone()
+ }
}
}
+
+/// Options for filtering streams of Docker events
#[derive(Default)]
pub struct EventsOptions {
params: HashMap<&'static str, String>,
@@ -156,7 +152,7 @@ impl EventsOptions {
}
}
-/// Interface for buiding an events request
+/// Builder interface for `EventOptions`
#[derive(Default)]
pub struct EventsOptionsBuilder {
params: HashMap<&'static str, String>,
@@ -185,6 +181,7 @@ impl EventsOptionsBuilder {
}
+/// Options for controlling log request results
#[derive(Default)]
pub struct LogsOptions {
params: HashMap<&'static str, String>,
@@ -206,6 +203,7 @@ impl LogsOptions {
}
}
+/// Builder interface for `LogsOptions`
#[derive(Default)]
pub struct LogsOptionsBuilder {
params: HashMap<&'static str, String>,
@@ -255,6 +253,7 @@ pub enum ImageFilter {
Label(String, String),
}
+/// Options for filtering image list results
#[derive(Default)]
pub struct ImageListOptions {
params: HashMap<&'static str, String>,
@@ -273,6 +272,7 @@ impl ImageListOptions {
}
}
+/// Builder interface for `ImageListOptions`
#[derive(Default)]
pub struct ImageListOptionsBuilder {
params: HashMap<&'static str, String>,
diff --git a/src/errors.rs b/src/errors.rs
index cac84b5..d18399e 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -1,3 +1,5 @@
+//! Representations of various client errors
+
use std::io::Error as IoError;
use hyper::Error as HttpError;
use hyper::status::StatusCode;
diff --git a/src/lib.rs b/src/lib.rs
index 389654a..6e4039a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -28,11 +28,8 @@ pub mod transport;
pub mod errors;
pub use errors::Error;
-pub use builder::{ContainerListOptions, ContainerFilter, EventsOptions, ImageFilter,
+pub use builder::{ContainerOptions, ContainerListOptions, ContainerFilter, EventsOptions, ImageFilter,
ImageListOptions, LogsOptions};
-
-// fixme: remove this here
-use builder::ContainerBuilder;
use hyper::{Client, Url};
use hyper::net::{HttpsConnector, Openssl};
use hyper::method::Method;
@@ -40,7 +37,7 @@ use hyperlocal::UnixSocketConnector;
use openssl::x509::X509FileType;
use openssl::ssl::{SslContext, SslMethod};
use rep::Image as ImageRep;
-use rep::{Change, ContainerDetails, Container as ContainerRep, Event, Exit, History, ImageDetails,
+use rep::{Change, ContainerCreateInfo, ContainerDetails, Container as ContainerRep, Event, Exit, History, ImageDetails,
Info, SearchResult, Stats, Status, Top, Version};
use rustc_serialize::json::{self, Json};
use std::env::{self, VarError};
@@ -333,8 +330,13 @@ impl<'a> Containers<'a> {
}
/// Returns a builder interface for creating a new container instance
- pub fn create(&'a self, image: &'a str) -> ContainerBuilder {
- ContainerBuilder::new(self.docker, image)
+ pub fn create(&'a self, opts: &ContainerOptions) -> Result<ContainerCreateInfo> {
+ let data = try!(opts.serialize());
+ let mut bytes = data.as_bytes();
+ let raw = try!(self.docker.post("/containers/create",
+ Some(Body::new(&mut Box::new(&mut bytes),
+ bytes.len() as u64))));
+ Ok(try!(json::decode::<ContainerCreateInfo>(&raw)))
}
}