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
// 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.

//! Ports management via Port API.

use std::collections::HashSet;
use std::mem;
use std::net;
use std::rc::Rc;
use std::time::Duration;

use chrono::{DateTime, FixedOffset};
use eui48::MacAddress;
use fallible_iterator::{FallibleIterator, IntoFallibleIterator};

use super::super::common::{
    DeletionWaiter, IntoVerified, NetworkRef, PortRef, Refresh, ResourceIterator, ResourceQuery,
    SecurityGroupRef, SubnetRef,
};
use super::super::session::Session;
use super::super::utils::Query;
use super::super::{Error, Result, Sort};
use super::{api, protocol, Network, Subnet};

/// A query to port list.
#[derive(Clone, Debug)]
pub struct PortQuery {
    session: Rc<Session>,
    query: Query,
    can_paginate: bool,
    network: Option<NetworkRef>,
}

/// A fixed IP address of a port.
#[derive(Clone, Debug)]
pub struct PortIpAddress {
    session: Rc<Session>,
    /// IP address.
    pub ip_address: net::IpAddr,
    /// ID of the subnet the address belongs to.
    pub subnet_id: String,
}

/// Structure representing a port - a virtual NIC.
#[derive(Clone, Debug)]
pub struct Port {
    session: Rc<Session>,
    inner: protocol::Port,
    fixed_ips: Vec<PortIpAddress>,
    dirty: HashSet<&'static str>,
}

/// A request of a fixed IP address.
#[derive(Clone, Debug)]
pub enum PortIpRequest {
    /// Request this IP from any subnet.
    IpAddress(net::IpAddr),
    /// Request any IP from the given subnet.
    AnyIpFromSubnet(SubnetRef),
    /// Request this IP from the given subnet.
    IpFromSubnet(net::IpAddr, SubnetRef),
}

/// A request to create a port
#[derive(Clone, Debug)]
pub struct NewPort {
    session: Rc<Session>,
    inner: protocol::Port,
    network: NetworkRef,
    fixed_ips: Vec<PortIpRequest>,
}

fn convert_fixed_ips(session: &Rc<Session>, inner: &mut protocol::Port) -> Vec<PortIpAddress> {
    let mut fixed_ips = Vec::new();
    mem::swap(&mut inner.fixed_ips, &mut fixed_ips);
    fixed_ips
        .into_iter()
        .map(|ip| PortIpAddress {
            session: session.clone(),
            ip_address: ip.ip_address,
            subnet_id: ip.subnet_id,
        })
        .collect()
}

impl Port {
    /// Load a Port object.
    pub(crate) fn new(session: Rc<Session>, mut inner: protocol::Port) -> Port {
        let fixed_ips = convert_fixed_ips(&session, &mut inner);
        Port {
            session,
            inner,
            fixed_ips,
            dirty: HashSet::new(),
        }
    }

    /// Load a Port object.
    pub(crate) fn load<Id: AsRef<str>>(session: Rc<Session>, id: Id) -> Result<Port> {
        let inner = api::get_port(&session, id)?;
        Ok(Port::new(session, inner))
    }

    transparent_property! {
        #[doc = "The administrative state of the port."]
        admin_state_up: bool
    }

    update_field! {
        #[doc = "Update the administrative state."]
        set_admin_state_up, with_admin_state_up -> admin_state_up: bool
    }

    /// Whether the `device_owner` is a Compute server.
    pub fn attached_to_server(&self) -> bool {
        match self.inner.device_owner {
            Some(ref x) => x.starts_with("compute:"),
            None => false,
        }
    }

    transparent_property! {
        #[doc = "Creation data and time (if available)."]
        created_at: Option<DateTime<FixedOffset>>
    }

    transparent_property! {
        #[doc = "Port description."]
        description: ref Option<String>
    }

    update_field! {
        #[doc = "Update the description."]
        set_description, with_description -> description: optional String
    }

    transparent_property! {
        #[doc = "ID of object (server, router, etc) to which this port is attached."]
        device_id: ref Option<String>
    }

    update_field! {
        #[doc = "Update the device ID."]
        set_device_id, with_device_id -> device_id: optional String
    }

    transparent_property! {
        #[doc = "Type of object to which this port is attached."]
        device_owner: ref Option<String>
    }

    update_field! {
        #[doc = "Update the device owner."]
        set_device_owner, with_device_owner -> device_owner: optional String
    }

