-
Notifications
You must be signed in to change notification settings - Fork 36
/
device.go
313 lines (272 loc) · 8.48 KB
/
device.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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"fmt"
"net/http"
"strings"
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
)
type device struct {
controller *controller
resourceURI string
systemID string
hostname string
fqdn string
parent string
owner string
ipAddresses []string
interfaceSet []*interface_
zone *zone
pool *pool
}
// SystemID implements Device.
func (d *device) SystemID() string {
return d.systemID
}
// Hostname implements Device.
func (d *device) Hostname() string {
return d.hostname
}
// FQDN implements Device.
func (d *device) FQDN() string {
return d.fqdn
}
// Parent implements Device.
func (d *device) Parent() string {
return d.parent
}
// Owner implements Device.
func (d *device) Owner() string {
return d.owner
}
// IPAddresses implements Device.
func (d *device) IPAddresses() []string {
return d.ipAddresses
}
// Zone implements Device.
func (d *device) Zone() Zone {
if d.zone == nil {
return nil
}
return d.zone
}
// Pool implements Device.
func (d *device) Pool() Pool {
if d.pool == nil {
return nil
}
return d.pool
}
// InterfaceSet implements Device.
func (d *device) InterfaceSet() []Interface {
result := make([]Interface, len(d.interfaceSet))
for i, v := range d.interfaceSet {
v.controller = d.controller
result[i] = v
}
return result
}
// CreateInterfaceArgs is an argument struct for passing parameters to
// the Machine.CreateInterface method.
type CreateInterfaceArgs struct {
// Name of the interface (required).
Name string
// MACAddress is the MAC address of the interface (required).
MACAddress string
// VLAN is the untagged VLAN the interface is connected to (required).
VLAN VLAN
// Tags to attach to the interface (optional).
Tags []string
// MTU - Maximum transmission unit. (optional)
MTU int
// AcceptRA - Accept router advertisements. (IPv6 only)
AcceptRA bool
// Autoconf - Perform stateless autoconfiguration. (IPv6 only)
Autoconf bool
}
// Validate checks the required fields are set for the arg structure.
func (a *CreateInterfaceArgs) Validate() error {
if a.Name == "" {
return errors.NotValidf("missing Name")
}
if a.MACAddress == "" {
return errors.NotValidf("missing MACAddress")
}
if a.VLAN == nil {
return errors.NotValidf("missing VLAN")
}
return nil
}
// interfacesURI used to add interfaces for this device. The operations
// are on the nodes endpoint, not devices.
func (d *device) interfacesURI() string {
return strings.Replace(d.resourceURI, "devices", "nodes", 1) + "interfaces/"
}
// CreateInterface implements Device.
func (d *device) CreateInterface(args CreateInterfaceArgs) (Interface, error) {
if err := args.Validate(); err != nil {
return nil, errors.Trace(err)
}
params := NewURLParams()
params.Values.Add("name", args.Name)
params.Values.Add("mac_address", args.MACAddress)
params.Values.Add("vlan", fmt.Sprint(args.VLAN.ID()))
params.MaybeAdd("tags", strings.Join(args.Tags, ","))
params.MaybeAddInt("mtu", args.MTU)
params.MaybeAddBool("accept_ra", args.AcceptRA)
params.MaybeAddBool("autoconf", args.Autoconf)
result, err := d.controller.post(d.interfacesURI(), "create_physical", params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound, http.StatusConflict:
return nil, errors.Wrap(err, NewBadRequestError(svrErr.BodyMessage))
case http.StatusForbidden:
return nil, errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
case http.StatusServiceUnavailable:
return nil, errors.Wrap(err, NewCannotCompleteError(svrErr.BodyMessage))
}
}
return nil, NewUnexpectedError(err)
}
iface, err := readInterface(d.controller.apiVersion, result)
if err != nil {
return nil, errors.Trace(err)
}
iface.controller = d.controller
// TODO: add to the interfaces for the device when the interfaces are returned.
// lp:bug 1567213.
return iface, nil
}
// Delete implements Device.
func (d *device) Delete() error {
err := d.controller.delete(d.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
}
func readDevice(controllerVersion version.Number, source interface{}) (*device, error) {
readFunc, err := getDeviceDeserializationFunc(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, "device base schema check failed")
}
valid := coerced.(map[string]interface{})
return readFunc(valid)
}
func readDevices(controllerVersion version.Number, source interface{}) ([]*device, error) {
readFunc, err := getDeviceDeserializationFunc(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, "device base schema check failed")
}
valid := coerced.([]interface{})
return readDeviceList(valid, readFunc)
}
func getDeviceDeserializationFunc(controllerVersion version.Number) (deviceDeserializationFunc, error) {
var deserialisationVersion version.Number
for v := range deviceDeserializationFuncs {
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
deserialisationVersion = v
}
}
if deserialisationVersion == version.Zero {
return nil, NewUnsupportedVersionError("no device read func for version %s", controllerVersion)
}
return deviceDeserializationFuncs[deserialisationVersion], nil
}
// readDeviceList expects the values of the sourceList to be string maps.
func readDeviceList(sourceList []interface{}, readFunc deviceDeserializationFunc) ([]*device, error) {
result := make([]*device, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, NewDeserializationError("unexpected value for device %d, %T", i, value)
}
device, err := readFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "device %d", i)
}
result = append(result, device)
}
return result, nil
}
type deviceDeserializationFunc func(map[string]interface{}) (*device, error)
var deviceDeserializationFuncs = map[version.Number]deviceDeserializationFunc{
twoDotOh: device_2_0,
}
func device_2_0(source map[string]interface{}) (*device, error) {
fields := schema.Fields{
"resource_uri": schema.String(),
"system_id": schema.String(),
"hostname": schema.String(),
"fqdn": schema.String(),
"parent": schema.OneOf(schema.Nil(""), schema.String()),
"owner": schema.OneOf(schema.Nil(""), schema.String()),
"ip_addresses": schema.List(schema.String()),
"interface_set": schema.List(schema.StringMap(schema.Any())),
"zone": schema.StringMap(schema.Any()),
"pool": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
}
defaults := schema.Defaults{
"owner": "",
"parent": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "device 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.
interfaceSet, err := readInterfaceList(valid["interface_set"].([]interface{}), interface_2_0)
if err != nil {
return nil, errors.Trace(err)
}
zone, err := zone_2_0(valid["zone"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
var pool *pool
if valid["pool"] != nil {
if pool, err = pool_2_0(valid["pool"].(map[string]interface{})); err != nil {
return nil, errors.Trace(err)
}
}
owner, _ := valid["owner"].(string)
parent, _ := valid["parent"].(string)
result := &device{
resourceURI: valid["resource_uri"].(string),
systemID: valid["system_id"].(string),
hostname: valid["hostname"].(string),
fqdn: valid["fqdn"].(string),
parent: parent,
owner: owner,
ipAddresses: convertToStringSlice(valid["ip_addresses"]),
interfaceSet: interfaceSet,
zone: zone,
pool: pool,
}
return result, nil
}