2018-02-05 16:05:59 -05:00
|
|
|
package volume // import "github.com/docker/docker/api/server/router/volume"
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-01-09 10:01:43 -05:00
|
|
|
"errors"
|
|
|
|
"io"
|
2015-06-12 09:25:32 -04:00
|
|
|
"net/http"
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
"github.com/docker/docker/api/server/httputils"
|
2016-11-17 00:46:37 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-10-14 16:20:13 -04:00
|
|
|
volumetypes "github.com/docker/docker/api/types/volume"
|
2018-01-09 10:01:43 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-09-29 17:32:07 -04:00
|
|
|
"golang.org/x/net/context"
|
2015-06-12 09:25:32 -04:00
|
|
|
)
|
|
|
|
|
2015-10-27 15:06:35 -04:00
|
|
|
func (v *volumeRouter) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
volumes, warnings, err := v.backend.Volumes(r.Form.Get("filters"))
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-04 11:40:17 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, &volumetypes.VolumesListOKBody{Volumes: volumes, Warnings: warnings})
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2015-10-27 15:06:35 -04:00
|
|
|
func (v *volumeRouter) getVolumeByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:26:45 -05:00
|
|
|
volume, err := v.backend.VolumeInspect(vars["name"])
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-27 15:06:35 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, volume)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2015-10-27 15:06:35 -04:00
|
|
|
func (v *volumeRouter) postVolumesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.CheckForJSON(r); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-06 12:57:17 -04:00
|
|
|
var req volumetypes.VolumesCreateBody
|
2015-06-12 09:25:32 -04:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2018-01-09 10:01:43 -05:00
|
|
|
if err == io.EOF {
|
|
|
|
return errdefs.InvalidParameter(errors.New("got EOF while reading request body"))
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
volume, err := v.backend.VolumeCreate(req.Name, req.Driver, req.DriverOpts, req.Labels)
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, volume)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2015-10-27 15:06:35 -04:00
|
|
|
func (v *volumeRouter) deleteVolumes(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
2015-09-23 19:42:08 -04:00
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-06-10 10:40:09 -04:00
|
|
|
force := httputils.BoolValue(r, "force")
|
|
|
|
if err := v.backend.VolumeRm(vars["name"], force); err != nil {
|
2015-06-12 09:25:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
|
|
|
|
func (v *volumeRouter) postVolumesPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
|
|
|
|
if err := httputils.ParseForm(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-26 07:59:45 -04:00
|
|
|
pruneFilters, err := filters.FromJSON(r.Form.Get("filters"))
|
2017-02-04 12:10:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:52:33 -04:00
|
|
|
pruneReport, err := v.backend.VolumesPrune(ctx, pruneFilters)
|
2016-08-23 19:25:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, pruneReport)
|
|
|
|
}
|