    transparent_property! {
        #[doc = "DNS domain for the port (if available)."]
        dns_domain: ref Option<String>
    }

    update_field! {
        #[doc = "Update the DNS domain."]
        set_dns_domain, with_dns_domain -> dns_domain: optional String
    }

    transparent_property! {
        #[doc = "DNS name for the port (if available)."]
        dns_name: ref Option<String>
    }

    update_field! {
        #[doc = "Update the DNS name."]
        set_dns_name, with_dns_name -> dns_name: optional String
    }

    transparent_property! {
        #[doc = "DHCP options configured for this port."]
        extra_dhcp_opts: ref Vec<protocol::PortExtraDhcpOption>
    }

    /// Mutable access to DHCP options.
    #[allow(unused_results)]
    pub fn extra_dhcp_opts_mut(&mut self) -> &mut Vec<protocol::PortExtraDhcpOption> {
        self.dirty.insert("extra_dhcp_opts");
        &mut self.inner.extra_dhcp_opts
    }

    update_field! {
        #[doc = "Update the DHCP options."]
        set_extra_dhcp_opts, with_extra_dhcp_opts -> extra_dhcp_opts: Vec<protocol::PortExtraDhcpOption>
    }

    /// Fixed IP addresses of the port.
    pub fn fixed_ips(&self) -> &Vec<PortIpAddress> {
        &self.fixed_ips
    }

    // TODO(dtantsur): updating fixed IPs with validation

    transparent_property! {
        #[doc = "MAC address of the port."]
        mac_address: MacAddress
    }

    update_field! {
        #[doc = "Update the MAC address (admin-only)."]
        set_mac_address, with_mac_address -> mac_address: MacAddress
    }

    transparent_property! {
        #[doc = "Unique ID."]
        id: ref String
    }

    transparent_property! {
        #[doc = "Port name."]
        name: ref Option<String>
    }

    update_field! {
        #[doc = "Update the port name."]
        set_name, with_name -> name: optional String
    }

    /// Get network associated with this port.
    pub fn network(&self) -> Result<Network> {
        Network::load(self.session.clone(), &self.inner.network_id)
    }

    transparent_property! {
        #[doc = "ID of the network this port belongs to."]
        network_id: ref String
    }

    transparent_property! {
        #[doc = "Port status."]
        status: protocol::NetworkStatus
    }

    transparent_property! {
        #[doc = "Last update data and time (if available)."]
        updated_at: Option<DateTime<FixedOffset>>
    }

    /// Delete the port.
    pub fn delete(self) -> Result<DeletionWaiter<Port>> {
        api::delete_port(&self.session, &self.inner.id)?;
        Ok(DeletionWaiter::new(
            self,
            Duration::new(60, 0),
            Duration::new(1, 0),
        ))
    }

    /// Whether the port is modified.
    pub fn is_dirty(&self) -> bool {
        !self.dirty.is_empty()
    }

    /// Save the changes to the port.
    pub fn save(&mut self) -> Result<()> {
        let mut update = protocol::PortUpdate::default();
        save_fields! {
            self -> update: admin_state_up extra_dhcp_opts mac_address
        };
        save_option_fields! {
            self -> update: description device_id device_owner dns_domain
                dns_name name
        };
        let mut inner = api::update_port(&self.session, self.id(), update)?;
        self.fixed_ips = convert_fixed_ips(&self.session, &mut inner);
        self.dirty.clear();
        self.inner = inner;
        Ok(())
    }
}

impl Refresh for Port {
    /// Refresh the port.
    fn refresh(&mut self) -> Result<()> {
        self.inner = api::get_port_by_id(&self.session, &self.inner.id)?;
        self.fixed_ips = convert_fixed_ips(&self.session, &mut self.inner);
        self.dirty.clear();
        Ok(())
    }
}

impl PortIpAddress {
    /// Get subnet to which this IP address belongs.
    pub fn subnet(&self) -> Result<Subnet> {
        Subnet::load(self.session.clone(), self.subnet_id.clone())
    }
}

impl PortQuery {
    pub(crate) fn new(session: Rc<Session>) -> PortQuery {
        PortQuery {
            session,
            query: Query::new(),
            can_paginate: true,
            network: None,
        }
    }

