|
| 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 | +} |
0 commit comments