network_inspect.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "io"
  7. "net/url"
  8. "github.com/docker/docker/api/types"
  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. if networkID == "" {
  18. return types.NetworkResource{}, nil, objectNotFoundError{object: "network", id: networkID}
  19. }
  20. var (
  21. networkResource types.NetworkResource
  22. resp serverResponse
  23. err error
  24. )
  25. query := url.Values{}
  26. if options.Verbose {
  27. query.Set("verbose", "true")
  28. }
  29. if options.Scope != "" {
  30. query.Set("scope", options.Scope)
  31. }
  32. resp, err = cli.get(ctx, "/networks/"+networkID, query, nil)
  33. defer ensureReaderClosed(resp)
  34. if err != nil {
  35. return networkResource, nil, err
  36. }
  37. body, err := io.ReadAll(resp.body)
  38. if err != nil {
  39. return networkResource, nil, err
  40. }
  41. rdr := bytes.NewReader(body)
  42. err = json.NewDecoder(rdr).Decode(&networkResource)
  43. return networkResource, body, err
  44. }