Skip to content

Commit 6a6ac88

Browse files
committed
[grid] Adding container inspect to the api
1 parent 52deabe commit 6a6ac88

4 files changed

Lines changed: 79 additions & 0 deletions

File tree

java/server/src/org/openqa/selenium/docker/Container.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@ public void delete() {
6161
protocol.deleteContainer(id);
6262
}
6363
}
64+
65+
public ContainerInfo inspect() {
66+
LOG.info("Inspecting " + getId());
67+
68+
return protocol.inspectContainer(getId());
69+
}
6470
}

java/server/src/org/openqa/selenium/docker/DockerProtocol.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ public interface DockerProtocol {
2929
void stopContainer(ContainerId id, Duration timeout) throws DockerException;
3030
boolean exists(ContainerId id);
3131
void deleteContainer(ContainerId id) throws DockerException;
32+
ContainerInfo inspectContainer(ContainerId id) throws DockerException;
3233
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.docker.v1_40;
19+
20+
import org.openqa.selenium.docker.ContainerId;
21+
import org.openqa.selenium.docker.ContainerInfo;
22+
import org.openqa.selenium.internal.Require;
23+
import org.openqa.selenium.json.Json;
24+
import org.openqa.selenium.remote.http.Contents;
25+
import org.openqa.selenium.remote.http.HttpHandler;
26+
import org.openqa.selenium.remote.http.HttpRequest;
27+
import org.openqa.selenium.remote.http.HttpResponse;
28+
29+
import java.util.Map;
30+
import java.util.logging.Logger;
31+
32+
import static java.net.HttpURLConnection.HTTP_OK;
33+
import static org.openqa.selenium.json.Json.MAP_TYPE;
34+
import static org.openqa.selenium.remote.http.HttpMethod.GET;
35+
36+
class InspectContainer {
37+
private static final Logger LOG = Logger.getLogger(InspectContainer.class.getName());
38+
private static final Json JSON = new Json();
39+
private final HttpHandler client;
40+
41+
public InspectContainer(HttpHandler client) {
42+
this.client = Require.nonNull("HTTP client", client);
43+
}
44+
45+
public ContainerInfo apply(ContainerId id) {
46+
Require.nonNull("Container id", id);
47+
48+
HttpResponse res = client.execute(
49+
new HttpRequest(GET, String.format("/v1.40/containers/%s/json", id))
50+
.addHeader("Content-Length", "0")
51+
.addHeader("Content-Type", "text/plain"));
52+
if (res.getStatus() != HTTP_OK) {
53+
LOG.warning("Unable to inspect container " + id);
54+
}
55+
Map<String, Object> rawInspectInfo = JSON.toType(Contents.string(res), MAP_TYPE);
56+
Map<String, Object> networkSettings = (Map<String, Object>) rawInspectInfo.get("NetworkSettings");
57+
String ip = (String) networkSettings.get("IPAddress");
58+
return new ContainerInfo(id, ip);
59+
}
60+
}

java/server/src/org/openqa/selenium/docker/v1_40/V140Docker.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openqa.selenium.docker.Container;
2121
import org.openqa.selenium.docker.ContainerId;
2222
import org.openqa.selenium.docker.ContainerConfig;
23+
import org.openqa.selenium.docker.ContainerInfo;
2324
import org.openqa.selenium.docker.DockerException;
2425
import org.openqa.selenium.docker.DockerProtocol;
2526
import org.openqa.selenium.docker.Image;
@@ -41,6 +42,7 @@ public class V140Docker implements DockerProtocol {
4142
private final StopContainer stopContainer;
4243
private final DeleteContainer deleteContainer;
4344
private final ContainerExists containerExists;
45+
private final InspectContainer inspectContainer;
4446

4547
public V140Docker(HttpHandler client) {
4648
Require.nonNull("HTTP client", client);
@@ -52,6 +54,7 @@ public V140Docker(HttpHandler client) {
5254
stopContainer = new StopContainer(client);
5355
deleteContainer = new DeleteContainer(client);
5456
containerExists = new ContainerExists(client);
57+
inspectContainer = new InspectContainer(client);
5558
}
5659

5760
@Override
@@ -128,4 +131,13 @@ public void deleteContainer(ContainerId id) throws DockerException {
128131

129132
deleteContainer.apply(id);
130133
}
134+
135+
@Override
136+
public ContainerInfo inspectContainer(ContainerId id) throws DockerException {
137+
Require.nonNull("Container id", id);
138+
139+
LOG.info("Inspecting container: " + id);
140+
141+
return inspectContainer.apply(id);
142+
}
131143
}

0 commit comments

Comments
 (0)