mirror of https://github.com/docker/docs.git
Merge pull request #1739 from vieux/support_filter_network_ls
support docker network ls --filter type=XXX
This commit is contained in:
commit
8aff2084b7
|
|
@ -245,8 +245,16 @@ func getNetworks(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
types := filters.Get("type")
|
||||||
|
for _, typ := range types {
|
||||||
|
if typ != "custom" && typ != "builtin" {
|
||||||
|
httpError(w, fmt.Sprintf("Invalid filter: 'type'='%s'", typ), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
out := []*dockerclient.NetworkResource{}
|
out := []*dockerclient.NetworkResource{}
|
||||||
networks := c.cluster.Networks().Filter(filters.Get("name"), filters.Get("id"))
|
networks := c.cluster.Networks().Filter(filters.Get("name"), filters.Get("id"), types)
|
||||||
for _, network := range networks {
|
for _, network := range networks {
|
||||||
tmp := (*network).NetworkResource
|
tmp := (*network).NetworkResource
|
||||||
if tmp.Scope == "local" {
|
if tmp.Scope == "local" {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@ type Network struct {
|
||||||
Engine *Engine
|
Engine *Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (network *Network) isPreDefined() bool {
|
||||||
|
return (network.Name == "none" || network.Name == "host" || network.Name == "bridge")
|
||||||
|
}
|
||||||
|
|
||||||
// Networks represents an array of networks
|
// Networks represents an array of networks
|
||||||
type Networks []*Network
|
type Networks []*Network
|
||||||
|
|
||||||
|
|
@ -37,17 +41,40 @@ func (networks Networks) Uniq() Networks {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter returns networks filtered by names or ids
|
// Filter returns networks filtered by names or ids
|
||||||
func (networks Networks) Filter(names []string, ids []string) Networks {
|
func (networks Networks) Filter(names []string, ids []string, types []string) Networks {
|
||||||
if len(names) == 0 && len(ids) == 0 {
|
typeFilter := func(network *Network) bool {
|
||||||
return networks.Uniq()
|
if len(types) > 0 {
|
||||||
|
for _, typ := range types {
|
||||||
|
if typ == "custom" && !network.isPreDefined() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if typ == "builtin" && network.isPreDefined() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
out := Networks{}
|
out := Networks{}
|
||||||
for _, idOrName := range append(names, ids...) {
|
if len(names) == 0 && len(ids) == 0 {
|
||||||
if network := networks.Get(idOrName); network != nil {
|
for _, network := range networks.Uniq() {
|
||||||
out = append(out, network)
|
if typeFilter(network) {
|
||||||
|
out = append(out, network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, idOrName := range append(names, ids...) {
|
||||||
|
if network := networks.Get(idOrName); network != nil {
|
||||||
|
if typeFilter(network) {
|
||||||
|
out = append(out, network)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ func TestNetworksFilter(t *testing.T) {
|
||||||
}, engine},
|
}, engine},
|
||||||
}
|
}
|
||||||
|
|
||||||
filtered := networks.Filter([]string{"network_name"}, []string{"abababab"})
|
filtered := networks.Filter([]string{"network_name"}, []string{"abababab"}, nil)
|
||||||
assert.Equal(t, len(filtered), 2)
|
assert.Equal(t, len(filtered), 2)
|
||||||
for _, network := range filtered {
|
for _, network := range filtered {
|
||||||
assert.True(t, network.ID == "aaaaaaaaaa1" || network.ID == "ababababab")
|
assert.True(t, network.ID == "aaaaaaaaaa1" || network.ID == "ababababab")
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,34 @@ function teardown() {
|
||||||
[ "${#lines[@]}" -eq 7 ]
|
[ "${#lines[@]}" -eq 7 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "docker network ls --filter type" {
|
||||||
|
# docker network ls --filter type is introduced in docker 1.10, skip older version without --filter type
|
||||||
|
run docker --version
|
||||||
|
if [[ "${output}" != "Docker version 1.1"* ]]; then
|
||||||
|
skip
|
||||||
|
fi
|
||||||
|
|
||||||
|
start_docker 2
|
||||||
|
swarm_manage
|
||||||
|
|
||||||
|
run docker_swarm network ls --filter type=builtin
|
||||||
|
[ "${#lines[@]}" -eq 7 ]
|
||||||
|
|
||||||
|
run docker_swarm network ls --filter type=custom
|
||||||
|
[ "${#lines[@]}" -eq 1 ]
|
||||||
|
|
||||||
|
run docker_swarm network ls --filter type=foo
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
|
||||||
|
docker_swarm network create -d bridge test
|
||||||
|
run docker_swarm network ls
|
||||||
|
[ "${#lines[@]}" -eq 8 ]
|
||||||
|
|
||||||
|
run docker_swarm network ls --filter type=custom
|
||||||
|
[ "${#lines[@]}" -eq 2 ]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@test "docker network inspect" {
|
@test "docker network inspect" {
|
||||||
start_docker_with_busybox 2
|
start_docker_with_busybox 2
|
||||||
swarm_manage
|
swarm_manage
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue