network_inspect.go 1.2 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. // NetworkInspect returns the information for a specific network configured in the docker host.
  11. func (cli *Client) NetworkInspect(ctx context.Context, networkID string) (types.NetworkResource, error) {
  12. networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID)
  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) (types.NetworkResource, []byte, error) {
  17. var networkResource types.NetworkResource
  18. resp, err := cli.get(ctx, "/networks/"+networkID, nil, nil)
  19. if err != nil {
  20. if resp.statusCode == http.StatusNotFound {
  21. return networkResource, nil, networkNotFoundError{networkID}
  22. }
  23. return networkResource, nil, err
  24. }
  25. defer ensureReaderClosed(resp)
  26. body, err := ioutil.ReadAll(resp.body)
  27. if err != nil {
  28. return networkResource, nil, err
  29. }
  30. rdr := bytes.NewReader(body)
  31. err = json.NewDecoder(rdr).Decode(&networkResource)
  32. return networkResource, body, err
  33. }