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

//! Cloud API.

#[allow(unused_imports)]
use std::io;
use std::rc::Rc;

use osauth::sync::SyncSession;
use osauth::{AuthType, Session};

#[allow(unused_imports)]
use super::common::{ContainerRef, FlavorRef, NetworkRef};
#[cfg(feature = "compute")]
use super::compute::{
    Flavor, FlavorQuery, FlavorSummary, KeyPair, KeyPairQuery, NewKeyPair, NewServer, Server,
    ServerQuery, ServerSummary,
};
#[cfg(feature = "image")]
use super::image::{Image, ImageQuery};
#[cfg(feature = "network")]
use super::network::{
    FloatingIp, FloatingIpQuery, Network, NetworkQuery, NewFloatingIp, NewNetwork, NewPort,
    NewRouter, NewSubnet, Port, PortQuery, Router, RouterQuery, Subnet, SubnetQuery,
};
#[cfg(feature = "object-storage")]
use super::object_storage::{Container, ContainerQuery, NewObject, Object, ObjectQuery};
use super::{EndpointFilters, InterfaceType, Result};

/// OpenStack cloud API.
///
/// Provides high-level API for working with OpenStack clouds.
#[derive(Debug, Clone)]
pub struct Cloud {
    session: Rc<SyncSession>,
}

