mirror of
https://github.com/moby/moby.git
synced 2026-01-11 02:31:44 +00:00
client: refactor create network api implementation to wrap options/results
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/moby/moby/api/types/network"
|
||||
"github.com/moby/moby/api/types/system"
|
||||
)
|
||||
|
||||
@@ -116,7 +115,7 @@ type ImageAPIClient interface {
|
||||
// NetworkAPIClient defines API client methods for the networks
|
||||
type NetworkAPIClient interface {
|
||||
NetworkConnect(ctx context.Context, network string, options NetworkConnectOptions) (NetworkConnectResult, error)
|
||||
NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (network.CreateResponse, error)
|
||||
NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (NetworkCreateResult, error)
|
||||
NetworkDisconnect(ctx context.Context, network string, options NetworkDisconnectOptions) (NetworkDisconnectResult, error)
|
||||
NetworkInspect(ctx context.Context, network string, options NetworkInspectOptions) (NetworkInspectResult, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) (NetworkListResult, error)
|
||||
|
||||
@@ -7,8 +7,31 @@ import (
|
||||
"github.com/moby/moby/api/types/network"
|
||||
)
|
||||
|
||||
// NetworkCreateOptions holds options to create a network.
|
||||
type NetworkCreateOptions struct {
|
||||
Driver string // Driver is the driver-name used to create the network (e.g. `bridge`, `overlay`)
|
||||
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level).
|
||||
EnableIPv4 *bool // EnableIPv4 represents whether to enable IPv4.
|
||||
EnableIPv6 *bool // EnableIPv6 represents whether to enable IPv6.
|
||||
IPAM *network.IPAM // IPAM is the network's IP Address Management.
|
||||
Internal bool // Internal represents if the network is used internal only.
|
||||
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
|
||||
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
|
||||
ConfigOnly bool // ConfigOnly creates a config-only network. Config-only networks are place-holder networks for network configurations to be used by other networks. ConfigOnly networks cannot be used directly to run containers or services.
|
||||
ConfigFrom string // ConfigFrom specifies the source which will provide the configuration for this network. The specified network must be a config-only network; see [CreateOptions.ConfigOnly].
|
||||
Options map[string]string // Options specifies the network-specific options to use for when creating the network.
|
||||
Labels map[string]string // Labels holds metadata specific to the network being created.
|
||||
}
|
||||
|
||||
// NetworkCreateResult represents the result of a network create operation.
|
||||
type NetworkCreateResult struct {
|
||||
ID string
|
||||
|
||||
Warning []string
|
||||
}
|
||||
|
||||
// NetworkCreate creates a new network in the docker host.
|
||||
func (cli *Client) NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (network.CreateResponse, error) {
|
||||
func (cli *Client) NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (NetworkCreateResult, error) {
|
||||
req := network.CreateRequest{
|
||||
Name: name,
|
||||
Driver: options.Driver,
|
||||
@@ -20,18 +43,27 @@ func (cli *Client) NetworkCreate(ctx context.Context, name string, options Netwo
|
||||
Attachable: options.Attachable,
|
||||
Ingress: options.Ingress,
|
||||
ConfigOnly: options.ConfigOnly,
|
||||
ConfigFrom: options.ConfigFrom,
|
||||
Options: options.Options,
|
||||
Labels: options.Labels,
|
||||
}
|
||||
|
||||
if options.ConfigFrom != "" {
|
||||
req.ConfigFrom = &network.ConfigReference{Network: options.ConfigFrom}
|
||||
}
|
||||
|
||||
resp, err := cli.post(ctx, "/networks/create", nil, req, nil)
|
||||
defer ensureReaderClosed(resp)
|
||||
if err != nil {
|
||||
return network.CreateResponse{}, err
|
||||
return NetworkCreateResult{}, err
|
||||
}
|
||||
|
||||
var response network.CreateResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||
return response, err
|
||||
|
||||
var warnings []string
|
||||
if response.Warning != "" {
|
||||
warnings = []string{response.Warning}
|
||||
}
|
||||
|
||||
return NetworkCreateResult{ID: response.ID, Warning: warnings}, err
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package client
|
||||
|
||||
import "github.com/moby/moby/api/types/network"
|
||||
|
||||
// NetworkCreateOptions holds options to create a network.
|
||||
type NetworkCreateOptions struct {
|
||||
Driver string // Driver is the driver-name used to create the network (e.g. `bridge`, `overlay`)
|
||||
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level).
|
||||
EnableIPv4 *bool // EnableIPv4 represents whether to enable IPv4.
|
||||
EnableIPv6 *bool // EnableIPv6 represents whether to enable IPv6.
|
||||
IPAM *network.IPAM // IPAM is the network's IP Address Management.
|
||||
Internal bool // Internal represents if the network is used internal only.
|
||||
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
|
||||
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
|
||||
ConfigOnly bool // ConfigOnly creates a config-only network. Config-only networks are place-holder networks for network configurations to be used by other networks. ConfigOnly networks cannot be used directly to run containers or services.
|
||||
ConfigFrom *network.ConfigReference // ConfigFrom specifies the source which will provide the configuration for this network. The specified network must be a config-only network; see [CreateOptions.ConfigOnly].
|
||||
Options map[string]string // Options specifies the network-specific options to use for when creating the network.
|
||||
Labels map[string]string // Labels holds metadata specific to the network being created.
|
||||
}
|
||||
@@ -56,5 +56,6 @@ func TestNetworkCreate(t *testing.T) {
|
||||
})
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(networkResponse.ID, "network_id"))
|
||||
assert.Check(t, is.Equal(networkResponse.Warning, "warning"))
|
||||
assert.Check(t, is.Len(networkResponse.Warning, 1))
|
||||
assert.Check(t, is.Equal(networkResponse.Warning[0], "warning"))
|
||||
}
|
||||
|
||||
@@ -1027,13 +1027,13 @@ func (s *DockerSwarmSuite) TestAPINetworkInspectWithScope(c *testing.T) {
|
||||
name := "test-scoped-network"
|
||||
apiclient := d.NewClientT(c)
|
||||
|
||||
resp, err := apiclient.NetworkCreate(ctx, name, client.NetworkCreateOptions{Driver: "overlay"})
|
||||
create, err := apiclient.NetworkCreate(ctx, name, client.NetworkCreateOptions{Driver: "overlay"})
|
||||
assert.NilError(c, err)
|
||||
|
||||
res, err := apiclient.NetworkInspect(ctx, name, client.NetworkInspectOptions{})
|
||||
inspect, err := apiclient.NetworkInspect(ctx, name, client.NetworkInspectOptions{})
|
||||
assert.NilError(c, err)
|
||||
assert.Check(c, is.Equal("swarm", res.Network.Scope))
|
||||
assert.Check(c, is.Equal(resp.ID, res.Network.ID))
|
||||
assert.Check(c, is.Equal("swarm", inspect.Network.Scope))
|
||||
assert.Check(c, is.Equal(create.ID, inspect.Network.ID))
|
||||
|
||||
_, err = apiclient.NetworkInspect(ctx, name, client.NetworkInspectOptions{Scope: "local"})
|
||||
assert.Check(c, is.ErrorType(err, cerrdefs.IsNotFound))
|
||||
|
||||
@@ -63,7 +63,7 @@ func WithConfigOnly(co bool) func(*client.NetworkCreateOptions) {
|
||||
// WithConfigFrom sets the ConfigOnly flag in the create network request
|
||||
func WithConfigFrom(name string) func(*client.NetworkCreateOptions) {
|
||||
return func(n *client.NetworkCreateOptions) {
|
||||
n.ConfigFrom = &network.ConfigReference{Network: name}
|
||||
n.ConfigFrom = name
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -155,9 +155,7 @@ func TestDefaultNetworkOpts(t *testing.T) {
|
||||
networkName := "testnet"
|
||||
networkId := network.CreateNoError(ctx, t, c, networkName, func(create *client.NetworkCreateOptions) {
|
||||
if tc.configFrom {
|
||||
create.ConfigFrom = &networktypes.ConfigReference{
|
||||
Network: "from-net",
|
||||
}
|
||||
create.ConfigFrom = "from-net"
|
||||
}
|
||||
})
|
||||
defer c.NetworkRemove(ctx, networkName, client.NetworkRemoveOptions{})
|
||||
|
||||
3
vendor/github.com/moby/moby/client/client_interfaces.go
generated
vendored
3
vendor/github.com/moby/moby/client/client_interfaces.go
generated
vendored
@@ -5,7 +5,6 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/moby/moby/api/types/network"
|
||||
"github.com/moby/moby/api/types/system"
|
||||
)
|
||||
|
||||
@@ -116,7 +115,7 @@ type ImageAPIClient interface {
|
||||
// NetworkAPIClient defines API client methods for the networks
|
||||
type NetworkAPIClient interface {
|
||||
NetworkConnect(ctx context.Context, network string, options NetworkConnectOptions) (NetworkConnectResult, error)
|
||||
NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (network.CreateResponse, error)
|
||||
NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (NetworkCreateResult, error)
|
||||
NetworkDisconnect(ctx context.Context, network string, options NetworkDisconnectOptions) (NetworkDisconnectResult, error)
|
||||
NetworkInspect(ctx context.Context, network string, options NetworkInspectOptions) (NetworkInspectResult, error)
|
||||
NetworkList(ctx context.Context, options NetworkListOptions) (NetworkListResult, error)
|
||||
|
||||
40
vendor/github.com/moby/moby/client/network_create.go
generated
vendored
40
vendor/github.com/moby/moby/client/network_create.go
generated
vendored
@@ -7,8 +7,31 @@ import (
|
||||
"github.com/moby/moby/api/types/network"
|
||||
)
|
||||
|
||||
// NetworkCreateOptions holds options to create a network.
|
||||
type NetworkCreateOptions struct {
|
||||
Driver string // Driver is the driver-name used to create the network (e.g. `bridge`, `overlay`)
|
||||
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level).
|
||||
EnableIPv4 *bool // EnableIPv4 represents whether to enable IPv4.
|
||||
EnableIPv6 *bool // EnableIPv6 represents whether to enable IPv6.
|
||||
IPAM *network.IPAM // IPAM is the network's IP Address Management.
|
||||
Internal bool // Internal represents if the network is used internal only.
|
||||
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
|
||||
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
|
||||
ConfigOnly bool // ConfigOnly creates a config-only network. Config-only networks are place-holder networks for network configurations to be used by other networks. ConfigOnly networks cannot be used directly to run containers or services.
|
||||
ConfigFrom string // ConfigFrom specifies the source which will provide the configuration for this network. The specified network must be a config-only network; see [CreateOptions.ConfigOnly].
|
||||
Options map[string]string // Options specifies the network-specific options to use for when creating the network.
|
||||
Labels map[string]string // Labels holds metadata specific to the network being created.
|
||||
}
|
||||
|
||||
// NetworkCreateResult represents the result of a network create operation.
|
||||
type NetworkCreateResult struct {
|
||||
ID string
|
||||
|
||||
Warning []string
|
||||
}
|
||||
|
||||
// NetworkCreate creates a new network in the docker host.
|
||||
func (cli *Client) NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (network.CreateResponse, error) {
|
||||
func (cli *Client) NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (NetworkCreateResult, error) {
|
||||
req := network.CreateRequest{
|
||||
Name: name,
|
||||
Driver: options.Driver,
|
||||
@@ -20,18 +43,27 @@ func (cli *Client) NetworkCreate(ctx context.Context, name string, options Netwo
|
||||
Attachable: options.Attachable,
|
||||
Ingress: options.Ingress,
|
||||
ConfigOnly: options.ConfigOnly,
|
||||
ConfigFrom: options.ConfigFrom,
|
||||
Options: options.Options,
|
||||
Labels: options.Labels,
|
||||
}
|
||||
|
||||
if options.ConfigFrom != "" {
|
||||
req.ConfigFrom = &network.ConfigReference{Network: options.ConfigFrom}
|
||||
}
|
||||
|
||||
resp, err := cli.post(ctx, "/networks/create", nil, req, nil)
|
||||
defer ensureReaderClosed(resp)
|
||||
if err != nil {
|
||||
return network.CreateResponse{}, err
|
||||
return NetworkCreateResult{}, err
|
||||
}
|
||||
|
||||
var response network.CreateResponse
|
||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||
return response, err
|
||||
|
||||
var warnings []string
|
||||
if response.Warning != "" {
|
||||
warnings = []string{response.Warning}
|
||||
}
|
||||
|
||||
return NetworkCreateResult{ID: response.ID, Warning: warnings}, err
|
||||
}
|
||||
|
||||
19
vendor/github.com/moby/moby/client/network_create_opts.go
generated
vendored
19
vendor/github.com/moby/moby/client/network_create_opts.go
generated
vendored
@@ -1,19 +0,0 @@
|
||||
package client
|
||||
|
||||
import "github.com/moby/moby/api/types/network"
|
||||
|
||||
// NetworkCreateOptions holds options to create a network.
|
||||
type NetworkCreateOptions struct {
|
||||
Driver string // Driver is the driver-name used to create the network (e.g. `bridge`, `overlay`)
|
||||
Scope string // Scope describes the level at which the network exists (e.g. `swarm` for cluster-wide or `local` for machine level).
|
||||
EnableIPv4 *bool // EnableIPv4 represents whether to enable IPv4.
|
||||
EnableIPv6 *bool // EnableIPv6 represents whether to enable IPv6.
|
||||
IPAM *network.IPAM // IPAM is the network's IP Address Management.
|
||||
Internal bool // Internal represents if the network is used internal only.
|
||||
Attachable bool // Attachable represents if the global scope is manually attachable by regular containers from workers in swarm mode.
|
||||
Ingress bool // Ingress indicates the network is providing the routing-mesh for the swarm cluster.
|
||||
ConfigOnly bool // ConfigOnly creates a config-only network. Config-only networks are place-holder networks for network configurations to be used by other networks. ConfigOnly networks cannot be used directly to run containers or services.
|
||||
ConfigFrom *network.ConfigReference // ConfigFrom specifies the source which will provide the configuration for this network. The specified network must be a config-only network; see [CreateOptions.ConfigOnly].
|
||||
Options map[string]string // Options specifies the network-specific options to use for when creating the network.
|
||||
Labels map[string]string // Labels holds metadata specific to the network being created.
|
||||
}
|
||||
Reference in New Issue
Block a user