network_inspect.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package client
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/url"
  7. "github.com/docker/docker/api/types"
  8. "golang.org/x/net/context"
  9. )
  10. // NetworkInspect returns the information for a specific network configured in the docker host.
  11. func (cli *Client) NetworkInspect(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error) {
  12. networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID, options)
  13. return networkResource, err
  14. }
  15. // NetworkInspectWithRaw returns the information for a specific network configured in the docker host and its raw representation.
  16. func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
  17. var (
  18. networkResource types.NetworkResource
  19. resp serverResponse
  20. err error
  21. )
  22. query := url.Values{}
  23. if options.Verbose {
  24. query.Set("verbose", "true")
  25. }
  26. if options.Scope != "" {
  27. query.Set("scope", options.Scope)
  28. }
  29. resp, err = cli.get(ctx, "/networks/"+networkID, query, nil)
  30. if err != nil {
  31. return networkResource, nil, wrapResponseError(err, resp, "network", networkID)
  32. }
  33. defer ensureReaderClosed(resp)
  34. body, err := ioutil.ReadAll(resp.body)
  35. if err != nil {
  36. return networkResource, nil, err
  37. }
  38. rdr := bytes.NewReader(body)
  39. err = json.NewDecoder(rdr).Decode(&networkResource)
  40. return networkResource, body, err
  41. }