volume_inspect.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "path"
  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. // The empty ID needs to be handled here because with an empty ID the
  18. // request url will not contain a trailing / which calls the volume list API
  19. // instead of volume inspect
  20. if volumeID == "" {
  21. return types.Volume{}, nil, objectNotFoundError{object: "volume", id: volumeID}
  22. }
  23. var volume types.Volume
  24. resp, err := cli.get(ctx, path.Join("/volumes", volumeID), nil, nil)
  25. if err != nil {
  26. return volume, nil, wrapResponseError(err, resp, "volume", volumeID)
  27. }
  28. defer ensureReaderClosed(resp)
  29. body, err := ioutil.ReadAll(resp.body)
  30. if err != nil {
  31. return volume, nil, err
  32. }
  33. rdr := bytes.NewReader(body)
  34. err = json.NewDecoder(rdr).Decode(&volume)
  35. return volume, body, err
  36. }