From 0969e192f2aa23e3d54087eb37540503cadb1e99 Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Mon, 15 Jun 2015 02:27:24 -0700 Subject: [PATCH] Add implementation for lookupContainerID Signed-off-by: Alessandro Boch --- libnetwork/client/client_test.go | 2 ++ libnetwork/client/service.go | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/libnetwork/client/client_test.go b/libnetwork/client/client_test.go index 9da1cbd281..2ba610dd31 100644 --- a/libnetwork/client/client_test.go +++ b/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 diff --git a/libnetwork/client/service.go b/libnetwork/client/service.go index e2285d7e6f..35e040f805 100644 --- a/libnetwork/client/service.go +++ b/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