1 // Copyright 2015 go-dockerclient authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // ErrNoSuchVolume is the error returned when the volume does not exist.
15 ErrNoSuchVolume = errors.New("no such volume")
17 // ErrVolumeInUse is the error returned when the volume requested to be removed is still in use.
18 ErrVolumeInUse = errors.New("volume in use and cannot be removed")
21 // Volume represents a volume.
23 // See https://goo.gl/FZA4BK for more details.
25 Name string `json:"Name" yaml:"Name"`
26 Driver string `json:"Driver,omitempty" yaml:"Driver,omitempty"`
27 Mountpoint string `json:"Mountpoint,omitempty" yaml:"Mountpoint,omitempty"`
28 Labels map[string]string `json:"Labels,omitempty" yaml:"Labels,omitempty"`
31 // ListVolumesOptions specify parameters to the ListVolumes function.
33 // See https://goo.gl/FZA4BK for more details.
34 type ListVolumesOptions struct {
35 Filters map[string][]string
38 // ListVolumes returns a list of available volumes in the server.
40 // See https://goo.gl/FZA4BK for more details.
41 func (c *Client) ListVolumes(opts ListVolumesOptions) ([]Volume, error) {
42 resp, err := c.do("GET", "/volumes?"+queryString(opts), doOptions{})
46 defer resp.Body.Close()
47 m := make(map[string]interface{})
48 if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
52 volumesJSON, ok := m["Volumes"]
56 data, err := json.Marshal(volumesJSON)
60 if err := json.Unmarshal(data, &volumes); err != nil {
66 // CreateVolumeOptions specify parameters to the CreateVolume function.
68 // See https://goo.gl/pBUbZ9 for more details.
69 type CreateVolumeOptions struct {
72 DriverOpts map[string]string
75 // CreateVolume creates a volume on the server.
77 // See https://goo.gl/pBUbZ9 for more details.
78 func (c *Client) CreateVolume(opts CreateVolumeOptions) (*Volume, error) {
79 resp, err := c.do("POST", "/volumes/create", doOptions{data: opts})
83 defer resp.Body.Close()
85 if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
91 // InspectVolume returns a volume by its name.
93 // See https://goo.gl/0g9A6i for more details.
94 func (c *Client) InspectVolume(name string) (*Volume, error) {
95 resp, err := c.do("GET", "/volumes/"+name, doOptions{})
97 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
98 return nil, ErrNoSuchVolume
102 defer resp.Body.Close()
104 if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
110 // RemoveVolume removes a volume by its name.
112 // See https://goo.gl/79GNQz for more details.
113 func (c *Client) RemoveVolume(name string) error {
114 resp, err := c.do("DELETE", "/volumes/"+name, doOptions{})
116 if e, ok := err.(*Error); ok {
117 if e.Status == http.StatusNotFound {
118 return ErrNoSuchVolume
120 if e.Status == http.StatusConflict {
121 return ErrVolumeInUse
126 defer resp.Body.Close()