mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Merge pull request #51245 from thaJeztah/network_opts
client: NetworkInspect, NetworkList: wrap output structs, and remove NetworkInspectWithRaw
This commit is contained in:
@@ -130,9 +130,8 @@ type NetworkAPIClient interface {
|
||||
NetworkConnect(ctx context.Context, network, container string, config *network.EndpointSettings) error
|
||||
NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (network.CreateResponse, error)
|
||||
NetworkDisconnect(ctx context.Context, network, container string, force bool) error
|
||||
NetworkInspect(ctx context.Context, network string, options NetworkInspectOptions) (network.Inspect, error)
|
||||
NetworkInspectWithRaw(ctx context.Context, network string, options NetworkInspectOptions) (network.Inspect, []byte, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) ([]network.Summary, error)
|
||||
NetworkInspect(ctx context.Context, network string, options NetworkInspectOptions) (NetworkInspectResult, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) (NetworkListResult, error)
|
||||
NetworkRemove(ctx context.Context, network string) error
|
||||
NetworksPrune(ctx context.Context, opts NetworkPruneOptions) (NetworkPruneResult, error)
|
||||
}
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"github.com/moby/moby/api/types/network"
|
||||
)
|
||||
|
||||
// NetworkInspect returns the information for a specific network configured in the docker host.
|
||||
func (cli *Client) NetworkInspect(ctx context.Context, networkID string, options NetworkInspectOptions) (network.Inspect, error) {
|
||||
networkResource, _, err := cli.NetworkInspectWithRaw(ctx, networkID, options)
|
||||
return networkResource, err
|
||||
// NetworkInspectResult contains the result of a network inspection.
|
||||
type NetworkInspectResult struct {
|
||||
Network network.Inspect
|
||||
Raw []byte
|
||||
}
|
||||
|
||||
// NetworkInspectWithRaw returns the information for a specific network configured in the docker host and its raw representation.
|
||||
func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string, options NetworkInspectOptions) (network.Inspect, []byte, error) {
|
||||
// NetworkInspect returns the information for a specific network configured in the docker host.
|
||||
func (cli *Client) NetworkInspect(ctx context.Context, networkID string, options NetworkInspectOptions) (NetworkInspectResult, error) {
|
||||
networkID, err := trimID("network", networkID)
|
||||
if err != nil {
|
||||
return network.Inspect{}, nil, err
|
||||
return NetworkInspectResult{}, err
|
||||
}
|
||||
query := url.Values{}
|
||||
if options.Verbose {
|
||||
@@ -33,15 +30,10 @@ func (cli *Client) NetworkInspectWithRaw(ctx context.Context, networkID string,
|
||||
resp, err := cli.get(ctx, "/networks/"+networkID, query, nil)
|
||||
defer ensureReaderClosed(resp)
|
||||
if err != nil {
|
||||
return network.Inspect{}, nil, err
|
||||
return NetworkInspectResult{}, err
|
||||
}
|
||||
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return network.Inspect{}, nil, err
|
||||
}
|
||||
|
||||
var nw network.Inspect
|
||||
err = json.NewDecoder(bytes.NewReader(raw)).Decode(&nw)
|
||||
return nw, raw, err
|
||||
var out NetworkInspectResult
|
||||
out.Raw, err = decodeWithRaw(resp, &out.Network)
|
||||
return out, err
|
||||
}
|
||||
|
||||
@@ -77,13 +77,13 @@ func TestNetworkInspect(t *testing.T) {
|
||||
t.Run("no options", func(t *testing.T) {
|
||||
r, err := client.NetworkInspect(context.Background(), "network_id", NetworkInspectOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(r.Name, "mynetwork"))
|
||||
assert.Check(t, is.Equal(r.Network.Name, "mynetwork"))
|
||||
})
|
||||
t.Run("verbose", func(t *testing.T) {
|
||||
r, err := client.NetworkInspect(context.Background(), "network_id", NetworkInspectOptions{Verbose: true})
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(r.Name, "mynetwork"))
|
||||
_, ok := r.Services["web"]
|
||||
assert.Check(t, is.Equal(r.Network.Name, "mynetwork"))
|
||||
_, ok := r.Network.Services["web"]
|
||||
assert.Check(t, ok, "expected service `web` missing in the verbose output")
|
||||
})
|
||||
t.Run("global scope", func(t *testing.T) {
|
||||
|
||||
@@ -8,16 +8,21 @@ import (
|
||||
"github.com/moby/moby/api/types/network"
|
||||
)
|
||||
|
||||
// NetworkListResult holds the result from the [Client.NetworkList] method.
|
||||
type NetworkListResult struct {
|
||||
Items []network.Summary
|
||||
}
|
||||
|
||||
// NetworkList returns the list of networks configured in the docker host.
|
||||
func (cli *Client) NetworkList(ctx context.Context, options NetworkListOptions) ([]network.Summary, error) {
|
||||
func (cli *Client) NetworkList(ctx context.Context, options NetworkListOptions) (NetworkListResult, error) {
|
||||
query := url.Values{}
|
||||
options.Filters.updateURLValues(query)
|
||||
var networkResources []network.Summary
|
||||
resp, err := cli.get(ctx, "/networks", query, nil)
|
||||
defer ensureReaderClosed(resp)
|
||||
if err != nil {
|
||||
return networkResources, err
|
||||
return NetworkListResult{}, err
|
||||
}
|
||||
err = json.NewDecoder(resp.Body).Decode(&networkResources)
|
||||
return networkResources, err
|
||||
var res NetworkListResult
|
||||
err = json.NewDecoder(resp.Body).Decode(&res.Items)
|
||||
return res, err
|
||||
}
|
||||
|
||||
@@ -84,8 +84,8 @@ func TestNetworkList(t *testing.T) {
|
||||
}))
|
||||
assert.NilError(t, err)
|
||||
|
||||
networkResources, err := client.NetworkList(context.Background(), listCase.options)
|
||||
res, err := client.NetworkList(context.Background(), listCase.options)
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Len(networkResources, 1))
|
||||
assert.Check(t, is.Len(res.Items, 1))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user