1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove runconfig dependency from network routes.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2016-03-28 13:41:06 -04:00
parent cf91a1be45
commit 3ca29823d4
5 changed files with 58 additions and 48 deletions

View file

@ -1,6 +1,8 @@
package network
import (
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/filters"
"github.com/docker/engine-api/types/network"
"github.com/docker/libnetwork"
)
@ -11,8 +13,8 @@ type Backend interface {
FindNetwork(idName string) (libnetwork.Network, error)
GetNetworkByName(idName string) (libnetwork.Network, error)
GetNetworksByID(partialID string) []libnetwork.Network
GetAllNetworks() []libnetwork.Network
CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string, labels map[string]string, internal bool, enableIPv6 bool) (libnetwork.Network, error)
FilterNetworks(netFilters filters.Args) ([]libnetwork.Network, error)
CreateNetwork(types.NetworkCreate) (*types.NetworkCreateResponse, error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
DisconnectContainerFromNetwork(containerName string, network libnetwork.Network, force bool) error
DeleteNetwork(name string) error

View file

@ -2,13 +2,11 @@ package network
import (
"encoding/json"
"fmt"
"net/http"
"golang.org/x/net/context"
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/runconfig"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/filters"
"github.com/docker/engine-api/types/network"
@ -26,21 +24,14 @@ func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWrit
return err
}
if netFilters.Len() != 0 {
if err := netFilters.Validate(acceptedFilters); err != nil {
return err
}
}
list := []*types.NetworkResource{}
nwList := n.backend.GetAllNetworks()
displayable, err := filterNetworks(nwList, netFilters)
nwList, err := n.backend.FilterNetworks(netFilters)
if err != nil {
return err
}
for _, nw := range displayable {
for _, nw := range nwList {
list = append(list, buildNetworkResource(nw))
}
@ -61,7 +52,6 @@ func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r
func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
var create types.NetworkCreate
var warning string
if err := httputils.ParseForm(r); err != nil {
return err
@ -75,31 +65,12 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
return err
}
if runconfig.IsPreDefinedNetwork(create.Name) {
return httputils.WriteJSON(w, http.StatusForbidden,
fmt.Sprintf("%s is a pre-defined network and cannot be created", create.Name))
}
nw, err := n.backend.GetNetworkByName(create.Name)
if _, ok := err.(libnetwork.ErrNoSuchNetwork); err != nil && !ok {
return err
}
if nw != nil {
if create.CheckDuplicate {
return libnetwork.NetworkNameError(create.Name)
}
warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
}
nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options, create.Labels, create.Internal, create.EnableIPv6)
nw, err := n.backend.CreateNetwork(create)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusCreated, &types.NetworkCreateResponse{
ID: nw.ID(),
Warning: warning,
})
return httputils.WriteJSON(w, http.StatusCreated, nw)
}
func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View file

