mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: NetworkList: wrap result
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -131,7 +131,7 @@ type NetworkAPIClient interface {
|
||||
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) (NetworkInspectResult, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) ([]network.Summary, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) (NetworkListResult, error)
|
||||
NetworkRemove(ctx context.Context, network string) error
|
||||
NetworksPrune(ctx context.Context, opts NetworkPruneOptions) (NetworkPruneResult, error)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ func OnlyDefaultNetworks(ctx context.Context) bool {
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
networks, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
if err != nil || len(networks) > 0 {
|
||||
res, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
if err != nil || len(res.Items) > 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -31,12 +31,12 @@ func createAmbiguousNetworks(ctx context.Context, t *testing.T, apiClient client
|
||||
idPrefixNet := network.CreateNoError(ctx, t, apiClient, testNet[:12])
|
||||
fullIDNet := network.CreateNoError(ctx, t, apiClient, testNet)
|
||||
|
||||
nws, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Check(t, is.Equal(true, containsNetwork(nws, testNet)), "failed to create network testNet")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(nws, idPrefixNet)), "failed to create network idPrefixNet")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "failed to create network fullIDNet")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(res.Items, testNet)), "failed to create network testNet")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(res.Items, idPrefixNet)), "failed to create network idPrefixNet")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(res.Items, fullIDNet)), "failed to create network fullIDNet")
|
||||
return testNet, idPrefixNet, fullIDNet
|
||||
}
|
||||
|
||||
@@ -79,9 +79,9 @@ func TestDockerNetworkDeletePreferID(t *testing.T) {
|
||||
assert.NilError(t, err)
|
||||
|
||||
// networks "testNet" and "idPrefixNet" should be removed, but "fullIDNet" should still exist
|
||||
nws, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(false, containsNetwork(nws, testNet)), "Network testNet not removed")
|
||||
assert.Check(t, is.Equal(false, containsNetwork(nws, idPrefixNet)), "Network idPrefixNet not removed")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "Network fullIDNet not found")
|
||||
assert.Check(t, is.Equal(false, containsNetwork(res.Items, testNet)), "Network testNet not removed")
|
||||
assert.Check(t, is.Equal(false, containsNetwork(res.Items, idPrefixNet)), "Network idPrefixNet not removed")
|
||||
assert.Check(t, is.Equal(true, containsNetwork(res.Items, fullIDNet)), "Network fullIDNet not found")
|
||||
}
|
||||
|
||||
@@ -53,12 +53,12 @@ func LinkDoesntExist(ctx context.Context, t *testing.T, master string) {
|
||||
// IsNetworkAvailable provides a comparison to check if a docker network is available
|
||||
func IsNetworkAvailable(ctx context.Context, c client.NetworkAPIClient, name string) is.Comparison {
|
||||
return func() is.Result {
|
||||
networks, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
if err != nil {
|
||||
return is.ResultFromError(err)
|
||||
}
|
||||
for _, network := range networks {
|
||||
if network.Name == name {
|
||||
for _, nw := range res.Items {
|
||||
if nw.Name == name {
|
||||
return is.ResultSuccess
|
||||
}
|
||||
}
|
||||
@@ -69,12 +69,12 @@ func IsNetworkAvailable(ctx context.Context, c client.NetworkAPIClient, name str
|
||||
// IsNetworkNotAvailable provides a comparison to check if a docker network is not available
|
||||
func IsNetworkNotAvailable(ctx context.Context, c client.NetworkAPIClient, name string) is.Comparison {
|
||||
return func() is.Result {
|
||||
networks, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
if err != nil {
|
||||
return is.ResultFromError(err)
|
||||
}
|
||||
for _, network := range networks {
|
||||
if network.Name == name {
|
||||
for _, nw := range res.Items {
|
||||
if nw.Name == name {
|
||||
return is.ResultFailure(fmt.Sprintf("network %s is still present", name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,12 @@ import (
|
||||
// IsNetworkAvailable provides a comparison to check if a docker network is available
|
||||
func IsNetworkAvailable(ctx context.Context, c client.NetworkAPIClient, name string) cmp.Comparison {
|
||||
return func() cmp.Result {
|
||||
networks, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
if err != nil {
|
||||
return cmp.ResultFromError(err)
|
||||
}
|
||||
for _, network := range networks {
|
||||
if network.Name == name {
|
||||
for _, nw := range res.Items {
|
||||
if nw.Name == name {
|
||||
return cmp.ResultSuccess
|
||||
}
|
||||
}
|
||||
@@ -27,12 +27,12 @@ func IsNetworkAvailable(ctx context.Context, c client.NetworkAPIClient, name str
|
||||
// IsNetworkNotAvailable provides a comparison to check if a docker network is not available
|
||||
func IsNetworkNotAvailable(ctx context.Context, c client.NetworkAPIClient, name string) cmp.Comparison {
|
||||
return func() cmp.Result {
|
||||
networks, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
if err != nil {
|
||||
return cmp.ResultFromError(err)
|
||||
}
|
||||
for _, network := range networks {
|
||||
if network.Name == name {
|
||||
for _, nw := range res.Items {
|
||||
if nw.Name == name {
|
||||
return cmp.ResultFailure(fmt.Sprintf("network %s is still present", name))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,15 +130,15 @@ func TestAPINetworkFilter(t *testing.T) {
|
||||
ctx := setupTest(t)
|
||||
apiClient := testEnv.APIClient()
|
||||
|
||||
networks, err := apiClient.NetworkList(ctx, client.NetworkListOptions{
|
||||
res, err := apiClient.NetworkList(ctx, client.NetworkListOptions{
|
||||
Filters: make(client.Filters).Add("name", networkName),
|
||||
})
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
found := false
|
||||
for _, network := range networks {
|
||||
if network.Name == networkName {
|
||||
for _, nw := range res.Items {
|
||||
if nw.Name == networkName {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,22 +144,22 @@ func deleteAllVolumes(ctx context.Context, t testing.TB, c client.VolumeAPIClien
|
||||
|
||||
func deleteAllNetworks(ctx context.Context, t testing.TB, c client.NetworkAPIClient, daemonPlatform string, protectedNetworks map[string]struct{}) {
|
||||
t.Helper()
|
||||
networks, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := c.NetworkList(ctx, client.NetworkListOptions{})
|
||||
assert.Check(t, err, "failed to list networks")
|
||||
|
||||
for _, n := range networks {
|
||||
if n.Name == network.NetworkBridge || n.Name == network.NetworkNone || n.Name == network.NetworkHost {
|
||||
for _, nw := range res.Items {
|
||||
if nw.Name == network.NetworkBridge || nw.Name == network.NetworkNone || nw.Name == network.NetworkHost {
|
||||
continue
|
||||
}
|
||||
if _, ok := protectedNetworks[n.ID]; ok {
|
||||
if _, ok := protectedNetworks[nw.ID]; ok {
|
||||
continue
|
||||
}
|
||||
if daemonPlatform == "windows" && strings.ToLower(n.Name) == network.NetworkNat {
|
||||
if daemonPlatform == "windows" && strings.ToLower(nw.Name) == network.NetworkNat {
|
||||
// nat is a pre-defined network on Windows and cannot be removed
|
||||
continue
|
||||
}
|
||||
err := c.NetworkRemove(ctx, n.ID)
|
||||
assert.Check(t, err, "failed to remove network %s", n.ID)
|
||||
err := c.NetworkRemove(ctx, nw.ID)
|
||||
assert.Check(t, err, "failed to remove network %s", nw.ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -165,14 +165,14 @@ func ProtectNetworks(ctx context.Context, t testing.TB, testEnv *Execution) {
|
||||
func getExistingNetworks(ctx context.Context, t testing.TB, testEnv *Execution) []string {
|
||||
t.Helper()
|
||||
apiClient := testEnv.APIClient()
|
||||
networkList, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
res, err := apiClient.NetworkList(ctx, client.NetworkListOptions{})
|
||||
assert.NilError(t, err, "failed to list networks")
|
||||
|
||||
var networks []string
|
||||
for _, network := range networkList {
|
||||
networks = append(networks, network.ID)
|
||||
var nwIDs []string
|
||||
for _, nw := range res.Items {
|
||||
nwIDs = append(nwIDs, nw.ID)
|
||||
}
|
||||
return networks
|
||||
return nwIDs
|
||||
}
|
||||
|
||||
// ProtectPlugin adds the specified plugin(s) to be protected in case of clean
|
||||
|
||||
2
vendor/github.com/moby/moby/client/client_interfaces.go
generated
vendored
2
vendor/github.com/moby/moby/client/client_interfaces.go
generated
vendored
@@ -131,7 +131,7 @@ type NetworkAPIClient interface {
|
||||
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) (NetworkInspectResult, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) ([]network.Summary, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) (NetworkListResult, error)
|
||||
NetworkRemove(ctx context.Context, network string) error
|
||||
NetworksPrune(ctx context.Context, opts NetworkPruneOptions) (NetworkPruneResult, error)
|
||||
}
|
||||
|
||||
15
vendor/github.com/moby/moby/client/network_list.go
generated
vendored
15
vendor/github.com/moby/moby/client/network_list.go
generated
vendored
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user