From 7af9f988ac535e4ae2e87976db25d4f7047274db Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 21 Oct 2015 08:57:29 +0200 Subject: [PATCH] Add support for multiple network in inspect To be consistent with other inspect command (on container and images), add the possiblity to pass multiple network to the network inspect commands. `docker network inspect host bridge none` is possible now. Signed-off-by: Vincent Demeester --- api/client/network.go | 40 ++++++++++++++----- .../docker_cli_network_unix_test.go | 28 ++++++++++++- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/api/client/network.go b/api/client/network.go index cb19d0d8e7..2b3c922122 100644 --- a/api/client/network.go +++ b/api/client/network.go @@ -184,31 +184,49 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error { // CmdNetworkInspect inspects the network object for more details // -// Usage: docker network inspect +// Usage: docker network inspect [] // CmdNetworkInspect handles Network inspect UI func (cli *DockerCli) CmdNetworkInspect(args ...string) error { cmd := Cli.Subcmd("network inspect", []string{"NETWORK"}, "Displays detailed information on a network", false) - cmd.Require(flag.Exact, 1) + cmd.Require(flag.Min, 1) err := cmd.ParseFlags(args, true) if err != nil { return err } - obj, _, err := readBody(cli.call("GET", "/networks/"+cmd.Arg(0), nil, nil)) + status := 0 + var networks []*types.NetworkResource + for _, name := range cmd.Args() { + obj, _, err := readBody(cli.call("GET", "/networks/"+name, nil, nil)) + if err != nil { + if strings.Contains(err.Error(), "not found") { + fmt.Fprintf(cli.err, "Error: No such network: %s\n", name) + } else { + fmt.Fprintf(cli.err, "%s", err) + } + status = 1 + continue + } + networkResource := types.NetworkResource{} + if err := json.NewDecoder(bytes.NewReader(obj)).Decode(&networkResource); err != nil { + return err + } + + networks = append(networks, &networkResource) + } + + b, err := json.MarshalIndent(networks, "", " ") if err != nil { return err } - networkResource := &types.NetworkResource{} - if err := json.NewDecoder(bytes.NewReader(obj)).Decode(networkResource); err != nil { - return err - } - indented := new(bytes.Buffer) - if err := json.Indent(indented, obj, "", " "); err != nil { + if _, err := io.Copy(cli.out, bytes.NewReader(b)); err != nil { return err } - if _, err := io.Copy(cli.out, indented); err != nil { - return err + io.WriteString(cli.out, "\n") + + if status != 0 { + return Cli.StatusError{StatusCode: status} } return nil } diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index 0586f39b51..6c4dfe7196 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -119,10 +119,10 @@ func isNwPresent(c *check.C, name string) bool { func getNwResource(c *check.C, name string) *types.NetworkResource { out, _ := dockerCmd(c, "network", "inspect", name) - nr := types.NetworkResource{} + nr := []types.NetworkResource{} err := json.Unmarshal([]byte(out), &nr) c.Assert(err, check.IsNil) - return &nr + return &nr[0] } func (s *DockerNetworkSuite) TestDockerNetworkLsDefault(c *check.C) { @@ -145,6 +145,30 @@ func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) { c.Assert(err, checker.NotNil, check.Commentf("%v", out)) } +func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) { + out, _ := dockerCmd(c, "network", "inspect", "host", "none") + networkResources := []types.NetworkResource{} + err := json.Unmarshal([]byte(out), &networkResources) + c.Assert(err, check.IsNil) + c.Assert(networkResources, checker.HasLen, 2) + + // Should print an error, return an exitCode 1 *but* should print the host network + out, exitCode, err := dockerCmdWithError("network", "inspect", "host", "nonexistent") + c.Assert(err, checker.NotNil) + c.Assert(exitCode, checker.Equals, 1) + c.Assert(out, checker.Contains, "Error: No such network: nonexistent") + networkResources = []types.NetworkResource{} + inspectOut := strings.SplitN(out, "\n", 2)[1] + err = json.Unmarshal([]byte(inspectOut), &networkResources) + c.Assert(networkResources, checker.HasLen, 1) + + // Should print an error and return an exitCode, nothing else + out, exitCode, err = dockerCmdWithError("network", "inspect", "nonexistent") + c.Assert(err, checker.NotNil) + c.Assert(exitCode, checker.Equals, 1) + c.Assert(out, checker.Contains, "Error: No such network: nonexistent") +} + func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnect(c *check.C) { dockerCmd(c, "network", "create", "test") assertNwIsAvailable(c, "test")