Browse Source

Add implementation for lookupContainerID

Signed-off-by: Alessandro Boch <aboch@docker.com>
Alessandro Boch 10 years ago
parent
commit
0969e192f2
2 changed files with 22 additions and 2 deletions
  1. 2 0
      libnetwork/client/client_test.go
  2. 20 2
      libnetwork/client/service.go

+ 2 - 0
libnetwork/client/client_test.go

@@ -73,6 +73,8 @@ func setupMockHTTPCallback() {
 				rsp = string(mockServiceListJSON)
 			} else if strings.HasSuffix(path, "services/"+mockServiceID) {
 				rsp = string(mockServiceJSON)
+			} else if strings.Contains(path, "containers") {
+				return nopCloser{bytes.NewBufferString("")}, 400, fmt.Errorf("Bad Request")
 			}
 		case "POST":
 			var data []byte

+ 20 - 2
libnetwork/client/service.go

@@ -92,8 +92,26 @@ func lookupServiceID(cli *NetworkCli, nwName, svNameID string) (string, error) {
 }
 
 func lookupContainerID(cli *NetworkCli, cnNameID string) (string, error) {
-	// TODO : containerID to sandbox-key ?
-	return cnNameID, nil
+	// Container is a Docker resource, ask docker about it.
+	// In case of connecton error, we assume we are running in dnet and return whatever was passed to us
+	obj, _, err := readBody(cli.call("GET", fmt.Sprintf("/containers/%s/json", cnNameID), nil, nil))
+	if err != nil {
+		// We are probably running outside of docker
+		return cnNameID, nil
+	}
+
+	var x map[string]interface{}
+	err = json.Unmarshal(obj, &x)
+	if err != nil {
+		return "", err
+	}
+	if iid, ok := x["Id"]; ok {
+		if id, ok := iid.(string); ok {
+			return id, nil
+		}
+		return "", fmt.Errorf("Unexpected data type for container ID in json response")
+	}
+	return "", fmt.Errorf("Cannot find container ID in json response")
 }
 
 // CmdService handles the service UI