volume_inspect.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "github.com/docker/docker/api/types"
  8. "golang.org/x/net/context"
  9. )
  10. // VolumeInspect returns the information about a specific volume in the docker host.
  11. func (cli *Client) VolumeInspect(ctx context.Context, volumeID string) (types.Volume, error) {
  12. volume, _, err := cli.VolumeInspectWithRaw(ctx, volumeID)
  13. return volume, err
  14. }
  15. // VolumeInspectWithRaw returns the information about a specific volume in the docker host and its raw representation
  16. func (cli *Client) VolumeInspectWithRaw(ctx context.Context, volumeID string) (types.Volume, []byte, error) {
  17. var volume types.Volume
  18. resp, err := cli.get(ctx, "/volumes/"+volumeID, nil, nil)
  19. if err != nil {
  20. if resp.statusCode == http.StatusNotFound {
  21. return volume, nil, volumeNotFoundError{volumeID}
  22. }
  23. return volume, nil, err
  24. }
  25. defer ensureReaderClosed(resp)
  26. body, err := ioutil.ReadAll(resp.body)
  27. if err != nil {
  28. return volume, nil, err
  29. }
  30. rdr := bytes.NewReader(body)
  31. err = json.NewDecoder(rdr).Decode(&volume)
  32. return volume, body, err
  33. }