-
Notifications
You must be signed in to change notification settings - Fork 36
/
interface.go
440 lines (383 loc) · 12.1 KB
/
interface.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"fmt"
"net/http"
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
)
// Can't use "interface" as a type, so add an underscore. Yay.
type interface_ struct {
controller *controller
resourceURI string
id int
name string
type_ string
enabled bool
tags []string
vlan *vlan
links []*link
macAddress string
effectiveMTU int
parents []string
children []string
}
func (i *interface_) updateFrom(other *interface_) {
i.resourceURI = other.resourceURI
i.id = other.id
i.name = other.name
i.type_ = other.type_
i.enabled = other.enabled
i.tags = other.tags
i.vlan = other.vlan
i.links = other.links
i.macAddress = other.macAddress
i.effectiveMTU = other.effectiveMTU
i.parents = other.parents
i.children = other.children
}
// ID implements Interface.
func (i *interface_) ID() int {
return i.id
}
// Name implements Interface.
func (i *interface_) Name() string {
return i.name
}
// Parents implements Interface.
func (i *interface_) Parents() []string {
return i.parents
}
// Children implements Interface.
func (i *interface_) Children() []string {
return i.children
}
// Type implements Interface.
func (i *interface_) Type() string {
return i.type_
}
// Enabled implements Interface.
func (i *interface_) Enabled() bool {
return i.enabled
}
// Tags implements Interface.
func (i *interface_) Tags() []string {
return i.tags
}
// VLAN implements Interface.
func (i *interface_) VLAN() VLAN {
if i.vlan == nil {
return nil
}
return i.vlan
}
// Links implements Interface.
func (i *interface_) Links() []Link {
result := make([]Link, len(i.links))
for i, link := range i.links {
result[i] = link
}
return result
}
// MACAddress implements Interface.
func (i *interface_) MACAddress() string {
return i.macAddress
}
// EffectiveMTU implements Interface.
func (i *interface_) EffectiveMTU() int {
return i.effectiveMTU
}
// UpdateInterfaceArgs is an argument struct for calling Interface.Update.
type UpdateInterfaceArgs struct {
Name string
MACAddress string
VLAN VLAN
}
func (a *UpdateInterfaceArgs) vlanID() int {
if a.VLAN == nil {
return 0
}
return a.VLAN.ID()
}
// Update implements Interface.
func (i *interface_) Update(args UpdateInterfaceArgs) error {
var empty UpdateInterfaceArgs
if args == empty {
return nil
}
params := NewURLParams()
params.MaybeAdd("name", args.Name)
params.MaybeAdd("mac_address", args.MACAddress)
params.MaybeAddInt("vlan", args.vlanID())
source, err := i.controller.put(i.resourceURI, params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound:
return errors.Wrap(err, NewNoMatchError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
response, err := readInterface(i.controller.apiVersion, source)
if err != nil {
return errors.Trace(err)
}
i.updateFrom(response)
return nil
}
// Delete implements Interface.
func (i *interface_) Delete() error {
err := i.controller.delete(i.resourceURI)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound:
return errors.Wrap(err, NewNoMatchError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
return nil
}
// InterfaceLinkMode is the type of the various link mode constants used for
// LinkSubnetArgs.
type InterfaceLinkMode string
const (
// LinkModeDHCP - Bring the interface up with DHCP on the given subnet. Only
// one subnet can be set to DHCP. If the subnet is managed this interface
// will pull from the dynamic IP range.
LinkModeDHCP InterfaceLinkMode = "DHCP"
// LinkModeStatic - Bring the interface up with a STATIC IP address on the
// given subnet. Any number of STATIC links can exist on an interface.
LinkModeStatic InterfaceLinkMode = "STATIC"
// LinkModeLinkUp - Bring the interface up only on the given subnet. No IP
// address will be assigned to this interface. The interface cannot have any
// current DHCP or STATIC links.
LinkModeLinkUp InterfaceLinkMode = "LINK_UP"
)
// LinkSubnetArgs is an argument struct for passing parameters to
// the Interface.LinkSubnet method.
type LinkSubnetArgs struct {
// Mode is used to describe how the address is provided for the Link.
// Required field.
Mode InterfaceLinkMode
// Subnet is the subnet to link to. Required field.
Subnet Subnet
// IPAddress is only valid when the Mode is set to LinkModeStatic. If
// not specified with a Mode of LinkModeStatic, an IP address from the
// subnet will be auto selected.
IPAddress string
// DefaultGateway will set the gateway IP address for the Subnet as the
// default gateway for the machine or device the interface belongs to.
// Option can only be used with mode LinkModeStatic.
DefaultGateway bool
}
// Validate ensures that the Mode and Subnet are set, and that the other options
// are consistent with the Mode.
func (a *LinkSubnetArgs) Validate() error {
switch a.Mode {
case LinkModeDHCP, LinkModeLinkUp, LinkModeStatic:
case "":
return errors.NotValidf("missing Mode")
default:
return errors.NotValidf("unknown Mode value (%q)", a.Mode)
}
if a.Subnet == nil {
return errors.NotValidf("missing Subnet")
}
if a.IPAddress != "" && a.Mode != LinkModeStatic {
return errors.NotValidf("setting IP Address when Mode is not LinkModeStatic")
}
if a.DefaultGateway && a.Mode != LinkModeStatic {
return errors.NotValidf("specifying DefaultGateway for Mode %q", a.Mode)
}
return nil
}
// LinkSubnet implements Interface.
func (i *interface_) LinkSubnet(args LinkSubnetArgs) error {
if err := args.Validate(); err != nil {
return errors.Trace(err)
}
params := NewURLParams()
params.Values.Add("mode", string(args.Mode))
params.Values.Add("subnet", fmt.Sprint(args.Subnet.ID()))
params.MaybeAdd("ip_address", args.IPAddress)
params.MaybeAddBool("default_gateway", args.DefaultGateway)
source, err := i.controller.post(i.resourceURI, "link_subnet", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound, http.StatusBadRequest:
return errors.Wrap(err, NewBadRequestError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
case http.StatusServiceUnavailable:
return errors.Wrap(err, NewCannotCompleteError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
response, err := readInterface(i.controller.apiVersion, source)
if err != nil {
return errors.Trace(err)
}
i.updateFrom(response)
return nil
}
func (i *interface_) linkForSubnet(subnet Subnet) *link {
for _, link := range i.links {
if s := link.Subnet(); s != nil && s.ID() == subnet.ID() {
return link
}
}
return nil
}
// LinkSubnet implements Interface.
func (i *interface_) UnlinkSubnet(subnet Subnet) error {
if subnet == nil {
return errors.NotValidf("missing Subnet")
}
link := i.linkForSubnet(subnet)
if link == nil {
return errors.NotValidf("unlinked Subnet")
}
params := NewURLParams()
params.Values.Add("id", fmt.Sprint(link.ID()))
source, err := i.controller.post(i.resourceURI, "unlink_subnet", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound, http.StatusBadRequest:
return errors.Wrap(err, NewBadRequestError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}
response, err := readInterface(i.controller.apiVersion, source)
if err != nil {
return errors.Trace(err)
}
i.updateFrom(response)
return nil
}
func readInterface(controllerVersion version.Number, source interface{}) (*interface_, error) {
readFunc, err := getInterfaceDeserializationFunc(controllerVersion)
if err != nil {
return nil, errors.Trace(err)
}
checker := schema.StringMap(schema.Any())
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "interface base schema check failed")
}
valid := coerced.(map[string]interface{})
return readFunc(valid)
}
func readInterfaces(controllerVersion version.Number, source interface{}) ([]*interface_, error) {
readFunc, err := getInterfaceDeserializationFunc(controllerVersion)
if err != nil {
return nil, errors.Trace(err)
}
checker := schema.List(schema.StringMap(schema.Any()))
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "interface base schema check failed")
}
valid := coerced.([]interface{})
return readInterfaceList(valid, readFunc)
}
func getInterfaceDeserializationFunc(controllerVersion version.Number) (interfaceDeserializationFunc, error) {
var deserialisationVersion version.Number
for v := range interfaceDeserializationFuncs {
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
deserialisationVersion = v
}
}
if deserialisationVersion == version.Zero {
return nil, NewUnsupportedVersionError("no interface read func for version %s", controllerVersion)
}
return interfaceDeserializationFuncs[deserialisationVersion], nil
}
func readInterfaceList(sourceList []interface{}, readFunc interfaceDeserializationFunc) ([]*interface_, error) {
result := make([]*interface_, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, NewDeserializationError("unexpected value for interface %d, %T", i, value)
}
read, err := readFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "interface %d", i)
}
result = append(result, read)
}
return result, nil
}
type interfaceDeserializationFunc func(map[string]interface{}) (*interface_, error)
var interfaceDeserializationFuncs = map[version.Number]interfaceDeserializationFunc{
twoDotOh: interface_2_0,
}
func interface_2_0(source map[string]interface{}) (*interface_, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"id": schema.ForceInt(),
"name": schema.String(),
"type": schema.String(),
"enabled": schema.Bool(),
"tags": schema.OneOf(schema.Nil(""), schema.List(schema.String())),
"vlan": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
"links": schema.List(schema.StringMap(schema.Any())),
"mac_address": schema.OneOf(schema.Nil(""), schema.String()),
"effective_mtu": schema.ForceInt(),
"parents": schema.List(schema.String()),
"children": schema.List(schema.String()),
}
defaults := schema.Defaults{
"mac_address": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "interface 2.0 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
var vlan *vlan
// If it's not an attribute map then we know it's nil from the schema check.
if vlanMap, ok := valid["vlan"].(map[string]interface{}); ok {
vlan, err = vlan_2_0(vlanMap)
if err != nil {
return nil, errors.Trace(err)
}
}
links, err := readLinkList(valid["links"].([]interface{}), link_2_0)
if err != nil {
return nil, errors.Trace(err)
}
macAddress, _ := valid["mac_address"].(string)
result := &interface_{
resourceURI: valid["resource_uri"].(string),
id: valid["id"].(int),
name: valid["name"].(string),
type_: valid["type"].(string),
enabled: valid["enabled"].(bool),
tags: convertToStringSlice(valid["tags"]),
vlan: vlan,
links: links,
macAddress: macAddress,
effectiveMTU: valid["effective_mtu"].(int),
parents: convertToStringSlice(valid["parents"]),
children: convertToStringSlice(valid["children"]),
}
return result, nil
}