extract docker volume API from local

Signed-off-by: Dong Chen <dongluo.chen@docker.com>
This commit is contained in:
Dong Chen 2015-10-27 12:06:35 -07:00
parent 6276c676a8
commit 9862a4b43e
4 changed files with 51 additions and 14 deletions

View file

@ -116,8 +116,6 @@ func (r *router) initRoutes() {
NewGetRoute("/containers/{name:.*}/attach/ws", r.wsContainersAttach),
NewGetRoute("/exec/{id:.*}/json", r.getExecByID),
NewGetRoute("/containers/{name:.*}/archive", r.getContainersArchive),
NewGetRoute("/volumes", r.getVolumesList),
NewGetRoute("/volumes/{name:.*}", r.getVolumeByName),
// POST
NewPostRoute("/auth", r.postAuth),
NewPostRoute("/commit", r.postCommit),
@ -141,13 +139,11 @@ func (r *router) initRoutes() {
NewPostRoute("/exec/{name:.*}/start", r.postContainerExecStart),
NewPostRoute("/exec/{name:.*}/resize", r.postContainerExecResize),
NewPostRoute("/containers/{name:.*}/rename", r.postContainerRename),
NewPostRoute("/volumes/create", r.postVolumesCreate),
// PUT
NewPutRoute("/containers/{name:.*}/archive", r.putContainersArchive),
// DELETE
NewDeleteRoute("/containers/{name:.*}", r.deleteContainers),
NewDeleteRoute("/images/{name:.*}", r.deleteImages),
NewDeleteRoute("/volumes/{name:.*}", r.deleteVolumes),
}
}

View file

@ -0,0 +1,39 @@
package volume
import (
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/local"
"github.com/docker/docker/daemon"
)
// volumesRouter is a router to talk with the volumes controller
type volumeRouter struct {
daemon *daemon.Daemon
routes []router.Route
}
// NewRouter initializes a new volumes router
func NewRouter(d *daemon.Daemon) router.Router {
r := &volumeRouter{
daemon: d,
}
r.initRoutes()
return r
}
//Routes returns the available routers to the volumes controller
func (r *volumeRouter) Routes() []router.Route {
return r.routes
}
func (r *volumeRouter) initRoutes() {
r.routes = []router.Route{
// GET
local.NewGetRoute("/volumes", r.getVolumesList),
local.NewGetRoute("/volumes/{name:.*}", r.getVolumeByName),
// POST
local.NewPostRoute("/volumes/create", r.postVolumesCreate),
// DELETE
local.NewDeleteRoute("/volumes/{name:.*}", r.deleteVolumes),
}
}

View file

@ -1,4 +1,4 @@
package local
package volume
import (
"encoding/json"
@ -9,31 +9,31 @@ import (
"golang.org/x/net/context"
)
func (s *router) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
func (v *volumeRouter) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
volumes, err := s.daemon.Volumes(r.Form.Get("filters"))
volumes, err := v.daemon.Volumes(r.Form.Get("filters"))
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, &types.VolumesListResponse{Volumes: volumes})
}
func (s *router) getVolumeByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
func (v *volumeRouter) getVolumeByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
v, err := s.daemon.VolumeInspect(vars["name"])
volume, err := v.daemon.VolumeInspect(vars["name"])
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusOK, v)
return httputils.WriteJSON(w, http.StatusOK, volume)
}
func (s *router) postVolumesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
func (v *volumeRouter) postVolumesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
@ -47,18 +47,18 @@ func (s *router) postVolumesCreate(ctx context.Context, w http.ResponseWriter, r
return err
}
volume, err := s.daemon.VolumeCreate(req.Name, req.Driver, req.DriverOpts)
volume, err := v.daemon.VolumeCreate(req.Name, req.Driver, req.DriverOpts)
if err != nil {
return err
}
return httputils.WriteJSON(w, http.StatusCreated, volume)
}
func (s *router) deleteVolumes(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
func (v *volumeRouter) deleteVolumes(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
if err := s.daemon.VolumeRm(vars["name"]); err != nil {
if err := v.daemon.VolumeRm(vars["name"]); err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)

View file

@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api/server/router"
"github.com/docker/docker/api/server/router/local"
"github.com/docker/docker/api/server/router/network"
"github.com/docker/docker/api/server/router/volume"
"github.com/docker/docker/daemon"
"github.com/docker/docker/pkg/sockets"
"github.com/docker/docker/utils"
@ -170,6 +171,7 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
func (s *Server) InitRouters(d *daemon.Daemon) {
s.addRouter(local.NewRouter(d))
s.addRouter(network.NewRouter(d))
s.addRouter(volume.NewRouter(d))
for _, srv := range s.servers {
srv.srv.Handler = s.CreateMux()