158b2a1875
This fix tries to add a `scope` in the query of `/networks/<id>` (`NetworkInspect`) so that in case of duplicate network names, it is possible to locate the network ID based on the network scope (`local`, 'swarm', or `global`). Multiple networks might exist in different scopes, which is a legitimate case. For example, a network name `foo` might exists locally and in swarm network. However, before this PR it was not possible to query a network name `foo` in a specific scope like swarm. This fix fixes the issue by allowing a `scope` query in `/networks/<id>`. Additional test cases have been added to unit tests and integration tests. This fix is related to docker/cli#167, moby/moby#30897, moby/moby#33561, moby/moby#30242 This fix fixes docker/cli#167 Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
// NetworkInspect returns the information for a specific network configured in the docker host.
|
|
func (cli *Client) NetworkInspect(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error) {
|
|
networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID, options)
|
|
return networkResource, err
|
|
}
|
|
|
|
// NetworkInspectWithRaw returns the information for a specific network configured in the docker host and its raw representation.
|
|
func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
|
|
var (
|
|
networkResource types.NetworkResource
|
|
resp serverResponse
|
|
err error
|
|
)
|
|
query := url.Values{}
|
|
if options.Verbose {
|
|
query.Set("verbose", "true")
|
|
}
|
|
if options.Scope != "" {
|
|
query.Set("scope", options.Scope)
|
|
}
|
|
resp, err = cli.get(ctx, "/networks/"+networkID, query, nil)
|
|
if err != nil {
|
|
if resp.statusCode == http.StatusNotFound {
|
|
return networkResource, nil, networkNotFoundError{networkID}
|
|
}
|
|
return networkResource, nil, err
|
|
}
|
|
defer ensureReaderClosed(resp)
|
|
|
|
body, err := ioutil.ReadAll(resp.body)
|
|
if err != nil {
|
|
return networkResource, nil, err
|
|
}
|
|
rdr := bytes.NewReader(body)
|
|
err = json.NewDecoder(rdr).Decode(&networkResource)
|
|
return networkResource, body, err
|
|
}
|