File: ops.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (151 lines) | stat: -rw-r--r-- 3,982 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package network

import (
	"github.com/docker/docker/api/types/network"
)

// WithDriver sets the driver of the network
func WithDriver(driver string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Driver = driver
	}
}

// WithIPv4 enables/disables IPv4 on the network
func WithIPv4(enable bool) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		enableIPv4 := enable
		n.EnableIPv4 = &enableIPv4
	}
}

// WithIPv6 Enables IPv6 on the network
func WithIPv6() func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		enableIPv6 := true
		n.EnableIPv6 = &enableIPv6
	}
}

// WithIPv4Disabled makes sure IPv4 is disabled on the network.
func WithIPv4Disabled() func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		enable := false
		n.EnableIPv4 = &enable
	}
}

// WithIPv6Disabled makes sure IPv6 is disabled on the network.
func WithIPv6Disabled() func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		enable := false
		n.EnableIPv6 = &enable
	}
}

// WithInternal enables Internal flag on the create network request
func WithInternal() func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Internal = true
	}
}

// WithConfigOnly sets the ConfigOnly flag in the create network request
func WithConfigOnly(co bool) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.ConfigOnly = co
	}
}

// WithConfigFrom sets the ConfigOnly flag in the create network request
func WithConfigFrom(name string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.ConfigFrom = &network.ConfigReference{Network: name}
	}
}

// WithAttachable sets Attachable flag on the create network request
func WithAttachable() func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Attachable = true
	}
}

// WithScope sets the network scope.
func WithScope(s string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Scope = s
	}
}

// WithMacvlan sets the network as macvlan with the specified parent
func WithMacvlan(parent string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Driver = "macvlan"
		if parent != "" {
			n.Options = map[string]string{
				"parent": parent,
			}
		}
	}
}

// WithMacvlanPassthru sets the network as macvlan with the specified parent in passthru mode
func WithMacvlanPassthru(parent string) func(options *network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Driver = "macvlan"
		n.Options = map[string]string{
			"macvlan_mode": "passthru",
		}
		if parent != "" {
			n.Options["parent"] = parent
		}
	}
}

// WithIPvlan sets the network as ipvlan with the specified parent and mode
func WithIPvlan(parent, mode string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		n.Driver = "ipvlan"
		if n.Options == nil {
			n.Options = map[string]string{}
		}
		if parent != "" {
			n.Options["parent"] = parent
		}
		if mode != "" {
			n.Options["ipvlan_mode"] = mode
		}
	}
}

// WithOption adds the specified key/value pair to network's options
func WithOption(key, value string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		if n.Options == nil {
			n.Options = map[string]string{}
		}
		n.Options[key] = value
	}
}

// WithIPAM adds an IPAM with the specified Subnet and Gateway to the network
func WithIPAM(subnet, gateway string) func(*network.CreateOptions) {
	return WithIPAMRange(subnet, "", gateway)
}

// WithIPAMRange adds an IPAM with the specified Subnet, IPRange and Gateway to the network
func WithIPAMRange(subnet, iprange, gateway string) func(*network.CreateOptions) {
	return func(n *network.CreateOptions) {
		if n.IPAM == nil {
			n.IPAM = &network.IPAM{}
		}

		n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{
			Subnet:     subnet,
			IPRange:    iprange,
			Gateway:    gateway,
			AuxAddress: map[string]string{},
		})
	}
}