Browse code

Enhance `docker network rm` to delete multi net

This commit enhance `docker network rm` command to allow user to delete
multi networks at the same time.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>

Zhang Wei authored on 2015/10/30 01:10:20
Showing 4 changed files
... ...
@@ -87,19 +87,28 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
87 87
 	return nil
88 88
 }
89 89
 
90
-// CmdNetworkRm deletes a network
90
+// CmdNetworkRm deletes one or more networks
91 91
 //
92
-// Usage: docker network rm <NETWORK-NAME | NETWORK-ID>
92
+// Usage: docker network rm NETWORK-NAME|NETWORK-ID [NETWORK-NAME|NETWORK-ID...]
93 93
 func (cli *DockerCli) CmdNetworkRm(args ...string) error {
94
-	cmd := Cli.Subcmd("network rm", []string{"NETWORK"}, "Deletes a network", false)
95
-	cmd.Require(flag.Exact, 1)
94
+	cmd := Cli.Subcmd("network rm", []string{"NETWORK [NETWORK...]"}, "Deletes one or more networks", false)
95
+	cmd.Require(flag.Min, 1)
96 96
 	err := cmd.ParseFlags(args, true)
97 97
 	if err != nil {
98 98
 		return err
99 99
 	}
100
-	_, _, err = readBody(cli.call("DELETE", "/networks/"+cmd.Arg(0), nil, nil))
101
-	if err != nil {
102
-		return err
100
+
101
+	status := 0
102
+	for _, net := range cmd.Args() {
103
+		_, _, err = readBody(cli.call("DELETE", "/networks/"+net, nil, nil))
104
+		if err != nil {
105
+			fmt.Fprintf(cli.err, "%s\n", err)
106
+			status = 1
107
+			continue
108
+		}
109
+	}
110
+	if status != 0 {
111
+		return Cli.StatusError{StatusCode: status}
103 112
 	}
104 113
 	return nil
105 114
 }
... ...
@@ -193,7 +202,7 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
193 193
 //
194 194
 // Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
195 195
 func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
196
-	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on a network", false)
196
+	cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
197 197
 	cmd.Require(flag.Min, 1)
198 198
 	err := cmd.ParseFlags(args, true)
199 199
 	if err != nil {
... ...
@@ -208,7 +217,7 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
208 208
 			if strings.Contains(err.Error(), "not found") {
209 209
 				fmt.Fprintf(cli.err, "Error: No such network: %s\n", name)
210 210
 			} else {
211
-				fmt.Fprintf(cli.err, "%s", err)
211
+				fmt.Fprintf(cli.err, "%s\n", err)
212 212
 			}
213 213
 			status = 1
214 214
 			continue
... ...
@@ -10,18 +10,33 @@ parent = "smn_cli"
10 10
 
11 11
 # network rm
12 12
 
13
-    Usage:  docker network rm [OPTIONS] NAME | ID
13
+    Usage:  docker network rm [OPTIONS] NETWORK [NETWORK...]
14 14
 
15
-    Deletes a network
15
+    Deletes one or more networks
16 16
 
17 17
       --help=false       Print usage
18 18
 
19
-Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
19
+Removes one or more networks by name or identifier. To remove a network,
20
+you must first disconnect any containers connected to it.
21
+To remove the network named 'my-network':
20 22
 
21 23
 ```bash
22 24
   $ docker network rm my-network
23 25
 ```
24 26
 
27
+To delete multiple networks in a single `docker network rm` command, provide
28
+multiple network names or id's. The following example deletes a network with id
29
+`3695c422697f` and a network named `my-network`:
30
+
31
+```bash
32
+  $ docker network rm 3695c422697f my-network
33
+```
34
+
35
+When you specify multiple networks, the command attempts to delete each in turn.
36
+If the deletion of one network fails, the command continues to the next on the
37
+list and tries to delete that. The command reports success or failure for each
38
+deletion.
39
+
25 40
 ## Related information
26 41
 
27 42
 * [network disconnect ](network_disconnect.md)
... ...
@@ -270,6 +270,29 @@ func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) {
270 270
 	c.Assert(err, checker.NotNil, check.Commentf("%v", out))
271 271
 }
272 272
 
273
+func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
274
+	dockerCmd(c, "network", "create", "testDelMulti0")
275
+	assertNwIsAvailable(c, "testDelMulti0")
276
+	dockerCmd(c, "network", "create", "testDelMulti1")
277
+	assertNwIsAvailable(c, "testDelMulti1")
278
+	dockerCmd(c, "network", "create", "testDelMulti2")
279
+	assertNwIsAvailable(c, "testDelMulti2")
280
+	out, _ := dockerCmd(c, "run", "-d", "--net", "testDelMulti2", "busybox", "top")
281
+	waitRun(strings.TrimSpace(out))
282
+
283
+	// delete three networks at the same time, since testDelMulti2
284
+	// contains active container, it's deletion should fail.
285
+	out, _, err := dockerCmdWithError("network", "rm", "testDelMulti0", "testDelMulti1", "testDelMulti2")
286
+	// err should not be nil due to deleting testDelMulti2 failed.
287
+	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
288
+	// testDelMulti2 should fail due to network has active endpoints
289
+	c.Assert(out, checker.Contains, "has active endpoints")
290
+	assertNwNotAvailable(c, "testDelMulti0")
291
+	assertNwNotAvailable(c, "testDelMulti1")
292
+	// testDelMulti2 can't be deleted, so it should exists
293
+	assertNwIsAvailable(c, "testDelMulti2")
294
+}
295
+
273 296
 func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
274 297
 	out, _ := dockerCmd(c, "network", "inspect", "host", "none")
275 298
 	networkResources := []types.NetworkResource{}
... ...
@@ -2,24 +2,39 @@
2 2
 % Docker Community
3 3
 % OCT 2015
4 4
 # NAME
5
-docker-network-rm - remove a new network
5
+docker-network-rm - remove one or more networks
6 6
 
7 7
 # SYNOPSIS
8
-**docker network rm**
8
+**docker network rm** 
9 9
 [**--help**]
10
-NETWORK
10
+NETWORK [NETWORK...]
11 11
 
12 12
 # DESCRIPTION
13 13
 
14
-Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
14
+Removes one or more networks by name or identifier. To remove a network,
15
+you must first disconnect any containers connected to it.
16
+To remove the network named 'my-network':
15 17
 
16
-```
18
+```bash
17 19
   $ docker network rm my-network
18 20
 ```
19 21
 
22
+To delete multiple networks in a single `docker network rm` command, provide
23
+multiple network names or id's. The following example deletes a network with id
24
+`3695c422697f` and a network named `my-network`:
25
+
26
+```bash
27
+  $ docker network rm 3695c422697f my-network
28
+```
29
+
30
+When you specify multiple networks, the command attempts to delete each in turn.
31
+If the deletion of one network fails, the command continues to the next on the
32
+list and tries to delete that. The command reports success or failure for each
33
+deletion.
34
+
20 35
 # OPTIONS
21 36
 **NETWORK**
22
-  Specify network name
37
+  Specify network name or id
23 38
 
24 39
 **--help**
25 40
   Print usage statement