-
Notifications
You must be signed in to change notification settings - Fork 23
/
linklayerdevice.go
216 lines (186 loc) · 6.29 KB
/
linklayerdevice.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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
type linklayerdevices struct {
Version int `yaml:"version"`
LinkLayerDevices_ []*linklayerdevice `yaml:"link-layer-devices"`
}
type linklayerdevice struct {
Name_ string `yaml:"name"`
MTU_ uint `yaml:"mtu"`
ProviderID_ string `yaml:"provider-id,omitempty"`
MachineID_ string `yaml:"machine-id"`
Type_ string `yaml:"type"`
MACAddress_ string `yaml:"mac-address"`
IsAutoStart_ bool `yaml:"is-autostart"`
IsUp_ bool `yaml:"is-up"`
ParentName_ string `yaml:"parent-name"`
VirtualPortType_ string `yaml:"virtual-port-type,omitempty"`
}
// ProviderID implements LinkLayerDevice.
func (i *linklayerdevice) ProviderID() string {
return i.ProviderID_
}
// MachineID implements LinkLayerDevice.
func (i *linklayerdevice) MachineID() string {
return i.MachineID_
}
// Name implements LinkLayerDevice.
func (i *linklayerdevice) Name() string {
return i.Name_
}
// MTU implements LinkLayerDevice.
func (i *linklayerdevice) MTU() uint {
return i.MTU_
}
// Type implements LinkLayerDevice.
func (i *linklayerdevice) Type() string {
return i.Type_
}
// MACAddress implements LinkLayerDevice.
func (i *linklayerdevice) MACAddress() string {
return i.MACAddress_
}
// IsAutoStart implements LinkLayerDevice.
func (i *linklayerdevice) IsAutoStart() bool {
return i.IsAutoStart_
}
// IsUp implements LinkLayerDevice.
func (i *linklayerdevice) IsUp() bool {
return i.IsUp_
}
// ParentName implements LinkLayerDevice.
func (i *linklayerdevice) ParentName() string {
return i.ParentName_
}
// VirtualPortType implements LinkLayerDevice.
func (i *linklayerdevice) VirtualPortType() string {
return i.VirtualPortType_
}
// LinkLayerDeviceArgs is an argument struct used to create a
// new internal linklayerdevice type that supports the LinkLayerDevice interface.
type LinkLayerDeviceArgs struct {
Name string
MTU uint
ProviderID string
MachineID string
Type string
MACAddress string
IsAutoStart bool
IsUp bool
ParentName string
VirtualPortType string
}
func newLinkLayerDevice(args LinkLayerDeviceArgs) *linklayerdevice {
return &linklayerdevice{
ProviderID_: args.ProviderID,
MachineID_: args.MachineID,
Name_: args.Name,
MTU_: args.MTU,
Type_: args.Type,
MACAddress_: args.MACAddress,
IsAutoStart_: args.IsAutoStart,
IsUp_: args.IsUp,
ParentName_: args.ParentName,
VirtualPortType_: args.VirtualPortType,
}
}
func importLinkLayerDevices(source map[string]interface{}) ([]*linklayerdevice, error) {
checker := versionedChecker("link-layer-devices")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "link-layer-devices version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := linklayerdeviceDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["link-layer-devices"].([]interface{})
return importLinkLayerDeviceList(sourceList, importFunc)
}
func importLinkLayerDeviceList(sourceList []interface{}, importFunc linklayerdeviceDeserializationFunc) ([]*linklayerdevice, error) {
result := make([]*linklayerdevice, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for link-layer-device %d, %T", i, value)
}
linklayerdevice, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "link-layer-device %d", i)
}
result = append(result, linklayerdevice)
}
return result, nil
}
type linklayerdeviceDeserializationFunc func(map[string]interface{}) (*linklayerdevice, error)
var linklayerdeviceDeserializationFuncs = map[int]linklayerdeviceDeserializationFunc{
1: importLinkLayerDeviceV1,
2: importLinkLayerDeviceV2,
}
func importLinkLayerDeviceV1(source map[string]interface{}) (*linklayerdevice, error) {
fields, defaults := linkLayerDeviceV1Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "linklayerdevice v1 schema check failed")
}
return linkLayerDeviceV1(coerced.(map[string]interface{})), nil
}
func importLinkLayerDeviceV2(source map[string]interface{}) (*linklayerdevice, error) {
fields, defaults := linkLayerDeviceV2Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "linklayerdevice v2 schema check failed")
}
return linkLayerDeviceV2(coerced.(map[string]interface{})), nil
}
func linkLayerDeviceV1(valid map[string]interface{}) *linklayerdevice {
return &linklayerdevice{
ProviderID_: valid["provider-id"].(string),
MachineID_: valid["machine-id"].(string),
Name_: valid["name"].(string),
MTU_: uint(valid["mtu"].(int64)),
Type_: valid["type"].(string),
MACAddress_: valid["mac-address"].(string),
IsAutoStart_: valid["is-autostart"].(bool),
IsUp_: valid["is-up"].(bool),
ParentName_: valid["parent-name"].(string),
}
}
func linkLayerDeviceV2(valid map[string]interface{}) *linklayerdevice {
lld := linkLayerDeviceV1(valid)
lld.VirtualPortType_ = valid["virtual-port-type"].(string)
return lld
}
func linkLayerDeviceV1Schema() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"provider-id": schema.String(),
"machine-id": schema.String(),
"name": schema.String(),
"mtu": schema.Int(),
"type": schema.String(),
"mac-address": schema.String(),
"is-autostart": schema.Bool(),
"is-up": schema.Bool(),
"parent-name": schema.String(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"provider-id": "",
}
return fields, defaults
}
func linkLayerDeviceV2Schema() (schema.Fields, schema.Defaults) {
fields, defaults := linkLayerDeviceV1Schema()
fields["virtual-port-type"] = schema.String()
defaults["virtual-port-type"] = ""
return fields, defaults
}