mirror of https://github.com/docker/docs.git
Merge pull request #1320 from dnephin/support_filter_networks
Support filtering networks by id or name
This commit is contained in:
commit
975eaa9e73
|
@ -171,8 +171,20 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// GET /networks
|
// GET /networks
|
||||||
func getNetworks(c *context, w http.ResponseWriter, r *http.Request) {
|
func getNetworks(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
httpError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filters, err := dockerfilters.FromParam(r.Form.Get("filters"))
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
out := []*dockerclient.NetworkResource{}
|
out := []*dockerclient.NetworkResource{}
|
||||||
for _, network := range c.cluster.Networks().Uniq() {
|
networks := c.cluster.Networks().Filter(filters["name"], filters["id"])
|
||||||
|
for _, network := range networks {
|
||||||
tmp := (*network).NetworkResource
|
tmp := (*network).NetworkResource
|
||||||
if tmp.Scope == "local" {
|
if tmp.Scope == "local" {
|
||||||
tmp.Name = network.Engine.Name + "/" + network.Name
|
tmp.Name = network.Engine.Name + "/" + network.Name
|
||||||
|
|
|
@ -30,6 +30,21 @@ func (networks Networks) Uniq() Networks {
|
||||||
return uniq
|
return uniq
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filter returns networks filtered by names or ids
|
||||||
|
func (networks Networks) Filter(names []string, ids []string) Networks {
|
||||||
|
if len(names) == 0 && len(ids) == 0 {
|
||||||
|
return networks.Uniq()
|
||||||
|
}
|
||||||
|
|
||||||
|
out := Networks{}
|
||||||
|
for _, idOrName := range append(names, ids...) {
|
||||||
|
if network := networks.Get(idOrName); network != nil {
|
||||||
|
out = append(out, network)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// Get returns a network using it's ID or Name
|
// Get returns a network using it's ID or Name
|
||||||
func (networks Networks) Get(IDOrName string) *Network {
|
func (networks Networks) Get(IDOrName string) *Network {
|
||||||
// Abort immediately if the name is empty.
|
// Abort immediately if the name is empty.
|
||||||
|
@ -78,5 +93,4 @@ func (networks Networks) Get(IDOrName string) *Network {
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package cluster
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/samalba/dockerclient"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNetworksFilter(t *testing.T) {
|
||||||
|
engine := &Engine{ID: "id"}
|
||||||
|
networks := Networks{
|
||||||
|
{dockerclient.NetworkResource{
|
||||||
|
ID: "ababababab",
|
||||||
|
Name: "something",
|
||||||
|
}, engine},
|
||||||
|
{dockerclient.NetworkResource{
|
||||||
|
ID: "aaaaaaaaaa1",
|
||||||
|
Name: "network_name",
|
||||||
|
}, engine},
|
||||||
|
{dockerclient.NetworkResource{
|
||||||
|
ID: "bbbbbbbbbb",
|
||||||
|
Name: "somethingelse",
|
||||||
|
}, engine},
|
||||||
|
{dockerclient.NetworkResource{
|
||||||
|
ID: "aaaaaaaaa2",
|
||||||
|
Name: "foo",
|
||||||
|
}, engine},
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered := networks.Filter([]string{"network_name"}, []string{"abababab"})
|
||||||
|
assert.Equal(t, len(filtered), 2)
|
||||||
|
for _, network := range filtered {
|
||||||
|
assert.True(t, network.ID == "aaaaaaaaaa1" || network.ID == "ababababab")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue