github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/client/network_inspect.go (about)

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