summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnthony Griffon <anthony.griffon@club-internet.fr>2019-10-14 00:07:43 +0200
committerDoug Tangren <d.tangren@gmail.com>2019-10-13 18:07:43 -0400
commitdf9e408e4b39729ac22af28628a3ff2c69194d9a (patch)
tree59e269f9bcf5ba9ca5ac49a467f9bfcd4ccfcbb7
parent1d6c11f12b2b24e1be67476989dc22c4e4935351 (diff)
Add 'publish' method to expose a port (#198)
* Renamed 'expose' to 'port' and add 'expose' to expose a port without linking it to the host * update Version to 0.5.1
-rw-r--r--Cargo.toml2
-rw-r--r--src/builder.rs60
2 files changed, 59 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 81f1c6d..acd35cd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "shiplift"
-version = "0.5.0"
+version = "0.5.1"
authors = ["softprops <d.tangren@gmail.com>"]
description = "A Rust interface for maneuvering Docker containers"
documentation = "https://docs.rs/shiplift"
diff --git a/src/builder.rs b/src/builder.rs
index cb924ac..bf7381f 100644
--- a/src/builder.rs
+++ b/src/builder.rs
@@ -685,6 +685,40 @@ impl ContainerOptionsBuilder {
self
}
+ /// Publish a port in the container without assigning a port on the host
+ pub fn publish(
+ &mut self,
+ srcport: u32,
+ protocol: &str,
+ ) -> &mut Self {
+ /* The idea here is to go thought the 'old' port binds
+ * and to apply them to the local 'exposedport_bindings' variable,
+ * add the bind we want and replace the 'old' value */
+ let mut exposed_port_bindings: HashMap<String, Value> = HashMap::new();
+ for (key, val) in self
+ .params
+ .get("ExposedPorts")
+ .unwrap_or(&json!(null))
+ .as_object()
+ .unwrap_or(&Map::new())
+ .iter()
+ {
+ exposed_port_bindings.insert(key.to_string(), json!(val));
+ }
+ exposed_port_bindings.insert(format!("{}/{}", srcport, protocol), json!({}));
+
+ // Replicate the port bindings over to the exposed ports config
+ let mut exposed_ports: HashMap<String, Value> = HashMap::new();
+ let empty_config: HashMap<String, Value> = HashMap::new();
+ for key in exposed_port_bindings.keys() {
+ exposed_ports.insert(key.to_string(), json!(empty_config));
+ }
+
+ self.params.insert("ExposedPorts", json!(exposed_ports));
+
+ self
+ }
+
pub fn links(
&mut self,
links: Vec<&str>,
@@ -1653,6 +1687,26 @@ mod tests {
);
}
+ #[test]
+ fn container_options_publish() {
+ let options = ContainerOptionsBuilder::new("test_image")
+ .publish(80, "tcp")
+ .build();
+ assert_eq!(
+ r#"{"ExposedPorts":{"80/tcp":{}},"HostConfig":{},"Image":"test_image"}"#,
+ options.serialize().unwrap()
+ );
+ // try exposing two
+ let options = ContainerOptionsBuilder::new("test_image")
+ .publish(80, "tcp")
+ .publish(81, "tcp")
+ .build();
+ assert_eq!(
+ r#"{"ExposedPorts":{"80/tcp":{},"81/tcp":{}},"HostConfig":{},"Image":"test_image"}"#,
+ options.serialize().unwrap()
+ );
+ }
+
/// Test container options that are nested 3 levels deep.
#[test]
fn container_options_nested() {
@@ -1676,7 +1730,7 @@ mod tests {
assert_eq!(
r#"{"HostConfig":{"RestartPolicy":{"MaximumRetryCount":10,"Name":"on-failure"}},"Image":"test_image"}"#,
options.serialize().unwrap()
- );
+ );
options = ContainerOptionsBuilder::new("test_image")
.restart_policy("always", 0)
@@ -1721,7 +1775,9 @@ mod tests {
.server_address("https://example.org")
.build();
assert_eq!(
- base64::encode(r#"{"username":"user_abc","password":"password_abc","email":"email_abc","serveraddress":"https://example.org"}"#),
+ base64::encode(
+ r#"{"username":"user_abc","password":"password_abc","email":"email_abc","serveraddress":"https://example.org"}"#
+ ),
options.serialize()
);
}