Skip to content

Commit 11786d2

Browse files
committed
Images.pull, import and Image.history
1 parent 2965aee commit 11786d2

File tree

6 files changed

+60
-30
lines changed

6 files changed

+60
-30
lines changed

src/main/java/com/amihaiemil/docker/Image.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public interface Image extends JsonObject {
4949

5050
/**
5151
* Return parent layers of this Image.
52-
* @return Images parent Images.
52+
* @return An Iterable containing the parents of this Image.
5353
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/ImageHistory">Image History</a>
5454
*/
55-
Images history();
55+
Iterable<Image> history();
5656

5757
/**
5858
* Remove an image, along with any untagged parent images that were

src/main/java/com/amihaiemil/docker/Images.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,31 @@
4040
public interface Images extends Iterable<Image> {
4141

4242
/**
43-
* Creates an image by pulling it from a registry.
43+
* Pull an Image from the Docker registry.
4444
* @param name Name of the image to pull.
45-
* @param source The URL from which the image can be retrieved.
46-
* @param repo Repository name for the image when it is pulled.
4745
* @param tag Tag or digest for the image.
48-
* @return This {@link Images}.
46+
* @return The created {@link Image}.
4947
* @throws IOException If an I/O error occurs.
5048
* @throws UnexpectedResponseException If the API responds with an
5149
* unexpected status.
5250
* @checkstyle ParameterNumber (4 lines)
5351
*/
54-
Images create(
55-
final String name, final URL source, final String repo, final String tag
52+
Image pull(
53+
final String name, final String tag
54+
) throws IOException, UnexpectedResponseException;
55+
56+
/**
57+
* Import an Image.
58+
* @param source The URL from which the image can be retrieved.
59+
* @param repo Repository name given to an image when it is imported.
60+
* The repo may include a tag.
61+
* @return The imported Image.
62+
* @throws IOException If an I/O error occurs.
63+
* @throws UnexpectedResponseException If the API responds with an
64+
* undexpected status.
65+
*/
66+
Image importImage(
67+
final URL source, final String repo
5668
) throws IOException, UnexpectedResponseException;
5769

5870
/**

src/main/java/com/amihaiemil/docker/RtImage.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.http.HttpStatus;
3232
import org.apache.http.client.HttpClient;
3333
import org.apache.http.client.methods.HttpDelete;
34+
import org.apache.http.client.methods.HttpGet;
3435
import org.apache.http.client.methods.HttpPost;
3536

3637
/**
@@ -69,9 +70,17 @@ public JsonObject inspect()
6970
}
7071

7172
@Override
72-
public Images history() {
73-
return new RtImages(
74-
this.client, URI.create(this.baseUri.toString() + "/history")
73+
public Iterable<Image> history() {
74+
return () -> new ResourcesIterator<>(
75+
this.client,
76+
new HttpGet(this.baseUri.toString().concat("/history")),
77+
json -> new RtImage(
78+
json,
79+
this.client,
80+
URI.create(
81+
this.baseUri.toString() + "/" + json.getString("Id")
82+
)
83+
)
7584
);
7685
}
7786

src/main/java/com/amihaiemil/docker/RtImages.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.http.client.methods.HttpGet;
3535
import org.apache.http.client.methods.HttpPost;
3636

37+
import javax.json.Json;
38+
3739
/**
3840
* Runtime {@link Images}.
3941
* @author George Aristy (george.aristy@gmail.com)
@@ -61,16 +63,13 @@ final class RtImages implements Images {
6163
this.baseUri = uri;
6264
}
6365

64-
// @checkstyle ParameterNumber (4 lines)
6566
@Override
66-
public Images create(
67-
final String name, final URL source, final String repo, final String tag
67+
public Image pull(
68+
final String name, final String tag
6869
) throws IOException, UnexpectedResponseException {
6970
final HttpPost create = new HttpPost(
7071
new UncheckedUriBuilder(this.baseUri.toString().concat("/create"))
7172
.addParameter("fromImage", name)
72-
.addParameter("fromSrc", source.toString())
73-
.addParameter("repo", repo)
7473
.addParameter("tag", tag)
7574
.build()
7675
);
@@ -79,12 +78,28 @@ public Images create(
7978
create,
8079
new MatchStatus(create.getURI(), HttpStatus.SC_OK)
8180
);
82-
return this;
81+
return new RtImage(
82+
Json.createObjectBuilder().add("Name", name).build(),
83+
this.client,
84+
URI.create(
85+
this.baseUri.toString() + "/" + name
86+
)
87+
);
8388
} finally {
8489
create.releaseConnection();
8590
}
8691
}
8792

93+
@Override
94+
public Image importImage(
95+
final URL source, final String repo
96+
) throws IOException, UnexpectedResponseException {
97+
throw new UnsupportedOperationException(
98+
"Not yet implemented. If you can contribute please,"
99+
+ " do it here: https://www.github.com/amihaiemil/docker-java-api"
100+
);
101+
}
102+
88103
@Override
89104
public void prune() throws IOException, UnexpectedResponseException {
90105
final HttpPost prune = new HttpPost(

src/test/java/com/amihaiemil/docker/RtImageTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void returnsHistory() {
109109
).history(),
110110
Matchers.allOf(
111111
Matchers.notNullValue(),
112-
Matchers.instanceOf(Images.class)
112+
Matchers.instanceOf(Iterable.class)
113113
)
114114
);
115115
}

src/test/java/com/amihaiemil/docker/RtImagesTestCase.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.amihaiemil.docker.mock.Condition;
3030
import com.amihaiemil.docker.mock.Response;
3131
import java.net.URI;
32-
import java.net.URL;
3332
import java.util.concurrent.atomic.AtomicInteger;
3433
import javax.json.Json;
3534
import org.apache.http.HttpStatus;
@@ -48,10 +47,9 @@ public final class RtImagesTestCase {
4847
/**
4948
* Must return the same number of images as there are elements in the
5049
* json array returned by the service.
51-
* @throws Exception If an error occurs.
5250
*/
5351
@Test
54-
public void iteratesImages() throws Exception {
52+
public void iteratesImages() {
5553
final AtomicInteger count = new AtomicInteger();
5654
new RtImages(
5755
new AssertRequest(
@@ -110,7 +108,7 @@ public void iterateFailsIfResponseIs500() throws Exception {
110108
}
111109

112110
/**
113-
* {@link RtImages#create(String, URL, String, String)} must construct the
111+
* {@link RtImages#pull(String, String)} must construct the
114112
* URL with parameters correctly.
115113
* <p>
116114
* Notice the escaped characters for the 'fromSrc' parameter's value.
@@ -128,17 +126,13 @@ public void createSetsGivenParameters() throws Exception {
128126
System.out.println(req.getRequestLine().getUri());
129127
return req.getRequestLine().getUri().endsWith(
130128
// @checkstyle LineLength (1 line)
131-
"/create?fromImage=testImage&fromSrc=http%3A%2F%2Fdocker.registry.com&repo=testRepo&tag=1.23"
129+
"/create?fromImage=testImage&tag=1.23"
132130
);
133131
}
134132
)
135133
),
136134
URI.create("http://localhost")
137-
)
138-
.create(
139-
"testImage", new URL("http://docker.registry.com"),
140-
"testRepo", "1.23"
141-
);
135+
).pull("testImage", "1.23");
142136
}
143137

144138
/**
@@ -153,7 +147,7 @@ public void createErrorOnStatus404() throws Exception {
153147
new Response(HttpStatus.SC_NOT_FOUND)
154148
),
155149
URI.create("http://localhost")
156-
).create("", new URL("http://registry.docker.com"), "", "");
150+
).pull("", "");
157151
}
158152

159153
/**
@@ -168,7 +162,7 @@ public void createErrorOnStatus500() throws Exception {
168162
new Response(HttpStatus.SC_INTERNAL_SERVER_ERROR)
169163
),
170164
URI.create("http://localhost")
171-
).create("", new URL("http://registry.docker.com"), "", "");
165+
).pull("", "");
172166
}
173167

174168
/**

0 commit comments

Comments
 (0)