volume.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.
  4. package docker
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "net/http"
  9. )
  10. var (
  11. // ErrNoSuchVolume is the error returned when the volume does not exist.
  12. ErrNoSuchVolume = errors.New("no such volume")
  13. // ErrVolumeInUse is the error returned when the volume requested to be removed is still in use.
  14. ErrVolumeInUse = errors.New("volume in use and cannot be removed")
  15. )
  16. // Volume represents a volume.
  17. //
  18. // See https://goo.gl/FZA4BK for more details.
  19. type Volume struct {
  20. Name string `json:"Name" yaml:"Name"`
  21. Driver string `json:"Driver,omitempty" yaml:"Driver,omitempty"`
  22. Mountpoint string `json:"Mountpoint,omitempty" yaml:"Mountpoint,omitempty"`
  23. Labels map[string]string `json:"Labels,omitempty" yaml:"Labels,omitempty"`
  24. }
  25. // ListVolumesOptions specify parameters to the ListVolumes function.
  26. //
  27. // See https://goo.gl/FZA4BK for more details.
  28. type ListVolumesOptions struct {
  29. Filters map[string][]string
  30. }
  31. // ListVolumes returns a list of available volumes in the server.
  32. //
  33. // See https://goo.gl/FZA4BK for more details.
  34. func (c *Client) ListVolumes(opts ListVolumesOptions) ([]Volume, error) {
  35. resp, err := c.do("GET", "/volumes?"+queryString(opts), doOptions{})
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer resp.Body.Close()
  40. m := make(map[string]interface{})
  41. if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
  42. return nil, err
  43. }
  44. var volumes []Volume
  45. volumesJSON, ok := m["Volumes"]
  46. if !ok {
  47. return volumes, nil
  48. }
  49. data, err := json.Marshal(volumesJSON)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if err := json.Unmarshal(data, &volumes); err != nil {
  54. return nil, err
  55. }
  56. return volumes, nil
  57. }
  58. // CreateVolumeOptions specify parameters to the CreateVolume function.
  59. //
  60. // See https://goo.gl/pBUbZ9 for more details.
  61. type CreateVolumeOptions struct {
  62. Name string
  63. Driver string
  64. DriverOpts map[string]string
  65. }
  66. // CreateVolume creates a volume on the server.
  67. //
  68. // See https://goo.gl/pBUbZ9 for more details.
  69. func (c *Client) CreateVolume(opts CreateVolumeOptions) (*Volume, error) {
  70. resp, err := c.do("POST", "/volumes/create", doOptions{data: opts})
  71. if err != nil {
  72. return nil, err
  73. }
  74. defer resp.Body.Close()
  75. var volume Volume
  76. if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
  77. return nil, err
  78. }
  79. return &volume, nil
  80. }
  81. // InspectVolume returns a volume by its name.
  82. //
  83. // See https://goo.gl/0g9A6i for more details.
  84. func (c *Client) InspectVolume(name string) (*Volume, error) {
  85. resp, err := c.do("GET", "/volumes/"+name, doOptions{})
  86. if err != nil {
  87. if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
  88. return nil, ErrNoSuchVolume
  89. }
  90. return nil, err
  91. }
  92. defer resp.Body.Close()
  93. var volume Volume
  94. if err := json.NewDecoder(resp.Body).Decode(&volume); err != nil {
  95. return nil, err
  96. }
  97. return &volume, nil
  98. }
  99. // RemoveVolume removes a volume by its name.
  100. //
  101. // See https://goo.gl/79GNQz for more details.
  102. func (c *Client) RemoveVolume(name string) error {
  103. resp, err := c.do("DELETE", "/volumes/"+name, doOptions{})
  104. if err != nil {
  105. if e, ok := err.(*Error); ok {
  106. if e.Status == http.StatusNotFound {
  107. return ErrNoSuchVolume
  108. }
  109. if e.Status == http.StatusConflict {
  110. return ErrVolumeInUse
  111. }
  112. }
  113. return nil
  114. }
  115. defer resp.Body.Close()
  116. return nil
  117. }