]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/fsouza/go-dockerclient/network.go
provider: Ensured Go 1.11 in TravisCI and README
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / fsouza / go-dockerclient / network.go
CommitLineData
9b12e4fe
JC
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.
4
5package docker
6
7import (
8 "bytes"
9 "encoding/json"
10 "errors"
11 "fmt"
12 "net/http"
13)
14
15// ErrNetworkAlreadyExists is the error returned by CreateNetwork when the
16// network already exists.
17var ErrNetworkAlreadyExists = errors.New("network already exists")
18
19// Network represents a network.
20//
21// See https://goo.gl/6GugX3 for more details.
22type Network struct {
23 Name string
24 ID string `json:"Id"`
25 Scope string
26 Driver string
27 IPAM IPAMOptions
28 Containers map[string]Endpoint
29 Options map[string]string
30 Internal bool
31 EnableIPv6 bool `json:"EnableIPv6"`
32}
33
34// Endpoint contains network resources allocated and used for a container in a network
35//
36// See https://goo.gl/6GugX3 for more details.
37type Endpoint struct {
38 Name string
39 ID string `json:"EndpointID"`
40 MacAddress string
41 IPv4Address string
42 IPv6Address string
43}
44
45// ListNetworks returns all networks.
46//
47// See https://goo.gl/6GugX3 for more details.
48func (c *Client) ListNetworks() ([]Network, error) {
49 resp, err := c.do("GET", "/networks", doOptions{})
50 if err != nil {
51 return nil, err
52 }
53 defer resp.Body.Close()
54 var networks []Network
55 if err := json.NewDecoder(resp.Body).Decode(&networks); err != nil {
56 return nil, err
57 }
58 return networks, nil
59}
60
61// NetworkFilterOpts is an aggregation of key=value that Docker
62// uses to filter networks
63type NetworkFilterOpts map[string]map[string]bool
64
65// FilteredListNetworks returns all networks with the filters applied
66//
67// See goo.gl/zd2mx4 for more details.
68func (c *Client) FilteredListNetworks(opts NetworkFilterOpts) ([]Network, error) {
69 params := bytes.NewBuffer(nil)
70 if err := json.NewEncoder(params).Encode(&opts); err != nil {
71 return nil, err
72 }
73 path := "/networks?filters=" + params.String()
74 resp, err := c.do("GET", path, doOptions{})
75 if err != nil {
76 return nil, err
77 }
78 defer resp.Body.Close()
79 var networks []Network
80 if err := json.NewDecoder(resp.Body).Decode(&networks); err != nil {
81 return nil, err
82 }
83 return networks, nil
84}
85
86// NetworkInfo returns information about a network by its ID.
87//
88// See https://goo.gl/6GugX3 for more details.
89func (c *Client) NetworkInfo(id string) (*Network, error) {
90 path := "/networks/" + id
91 resp, err := c.do("GET", path, doOptions{})
92 if err != nil {
93 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
94 return nil, &NoSuchNetwork{ID: id}
95 }
96 return nil, err
97 }
98 defer resp.Body.Close()
99 var network Network
100 if err := json.NewDecoder(resp.Body).Decode(&network); err != nil {
101 return nil, err
102 }
103 return &network, nil
104}
105
106// CreateNetworkOptions specify parameters to the CreateNetwork function and
107// (for now) is the expected body of the "create network" http request message
108//
109// See https://goo.gl/6GugX3 for more details.
110type 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"`
118}
119
120// IPAMOptions controls IP Address Management when creating a network
121//
122// See https://goo.gl/T8kRVH for more details.
123type IPAMOptions struct {
124 Driver string `json:"Driver"`
125 Config []IPAMConfig `json:"Config"`
126}
127
128// IPAMConfig represents IPAM configurations
129//
130// See https://goo.gl/T8kRVH for more details.
131type 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"`
136}
137
138// CreateNetwork creates a new network, returning the network instance,
139// or an error in case of failure.
140//
141// See https://goo.gl/6GugX3 for more details.
142func (c *Client) CreateNetwork(opts CreateNetworkOptions) (*Network, error) {
143 resp, err := c.do(
144 "POST",
145 "/networks/create",
146 doOptions{
147 data: opts,
148 },
149 )
150 if err != nil {
151 if e, ok := err.(*Error); ok && e.Status == http.StatusConflict {
152 return nil, ErrNetworkAlreadyExists
153 }
154 return nil, err
155 }
156 defer resp.Body.Close()
157
158 type createNetworkResponse struct {
159 ID string
160 }
161 var (
162 network Network
163 cnr createNetworkResponse
164 )
165 if err := json.NewDecoder(resp.Body).Decode(&cnr); err != nil {
166 return nil, err
167 }
168
169 network.Name = opts.Name
170 network.ID = cnr.ID
171 network.Driver = opts.Driver
172
173 return &network, nil
174}
175
176// RemoveNetwork removes a network or returns an error in case of failure.
177//
178// See https://goo.gl/6GugX3 for more details.
179func (c *Client) RemoveNetwork(id string) error {
180 resp, err := c.do("DELETE", "/networks/"+id, doOptions{})
181 if err != nil {
182 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
183 return &NoSuchNetwork{ID: id}
184 }
185 return err
186 }
187 resp.Body.Close()
188 return nil
189}
190
191// NetworkConnectionOptions specify parameters to the ConnectNetwork and
192// DisconnectNetwork function.
193//
194// See https://goo.gl/RV7BJU for more details.
195type NetworkConnectionOptions struct {
196 Container string
197
198 // EndpointConfig is only applicable to the ConnectNetwork call
199 EndpointConfig *EndpointConfig `json:"EndpointConfig,omitempty"`
200
201 // Force is only applicable to the DisconnectNetwork call
202 Force bool
203}
204
205// EndpointConfig stores network endpoint details
206//
207// See https://goo.gl/RV7BJU for more details.
208type EndpointConfig struct {
209 IPAMConfig *EndpointIPAMConfig
210 Links []string
211 Aliases []string
212}
213
214// EndpointIPAMConfig represents IPAM configurations for an
215// endpoint
216//
217// See https://goo.gl/RV7BJU for more details.
218type EndpointIPAMConfig struct {
219 IPv4Address string `json:",omitempty"`
220 IPv6Address string `json:",omitempty"`
221}
222
223// ConnectNetwork adds a container to a network or returns an error in case of
224// failure.
225//
226// See https://goo.gl/6GugX3 for more details.
227func (c *Client) ConnectNetwork(id string, opts NetworkConnectionOptions) error {
228 resp, err := c.do("POST", "/networks/"+id+"/connect", doOptions{data: opts})
229 if err != nil {
230 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
231 return &NoSuchNetworkOrContainer{NetworkID: id, ContainerID: opts.Container}
232 }
233 return err
234 }
235 resp.Body.Close()
236 return nil
237}
238
239// DisconnectNetwork removes a container from a network or returns an error in
240// case of failure.
241//
242// See https://goo.gl/6GugX3 for more details.
243func (c *Client) DisconnectNetwork(id string, opts NetworkConnectionOptions) error {
244 resp, err := c.do("POST", "/networks/"+id+"/disconnect", doOptions{data: opts})
245 if err != nil {
246 if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
247 return &NoSuchNetworkOrContainer{NetworkID: id, ContainerID: opts.Container}
248 }
249 return err
250 }
251 resp.Body.Close()
252 return nil
253}
254
255// NoSuchNetwork is the error returned when a given network does not exist.
256type NoSuchNetwork struct {
257 ID string
258}
259
260func (err *NoSuchNetwork) Error() string {
261 return fmt.Sprintf("No such network: %s", err.ID)
262}
263
264// NoSuchNetworkOrContainer is the error returned when a given network or
265// container does not exist.
266type NoSuchNetworkOrContainer struct {
267 NetworkID string
268 ContainerID string
269}
270
271func (err *NoSuchNetworkOrContainer) Error() string {
272 return fmt.Sprintf("No such network (%s) or container (%s)", err.NetworkID, err.ContainerID)
273}