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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// 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 osauth::common::empty_as_default;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::super::common::{NetworkRef, SecurityGroupRef};
use super::super::Result;
use crate::session::Session;

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

protocol_enum! {
    #[doc = "Network IP protocol."]
    enum NetworkProtocol {
        TCP = "tcp",
        UDP = "udp"
    }
}

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 RouterSortKey {
        AdminStateUp = "admin_state_up",
        FlavorId = "flavor_id",
        Id = "id",
        Name = "name",
        ProjectId = "project_id",
        Status = "status"
    }
}

protocol_enum! {
    #[doc = "Possible router statuses."]
    enum RouterStatus {
        Active = "ACTIVE",
        Allocating = "ALLOCATING",
        Error = "ERROR"
    }
}

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 = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub description: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        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 = "empty_as_default",
        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 status: NetworkStatus,
    #[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,
            status: NetworkStatus::Active,
            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's IP address.
#[derive(Debug, Clone, Deserialize, Serialize, Copy)]
pub struct AllowedAddressPair {
    pub ip_address: net::IpAddr,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mac_address: Option<MacAddress>,
}

/// A port.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Port {
    pub admin_state_up: bool,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub allowed_address_pairs: Vec<AllowedAddressPair>,
    #[serde(default, skip_serializing)]
    pub created_at: Option<DateTime<FixedOffset>>,
    #[serde(
        deserialize_with = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub description: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub device_id: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub device_owner: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub dns_domain: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        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")]
    pub mac_address: MacAddress,
    #[serde(
        deserialize_with = "empty_as_default",
        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<SecurityGroupRef>,
    #[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")]
    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<SecurityGroupRef>>,
}

/// 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>,
}

protocol_enum! {
    #[doc = "Allowed conntrack helpers as defined [here](https://opendev.org/openstack/neutron/src/branch/master/neutron/conf/extensions/conntrack_helper.py)"]
    enum Helper {
        Amanda = "amanda",
        FTP = "ftp",
        H323 = "h323",
        IRC = "irc",
        NetbiosNS = "netbios-ns",
        PPTP = "pptp",
        SANE = "sane",
        SIP = "sip",
        SNMP = "snmp",
        TFTP = "tftp"
    }
}

/// ConntrackHelper object.
/// See [here](https://home.regit.org/netfilter-en/secure-use-of-helpers/) for in-depth info about
/// conntrack helpers.
#[derive(Debug, Clone, Copy, Deserialize, PartialEq)]
pub struct ConntrackHelper {
    /// Conntrack Helper
    pub helper: Helper,
    /// Network IP protocol.
    pub protocol: NetworkProtocol,
    /// TCP or UDP port
    pub port: u16,
}

/// External gateway information.
#[non_exhaustive]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ExternalGateway {
    /// External network.
    pub network_id: NetworkRef,
    /// Whether to enable source NAT.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_snat: Option<bool>,
    /// A list of external fixed IPs.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub external_fixed_ips: Vec<FixedIp>,
}

impl ExternalGateway {
    /// Create a new external gateway.
    pub fn new<N: Into<NetworkRef>>(external_network: N) -> ExternalGateway {
        ExternalGateway {
            network_id: external_network.into(),
            enable_snat: None,
            external_fixed_ips: Vec::new(),
        }
    }

    pub(crate) async fn into_verified(self, session: &Session) -> Result<Self> {
        Ok(ExternalGateway {
            network_id: self.network_id.into_verified(session).await?,
            ..self
        })
    }
}

/// Routes.
#[derive(Debug, Serialize)]
pub struct Routes {
    pub routes: Vec<HostRoute>,
}

/// A router.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Router {
    pub admin_state_up: bool,
    #[serde(default, skip_serializing)]
    pub availability_zone_hints: Vec<String>,
    #[serde(default, skip_serializing)]
    pub availability_zones: Vec<String>,
    #[serde(default, skip_serializing)]
    pub conntrack_helpers: Vec<ConntrackHelper>,
    #[serde(default, skip_serializing)]
    pub created_at: Option<DateTime<FixedOffset>>,
    #[serde(
        deserialize_with = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub distributed: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "external_gateway_info"
    )]
    pub external_gateway: Option<ExternalGateway>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flavor_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ha: Option<bool>,
    #[serde(skip_serializing)]
    pub id: String,
    #[serde(
        deserialize_with = "empty_as_default",
        skip_serializing_if = "Option::is_none"
    )]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_id: Option<String>,
    #[serde(default, skip_serializing)]
    pub revision_number: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub routes: Option<Vec<HostRoute>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_type_id: Option<String>,
    #[serde(skip_serializing)]
    pub status: RouterStatus,
    #[serde(skip_serializing)]
    pub tags: Option<Vec<String>>,
    #[serde(default, skip_serializing)]
    pub updated_at: Option<DateTime<FixedOffset>>,
}

impl Default for Router {
    fn default() -> Router {
        Router {
            admin_state_up: true,
            availability_zones: vec![],
            availability_zone_hints: vec![],
            created_at: None,
            conntrack_helpers: vec![],
            description: None,
            distributed: None,
            external_gateway: None,
            flavor_id: None,
            ha: None,
            id: String::new(),
            name: None,
            project_id: None,
            revision_number: None,
            routes: None,
            service_type_id: None,
            status: RouterStatus::Active,
            tags: None,
            updated_at: None,
        }
    }
}

impl Router {
    pub(crate) async fn into_verified(self, session: &Session) -> Result<Self> {
        Ok(Router {
            external_gateway: match self.external_gateway {
                Some(gw) => Some(gw.into_verified(session).await?),
                None => None,
            },
            ..self
        })
    }
}

/// A router.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RouterRoot {
    pub router: Router,
}

/// A Router.
#[derive(Debug, Clone, Default, Serialize)]
pub struct RouterUpdate {
    #[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 distributed: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "external_gateway_info"
    )]
    pub external_gateway: Option<ExternalGateway>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ha: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub routes: Option<Vec<HostRoute>>,
}

/// A router.
#[derive(Debug, Clone, Serialize)]
pub struct RouterUpdateRoot {
    pub router: RouterUpdate,
}

/// A list of routers.
#[derive(Debug, Clone, Deserialize)]
pub struct RoutersRoot {
    pub routers: Vec<Router>,
}

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

/// A host route.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, 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 = "empty_as_default",
        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 = "empty_as_default",
        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,
            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(Clone, Debug, Deserialize, Eq, PartialEq)]
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 = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub description: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub dns_domain: Option<String>,
    #[serde(
        deserialize_with = "empty_as_default",
        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>,
}