github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/server/router/network/network.go (about) 1 package network 2 3 import ( 4 "net/http" 5 6 "github.com/docker/docker/api/server/httputils" 7 "github.com/docker/docker/api/server/router" 8 "github.com/docker/docker/api/server/router/local" 9 "github.com/docker/docker/errors" 10 "golang.org/x/net/context" 11 ) 12 13 // networkRouter is a router to talk with the network controller 14 type networkRouter struct { 15 backend Backend 16 routes []router.Route 17 } 18 19 // NewRouter initializes a new network router 20 func NewRouter(b Backend) router.Router { 21 r := &networkRouter{ 22 backend: b, 23 } 24 r.initRoutes() 25 return r 26 } 27 28 // Routes returns the available routes to the network controller 29 func (r *networkRouter) Routes() []router.Route { 30 return r.routes 31 } 32 33 func (r *networkRouter) initRoutes() { 34 r.routes = []router.Route{ 35 // GET 36 local.NewGetRoute("/networks", r.controllerEnabledMiddleware(r.getNetworksList)), 37 local.NewGetRoute("/networks/{id:.*}", r.controllerEnabledMiddleware(r.getNetwork)), 38 // POST 39 local.NewPostRoute("/networks/create", r.controllerEnabledMiddleware(r.postNetworkCreate)), 40 local.NewPostRoute("/networks/{id:.*}/connect", r.controllerEnabledMiddleware(r.postNetworkConnect)), 41 local.NewPostRoute("/networks/{id:.*}/disconnect", r.controllerEnabledMiddleware(r.postNetworkDisconnect)), 42 // DELETE 43 local.NewDeleteRoute("/networks/{id:.*}", r.controllerEnabledMiddleware(r.deleteNetwork)), 44 } 45 } 46 47 func (r *networkRouter) controllerEnabledMiddleware(handler httputils.APIFunc) httputils.APIFunc { 48 if r.backend.NetworkControllerEnabled() { 49 return handler 50 } 51 return networkControllerDisabled 52 } 53 54 func networkControllerDisabled(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { 55 return errors.ErrorNetworkControllerNotEnabled.WithArgs() 56 }