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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// Copyright 2018 Dmitry Tantsur <divius.inside@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! JSON structures and protocol bits for the Network API.

#![allow(non_snake_case)]
#![allow(missing_docs)]

use std::marker::PhantomData;
use std::net;
use std::ops::Not;

use chrono::{DateTime, FixedOffset};
use eui48::MacAddress;
use ipnet;
use serde_json::Value;

use super::super::common;


protocol_enum! {
    #[doc = "IP protocol version."]
    enum IpVersion: u8 {
        V4 = 4,
        V6 = 6
    }
}

protocol_enum! {
    #[doc = "Possible network statuses."]
    enum NetworkStatus {
        Active = "ACTIVE",
        Down = "DOWN",
        Building = "BUILD",
        Error = "ERROR"
    }
}

protocol_enum! {
    #[doc = "Available sort keys."]
    enum NetworkSortKey {
        CreatedAt = "created_at",
        Id = "id",
        Name = "name",
        UpdatedAt = "updated_at"
    }
}

protocol_enum! {
    #[doc = "Possible floating IP statuses."]
    enum FloatingIpStatus {
        Active = "ACTIVE",
        Down = "DOWN",
        Error = "ERROR"
    }
}

protocol_enum! {
    #[doc = "Available sort keys."]
    enum FloatingIpSortKey {
        FixedIpAddress = "fixed_ip_address",
        FloatingIpAddress = "floating_ip_address",
        FloatingNetworkId = "floating_network_id",
        Id = "id",
        RouterId = "router_id",
        Status = "status"
    }
}

impl Default for NetworkSortKey {
    fn default() -> NetworkSortKey {
        NetworkSortKey::CreatedAt
    }
}

protocol_enum! {
    #[doc = "Available sort keys."]
    enum PortSortKey {
        AdminStateUp = "admin_state_up",
        DeviceId = "device_id",
        DeviceOwner = "device_owner",
        Id = "id",
        MacAddress = "mac_address",
        Name = "name",
        NetworkId = "network_id",
        Status = "status"
    }
}

protocol_enum! {
    #[doc = "Available sort keys."]
    enum SubnetSortKey {
        Cidr = "cidr",
        DhcpEnabled = "enable_dhcp",
        GatewayIp = "gateway_ip",
        Id = "id",
        IpVersion = "ip_version",
        Ipv6AddressMode = "ipv6_address_mode",
        Ipv6RouterAdvertisementMode = "ipv6_ra_mode",
        Name = "name",
        NetworkId = "network_id"
    }
}

protocol_enum! {
    #[doc = "IPv6 modes for assigning IP addresses."]
    enum Ipv6Mode {
        DhcpStateful = "dhcpv6-stateful",
        DhcpStateless = "dhcpv6-stateless",
        Slaac = "slaac"
    }
}

/// An network.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Network {
    pub admin_state_up: bool,
    #[serde(default, skip_serializing)]
    pub availability_zones: Vec<String>,
    #[serde(default, skip_serializing)]
    pub created_at: Option<DateTime<FixedOffset>>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub dns_domain: Option<String>,
    #[serde(rename = "router:external", skip_serializing_if = "Option::is_none")]
    pub external: Option<bool>,
    #[serde(skip_serializing)]
    pub id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_default: Option<bool>,
    #[serde(default, skip_serializing)]
    pub l2_adjacency: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mtu: Option<u32>,
    #[serde(deserialize_with = "common::protocol::empty_as_none",
            skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub port_security_enabled: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    #[serde(default, skip_serializing_if = "Not::not")]
    pub shared: bool,
    #[serde(skip_serializing)]
    pub subnets: Vec<String>,
    #[serde(default, skip_serializing)]
    pub updated_at: Option<DateTime<FixedOffset>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub vlan_transparent: Option<bool>,
}

impl Default for Network {
    fn default() -> Network {
        Network {
            admin_state_up: true,
            availability_zones: Vec::new(),
            created_at: None,
            description: None,
            dns_domain: None,
            external: None,
            id: String::new(),
            is_default: None,
            l2_adjacency: None,
            mtu: None,
            name: None,
            port_security_enabled: None,
            project_id: None,
            shared: false,
            subnets: Vec::new(),
            updated_at: None,
            vlan_transparent: None,
        }
    }
}

/// A network.
#[derive(Debug, Clone, Default, Serialize)]
pub struct NetworkUpdate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admin_state_up: Option<bool>,
    #[serde(rename = "router:external", skip_serializing_if = "Option::is_none")]
    pub external: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dns_domain: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_default: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mtu: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub port_security_enabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shared: Option<bool>,
}