    /// Add marker to the request.
    ///
    /// Using this disables automatic pagination.
    pub fn with_marker<T: Into<String>>(mut self, marker: T) -> Self {
        self.can_paginate = false;
        self.query.push_str("marker", marker);
        self
    }

    /// Add limit to the request.
    ///
    /// Using this disables automatic pagination.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.can_paginate = false;
        self.query.push("limit", limit);
        self
    }

    /// Add sorting to the request.
    pub fn sort_by(mut self, sort: Sort<protocol::PortSortKey>) -> Self {
        let (field, direction) = sort.into();
        self.query.push_str("sort_key", field);
        self.query.push("sort_dir", direction);
        self
    }

    query_filter! {
        #[doc = "Filter by administrative state."]
        set_admin_state_up, with_admin_state_up -> admin_state_up: bool
    }

    query_filter! {
        #[doc = "Filter by description."]
        set_description, with_description -> description
    }

    query_filter! {
        #[doc = "Filter by the ID of the object attached to the port."]
        set_device_id, with_device_id -> device_id
    }

    query_filter! {
        #[doc = "Filter by the ID of the object attached to the port."]
        set_device_owner, with_device_owner -> device_owner
    }

    query_filter! {
        #[doc = "Filter by MAC address."]
        set_mac_address, with_mac_address -> mac_address
    }

    query_filter! {
        #[doc = "Filter by port name."]
        set_name, with_name -> name
    }

    /// Filter by network.
    pub fn set_network<N: Into<NetworkRef>>(&mut self, value: N) {
        self.network = Some(value.into());
    }

    /// Filter by network.
    pub fn with_network<N: Into<NetworkRef>>(mut self, value: N) -> Self {
        self.set_network(value);
        self
    }

    query_filter! {
        #[doc = "Filter by status."]
        set_status, with_status -> status: protocol::NetworkStatus
    }

    /// Convert this query into an iterator executing the request.
    ///
    /// Returns a `FallibleIterator`, which is an iterator with each `next`
    /// call returning a `Result`.
    ///
    /// Note that no requests are done until you start iterating.
    pub fn into_iter(self) -> ResourceIterator<PortQuery> {
        debug!("Fetching ports with {:?}", self.query);
        ResourceIterator::new(self)
    }

    /// Execute this request and return all results.
    ///
    /// A convenience shortcut for `self.into_iter().collect()`.
    pub fn all(self) -> Result<Vec<Port>> {
        self.into_iter().collect()
    }

    /// Return one and exactly one result.
    ///
    /// Fails with `ResourceNotFound` if the query produces no results and
    /// with `TooManyItems` if the query produces more than one result.
    pub fn one(mut self) -> Result<Port> {
        debug!("Fetching one port with {:?}", self.query);
        if self.can_paginate {
            // We need only one result. We fetch maximum two to be able
            // to check if the query yieled more than one result.
            self.query.push("limit", 2);
        }

        self.into_iter().one()
    }
}

impl ResourceQuery for PortQuery {
    type Item = Port;

    const DEFAULT_LIMIT: usize = 50;

    fn can_paginate(&self) -> Result<bool> {
        Ok(self.can_paginate)
    }

    fn extract_marker(&self, resource: &Self::Item) -> String {
        resource.id().clone()
    }

    fn fetch_chunk(&self, limit: Option<usize>, marker: Option<String>) -> Result<Vec<Self::Item>> {
        let query = self.query.with_marker_and_limit(limit, marker);
        Ok(api::list_ports(&self.session, &query)?
            .into_iter()
            .map(|item| Port::new(self.session.clone(), item))
            .collect())
    }

    fn validate(&mut self) -> Result<()> {
        if let Some(network) = self.network.take() {
            let verified = network.into_verified(&self.session)?;
            self.query.push_str("network_id", verified);
        }
        Ok(())
    }
}

impl NewPort {
    /// Start creating a port.
    pub(crate) fn new(session: Rc<Session>, network: NetworkRef) -> NewPort {
        NewPort {
            session,
            inner: protocol::Port {
                admin_state_up: true,
                allowed_address_pairs: Vec::new(),
                created_at: None,
                description: None,
                device_id: None,
                device_owner: None,
                dns_domain: None,
                dns_name: None,
                extra_dhcp_opts: Vec::new(),
                fixed_ips: Vec::new(),
                id: String::new(),
                mac_address: Default::default(),
                name: None,
                // Will be replaced in create()
                network_id: String::new(),
                project_id: None,
                security_groups: Vec::new(),
                // Dummy value, not used when serializing
                status: protocol::NetworkStatus::Active,
                updated_at: None,
            },
            network,
            fixed_ips: Vec::new(),
        }
    }

