diff --git a/src/main/java/com/amihaiemil/docker/Docker.java b/src/main/java/com/amihaiemil/docker/Docker.java
index ab4f9dc..2c5afed 100644
--- a/src/main/java/com/amihaiemil/docker/Docker.java
+++ b/src/main/java/com/amihaiemil/docker/Docker.java
@@ -98,12 +98,19 @@ public interface Docker {
Plugins plugins();
/**
- * Entry point for Version API.
+ * Version of this Docker engine.
* @return Version.
* @throws IOException If an I/O error occurs.
*/
Version version() throws IOException;
+ /**
+ * Detailed information about this Docker engine, in JSON.
+ * @return Info.
+ * @throws IOException If an I/O problem occurs.
+ */
+ Info info() throws IOException;
+
/**
* The underlying, immutable, Apache HttpClient.
*
diff --git a/src/main/java/com/amihaiemil/docker/Info.java b/src/main/java/com/amihaiemil/docker/Info.java
new file mode 100644
index 0000000..42535c4
--- /dev/null
+++ b/src/main/java/com/amihaiemil/docker/Info.java
@@ -0,0 +1,43 @@
+/**
+ * Copyright (c) 2018-2020, Mihai Emil Andronache
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1)Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2)Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3)Neither the name of docker-java-api nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.amihaiemil.docker;
+
+import javax.json.JsonObject;
+
+/**
+ * General information about the Docker engine.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 0.0.13
+ */
+public interface Info extends JsonObject {
+
+ /**
+ * The Docker engine to which this Info refers to.
+ * @return Docker.
+ */
+ Docker docker();
+}
diff --git a/src/main/java/com/amihaiemil/docker/RtDocker.java b/src/main/java/com/amihaiemil/docker/RtDocker.java
index 0c3992a..57f5234 100644
--- a/src/main/java/com/amihaiemil/docker/RtDocker.java
+++ b/src/main/java/com/amihaiemil/docker/RtDocker.java
@@ -49,7 +49,7 @@ abstract class RtDocker implements Docker {
* Base URI.
*/
private final URI baseUri;
-
+
/**
* Ctor.
* @param client Given HTTP Client.
@@ -59,7 +59,7 @@ abstract class RtDocker implements Docker {
this.client = client;
this.baseUri = baseUri;
}
-
+
@Override
public final boolean ping() throws IOException {
final HttpGet ping = new HttpGet(this.baseUri.toString() + "/_ping");
@@ -126,7 +126,7 @@ public final Execs execs() {
public final Swarm swarm() {
return new RtSwarm(
this.client,
- URI.create(this.baseUri.toString().concat("/swarm")),
+ URI.create(this.baseUri.toString().concat("/swarm")),
this
);
}
@@ -157,10 +157,29 @@ public Version version() throws IOException {
final String versionUri = this.baseUri.toString() + "/version";
return new RtVersion(
this.client,
- URI.create(versionUri)
+ URI.create(versionUri),
+ this
);
}
+ @Override
+ public Info info() throws IOException {
+ final HttpGet info = new HttpGet(this.baseUri.toString() + "/info");
+ try {
+ return new RtInfo(
+ this.client.execute(
+ info,
+ new ReadJsonObject(
+ new MatchStatus(info.getURI(), HttpStatus.SC_OK)
+ )
+ ),
+ this
+ );
+ } finally {
+ info.releaseConnection();
+ }
+ }
+
@Override
public HttpClient httpClient() {
return this.client;
diff --git a/src/main/java/com/amihaiemil/docker/RtInfo.java b/src/main/java/com/amihaiemil/docker/RtInfo.java
new file mode 100644
index 0000000..65db46b
--- /dev/null
+++ b/src/main/java/com/amihaiemil/docker/RtInfo.java
@@ -0,0 +1,57 @@
+/**
+ * Copyright (c) 2018-2020, Mihai Emil Andronache
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1)Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2)Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3)Neither the name of docker-java-api nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.amihaiemil.docker;
+
+import javax.json.JsonObject;
+
+/**
+ * Info about the Docker engine, in JSON format.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 0.0.13
+ */
+final class RtInfo extends JsonResource implements Info {
+
+ /**
+ * Docker to which iot refers to.
+ */
+ private final Docker docker;
+
+ /**
+ * Constructor.
+ * @param rep This info in JSON format.
+ * @param docker The Docker to which it refers to.
+ */
+ RtInfo(final JsonObject rep, final Docker docker) {
+ super(rep);
+ this.docker = docker;
+ }
+
+ @Override
+ public Docker docker() {
+ return this.docker;
+ }
+}
diff --git a/src/main/java/com/amihaiemil/docker/RtVersion.java b/src/main/java/com/amihaiemil/docker/RtVersion.java
index fc9718d..42fb317 100644
--- a/src/main/java/com/amihaiemil/docker/RtVersion.java
+++ b/src/main/java/com/amihaiemil/docker/RtVersion.java
@@ -39,14 +39,26 @@
* @since 0.0.11
*/
final class RtVersion extends JsonResource implements Version {
+
+ /**
+ * Docker to which this Version belongs.
+ */
+ private final Docker docker;
+
/**
* Ctor.
* @param client The http client.
* @param uri The URI for this version.
+ * @param dkr Parent Docker.
* @throws IOException If an I/O error occurs.
*/
- RtVersion(final HttpClient client, final URI uri) throws IOException {
+ RtVersion(
+ final HttpClient client,
+ final URI uri,
+ final Docker dkr
+ ) throws IOException {
super(fetch(client, uri));
+ this.docker = dkr;
}
/**
@@ -134,4 +146,9 @@ public String arch() {
public boolean experimental() {
return this.getBoolean("Experimental");
}
+
+ @Override
+ public Docker docker() {
+ return this.docker;
+ }
}
diff --git a/src/main/java/com/amihaiemil/docker/Version.java b/src/main/java/com/amihaiemil/docker/Version.java
index 09601a1..5b6c35e 100644
--- a/src/main/java/com/amihaiemil/docker/Version.java
+++ b/src/main/java/com/amihaiemil/docker/Version.java
@@ -49,4 +49,10 @@ public interface Version extends JsonObject {
* @return Whether experimental docker features are enabled
*/
boolean experimental();
+
+ /**
+ * The Docker engine to which this Version belongs.
+ * @return Docker.
+ */
+ Docker docker();
}
diff --git a/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java b/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java
index 7ad6bf4..da348fa 100644
--- a/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java
+++ b/src/test/java/com/amihaiemil/docker/UnixDockerITCase.java
@@ -62,6 +62,43 @@ public void pingsDocker() throws Exception {
MatcherAssert.assertThat(docker.ping(), Matchers.is(Boolean.TRUE));
}
+ /**
+ * UnixDocker can return Info about Docker.
+ * @throws Exception If something goes wrong.
+ */
+ @Test
+ public void returnsInfo() throws Exception {
+ final Docker docker = new UnixDocker(
+ new File("/var/run/docker.sock")
+ );
+ final Info info = docker.info();
+ MatcherAssert.assertThat(info, Matchers.notNullValue());
+ MatcherAssert.assertThat(info.docker(), Matchers.is(docker));
+ MatcherAssert.assertThat(
+ info.getString("OSType"),
+ Matchers.equalTo("linux")
+ );
+ }
+
+ /**
+ * UnixDocker can return the Version of Docker.
+ * @throws Exception If something goes wrong.
+ */
+ @Test
+ public void returnsVersion() throws Exception {
+ final Docker docker = new UnixDocker(
+ new File("/var/run/docker.sock")
+ );
+ final Version version = docker.version();
+ MatcherAssert.assertThat(version, Matchers.notNullValue());
+ MatcherAssert.assertThat(version.docker(), Matchers.is(docker));
+ MatcherAssert.assertThat(
+ version.platformName(),
+ Matchers.equalTo("Docker Engine - Community")
+ );
+ }
+
+
/**
* Docker can return its Events unfiltered.
* @throws Exception If something goes wrong.
diff --git a/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java b/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java
index db64e11..b21cb98 100644
--- a/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java
+++ b/src/test/java/com/amihaiemil/docker/UnixDockerTestCase.java
@@ -26,6 +26,7 @@
package com.amihaiemil.docker;
import com.amihaiemil.docker.mock.AssertRequest;
+import com.amihaiemil.docker.mock.Condition;
import com.amihaiemil.docker.mock.Response;
import java.io.File;
import org.apache.http.HttpStatus;
@@ -33,6 +34,8 @@
import org.hamcrest.Matchers;
import org.junit.Test;
+import javax.json.Json;
+
/**
* Unit tests for LocalUnixDocker.
* @author Mihai Andronache (amihaiemil@gmail.com)
@@ -191,4 +194,38 @@ public void unsupportedOperationPlugins() {
new File("/var/run/docker.sock")
).plugins();
}
+
+ /**
+ * RtDocker can return info about itself.
+ * @throws Exception If something goes wrong.
+ */
+ @Test
+ public void returnsInfo() throws Exception {
+ final Docker docker = new UnixDocker(
+ new AssertRequest(
+ new Response(
+ HttpStatus.SC_OK,
+ "{\"info\": \"running\"}"
+ ),
+ new Condition(
+ "info() must send a GET request",
+ req -> "GET".equals(req.getRequestLine().getMethod())
+ ),
+ new Condition(
+ "info() resource URL must be unix://localhost:80/1.40/info",
+ req -> req.getRequestLine()
+ .getUri().equals("unix://localhost:80/1.40/info")
+ )
+ ),
+ "1.40"
+ );
+ MatcherAssert.assertThat(
+ docker.info(),
+ Matchers.equalTo(
+ Json.createObjectBuilder()
+ .add("info", "running")
+ .build()
+ )
+ );
+ }
}