/// A network.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NetworkRoot {
    pub network: Network
}

/// A network.
#[derive(Debug, Clone, Serialize)]
pub struct NetworkUpdateRoot {
    pub network: NetworkUpdate
}

/// A list of networks.
#[derive(Debug, Clone, Deserialize)]
pub struct NetworksRoot {
    pub networks: Vec<Network>
}

/// An extra DHCP option.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PortExtraDhcpOption {
    /// IP protocol version (if required).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ip_version: Option<IpVersion>,
    /// Option name.
    #[serde(rename = "opt_name")]
    pub name: String,
    /// Option value.
    #[serde(rename = "opt_value")]
    pub value: String,
    #[doc(hidden)]
    #[serde(skip)]
    pub __nonexhaustive: PhantomData<()>,
}

impl PortExtraDhcpOption {
    /// Create a new DHCP option.
    pub fn new<S1, S2>(name: S1, value: S2) -> PortExtraDhcpOption
            where S1: Into<String>, S2: Into<String> {
        PortExtraDhcpOption {
            ip_version: None,
            name: name.into(),
            value: value.into(),
            __nonexhaustive: PhantomData,
        }
    }

    /// Create a new DHCP option with an IP version.
    pub fn new_with_ip_version<S1, S2>(name: S1, value: S2, ip_version: IpVersion)
            -> PortExtraDhcpOption where S1: Into<String>, S2: Into<String> {
        PortExtraDhcpOption {
            ip_version: Some(ip_version),
            name: name.into(),
            value: value.into(),
            __nonexhaustive: PhantomData,
        }
    }
}

/// A port's IP address.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FixedIp {
    #[serde(skip_serializing_if = "::std::net::IpAddr::is_unspecified")]
    pub ip_address: net::IpAddr,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub subnet_id: String
}

/// A port.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Port {
    pub admin_state_up: bool,
    #[serde(default, skip_serializing)]
    pub created_at: Option<DateTime<FixedOffset>>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub device_id: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub device_owner: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub dns_domain: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub dns_name: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub extra_dhcp_opts: Vec<PortExtraDhcpOption>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fixed_ips: Vec<FixedIp>,
    #[serde(skip_serializing)]
    pub id: String,
    #[serde(skip_serializing_if = "MacAddress::is_nil",
            serialize_with = "common::protocol::ser_mac")]
    pub mac_address: MacAddress,
    #[serde(deserialize_with = "common::protocol::empty_as_none",
            skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub network_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub security_groups: Vec<String>,
    #[serde(skip_serializing)]
    pub status: NetworkStatus,
    #[serde(default, skip_serializing)]
    pub updated_at: Option<DateTime<FixedOffset>>,
}

/// A port.
#[derive(Debug, Clone, Serialize, Default)]
pub struct PortUpdate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admin_state_up: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub device_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub device_owner: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dns_domain: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dns_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra_dhcp_opts: Option<Vec<PortExtraDhcpOption>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fixed_ips: Option<Vec<FixedIp>>,
    #[serde(skip_serializing_if = "Option::is_none",
            serialize_with = "common::protocol::ser_opt_mac")]
    pub mac_address: Option<MacAddress>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub security_groups: Option<Vec<String>>,
}

/// A port.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PortRoot {
    pub port: Port
}

/// A port update.
#[derive(Debug, Clone, Serialize)]
pub struct PortUpdateRoot {
    pub port: PortUpdate
}

/// A list of ports.
#[derive(Debug, Clone, Deserialize)]
pub struct PortsRoot {
    pub ports: Vec<Port>
}

/// An allocation pool.
#[derive(Copy, Debug, Clone, Deserialize, Serialize)]
pub struct AllocationPool {
    /// Start IP address.
    pub start: net::IpAddr,
    /// End IP address.
    pub end: net::IpAddr
}

/// A host router.
#[derive(Copy, Debug, Clone, Deserialize, Serialize)]
pub struct HostRoute {
    /// Destination network.
    pub destination: ipnet::IpNet,
    /// Next hop address.
    #[serde(rename = "nexthop")]
    pub next_hop: net::IpAddr,
}