impl Cloud {
    /// Create a new cloud object with a given authentication plugin.
    ///
    /// See [`auth` module](auth/index.html) for details on how to authenticate
    /// against OpenStack clouds.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// fn cloud() -> openstack::Result<openstack::Cloud> {
    ///     let scope = openstack::auth::Scope::Project {
    ///         project: openstack::IdOrName::from_name("project1"),
    ///         domain: Some(openstack::IdOrName::from_name("Default")),
    ///     };
    ///     let auth = openstack::auth::Password::new(
    ///             "https://cloud.example.com",
    ///             "user1", "pa$$word", "Default")
    ///         .expect("Invalid authentication URL")
    ///         .with_scope(scope);
    ///     Ok(openstack::Cloud::new(auth))
    /// }
    /// ```
    ///
    /// # See Also
    ///
    /// * [from_config](#method.from_config) to create a Cloud from clouds.yaml
    /// * [from_env](#method.from_env) to create a Cloud from environment variables
    pub fn new<Auth: AuthType + 'static>(auth_type: Auth) -> Cloud {
        Cloud {
            session: Rc::new(SyncSession::new(Session::new(auth_type))),
        }
    }

    /// Create a new cloud object from a configuration file
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # fn cloud_from_config() -> openstack::Result<()> {
    /// let os = openstack::Cloud::from_config("cloud-1")?;
    /// # Ok(()) }
    /// ```
    pub fn from_config<S: AsRef<str>>(cloud_name: S) -> Result<Cloud> {
        Ok(Cloud {
            session: Rc::new(SyncSession::from_config(cloud_name)?),
        })
    }

    /// Create a new cloud object from environment variables.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # fn cloud_from_env() -> openstack::Result<()> {
    /// let os = openstack::Cloud::from_env()?;
    /// # Ok(()) }
    /// ```
    pub fn from_env() -> Result<Cloud> {
        Ok(Cloud {
            session: Rc::new(SyncSession::from_env()?),
        })
    }

    /// Endpoint filters for this cloud.
    #[inline]
    pub fn endpoint_filters(&self) -> &EndpointFilters {
        self.session.endpoint_filters()
    }

    /// Modify endpoint filters for this cloud.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// fn cloud_from_env() -> openstack::Result<openstack::Cloud> {
    ///     let mut cloud = openstack::Cloud::from_env()?;
    ///     {
    ///         let mut filters = cloud.endpoint_filters_mut();
    ///         filters.set_region("internal-1");
    ///         // Give priority to internal endpoints.
    ///         filters.set_interfaces(&[
    ///             openstack::InterfaceType::Internal,
    ///             openstack::InterfaceType::Public,
    ///         ][..])
    ///     }
    ///     Ok(cloud)
    /// }
    /// ```
    ///
    /// Removes cached endpoint information and detaches this object from a shared `Session`.
    pub fn endpoint_filters_mut(&mut self) -> &mut EndpointFilters {
        Rc::make_mut(&mut self.session).endpoint_filters_mut()
    }

    /// Convert this cloud into one using the given endpoint interface.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// fn cloud_from_env() -> openstack::Result<openstack::Cloud> {
    ///     openstack::Cloud::from_env()
    ///         .map(|os| os.with_endpoint_interface(openstack::InterfaceType::Internal))
    /// }
    /// ```
    ///
    /// Removes cached endpoint information and detaches this object from a shared `Session`.
    pub fn with_endpoint_interface(mut self, endpoint_interface: InterfaceType) -> Cloud {
        Rc::make_mut(&mut self.session).set_endpoint_interface(endpoint_interface);
        self
    }

    /// Convert this cloud into one using the given endpoint filters.
    ///
    /// Removes cached endpoint information and detaches this object from a shared `Session`.
    #[inline]
    pub fn with_endpoint_filters(mut self, endpoint_filters: EndpointFilters) -> Cloud {
        *self.endpoint_filters_mut() = endpoint_filters;
        self
    }

    /// Refresh this `Cloud` object (renew token, refetch service catalog, etc).
    pub fn refresh(&mut self) -> Result<()> {
        Rc::make_mut(&mut self.session).refresh()
    }

    /// Create a new container.
    ///
    /// If the container already exists, this call returns successfully.
    #[cfg(feature = "object-storage")]
    pub fn create_container<Id: AsRef<str>>(&self, name: Id) -> Result<Container> {
        Container::create(self.session.clone(), name)
    }

    /// Create a new object.
    #[cfg(feature = "object-storage")]
    pub fn create_object<C, Id, R>(&self, container: C, name: Id, body: R) -> Result<Object>
    where
        C: Into<ContainerRef>,
        Id: AsRef<str>,
        R: io::Read + Sync + Send + 'static,
    {
        Object::create(self.session.clone(), container, name, body)
    }

    /// Build a query against container list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "object-storage")]
    #[inline]
    pub fn find_containers(&self) -> ContainerQuery {
        ContainerQuery::new(self.session.clone())
    }

    /// Build a query against object list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "object-storage")]
    pub fn find_objects<C>(&self, container: C) -> ObjectQuery
    where
        C: Into<ContainerRef>,
    {
        ObjectQuery::new(self.session.clone(), container)
    }

    /// Build a query against flavor list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "compute")]
    pub fn find_flavors(&self) -> FlavorQuery {
        FlavorQuery::new(self.session.clone())
    }

    /// Build a query against floating IP list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "network")]
    pub fn find_floating_ips(&self) -> FloatingIpQuery {
        FloatingIpQuery::new(self.session.clone())
    }

    /// Build a query against image list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "image")]
    pub fn find_images(&self) -> ImageQuery {
        ImageQuery::new(self.session.clone())
    }

    /// Build a query against key pairs list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "compute")]
    pub fn find_keypairs(&self) -> KeyPairQuery {
        KeyPairQuery::new(self.session.clone())
    }

    /// Build a query against network list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "network")]
    pub fn find_networks(&self) -> NetworkQuery {
        NetworkQuery::new(self.session.clone())
    }

    /// Build a query against port list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "network")]
    pub fn find_ports(&self) -> PortQuery {
        PortQuery::new(self.session.clone())
    }

    /// Build a query against router list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "network")]
    pub fn find_routers(&self) -> RouterQuery {
        RouterQuery::new(self.session.clone())
    }

    /// Build a query against server list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    ///
    /// # Example
    ///
    /// Sorting servers by `access_ip_v4` and getting first 5 results:
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let sorting = openstack::compute::ServerSortKey::AccessIpv4;
    /// let server_list = os.find_servers()
    ///     .sort_by(openstack::Sort::Asc(sorting)).with_limit(5)
    ///     .all().expect("Unable to fetch servers");
    /// ```
    #[cfg(feature = "compute")]
    pub fn find_servers(&self) -> ServerQuery {
        ServerQuery::new(self.session.clone())
    }

    /// Build a query against subnet list.
    ///
    /// The returned object is a builder that should be used to construct
    /// the query.
    #[cfg(feature = "network")]
    pub fn find_subnets(&self) -> SubnetQuery {
        SubnetQuery::new(self.session.clone())
    }

    /// Get object container metadata by its name.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let ctr = os.get_container("www").expect("Unable to get a container");
    /// ```
    #[cfg(feature = "object-storage")]
    pub fn get_container<Id: AsRef<str>>(&self, name: Id) -> Result<Container> {
        Container::load(self.session.clone(), name)
    }

    /// Get object metadata by its name.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let obj = os.get_object("www", "/foo/bar").expect("Unable to get an object");
    /// ```
    #[cfg(feature = "object-storage")]
    pub fn get_object<C, Id>(&self, container: C, name: Id) -> Result<Object>
    where
        C: Into<ContainerRef>,
        Id: AsRef<str>,
    {
        Object::load(self.session.clone(), container, name)
    }

    /// Find a flavor by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_flavor("m1.medium").expect("Unable to get a flavor");
    /// ```
    #[cfg(feature = "compute")]
    pub fn get_flavor<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Flavor> {
        Flavor::load(self.session.clone(), id_or_name)
    }

    /// Find a floating IP by its ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_floating_ip("031e08c7-2ca7-4c0b-9923-030c8d946ba4")
    ///     .expect("Unable to get a floating IP");
    /// ```
    #[cfg(feature = "network")]
    pub fn get_floating_ip<Id: AsRef<str>>(&self, id: Id) -> Result<FloatingIp> {
        FloatingIp::load(self.session.clone(), id)
    }

    /// Find an image by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_image("centos7").expect("Unable to get a image");
    /// ```
    #[cfg(feature = "image")]
    pub fn get_image<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Image> {
        Image::new(self.session.clone(), id_or_name)
    }

    /// Find a key pair by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_keypair("default").expect("Unable to get a key pair");
    /// ```
    #[cfg(feature = "compute")]
    pub fn get_keypair<Id: AsRef<str>>(&self, name: Id) -> Result<KeyPair> {
        KeyPair::new(self.session.clone(), name)
    }

    /// Find an network by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_network("centos7").expect("Unable to get a network");
    /// ```
    #[cfg(feature = "network")]
    pub fn get_network<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Network> {
        Network::load(self.session.clone(), id_or_name)
    }

    /// Find an port by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_port("4d9c1710-fa02-49f9-8218-291024ef4140")
    ///     .expect("Unable to get a port");
    /// ```
    #[cfg(feature = "network")]
    pub fn get_port<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Port> {
        Port::load(self.session.clone(), id_or_name)
    }

    /// Find a router by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let router = os.get_router("router_name").expect("Unable to get a router");
    /// ```
    #[cfg(feature = "network")]
    pub fn get_router<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Router> {
        Router::load(self.session.clone(), id_or_name)
    }

    /// Find a server by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_server("8a1c355b-2e1e-440a-8aa8-f272df72bc32")
    ///     .expect("Unable to get a server");
    /// ```
    #[cfg(feature = "compute")]
    pub fn get_server<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Server> {
        Server::load(self.session.clone(), id_or_name)
    }

    /// Find an subnet by its name or ID.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server = os.get_subnet("private-subnet")
    ///     .expect("Unable to get a subnet");
    /// ```
    #[cfg(feature = "network")]
    pub fn get_subnet<Id: AsRef<str>>(&self, id_or_name: Id) -> Result<Subnet> {
        Subnet::load(self.session.clone(), id_or_name)
    }

    /// List all containers.
    ///
    /// This call can yield a lot of results, use the
    /// [find_containers](#method.find_containers) call to limit the number of
    /// containers to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_containers().expect("Unable to fetch containers");
    /// ```
    #[cfg(feature = "object-storage")]
    pub fn list_containers(&self) -> Result<Vec<Container>> {
        self.find_containers().all()
    }

    /// List all objects.
    ///
    /// This call can yield a lot of results, use the
    /// [find_objects](#method.find_objects) call to limit the number of
    /// objects to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_objects("www").expect("Unable to fetch objects");
    /// ```
    #[cfg(feature = "object-storage")]
    pub fn list_objects<C>(&self, container: C) -> Result<Vec<Object>>
    where
        C: Into<ContainerRef>,
    {
        self.find_objects(container).all()
    }

    /// List all flavors.
    ///
    /// This call can yield a lot of results, use the
    /// [find_flavors](#method.find_flavors) call to limit the number of
    /// flavors to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_flavors().expect("Unable to fetch flavors");
    /// ```
    #[cfg(feature = "compute")]
    pub fn list_flavors(&self) -> Result<Vec<FlavorSummary>> {
        self.find_flavors().all()
    }

    /// List all floating IPs
    ///
    /// This call can yield a lot of results, use the
    /// [find_floating_ips](#method.find_floating_ips) call to limit the number of
    /// networks to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_floating_ips().expect("Unable to fetch floating IPs");
    /// ```
    #[cfg(feature = "network")]
    pub fn list_floating_ips(&self) -> Result<Vec<FloatingIp>> {
        self.find_floating_ips().all()
    }

    /// List all images.
    ///
    /// This call can yield a lot of results, use the
    /// [find_images](#method.find_images) call to limit the number of
    /// images to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_images().expect("Unable to fetch images");
    /// ```
    #[cfg(feature = "image")]
    pub fn list_images(&self) -> Result<Vec<Image>> {
        self.find_images().all()
    }

    /// List all key pairs.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let result = os.list_keypairs().expect("Unable to fetch key pairs");
    /// ```
    #[cfg(feature = "compute")]
    pub fn list_keypairs(&self) -> Result<Vec<KeyPair>> {
        self.find_keypairs().all()
    }

    /// List all networks.
    ///
    /// This call can yield a lot of results, use the
    /// [find_networks](#method.find_networks) call to limit the number of
    /// networks to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_networks().expect("Unable to fetch networks");
    /// ```
    #[cfg(feature = "network")]
    pub fn list_networks(&self) -> Result<Vec<Network>> {
        self.find_networks().all()
    }

    /// List all ports.
    ///
    /// This call can yield a lot of results, use the
    /// [find_ports](#method.find_ports) call to limit the number of
    /// ports to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_ports().expect("Unable to fetch ports");
    /// ```
    #[cfg(feature = "network")]
    pub fn list_ports(&self) -> Result<Vec<Port>> {
        self.find_ports().all()
    }

    /// List all routers.
    ///
    /// This call can yield a lot of results, use the
    /// [find_routers](#method.find_routers) call to limit the number of
    /// routers to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let router_list = os.list_routers().expect("Unable to fetch routers");
    /// ```
    #[cfg(feature = "network")]
    pub fn list_routers(&self) -> Result<Vec<Router>> {
        self.find_routers().all()
    }

    /// List all servers.
    ///
    /// This call can yield a lot of results, use the
    /// [find_servers](#method.find_servers) call to limit the number of
    /// servers to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_servers().expect("Unable to fetch servers");
    /// ```
    #[cfg(feature = "compute")]
    pub fn list_servers(&self) -> Result<Vec<ServerSummary>> {
        self.find_servers().all()
    }

    /// List all subnets.
    ///
    /// This call can yield a lot of results, use the
    /// [find_subnets](#method.find_subnets) call to limit the number of
    /// subnets to receive.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openstack;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let server_list = os.list_subnets().expect("Unable to fetch subnets");
    /// ```
    #[cfg(feature = "network")]
    pub fn list_subnets(&self) -> Result<Vec<Subnet>> {
        self.find_subnets().all()
    }

    /// Prepare a new object for creation.
    ///
    /// This call returns a `NewObject` object, which is a builder
    /// to create object in object storage.
    #[cfg(feature = "object-storage")]
    pub fn new_object<C, O, B>(&self, container: C, object: O, body: B) -> NewObject<B>
    where
        C: Into<ContainerRef>,
        O: Into<String>,
        B: io::Read + Sync + Send + 'static,
    {
        NewObject::new(self.session.clone(), container.into(), object.into(), body)
    }

    /// Prepare a new floating IP for creation.
    ///
    /// This call returns a `NewFloatingIp` object, which is a builder
    /// to populate floating IP fields.
    #[cfg(feature = "network")]
    pub fn new_floating_ip<N>(&self, floating_network: N) -> NewFloatingIp
    where
        N: Into<NetworkRef>,
    {
        NewFloatingIp::new(self.session.clone(), floating_network.into())
    }

    /// Prepare a new key pair for creation.
    ///
    /// This call returns a `NewKeyPair` object, which is a builder to populate
    /// key pair fields.
    #[cfg(feature = "compute")]
    pub fn new_keypair<S>(&self, name: S) -> NewKeyPair
    where
        S: Into<String>,
    {
        NewKeyPair::new(self.session.clone(), name.into())
    }

    /// Prepare a new network for creation.
    ///
    /// This call returns a `NewNetwork` object, which is a builder to populate
    /// network fields.
    #[cfg(feature = "network")]
    pub fn new_network(&self) -> NewNetwork {
        NewNetwork::new(self.session.clone())
    }

    /// Prepare a new port for creation.
    ///
    /// This call returns a `NewPort` object, which is a builder to populate
    /// port fields.
    #[cfg(feature = "network")]
    pub fn new_port<N>(&self, network: N) -> NewPort
    where
        N: Into<NetworkRef>,
    {
        NewPort::new(self.session.clone(), network.into())
    }

    /// Prepare a new router for creation.
    ///
    /// This call returns a `NewRouter` object, which is a builder to populate
    /// router fields.
    #[cfg(feature = "network")]
    pub fn new_router(&self) -> NewRouter {
        NewRouter::new(self.session.clone())
    }

    /// Prepare a new server for creation.
    ///
    /// This call returns a `NewServer` object, which is a builder to populate
    /// server fields.
    #[cfg(feature = "compute")]
    pub fn new_server<S, F>(&self, name: S, flavor: F) -> NewServer
    where
        S: Into<String>,
        F: Into<FlavorRef>,
    {
        NewServer::new(self.session.clone(), name.into(), flavor.into())
    }

    /// Prepare a new subnet for creation.
    ///
    /// This call returns a `NewSubnet` object, which is a builder to populate
    /// subnet fields.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// extern crate ipnet;
    /// extern crate openstack;
    /// use std::net;
    ///
    /// let os = openstack::Cloud::from_env().expect("Unable to authenticate");
    /// let cidr = ipnet::Ipv4Net::new(net::Ipv4Addr::new(192, 168, 1, 0), 24)
    ///     .unwrap().into();
    /// let new_subnet = os.new_subnet("private-net", cidr)
    ///     .with_name("private-subnet")
    ///     .create().expect("Unable to create subnet");
    /// ```
    #[cfg(feature = "network")]
    pub fn new_subnet<N>(&self, network: N, cidr: ipnet::IpNet) -> NewSubnet
    where
        N: Into<NetworkRef>,
    {
        NewSubnet::new(self.session.clone(), network.into(), cidr)
    }
}

impl From<Session> for Cloud {
    fn from(value: Session) -> Cloud {
        Cloud {
            session: Rc::new(SyncSession::new(value)),
        }
    }
}

impl From<SyncSession> for Cloud {
    fn from(value: SyncSession) -> Cloud {
        Cloud {
            session: Rc::new(value),
        }
    }
}