github.com/hms58/moby@v1.13.1/api/server/router/volume/volume_routes.go (about)

     1  package volume
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/docker/docker/api/server/httputils"
     8  	"github.com/docker/docker/api/types/filters"
     9  	volumetypes "github.com/docker/docker/api/types/volume"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  func (v *volumeRouter) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    14  	if err := httputils.ParseForm(r); err != nil {
    15  		return err
    16  	}
    17  
    18  	volumes, warnings, err := v.backend.Volumes(r.Form.Get("filters"))
    19  	if err != nil {
    20  		return err
    21  	}
    22  	return httputils.WriteJSON(w, http.StatusOK, &volumetypes.VolumesListOKBody{Volumes: volumes, Warnings: warnings})
    23  }
    24  
    25  func (v *volumeRouter) getVolumeByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    26  	if err := httputils.ParseForm(r); err != nil {
    27  		return err
    28  	}
    29  
    30  	volume, err := v.backend.VolumeInspect(vars["name"])
    31  	if err != nil {
    32  		return err
    33  	}
    34  	return httputils.WriteJSON(w, http.StatusOK, volume)
    35  }
    36  
    37  func (v *volumeRouter) postVolumesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    38  	if err := httputils.ParseForm(r); err != nil {
    39  		return err
    40  	}
    41  
    42  	if err := httputils.CheckForJSON(r); err != nil {
    43  		return err
    44  	}
    45  
    46  	var req volumetypes.VolumesCreateBody
    47  	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
    48  		return err
    49  	}
    50  
    51  	volume, err := v.backend.VolumeCreate(req.Name, req.Driver, req.DriverOpts, req.Labels)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	return httputils.WriteJSON(w, http.StatusCreated, volume)
    56  }
    57  
    58  func (v *volumeRouter) deleteVolumes(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    59  	if err := httputils.ParseForm(r); err != nil {
    60  		return err
    61  	}
    62  	force := httputils.BoolValue(r, "force")
    63  	if err := v.backend.VolumeRm(vars["name"], force); err != nil {
    64  		return err
    65  	}
    66  	w.WriteHeader(http.StatusNoContent)
    67  	return nil
    68  }
    69  
    70  func (v *volumeRouter) postVolumesPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    71  	if err := httputils.ParseForm(r); err != nil {
    72  		return err
    73  	}
    74  
    75  	pruneReport, err := v.backend.VolumesPrune(filters.Args{})
    76  	if err != nil {
    77  		return err
    78  	}
    79  	return httputils.WriteJSON(w, http.StatusOK, pruneReport)
    80  }