/// A subnet.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Subnet {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub allocation_pools: Vec<AllocationPool>,
    pub cidr: ipnet::IpNet,
    #[serde(default, skip_serializing)]
    pub created_at: Option<DateTime<FixedOffset>>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(rename = "enable_dhcp")]
    pub dhcp_enabled: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dns_nameservers: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub gateway_ip: Option<net::IpAddr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub host_routes: Vec<HostRoute>,
    #[serde(skip_serializing)]
    pub id: String,
    pub ip_version: IpVersion,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ipv6_address_mode: Option<Ipv6Mode>,
    #[serde(default, rename = "ipv6_ra_mode",
            skip_serializing_if = "Option::is_none")]
    pub ipv6_router_advertisement_mode: Option<Ipv6Mode>,
    #[serde(deserialize_with = "common::protocol::empty_as_none",
            skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub network_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    #[serde(default, skip_serializing)]
    pub updated_at: Option<DateTime<FixedOffset>>,
}

impl Subnet {
    pub(crate) fn empty(cidr: ipnet::IpNet) -> Subnet {
        Subnet {
            allocation_pools: Vec::new(),
            cidr: cidr,
            created_at: None,
            description: None,
            dhcp_enabled: true,
            dns_nameservers: Vec::new(),
            gateway_ip: None,
            host_routes: Vec::new(),
            id: String::new(),
            ip_version: match cidr {
                ipnet::IpNet::V4(..) => IpVersion::V4,
                ipnet::IpNet::V6(..) => IpVersion::V6,
            },
            ipv6_address_mode: None,
            ipv6_router_advertisement_mode: None,
            name: None,
            network_id: String::new(),
            project_id: None,
            updated_at: None,
        }
    }
}

/// A subnet.
#[derive(Debug, Clone, Serialize, Default)]
pub struct SubnetUpdate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allocation_pools: Option<Vec<AllocationPool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(rename = "enable_dhcp", skip_serializing_if = "Option::is_none")]
    pub dhcp_enabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dns_nameservers: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gateway_ip: Option<net::IpAddr>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub host_routes: Option<Vec<HostRoute>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

/// A subnet.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SubnetRoot {
    pub subnet: Subnet
}

/// A subnet.
#[derive(Debug, Clone, Serialize)]
pub struct SubnetUpdateRoot {
    pub subnet: SubnetUpdate
}

/// A list of subnets.
#[derive(Debug, Clone, Deserialize)]
pub struct SubnetsRoot {
    pub subnets: Vec<Subnet>
}

#[derive(Debug, Clone, Deserialize)]
pub struct PortForwarding {
    /// TCP or UDP port used by floating IP.
    pub external_port: u16,
    /// Fixed IP address of internal port.
    pub internal_ip_address: net::IpAddr,
    /// TCP or UDP port used by internal port.
    pub internal_port: u16,
    /// Network IP protocol.
    pub protocol: String,
}

/// A floating IP.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FloatingIp {
    #[serde(default, skip_serializing)]
    pub created_at: Option<DateTime<FixedOffset>>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub dns_domain: Option<String>,
    #[serde(deserialize_with = "common::protocol::empty_as_none", default,
            skip_serializing_if = "Option::is_none")]
    pub dns_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fixed_ip_address: Option<net::IpAddr>,
    #[serde(skip_serializing_if = "::std::net::IpAddr::is_unspecified")]
    pub floating_ip_address: net::IpAddr,
    pub floating_network_id: String,
    #[serde(skip_serializing)]
    pub id: String,
    #[serde(default)]
    pub port_id: Option<String>,
    #[serde(default, skip_serializing)]
    pub port_forwardings: Vec<PortForwarding>,
    #[serde(default, skip_serializing)]
    pub router_id: Option<String>,
    #[serde(skip_serializing)]
    pub status: FloatingIpStatus,
    #[serde(skip_deserializing, skip_serializing_if = "Option::is_none")]
    pub subnet_id: Option<String>,
    #[serde(default, skip_serializing)]
    pub updated_at: Option<DateTime<FixedOffset>>,
}

/// A port.
#[derive(Debug, Clone, Serialize, Default)]
pub struct FloatingIpUpdate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fixed_ip_address: Option<net::IpAddr>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub port_id: Option<Value>,
}

/// A floating IP.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct FloatingIpRoot {
    pub floatingip: FloatingIp
}

/// A floating IP.
#[derive(Debug, Clone, Serialize)]
pub struct FloatingIpUpdateRoot {
    pub floatingip: FloatingIpUpdate
}

/// Floating IPs.
#[derive(Debug, Clone, Deserialize)]
pub struct FloatingIpsRoot {
    pub floatingips: Vec<FloatingIp>
}