Repository: moby/moby
File: client/network_create.go (Go)

Code before:
package client

import (
	"context"
	"encoding/json"

	"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.
}

// NetworkCreateResult represents the result of a network create operation.
type NetworkCreateResult struct {
	NetworkID string

	Warning string
}

// NetworkCreate creates a new network in the docker host.
func (cli *Client) NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (NetworkCreateResult, error) {
	req := network.CreateRequest{
		Name:       name,
		Driver:     options.Driver,
		Scope:      options.Scope,
		EnableIPv4: options.EnableIPv4,
		EnableIPv6: options.EnableIPv6,
		IPAM:       options.IPAM,
		Internal:   options.Internal,
		Attachable: options.Attachable,
		Ingress:    options.Ingress,
		ConfigOnly: options.ConfigOnly,
		ConfigFrom: options.ConfigFrom,

Review comment (suggestion):
For some of these, I'm wondering if we can avoid pointer values. Some of the pointer-values may have been to control JSON marshalling ("omit empty"), which is not a concern for the _option types_. If we can detect "zero value" (or in this case, `ConfigReference.Name` being empty?), we could probably still convert it into the right API request (which could continue to use pointers).


Not sure if we can do that trick for `EnableIPV(4|6)` - maybe @robmry has thoughts on that; for those, we could even consider providing more sensible defaults in the options-struct;

```
DisableIPv4 bool
DisableIPv6 bool
```


That is, assuming most networks should have IPv4 and IPv6 enabled by default (?) (well, possibly depending on daemon settings).

Code after:
package client

import (
	"context"
	"encoding/json"

	"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) (NetworkCreateResult, error) {
	req := network.CreateRequest{
		Name:       name,
		Driver:     options.Driver,
		Scope:      options.Scope,
		EnableIPv4: options.EnableIPv4,
		EnableIPv6: options.EnableIPv6,
		IPAM:       options.IPAM,
		Internal:   options.Internal,
		Attachable: options.Attachable,
		Ingress:    options.Ingress,
		ConfigOnly: options.ConfigOnly,
		Options:    options.Options,