2015-09-23 19:42:08 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2015-10-16 18:45:54 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/router"
|
2015-09-25 06:19:17 -04:00
|
|
|
"github.com/docker/docker/api/server/router/local"
|
2015-10-16 18:45:54 -04:00
|
|
|
"github.com/docker/docker/errors"
|
|
|
|
"golang.org/x/net/context"
|
2015-09-23 19:42:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// networkRouter is a router to talk with the network controller
|
|
|
|
type networkRouter struct {
|
2015-10-26 17:47:48 -04:00
|
|
|
backend Backend
|
|
|
|
routes []router.Route
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
// NewRouter initializes a new network router
|
2015-10-26 17:47:48 -04:00
|
|
|
func NewRouter(b Backend) router.Router {
|
2015-09-25 06:19:17 -04:00
|
|
|
r := &networkRouter{
|
2015-10-26 17:47:48 -04:00
|
|
|
backend: b,
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
|
|
|
r.initRoutes()
|
|
|
|
return r
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
// Routes returns the available routes to the network controller
|
|
|
|
func (r *networkRouter) Routes() []router.Route {
|
|
|
|
return r.routes
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|
|
|
|
|
2015-09-25 06:19:17 -04:00
|
|
|
func (r *networkRouter) initRoutes() {
|
|
|
|
r.routes = []router.Route{
|
|
|
|
// GET
|
2015-10-16 18:45:54 -04:00
|
|
|
local.NewGetRoute("/networks", r.controllerEnabledMiddleware(r.getNetworksList)),
|
|
|
|
local.NewGetRoute("/networks/{id:.*}", r.controllerEnabledMiddleware(r.getNetwork)),
|
2015-09-25 06:19:17 -04:00
|
|
|
// POST
|
2015-10-16 18:45:54 -04:00
|
|
|
local.NewPostRoute("/networks/create", r.controllerEnabledMiddleware(r.postNetworkCreate)),
|
|
|
|
local.NewPostRoute("/networks/{id:.*}/connect", r.controllerEnabledMiddleware(r.postNetworkConnect)),
|
|
|
|
local.NewPostRoute("/networks/{id:.*}/disconnect", r.controllerEnabledMiddleware(r.postNetworkDisconnect)),
|
2015-09-25 06:19:17 -04:00
|
|
|
// DELETE
|
2015-10-16 18:45:54 -04:00
|
|
|
local.NewDeleteRoute("/networks/{id:.*}", r.controllerEnabledMiddleware(r.deleteNetwork)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *networkRouter) controllerEnabledMiddleware(handler httputils.APIFunc) httputils.APIFunc {
|
2015-10-26 17:47:48 -04:00
|
|
|
if r.backend.NetworkControllerEnabled() {
|
2015-10-16 18:45:54 -04:00
|
|
|
return handler
|
2015-09-25 06:19:17 -04:00
|
|
|
}
|
2015-10-16 18:45:54 -04:00
|
|
|
return networkControllerDisabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkControllerDisabled(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
return errors.ErrorNetworkControllerNotEnabled.WithArgs()
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|