Browse Source

Merge pull request #17489 from WeiZhang555/network-rm-multi

Enhance `docker network rm` to delete multi net
Vincent Demeester 9 years ago
parent
commit
a600bf4eab

+ 18 - 9
api/client/network.go

@@ -87,19 +87,28 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
 	return nil
 }
 
-// CmdNetworkRm deletes a network
+// CmdNetworkRm deletes one or more networks
 //
-// Usage: docker network rm <NETWORK-NAME | NETWORK-ID>
+// Usage: docker network rm NETWORK-NAME|NETWORK-ID [NETWORK-NAME|NETWORK-ID...]
 func (cli *DockerCli) CmdNetworkRm(args ...string) error {
-	cmd := Cli.Subcmd("network rm", []string{"NETWORK"}, "Deletes a network", false)
-	cmd.Require(flag.Exact, 1)
+	cmd := Cli.Subcmd("network rm", []string{"NETWORK [NETWORK...]"}, "Deletes one or more networks", false)
+	cmd.Require(flag.Min, 1)
 	err := cmd.ParseFlags(args, true)
 	if err != nil {
 		return err
 	}
-	_, _, err = readBody(cli.call("DELETE", "/networks/"+cmd.Arg(0), nil, nil))
-	if err != nil {
-		return err
+
+	status := 0
+	for _, net := range cmd.Args() {
+		_, _, err = readBody(cli.call("DELETE", "/networks/"+net, nil, nil))
+		if err != nil {
+			fmt.Fprintf(cli.err, "%s\n", err)
+			status = 1
+			continue
+		}
+	}
+	if status != 0 {
+		return Cli.StatusError{StatusCode: status}
 	}
 	return nil
 }
@@ -193,7 +202,7 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
 //
 // Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
 func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
-	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on a network", false)
+	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
 	cmd.Require(flag.Min, 1)
 	err := cmd.ParseFlags(args, true)
 	if err != nil {
@@ -208,7 +217,7 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
 			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)
+				fmt.Fprintf(cli.err, "%s\n", err)
 			}
 			status = 1
 			continue

+ 18 - 3
docs/reference/commandline/network_rm.md

@@ -10,18 +10,33 @@ parent = "smn_cli"
 
 # network rm
 
-    Usage:  docker network rm [OPTIONS] NAME | ID
+    Usage:  docker network rm [OPTIONS] NETWORK [NETWORK...]
 
-    Deletes a network
+    Deletes one or more networks
 
       --help=false       Print usage
 
-Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
+Removes one or more networks by name or identifier. To remove a network,
+you must first disconnect any containers connected to it.
+To remove the network named 'my-network':
 
 ```bash
   $ docker network rm my-network
 ```
 
+To delete multiple networks in a single `docker network rm` command, provide
+multiple network names or id's. The following example deletes a network with id
+`3695c422697f` and a network named `my-network`:
+
+```bash
+  $ docker network rm 3695c422697f my-network
+```
+
+When you specify multiple networks, the command attempts to delete each in turn.
+If the deletion of one network fails, the command continues to the next on the
+list and tries to delete that. The command reports success or failure for each
+deletion.
+
 ## Related information
 
 * [network disconnect ](network_disconnect.md)

+ 23 - 0
integration-cli/docker_cli_network_unix_test.go

@@ -270,6 +270,29 @@ func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) {
 	c.Assert(err, checker.NotNil, check.Commentf("%v", out))
 }
 
+func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
+	dockerCmd(c, "network", "create", "testDelMulti0")
+	assertNwIsAvailable(c, "testDelMulti0")
+	dockerCmd(c, "network", "create", "testDelMulti1")
+	assertNwIsAvailable(c, "testDelMulti1")
+	dockerCmd(c, "network", "create", "testDelMulti2")
+	assertNwIsAvailable(c, "testDelMulti2")
+	out, _ := dockerCmd(c, "run", "-d", "--net", "testDelMulti2", "busybox", "top")
+	waitRun(strings.TrimSpace(out))
+
+	// delete three networks at the same time, since testDelMulti2
+	// contains active container, it's deletion should fail.
+	out, _, err := dockerCmdWithError("network", "rm", "testDelMulti0", "testDelMulti1", "testDelMulti2")
+	// err should not be nil due to deleting testDelMulti2 failed.
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
+	// testDelMulti2 should fail due to network has active endpoints
+	c.Assert(out, checker.Contains, "has active endpoints")
+	assertNwNotAvailable(c, "testDelMulti0")
+	assertNwNotAvailable(c, "testDelMulti1")
+	// testDelMulti2 can't be deleted, so it should exists
+	assertNwIsAvailable(c, "testDelMulti2")
+}
+
 func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
 	out, _ := dockerCmd(c, "network", "inspect", "host", "none")
 	networkResources := []types.NetworkResource{}

+ 21 - 6
man/docker-network-rm.1.md

@@ -2,24 +2,39 @@
 % Docker Community
 % OCT 2015
 # NAME
-docker-network-rm - remove a new network
+docker-network-rm - remove one or more networks
 
 # SYNOPSIS
-**docker network rm**
+**docker network rm** 
 [**--help**]
-NETWORK
+NETWORK [NETWORK...]
 
 # DESCRIPTION
 
-Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
+Removes one or more networks by name or identifier. To remove a network,
+you must first disconnect any containers connected to it.
+To remove the network named 'my-network':
 
-```
+```bash
   $ docker network rm my-network
 ```
 
+To delete multiple networks in a single `docker network rm` command, provide
+multiple network names or id's. The following example deletes a network with id
+`3695c422697f` and a network named `my-network`:
+
+```bash
+  $ docker network rm 3695c422697f my-network
+```
+
+When you specify multiple networks, the command attempts to delete each in turn.
+If the deletion of one network fails, the command continues to the next on the
+list and tries to delete that. The command reports success or failure for each
+deletion.
+
 # OPTIONS
 **NETWORK**
-  Specify network name
+  Specify network name or id
 
 **--help**
   Print usage statement