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 (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
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"
|
2022-03-05 04:51:34 -05:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2018-03-22 17:11:03 -04:00
|
|
|
"github.com/docker/docker/volume/service/opts"
|
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:11:03 -04:00
|
|
|
filters, err := filters.FromJSON(r.Form.Get("filters"))
|
|
|
|
if err != nil {
|
2022-03-02 07:52:51 -05:00
|
|
|
return errors.Wrap(err, "error reading volume filters")
|
2018-03-22 17:11:03 -04:00
|
|
|
}
|
|
|
|
volumes, warnings, err := v.backend.List(ctx, filters)
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-05 15:04:55 -05:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, &volume.ListResponse{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
|
|
|
|
}
|
|
|
|
|
2022-03-05 04:51:34 -05:00
|
|
|
vol, err := v.backend.Get(ctx, vars["name"], opts.WithGetResolveStatus)
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-05 04:51:34 -05:00
|
|
|
return httputils.WriteJSON(w, http.StatusOK, vol)
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-05 17:25:55 -05:00
|
|
|
var req volume.CreateOptions
|
2022-04-05 05:43:06 -04:00
|
|
|
if err := httputils.ReadJSON(r, &req); err != nil {
|
|
|
|
return err
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2022-03-05 04:51:34 -05:00
|
|
|
vol, err := v.backend.Create(ctx, req.Name, req.Driver, opts.WithCreateOptions(req.DriverOpts), opts.WithCreateLabels(req.Labels))
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-05 04:51:34 -05:00
|
|
|
return httputils.WriteJSON(w, http.StatusCreated, vol)
|
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")
|
2018-03-22 17:11:03 -04:00
|
|
|
if err := v.backend.Remove(ctx, vars["name"], opts.WithPurgeOnError(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
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:11:03 -04:00
|
|
|
pruneReport, err := v.backend.Prune(ctx, pruneFilters)
|
2016-08-23 19:25:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return httputils.WriteJSON(w, http.StatusOK, pruneReport)
|
|
|
|
}
|