@ -6,8 +6,11 @@ import (
"net/http"
"strings"
netsettings "github.com/docker/docker/daemon/network"
"github.com/docker/docker/errors"
"github.com/docker/docker/runconfig"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/filters"
"github.com/docker/engine-api/types/network"
"github.com/docker/libnetwork"
)
@ -77,8 +80,8 @@ func (daemon *Daemon) GetNetworksByID(partialID string) []libnetwork.Network {
return list
}
// GetAllNetworks returns a list containing all networks
func (daemon *Daemon) GetAllNetworks() []libnetwork.Network {
// getAllNetworks returns a list containing all networks
func (daemon *Daemon) getAllNetworks() []libnetwork.Network {
c := daemon.netController
list := []libnetwork.Network{}
l := func(nw libnetwork.Network) bool {
@ -91,12 +94,33 @@ func (daemon *Daemon) GetAllNetworks() []libnetwork.Network {
}
// CreateNetwork creates a network with the given name, driver and other optional parameters
func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, netOption map[string]string, labels map[string]string, internal bool, enableIPv6 bool) (libnetwork.Network, error) {
func (daemon *Daemon) CreateNetwork(create types.NetworkCreate) (*types.NetworkCreateResponse, error) {
if runconfig.IsPreDefinedNetwork(create.Name) {
err := fmt.Errorf("%s is a pre-defined network and cannot be created", create.Name)
return nil, errors.NewErrorWithStatusCode(err, http.StatusForbidden)
}
var warning string
nw, err := daemon.GetNetworkByName(create.Name)
if err != nil {
if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
return nil, err
}
}
if nw != nil {
if create.CheckDuplicate {
return nil, libnetwork.NetworkNameError(create.Name)
}
warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
}
c := daemon.netController
driver := create.Driver
if driver == "" {
driver = c.Config().Daemon.DefaultDriver
}
ipam := create.IPAM
v4Conf, v6Conf, err := getIpamConfig(ipam.Config)
if err != nil {
return nil, err
@ -104,20 +128,23 @@ func (daemon *Daemon) CreateNetwork(name, driver string, ipam network.IPAM, netO
nwOptions := []libnetwork.NetworkOption{
libnetwork.NetworkOptionIpam(ipam.Driver, "", v4Conf, v6Conf, ipam.Options),
libnetwork.NetworkOptionEnableIPv6(enableIPv6),
libnetwork.NetworkOptionDriverOpts(netOption),
libnetwork.NetworkOptionLabels(labels),
libnetwork.NetworkOptionEnableIPv6(create.EnableIPv6),
libnetwork.NetworkOptionDriverOpts(create.Options),
libnetwork.NetworkOptionLabels(create.Labels),
}
if internal {
if create.Internal {
nwOptions = append(nwOptions, libnetwork.NetworkOptionInternalNetwork())
}
n, err := c.NewNetwork(driver, name, nwOptions...)
n, err := c.NewNetwork(driver, create.Name, nwOptions...)
if err != nil {
return nil, err
}
daemon.LogNetworkEvent(n, "create")
return n, nil
return &types.NetworkCreateResponse{
ID: n.ID(),
Warning: warning,
}, nil
}
func getIpamConfig(data []network.IPAMConfig) ([]*libnetwork.IpamConf, []*libnetwork.IpamConf, error) {
@ -203,3 +230,13 @@ func (daemon *Daemon) DeleteNetwork(networkID string) error {
daemon.LogNetworkEvent(nw, "destroy")
return nil
}
func (daemon *Daemon) FilterNetworks(netFilters filters.Args) ([]libnetwork.Network, error) {
if netFilters.Len() != 0 {
if err := netFilters.Validate(netsettings.AcceptedFilters); err != nil {
return nil, err
}
}
nwList := daemon.getAllNetworks()
return netsettings.FilterNetworks(nwList, netFilters)
}

View file

@ -27,7 +27,7 @@ var (
// "name": true,
// "id": true,
// }
acceptedFilters = func() map[string]bool {
AcceptedFilters = func() map[string]bool {
ret := make(map[string]bool)
for k := range supportedFilters {
ret[k] = true
@ -84,9 +84,9 @@ func filterNetworkByID(nws []libnetwork.Network, id string) (retNws []libnetwork
return retNws, nil
}
// filterAllNetworks filters network list according to user specified filter
// FilterAllNetworks filters network list according to user specified filter
// and returns user chosen networks
func filterNetworks(nws []libnetwork.Network, filter filters.Args) ([]libnetwork.Network, error) {
func FilterNetworks(nws []libnetwork.Network, filter filters.Args) ([]libnetwork.Network, error) {
// if filter is empty, return original network list
if filter.Len() == 0 {
return nws, nil

View file

@ -296,8 +296,8 @@ func createNetwork(c *check.C, config types.NetworkCreate, shouldSucceed bool) s
return ""
}
c.Assert(status, checker.Equals, http.StatusCreated)
c.Assert(err, checker.IsNil)
c.Assert(status, checker.Equals, http.StatusCreated)
var nr types.NetworkCreateResponse
err = json.Unmarshal(resp, &nr)