    /// Request creation of the port.
    pub fn create(mut self) -> Result<Port> {
        self.inner.network_id = self.network.into_verified(&self.session)?.into();
        for request in self.fixed_ips {
            self.inner.fixed_ips.push(match request {
                PortIpRequest::IpAddress(ip) => protocol::FixedIp {
                    ip_address: ip,
                    subnet_id: Default::default(),
                },
                PortIpRequest::AnyIpFromSubnet(subnet) => protocol::FixedIp {
                    ip_address: net::IpAddr::V4(net::Ipv4Addr::new(0, 0, 0, 0)),
                    subnet_id: subnet.into_verified(&self.session)?.into(),
                },
                PortIpRequest::IpFromSubnet(ip, subnet) => protocol::FixedIp {
                    ip_address: ip,
                    subnet_id: subnet.into_verified(&self.session)?.into(),
                },
            });
        }

        let port = api::create_port(&self.session, self.inner)?;
        Ok(Port::new(self.session, port))
    }

    creation_inner_field! {
        #[doc = "Set administrative status for the port."]
        set_admin_state_up, with_admin_state_up -> admin_state_up: bool
    }

    creation_inner_vec! {
        #[doc = "Set allowed addresses for the port."]
        add_allowed_address_pair, with_allowed_address_pair -> allowed_address_pairs: protocol::AllowedAddressPair
    }

    creation_inner_field! {
        #[doc = "Set description of the port."]
        set_description, with_description -> description: optional String
    }

    creation_inner_field! {
        #[doc = "Set device ID of the port."]
        set_device_id, with_device_id -> device_id: optional String
    }

    creation_inner_field! {
        #[doc = "Set device owner of the port."]
        set_device_owner, with_device_owner -> device_owner: optional String
    }

    creation_inner_field! {
        #[doc = "Set DNS domain for the port."]
        set_dns_domain, with_dns_domain -> dns_domain: optional String
    }

    creation_inner_field! {
        #[doc = "Set DNS name for the port."]
        set_dns_name, with_dns_name -> dns_name: optional String
    }

    /// Extra DHCP options to configure on the port.
    pub fn extra_dhcp_opts(&mut self) -> &mut Vec<protocol::PortExtraDhcpOption> {
        &mut self.inner.extra_dhcp_opts
    }

    creation_inner_field! {
        #[doc = "Set extra DHCP options to configure on the port."]
        set_extra_dhcp_opts, with_extra_dhcp_opts -> extra_dhcp_opts:
            Vec<protocol::PortExtraDhcpOption>
    }

    /// Add a new fixed IP to the request.
    pub fn add_fixed_ip(&mut self, request: PortIpRequest) {
        self.fixed_ips.push(request);
    }

    /// Add a new fixed IP to the request.
    pub fn with_fixed_ip(mut self, request: PortIpRequest) -> Self {
        self.add_fixed_ip(request);
        self
    }

    creation_inner_field! {
        #[doc = "Set MAC address for the port (generated otherwise)."]
        set_mac_address, with_mac_address -> mac_address: MacAddress
    }

    creation_inner_field! {
        #[doc = "Set a name for the port."]
        set_name, with_name -> name: optional String
    }

    creation_inner_vec! {
        #[doc = "Set security groups for the port."]
        add_security_group, with_security_group -> security_groups: into SecurityGroupRef
    }
}

impl IntoFallibleIterator for PortQuery {
    type Item = Port;

    type Error = Error;

    type IntoFallibleIter = ResourceIterator<PortQuery>;

    fn into_fallible_iter(self) -> Self::IntoFallibleIter {
        self.into_iter()
    }
}

impl From<Port> for PortRef {
    fn from(value: Port) -> PortRef {
        PortRef::new_verified(value.inner.id)
    }
}

#[cfg(feature = "network")]
impl IntoVerified for PortRef {
    /// Verify this reference and convert to an ID, if possible.
    fn into_verified(self, session: &Session) -> Result<PortRef> {
        Ok(if self.verified {
            self
        } else {
            PortRef::new_verified(api::get_port(session, &self.value)?.id)
        })
    }
}