forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipaddress.go
366 lines (303 loc) · 10.3 KB
/
ipaddress.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
// 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 ipaddresses struct {
Version int `yaml:"version"`
IPAddresses_ []*ipaddress `yaml:"ip-addresses"`
}
type ipaddress struct {
ProviderID_ string `yaml:"provider-id,omitempty"`
DeviceName_ string `yaml:"device-name"`
MachineID_ string `yaml:"machine-id"`
SubnetCIDR_ string `yaml:"subnet-cidr"`
ConfigMethod_ string `yaml:"config-method"`
Value_ string `yaml:"value"`
DNSServers_ []string `yaml:"dns-servers"`
DNSSearchDomains_ []string `yaml:"dns-search-domains"`
GatewayAddress_ string `yaml:"gateway-address"`
IsDefaultGateway_ bool `yaml:"is-default-gateway"`
ProviderNetworkID_ string `yaml:"provider-network-id,omitempty"`
ProviderSubnetID_ string `yaml:"provider-subnet-id,omitempty"`
Origin_ string `yaml:"origin"`
IsShadow_ bool `yaml:"is-shadow"`
IsSecondary_ bool `yaml:"is-secondary"`
}
// ProviderID implements IPAddress.
func (i *ipaddress) ProviderID() string {
return i.ProviderID_
}
// DeviceName implements IPAddress.
func (i *ipaddress) DeviceName() string {
return i.DeviceName_
}
// MachineID implements IPAddress.
func (i *ipaddress) MachineID() string {
return i.MachineID_
}
// SubnetCIDR implements IPAddress.
func (i *ipaddress) SubnetCIDR() string {
return i.SubnetCIDR_
}
// ConfigMethod implements IPAddress.
func (i *ipaddress) ConfigMethod() string {
return i.ConfigMethod_
}
// Value implements IPAddress.
func (i *ipaddress) Value() string {
return i.Value_
}
// DNSServers implements IPAddress.
func (i *ipaddress) DNSServers() []string {
return i.DNSServers_
}
// DNSSearchDomains implements IPAddress.
func (i *ipaddress) DNSSearchDomains() []string {
return i.DNSSearchDomains_
}
// GatewayAddress implements IPAddress.
func (i *ipaddress) GatewayAddress() string {
return i.GatewayAddress_
}
// IsDefaultGateway implements IPAddress.
func (i *ipaddress) IsDefaultGateway() bool {
return i.IsDefaultGateway_
}
// ProviderNetworkID implements IPAddress.
func (i *ipaddress) ProviderNetworkID() string {
return i.ProviderNetworkID_
}
// ProviderSubnetID implements IPAddress.
func (i *ipaddress) ProviderSubnetID() string {
return i.ProviderSubnetID_
}
// Origin implements IPAddress.
func (i *ipaddress) Origin() string {
return i.Origin_
}
// IsShadow implements IPAddress.
func (i *ipaddress) IsShadow() bool {
return i.IsShadow_
}
// IsShadow implements IPAddress.
func (i *ipaddress) IsSecondary() bool {
return i.IsSecondary_
}
// IPAddressArgs is an argument struct used to create a
// new internal ipaddress type that supports the IPAddress interface.
type IPAddressArgs struct {
ProviderID string
DeviceName string
MachineID string
SubnetCIDR string
ConfigMethod string
Value string
DNSServers []string
DNSSearchDomains []string
GatewayAddress string
IsDefaultGateway bool
ProviderNetworkID string
ProviderSubnetID string
Origin string
IsShadow bool
IsSecondary bool
}
func newIPAddress(args IPAddressArgs) *ipaddress {
return &ipaddress{
ProviderID_: args.ProviderID,
DeviceName_: args.DeviceName,
MachineID_: args.MachineID,
SubnetCIDR_: args.SubnetCIDR,
ConfigMethod_: args.ConfigMethod,
Value_: args.Value,
DNSServers_: args.DNSServers,
DNSSearchDomains_: args.DNSSearchDomains,
GatewayAddress_: args.GatewayAddress,
IsDefaultGateway_: args.IsDefaultGateway,
ProviderNetworkID_: args.ProviderNetworkID,
ProviderSubnetID_: args.ProviderSubnetID,
Origin_: args.Origin,
IsShadow_: args.IsShadow,
IsSecondary_: args.IsSecondary,
}
}
func importIPAddresses(source map[string]interface{}) ([]*ipaddress, error) {
checker := versionedChecker("ip-addresses")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "ip-addresses version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := ipAddressDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["ip-addresses"].([]interface{})
return importIPAddressList(sourceList, importFunc)
}
type ipAddressDeserializationFunc func(map[string]interface{}) (*ipaddress, error)
func importIPAddressList(sourceList []interface{}, importFunc ipAddressDeserializationFunc) ([]*ipaddress, error) {
result := make([]*ipaddress, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for ip-address %d, %T", i, value)
}
ipaddress, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "ip-address %d", i)
}
result = append(result, ipaddress)
}
return result, nil
}
var ipAddressDeserializationFuncs = map[int]ipAddressDeserializationFunc{
1: importIPAddressV1,
2: importIPAddressV2,
3: importIPAddressV3,
4: importIPAddressV4,
5: importIPAddressV5,
}
func parseDnsFields(valid map[string]interface{}) ([]string, []string) {
dnsServersInterface := valid["dns-servers"].([]interface{})
dnsServers := make([]string, len(dnsServersInterface))
for i, d := range dnsServersInterface {
dnsServers[i] = d.(string)
}
dnsSearchInterface := valid["dns-search-domains"].([]interface{})
dnsSearch := make([]string, len(dnsSearchInterface))
for i, d := range dnsSearchInterface {
dnsSearch[i] = d.(string)
}
return dnsServers, dnsSearch
}
func importIPAddressV1(source map[string]interface{}) (*ipaddress, error) {
fields, defaults := ipAddressV1Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "ip address v1 schema check failed")
}
return ipAddressV1(coerced.(map[string]interface{})), nil
}
func importIPAddressV2(source map[string]interface{}) (*ipaddress, error) {
fields, defaults := ipAddressV2Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "ip address v2 schema check failed")
}
return ipAddressV2(coerced.(map[string]interface{})), nil
}
func importIPAddressV3(source map[string]interface{}) (*ipaddress, error) {
fields, defaults := ipAddressV3Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "ip address v3 schema check failed")
}
return ipAddressV3(coerced.(map[string]interface{})), nil
}
func importIPAddressV4(source map[string]interface{}) (*ipaddress, error) {
fields, defaults := ipAddressV4Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "ip address v4 schema check failed")
}
return ipAddressV4(coerced.(map[string]interface{})), nil
}
func importIPAddressV5(source map[string]interface{}) (*ipaddress, error) {
fields, defaults := ipAddressV5Schema()
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "ip address v5 schema check failed")
}
return ipAddressV5(coerced.(map[string]interface{})), nil
}
func ipAddressV5Schema() (schema.Fields, schema.Defaults) {
fields, defaults := ipAddressV4Schema()
fields["is-secondary"] = schema.Bool()
defaults["is-secondary"] = false
return fields, defaults
}
func ipAddressV4Schema() (schema.Fields, schema.Defaults) {
fields, defaults := ipAddressV3Schema()
fields["is-shadow"] = schema.Bool()
defaults["is-shadow"] = false
return fields, defaults
}
func ipAddressV3Schema() (schema.Fields, schema.Defaults) {
fields, defaults := ipAddressV2Schema()
fields["provider-network-id"] = schema.String()
fields["provider-subnet-id"] = schema.String()
fields["origin"] = schema.String()
defaults["provider-network-id"] = ""
defaults["provider-subnet-id"] = ""
defaults["origin"] = ""
return fields, defaults
}
func ipAddressV2Schema() (schema.Fields, schema.Defaults) {
fields, defaults := ipAddressV1Schema()
fields["is-default-gateway"] = schema.Bool()
defaults["is-default-gateway"] = false
return fields, defaults
}
func ipAddressV1Schema() (schema.Fields, schema.Defaults) {
fields := schema.Fields{
"provider-id": schema.String(),
"device-name": schema.String(),
"machine-id": schema.String(),
"subnet-cidr": schema.String(),
"config-method": schema.String(),
"value": schema.String(),
"dns-servers": schema.List(schema.String()),
"dns-search-domains": schema.List(schema.String()),
"gateway-address": schema.String(),
}
defaults := schema.Defaults{
"provider-id": "",
}
return fields, defaults
}
func ipAddressV5(valid map[string]interface{}) *ipaddress {
addr := ipAddressV4(valid)
addr.IsSecondary_ = valid["is-secondary"].(bool)
return addr
}
func ipAddressV4(valid map[string]interface{}) *ipaddress {
addr := ipAddressV3(valid)
addr.IsShadow_ = valid["is-shadow"].(bool)
return addr
}
func ipAddressV3(valid map[string]interface{}) *ipaddress {
addr := ipAddressV2(valid)
addr.ProviderNetworkID_ = valid["provider-network-id"].(string)
addr.ProviderSubnetID_ = valid["provider-subnet-id"].(string)
addr.Origin_ = valid["origin"].(string)
return addr
}
func ipAddressV2(valid map[string]interface{}) *ipaddress {
addr := ipAddressV1(valid)
addr.IsDefaultGateway_ = valid["is-default-gateway"].(bool)
return addr
}
func ipAddressV1(valid map[string]interface{}) *ipaddress {
dnsServers, dnsSearch := parseDnsFields(valid)
return &ipaddress{
ProviderID_: valid["provider-id"].(string),
DeviceName_: valid["device-name"].(string),
MachineID_: valid["machine-id"].(string),
SubnetCIDR_: valid["subnet-cidr"].(string),
ConfigMethod_: valid["config-method"].(string),
Value_: valid["value"].(string),
DNSServers_: dnsServers,
DNSSearchDomains_: dnsSearch,
GatewayAddress_: valid["gateway-address"].(string),
}
}