Browse code

Merge pull request #22319 from thaJeztah/network-driver-filter

Add "driver" filter for network ls

Vincent Demeester authored on 2016/04/30 17:38:10
Showing 7 changed files
... ...
@@ -1378,6 +1378,11 @@ _docker_network_inspect() {
1378 1378
 _docker_network_ls() {
1379 1379
 	local key=$(__docker_map_key_of_current_option '--filter|-f')
1380 1380
 	case "$key" in
1381
+		driver)
1382
+			local plugins=" $(__docker_plugins Network) "
1383
+			COMPREPLY=( $(compgen -W "$plugins" -- "${cur##*=}") )
1384
+			return
1385
+			;;
1381 1386
 		id)
1382 1387
 			cur="${cur##*=}"
1383 1388
 			__docker_complete_network_ids
... ...
@@ -1396,7 +1401,7 @@ _docker_network_ls() {
1396 1396
 
1397 1397
 	case "$prev" in
1398 1398
 		--filter|-f)
1399
-			COMPREPLY=( $( compgen -S = -W "id name type" -- "$cur" ) )
1399
+			COMPREPLY=( $( compgen -S = -W "driver id label name type" -- "$cur" ) )
1400 1400
 			__docker_nospace
1401 1401
 			return
1402 1402
 			;;
