github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/internal/network/ops.go (about)

     1  package network
     2  
     3  import (
     4  	"github.com/Prakhar-Agarwal-byte/moby/api/types"
     5  	"github.com/Prakhar-Agarwal-byte/moby/api/types/network"
     6  )
     7  
     8  // WithDriver sets the driver of the network
     9  func WithDriver(driver string) func(*types.NetworkCreate) {
    10  	return func(n *types.NetworkCreate) {
    11  		n.Driver = driver
    12  	}
    13  }
    14  
    15  // WithIPv6 Enables IPv6 on the network
    16  func WithIPv6() func(*types.NetworkCreate) {
    17  	return func(n *types.NetworkCreate) {
    18  		n.EnableIPv6 = true
    19  	}
    20  }
    21  
    22  // WithInternal enables Internal flag on the create network request
    23  func WithInternal() func(*types.NetworkCreate) {
    24  	return func(n *types.NetworkCreate) {
    25  		n.Internal = true
    26  	}
    27  }
    28  
    29  // WithAttachable sets Attachable flag on the create network request
    30  func WithAttachable() func(*types.NetworkCreate) {
    31  	return func(n *types.NetworkCreate) {
    32  		n.Attachable = true
    33  	}
    34  }
    35  
    36  // WithMacvlan sets the network as macvlan with the specified parent
    37  func WithMacvlan(parent string) func(*types.NetworkCreate) {
    38  	return func(n *types.NetworkCreate) {
    39  		n.Driver = "macvlan"
    40  		if parent != "" {
    41  			n.Options = map[string]string{
    42  				"parent": parent,
    43  			}
    44  		}
    45  	}
    46  }
    47  
    48  // WithIPvlan sets the network as ipvlan with the specified parent and mode
    49  func WithIPvlan(parent, mode string) func(*types.NetworkCreate) {
    50  	return func(n *types.NetworkCreate) {
    51  		n.Driver = "ipvlan"
    52  		if n.Options == nil {
    53  			n.Options = map[string]string{}
    54  		}
    55  		if parent != "" {
    56  			n.Options["parent"] = parent
    57  		}
    58  		if mode != "" {
    59  			n.Options["ipvlan_mode"] = mode
    60  		}
    61  	}
    62  }
    63  
    64  // WithOption adds the specified key/value pair to network's options
    65  func WithOption(key, value string) func(*types.NetworkCreate) {
    66  	return func(n *types.NetworkCreate) {
    67  		if n.Options == nil {
    68  			n.Options = map[string]string{}
    69  		}
    70  		n.Options[key] = value
    71  	}
    72  }
    73  
    74  // WithIPAM adds an IPAM with the specified Subnet and Gateway to the network
    75  func WithIPAM(subnet, gateway string) func(*types.NetworkCreate) {
    76  	return func(n *types.NetworkCreate) {
    77  		if n.IPAM == nil {
    78  			n.IPAM = &network.IPAM{}
    79  		}
    80  
    81  		n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{
    82  			Subnet:     subnet,
    83  			Gateway:    gateway,
    84  			AuxAddress: map[string]string{},
    85  		})
    86  	}
    87  }