1 // Copyright 2015 go-dockerclient authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // ErrNetworkAlreadyExists is the error returned by CreateNetwork when the
16 // network already exists.
17 var ErrNetworkAlreadyExists = errors.New("network already exists")
19 // Network represents a network.
21 // See https://goo.gl/6GugX3 for more details.
28 Containers map[string]Endpoint
29 Options map[string]string
31 EnableIPv6 bool `json:"EnableIPv6"`
34 // Endpoint contains network resources allocated and used for a container in a network
36 // See https://goo.gl/6GugX3 for more details.
37 type Endpoint struct {
39 ID string `json:"EndpointID"`
45 // ListNetworks returns all networks.
47 // See https://goo.gl/6GugX3 for more details.
48 func (c *Client) ListNetworks() ([]Network, error) {
49 resp, err := c.do("GET", "/networks", doOptions{})
53 defer resp.Body.Close()
54 var networks []Network
55 if err := json.NewDecoder(resp.Body).Decode(&networks); err != nil {
61 // NetworkFilterOpts is an aggregation of key=value that Docker
62 // uses to filter networks
63 type NetworkFilterOpts map[string]map[string]bool
65 // FilteredListNetworks returns all networks with the filters applied
67 // See goo.gl/zd2mx4 for more details.
68 func (c *Client) FilteredListNetworks(opts NetworkFilterOpts) ([]Network, error) {
69 params := bytes.NewBuffer(nil)
70 if err := json.NewEncoder(params).Encode(&opts); err != nil {
73 path := "/networks?filters=" + params.String()
74 resp, err := c.do("GET", path, doOptions{})
78 defer resp.Body.Close()
79 var networks []Network
80 if err := json.NewDecoder(resp.Body).Decode(&networks); err != nil {
86 // NetworkInfo returns information about a network by its ID.
88 // See https://goo.gl/6GugX3 for more details.
89 func (c *Client) NetworkInfo(id string) (*Network, error) {
90 path := "/networks/" + id
91 resp, err := c.do("GET", path, doOptions{})
93 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
94 return nil, &NoSuchNetwork{ID: id}
98 defer resp.Body.Close()
100 if err := json.NewDecoder(resp.Body).Decode(&network); err != nil {
106 // CreateNetworkOptions specify parameters to the CreateNetwork function and
107 // (for now) is the expected body of the "create network" http request message
109 // See https://goo.gl/6GugX3 for more details.
110 type CreateNetworkOptions struct {
111 Name string `json:"Name"`
112 CheckDuplicate bool `json:"CheckDuplicate"`
113 Driver string `json:"Driver"`
114 IPAM IPAMOptions `json:"IPAM"`
115 Options map[string]interface{} `json:"Options"`
116 Internal bool `json:"Internal"`
117 EnableIPv6 bool `json:"EnableIPv6"`
120 // IPAMOptions controls IP Address Management when creating a network
122 // See https://goo.gl/T8kRVH for more details.
123 type IPAMOptions struct {
124 Driver string `json:"Driver"`
125 Config []IPAMConfig `json:"Config"`
128 // IPAMConfig represents IPAM configurations
130 // See https://goo.gl/T8kRVH for more details.
131 type IPAMConfig struct {
132 Subnet string `json:",omitempty"`
133 IPRange string `json:",omitempty"`
134 Gateway string `json:",omitempty"`
135 AuxAddress map[string]string `json:"AuxiliaryAddresses,omitempty"`
138 // CreateNetwork creates a new network, returning the network instance,
139 // or an error in case of failure.
141 // See https://goo.gl/6GugX3 for more details.
142 func (c *Client) CreateNetwork(opts CreateNetworkOptions) (*Network, error) {
151 if e, ok := err.(*Error); ok && e.Status == http.StatusConflict {
152 return nil, ErrNetworkAlreadyExists
156 defer resp.Body.Close()
158 type createNetworkResponse struct {
163 cnr createNetworkResponse
165 if err := json.NewDecoder(resp.Body).Decode(&cnr); err != nil {
169 network.Name = opts.Name
171 network.Driver = opts.Driver
176 // RemoveNetwork removes a network or returns an error in case of failure.
178 // See https://goo.gl/6GugX3 for more details.
179 func (c *Client) RemoveNetwork(id string) error {
180 resp, err := c.do("DELETE", "/networks/"+id, doOptions{})
182 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
183 return &NoSuchNetwork{ID: id}
191 // NetworkConnectionOptions specify parameters to the ConnectNetwork and
192 // DisconnectNetwork function.
194 // See https://goo.gl/RV7BJU for more details.
195 type NetworkConnectionOptions struct {
198 // EndpointConfig is only applicable to the ConnectNetwork call
199 EndpointConfig *EndpointConfig `json:"EndpointConfig,omitempty"`
201 // Force is only applicable to the DisconnectNetwork call
205 // EndpointConfig stores network endpoint details
207 // See https://goo.gl/RV7BJU for more details.
208 type EndpointConfig struct {
209 IPAMConfig *EndpointIPAMConfig
214 // EndpointIPAMConfig represents IPAM configurations for an
217 // See https://goo.gl/RV7BJU for more details.
218 type EndpointIPAMConfig struct {
219 IPv4Address string `json:",omitempty"`
220 IPv6Address string `json:",omitempty"`
223 // ConnectNetwork adds a container to a network or returns an error in case of
226 // See https://goo.gl/6GugX3 for more details.
227 func (c *Client) ConnectNetwork(id string, opts NetworkConnectionOptions) error {
228 resp, err := c.do("POST", "/networks/"+id+"/connect", doOptions{data: opts})
230 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
231 return &NoSuchNetworkOrContainer{NetworkID: id, ContainerID: opts.Container}
239 // DisconnectNetwork removes a container from a network or returns an error in
242 // See https://goo.gl/6GugX3 for more details.
243 func (c *Client) DisconnectNetwork(id string, opts NetworkConnectionOptions) error {
244 resp, err := c.do("POST", "/networks/"+id+"/disconnect", doOptions{data: opts})
246 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
247 return &NoSuchNetworkOrContainer{NetworkID: id, ContainerID: opts.Container}
255 // NoSuchNetwork is the error returned when a given network does not exist.
256 type NoSuchNetwork struct {
260 func (err *NoSuchNetwork) Error() string {
261 return fmt.Sprintf("No such network: %s", err.ID)
264 // NoSuchNetworkOrContainer is the error returned when a given network or
265 // container does not exist.
266 type NoSuchNetworkOrContainer struct {
271 func (err *NoSuchNetworkOrContainer) Error() string {
272 return fmt.Sprintf("No such network (%s) or container (%s)", err.NetworkID, err.ContainerID)