... ...
@@ -13,10 +13,11 @@ type filterHandler func([]libnetwork.Network, string) ([]libnetwork.Network, err
13 13
 var (
14 14
 	// AcceptedFilters is an acceptable filters for validation
15 15
 	AcceptedFilters = map[string]bool{
16
-		"type":  true,
17
-		"name":  true,
18
-		"id":    true,
19
-		"label": true,
16
+		"driver": true,
17
+		"type":   true,
18
+		"name":   true,
19
+		"id":     true,
20
+		"label":  true,
20 21
 	}
21 22
 )
22 23
 
... ...
@@ -50,6 +51,11 @@ func FilterNetworks(nws []libnetwork.Network, filter filters.Args) ([]libnetwork
50 50
 
51 51
 	var displayNet []libnetwork.Network
52 52
 	for _, nw := range nws {
53
+		if filter.Include("driver") {
54
+			if !filter.ExactMatch("driver", nw.Type()) {
55
+				continue
56
+			}
57
+		}
53 58
 		if filter.Include("name") {
54 59
 			if !filter.Match("name", nw.Name()) {
55 60
 				continue
... ...
@@ -114,7 +114,7 @@ This section lists each version from latest to oldest.  Each listing includes a
114 114
 
115 115
 * `POST /containers/create` now takes `StorageOpt` field.
116 116
 * `GET /info` now returns `SecurityOptions` field, showing if `apparmor`, `seccomp`, or `selinux` is supported.
117
-* `GET /networks` now supports filtering by `label`.
117
+* `GET /networks` now supports filtering by `label` and `driver`.
118 118
 * `POST /containers/create` now takes `MaximumIOps` and `MaximumIOBps` fields. Windows daemon only.
119 119
 
120 120
 ### v1.23 API changes
... ...
@@ -3075,6 +3075,7 @@ Content-Type: application/json
3075 3075
 Query Parameters:
3076 3076
 
3077 3077
 - **filters** - JSON encoded network list filter. The filter value is one of:
3078
+  -   `driver=<driver-name>` Matches a network's driver.
3078 3079
   -   `id=<network-id>` Matches all or part of a network id.
3079 3080
   -   `label=<key>` or `label=<key>=<value>` of a network label.
3080 3081
   -   `name=<network-name>` Matches all or part of a network name.
... ...
@@ -51,11 +51,25 @@ Multiple filter flags are combined as an `OR` filter. For example,
51 51
 
52 52
 The currently supported filters are:
53 53
 
54
+* driver
54 55
 * id (network's id)
55 56
 * label (`label=<key>` or `label=<key>=<value>`)
56 57
 * name (network's name)
57 58
 * type (custom|builtin)
58 59
 
60
+#### Driver
61
+
62
+The `driver` filter matches networks based on their driver.
63
+
64
+The following example matches networks with the `bridge` driver:
65
+
66
+```bash
67
+$ docker network ls --filter driver=bridge
68
+NETWORK ID          NAME                DRIVER
69
+db9db329f835        test1               bridge
70
+f6e212da9dfd        test2               bridge
71
+```
72
+
59 73
 #### ID
60 74
 
61 75
 The `id` filter matches on all or part of a network's ID.
... ...
@@ -83,7 +97,7 @@ NETWORK ID          NAME                DRIVER
83 83
 
84 84
 #### Label
85 85
 
86
-The `label` filter matches containers based on the presence of a `label` alone or a `label` and a
86
+The `label` filter matches networks based on the presence of a `label` alone or a `label` and a
87 87
 value.
88 88
 
89 89
 The following filter matches networks with the `usage` label regardless of its value.
... ...
@@ -95,7 +109,7 @@ db9db329f835        test1               bridge
95 95
 f6e212da9dfd        test2               bridge
96 96
 ```
97 97
 
98
-The following filter matches containers with the `usage` label with the `prod` value.
98
+The following filter matches networks with the `usage` label with the `prod` value.
99 99
 
100 100
 ```bash
101 101
 $ docker network ls -f "label=usage=prod"
... ...
@@ -352,6 +352,15 @@ func (s *DockerNetworkSuite) TestDockerNetworkLsFilter(c *check.C) {
352 352
 	out, _ = dockerCmd(c, "network", "ls", "-f", "label=nonexistent")
353 353
 	outArr := strings.Split(strings.TrimSpace(out), "\n")
354 354
 	c.Assert(len(outArr), check.Equals, 1, check.Commentf("%s\n", out))
355
+
356
+	out, _ = dockerCmd(c, "network", "ls", "-f", "driver=null")
357
+	assertNwList(c, out, []string{"none"})
358
+
359
+	out, _ = dockerCmd(c, "network", "ls", "-f", "driver=host")
360
+	assertNwList(c, out, []string{"host"})
361
+
362
+	out, _ = dockerCmd(c, "network", "ls", "-f", "driver=bridge")
363
+	assertNwList(c, out, []string{"bridge", "dev", testNet})
355 364
 }
356 365
 
357 366
 func (s *DockerNetworkSuite) TestDockerNetworkCreateDelete(c *check.C) {
... ...
@@ -46,11 +46,25 @@ Multiple filter flags are combined as an `OR` filter. For example,
46 46
 
47 47
 The currently supported filters are:
48 48
 
49
+* driver
49 50
 * id (network's id)
50 51
 * label (`label=<key>` or `label=<key>=<value>`)
51 52
 * name (network's name)
52 53
 * type (custom|builtin)
53 54
 
55
+#### Driver
56
+
57
+The `driver` filter matches networks based on their driver.
58
+
59
+The following example matches networks with the `bridge` driver:
60
+
61
+```bash
62
+$ docker network ls --filter driver=bridge
63
+NETWORK ID          NAME                DRIVER
64
+db9db329f835        test1               bridge
65
+f6e212da9dfd        test2               bridge
66
+```
67
+
54 68
 #### ID
55 69
 
56 70
 The `id` filter matches on all or part of a network's ID.
... ...
@@ -78,7 +92,7 @@ NETWORK ID          NAME                DRIVER
78 78
 
79 79
 #### Label
80 80
 
81
-The `label` filter matches containers based on the presence of a `label` alone or a `label` and a
81
+The `label` filter matches networks based on the presence of a `label` alone or a `label` and a
82 82
 value.
83 83
 
84 84
 The following filter matches networks with the `usage` label regardless of its value.
... ...
@@ -90,7 +104,7 @@ db9db329f835        test1               bridge
90 90
 f6e212da9dfd        test2               bridge
91 91
 ```
92 92
 
93
-The following filter matches containers with the `usage` label with the `prod` value.
93
+The following filter matches networks with the `usage` label with the `prod` value.
94 94
 
95 95
 ```bash
96 96
 $ docker network ls -f "label=usage=prod"