2016-06-14 02:52:49 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2016-09-06 18:46:37 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-06-14 02:52:49 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2017-01-07 06:18:57 +00:00
|
|
|
func filterNetworkByType(nws []types.NetworkResource, netType string) ([]types.NetworkResource, error) {
|
|
|
|
retNws := []types.NetworkResource{}
|
2016-06-14 02:52:49 +00:00
|
|
|
switch netType {
|
|
|
|
case "builtin":
|
|
|
|
for _, nw := range nws {
|
|
|
|
if runconfig.IsPreDefinedNetwork(nw.Name) {
|
|
|
|
retNws = append(retNws, nw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "custom":
|
|
|
|
for _, nw := range nws {
|
|
|
|
if !runconfig.IsPreDefinedNetwork(nw.Name) {
|
|
|
|
retNws = append(retNws, nw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2017-07-19 14:20:13 +00:00
|
|
|
return nil, invalidFilter(netType)
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
return retNws, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
type invalidFilter string
|
|
|
|
|
|
|
|
func (e invalidFilter) Error() string {
|
|
|
|
return "Invalid filter: 'type'='" + string(e) + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidFilter) InvalidParameter() {}
|
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
// filterNetworks filters network list according to user specified filter
|
|
|
|
// and returns user chosen networks
|
|
|
|
func filterNetworks(nws []types.NetworkResource, filter filters.Args) ([]types.NetworkResource, error) {
|
|
|
|
// if filter is empty, return original network list
|
|
|
|
if filter.Len() == 0 {
|
|
|
|
return nws, nil
|
|
|
|
}
|
|
|
|
|
2016-06-28 22:06:44 +00:00
|
|
|
displayNet := []types.NetworkResource{}
|
2016-06-14 02:52:49 +00:00
|
|
|
for _, nw := range nws {
|
|
|
|
if filter.Include("driver") {
|
|
|
|
if !filter.ExactMatch("driver", nw.Driver) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Include("name") {
|
|
|
|
if !filter.Match("name", nw.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Include("id") {
|
|
|
|
if !filter.Match("id", nw.ID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Include("label") {
|
|
|
|
if !filter.MatchKVList("label", nw.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 18:45:48 +00:00
|
|
|
if filter.Include("scope") {
|
|
|
|
if !filter.ExactMatch("scope", nw.Scope) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 02:52:49 +00:00
|
|
|
displayNet = append(displayNet, nw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.Include("type") {
|
2017-01-07 06:18:57 +00:00
|
|
|
typeNet := []types.NetworkResource{}
|
2016-06-14 02:52:49 +00:00
|
|
|
errFilter := filter.WalkValues("type", func(fval string) error {
|
|
|
|
passList, err := filterNetworkByType(displayNet, fval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
typeNet = append(typeNet, passList...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if errFilter != nil {
|
|
|
|
return nil, errFilter
|
|
|
|
}
|
|
|
|
displayNet = typeNet
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayNet, nil
|
|
|
|
}
|