openstack 0.6.0

OpenStack SDK for Rust
Documentation
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
// 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.

use std::env;
use std::future;
use std::net;
use std::sync::Once;

use futures::TryStreamExt;
use openstack::waiter::Waiter;

use openstack::Refresh;

static INIT: Once = Once::new();

async fn set_up() -> openstack::Cloud {
    INIT.call_once(|| {
        env_logger::init();
    });

    openstack::Cloud::from_env()
        .await
        .expect("Failed to create an identity provider from the environment")
}

#[tokio::test]
async fn test_port_create_update_delete() {
    let os = set_up().await;
    let network_id = env::var("RUST_OPENSTACK_NETWORK").expect("Missing RUST_OPENSTACK_NETWORK");

    let mut port = os
        .new_port(network_id.clone())
        .with_name("rust-openstack-integration")
        .create()
        .await
        .expect("Could not create port");
    assert_eq!(port.name().as_ref().unwrap(), "rust-openstack-integration");
    assert!(port.device_id().is_none());
    assert!(port.device_owner().is_none());
    assert!(port.dns_name().is_none());
    assert!(port.dns_domain().is_none());
    assert!(!port.fixed_ips().is_empty());
    assert!(!port.is_dirty());
    assert_eq!(port.status(), openstack::network::NetworkStatus::Down);

    port.set_name("rust-openstack-integration-2");
    port.extra_dhcp_opts_mut()
        .push(openstack::network::PortExtraDhcpOption::new(
            "bootfile-name",
            "pxelinux.0",
        ));
    assert!(port.is_dirty());

    port.save().await.expect("Cannot update port");
    assert_eq!(
        port.name().as_ref().unwrap(),
        "rust-openstack-integration-2"
    );
    assert_eq!(1, port.extra_dhcp_opts().len());
    assert!(!port.is_dirty());

    port.refresh().await.expect("Cannot refresh port");
    assert_eq!(
        port.name().as_ref().unwrap(),
        "rust-openstack-integration-2"
    );
    assert!(!port.is_dirty());

    port.set_name("rust-openstack-integration-3");
    port.refresh().await.expect("Cannot refresh port");
    assert_eq!(
        port.name().as_ref().unwrap(),
        "rust-openstack-integration-2"
    );
    assert!(!port.is_dirty());

    let mut port_found = os
        .find_ports()
        .with_network(network_id)
        .with_name("rust-openstack-integration-2")
        .one()
        .await
        .expect("Cannot find port by network");
    assert_eq!(
        port_found.name().as_ref().unwrap(),
        "rust-openstack-integration-2"
    );
    assert!(!port_found.is_dirty());

    port.delete()
        .await
        .expect("Cannot request port deletion")
        .wait()
        .await
        .expect("Port was not deleted");

    os.get_port("rust-openstack-integration-2")
        .await
        .err()
        .expect("Port is still present");

    port_found
        .refresh()
        .await
        .err()
        .expect("Refresh succeeds on deleted port");
}

#[tokio::test]
async fn test_network_create_delete_simple() {
    let os = set_up().await;

    let network = os
        .new_network()
        .create()
        .await
        .expect("Could not create network");
    assert!(network.admin_state_up());
    assert!(network.dns_domain().is_none());
    assert_eq!(network.external(), Some(false));
    assert!(!network.shared());
    assert!(network.name().is_none());
    assert_eq!(network.status(), openstack::network::NetworkStatus::Active);

    let cidr = ipnet::Ipv4Net::new(net::Ipv4Addr::new(192, 168, 1, 0), 24)
        .unwrap()
        .into();
    let mut subnet = os
        .new_subnet(network.clone(), cidr)
        .create()
        .await
        .expect("Could not create subnet");
    assert_eq!(subnet.cidr(), cidr);
    assert!(subnet.dhcp_enabled());
    assert!(subnet.dns_nameservers().is_empty());
    assert_eq!(subnet.ip_version(), openstack::network::IpVersion::V4);
    assert!(subnet.name().is_none());

    subnet.refresh().await.expect("Cannot refresh subnet");

    subnet
        .delete()
        .await
        .expect("Cannot request subnet deletion")
        .wait()
        .await
        .expect("Subnet was not deleted");

    network
        .delete()
        .await
        .expect("Cannot request network deletion")
        .wait()
        .await
        .expect("Network was not deleted");
}

#[tokio::test]
async fn test_subnet_update() {
    let os = set_up().await;

    let network = os
        .new_network()
        .create()
        .await
        .expect("Could not create network");
    let cidr = ipnet::Ipv4Net::new(net::Ipv4Addr::new(192, 168, 1, 0), 24)
        .unwrap()
        .into();
    let mut subnet = os
        .new_subnet(network.clone(), cidr)
        .create()
        .await
        .expect("Could not create subnet");
    assert!(!subnet.is_dirty());

    subnet.dns_nameservers_mut().push("1.2.3.4".to_string());
    assert!(subnet.is_dirty());
    subnet.set_name("unused name");
    subnet.set_description("unused description");

    subnet.refresh().await.expect("Cannot refresh subnet");

    assert!(!subnet.is_dirty());
    assert!(subnet.dns_nameservers().is_empty());
    assert!(subnet.name().is_none());

    subnet.dns_nameservers_mut().push("8.8.8.8".to_string());
    subnet.set_dhcp_enabled(false);
    subnet.set_name("rust-openstack-integration-new");
    subnet.set_description("some description");

    assert_eq!("8.8.8.8", subnet.dns_nameservers()[0]);
    assert!(!subnet.dhcp_enabled());
    assert_eq!(
        subnet.name().as_ref().unwrap(),
        "rust-openstack-integration-new"
    );

    subnet.save().await.expect("Could not save subnet");

    assert!(!subnet.is_dirty());
    assert_eq!("8.8.8.8", subnet.dns_nameservers()[0]);
    assert!(!subnet.dhcp_enabled());
    assert_eq!(
        subnet.name().as_ref().unwrap(),
        "rust-openstack-integration-new"
    );

    subnet.refresh().await.expect("Cannot refresh subnet");

    assert_eq!("8.8.8.8", subnet.dns_nameservers()[0]);
    assert!(!subnet.dhcp_enabled());
    assert_eq!(
        subnet.name().as_ref().unwrap(),
        "rust-openstack-integration-new"
    );

    subnet
        .delete()
        .await
        .expect("Cannot request subnet deletion")
        .wait()
        .await
        .expect("Subnet was not deleted");

    network
        .delete()
        .await
        .expect("Cannot request network deletion")
        .wait()
        .await
        .expect("Network was not deleted");
}

#[tokio::test]
async fn test_network_update() {
    let os = set_up().await;

    let mut network = os
        .new_network()
        .with_admin_state_up(false)
        .with_name("rust-openstack-integration-new")
        .with_mtu(1400)
        .with_description("New network for testing")
        .create()
        .await
        .expect("Could not create network");
    assert!(!network.is_dirty());

    network.set_admin_state_up(true);
    network.set_name("rust-openstack-integration-new2");
    network.set_mtu(1420);

    assert!(network.is_dirty());
    assert!(network.admin_state_up());
    assert_eq!(network.mtu(), Some(1420));
    assert_eq!(
        network.name().as_ref().unwrap(),
        "rust-openstack-integration-new2"
    );

    network.save().await.expect("Could not save network");

    assert!(!network.is_dirty());
    assert!(network.admin_state_up());
    assert_eq!(network.mtu(), Some(1420));
    assert_eq!(
        network.name().as_ref().unwrap(),
        "rust-openstack-integration-new2"
    );

    network.set_name("rust-openstack-integration-new3");
    network.set_mtu(42);
    assert!(network.is_dirty());

    network.refresh().await.expect("Could not refresh network");

    assert!(!network.is_dirty());
    assert!(network.admin_state_up());
    assert_eq!(network.mtu(), Some(1420));
    assert_eq!(
        network.name().as_ref().unwrap(),
        "rust-openstack-integration-new2"
    );

    network
        .delete()
        .await
        .expect("Cannot request network deletion")
        .wait()
        .await
        .expect("Network was not deleted");
}

#[tokio::test]
async fn test_network_create_delete_with_fields() {
    let os = set_up().await;

    let network = os
        .new_network()
        .with_admin_state_up(false)
        .with_name("rust-openstack-integration-new")
        .with_mtu(1400)
        .with_description("New network for testing")
        .create()
        .await
        .expect("Could not create network");
    assert!(!network.admin_state_up());
    assert!(network.dns_domain().is_none());
    assert_eq!(network.external(), Some(false));
    assert!(!network.shared());
    assert_eq!(
        network.name().as_ref().unwrap(),
        "rust-openstack-integration-new"
    );

    let cidr = ipnet::Ipv4Net::new(net::Ipv4Addr::new(192, 168, 1, 0), 24)
        .unwrap()
        .into();
    let subnet = os
        .new_subnet("rust-openstack-integration-new", cidr)
        .with_name("rust-openstack-integration-new")
        .with_dhcp_enabled(false)
        .with_dns_nameserver("8.8.8.8")
        .create()
        .await
        .expect("Could not create subnet");
    assert_eq!(subnet.cidr(), cidr);
    assert!(!subnet.dhcp_enabled());
    assert_eq!(subnet.dns_nameservers(), &vec!["8.8.8.8".to_string()]);
    assert_eq!(subnet.ip_version(), openstack::network::IpVersion::V4);
    assert_eq!(
        subnet.name().as_ref().unwrap(),
        "rust-openstack-integration-new"
    );

    let subnets = os
        .find_subnets()
        .with_network("rust-openstack-integration-new")
        .all()
        .await
        .expect("Cannot find subnets by network name");
    assert_eq!(subnets.len(), 1);
    assert_eq!(subnets[0].id(), subnet.id());
    assert_eq!(subnets[0].name(), subnet.name());

    subnet
        .delete()
        .await
        .expect("Cannot request subnet deletion")
        .wait()
        .await
        .expect("Subnet was not deleted");

    network
        .delete()
        .await
        .expect("Cannot request network deletion")
        .wait()
        .await
        .expect("Network was not deleted");
}

#[tokio::test]
async fn test_floating_ip_create_delete() {
    let os = set_up().await;
    let floating_network_id = env::var("RUST_OPENSTACK_FLOATING_NETWORK")
        .expect("Missing RUST_OPENSTACK_FLOATING_NETWORK");

    let mut floating_ip = os
        .new_floating_ip(floating_network_id)
        .create()
        .await
        .expect("Cannot create a floating IP");
    assert!(!floating_ip.is_associated());
    floating_ip.port().await.err().unwrap();

    let net = floating_ip
        .floating_network()
        .await
        .expect("Cannot find floating network");
    let floating_ip_found = {
        let stream = os
            .find_floating_ips()
            .with_floating_network(net.name().clone().expect("Floating network has no name"))
            .into_stream();

        futures::pin_mut!(stream);

        stream
            .try_filter(|ip| future::ready(ip.id() == floating_ip.id()))
            .try_next()
            .await
    }
    .expect("Failed to list floating IPs")
    .expect("Floating IP was not found");
    assert_eq!(floating_ip_found.id(), floating_ip.id());

    floating_ip
        .refresh()
        .await
        .expect("Cannot refresh a floating IP");

    floating_ip
        .delete()
        .await
        .expect("Cannot request floating IP deletion")
        .wait()
        .await
        .expect("Floating IP was not deleted");
}

#[tokio::test]
async fn test_router_create_delete_simple() {
    let os = set_up().await;

    let router = os
        .new_router()
        .create()
        .await
        .expect("Could not create router.");
    assert!(router.admin_state_up());
    assert!(router.availability_zone_hints().is_empty());
    assert!(router.availability_zones().is_empty());
    assert!(router.created_at().is_some());
    assert!(router.conntrack_helpers().is_empty());
    assert!(router.description().is_none());
    assert!(router.distributed().is_none());
    assert!(router.external_gateway().is_none());
    assert!(router.flavor_id().is_none());
    assert!(router.ha().is_none());
    assert!(router.name().is_none());
    assert!(router.revision_number().is_some());
    assert_eq!(*router.routes(), Some(vec![]));
    assert!(router.service_type_id().is_none());
    assert_eq!(router.status(), openstack::network::RouterStatus::Active);

    let network = os
        .new_network()
        .create()
        .await
        .expect("Could not create network");
    let cidr = ipnet::Ipv4Net::new(net::Ipv4Addr::new(192, 168, 1, 0), 24)
        .unwrap()
        .into();
    let mut subnet = os
        .new_subnet(network.clone(), cidr)
        .create()
        .await
        .expect("Could not create subnet");

    subnet.refresh().await.expect("Cannot refresh subnet");

    let _ = router.delete();

    subnet
        .delete()
        .await
        .expect("Cannot request subnet deletion")
        .wait()
        .await
        .expect("Subnet was not deleted");

    network
        .delete()
        .await
        .expect("Cannot request network deletion")
        .wait()
        .await
        .expect("Network was not deleted");
}

#[tokio::test]
async fn test_router_create_update_delete_with_fields() {
    let os = set_up().await;

    let mut router = os
        .new_router()
        .with_admin_state_up(false)
        .with_availability_zone_hints(vec![String::from("nova")])
        .with_description("rust openstack integration")
        .with_name("rust-openstack-integration")
        .create()
        .await
        .expect("Could not create router.");

    assert!(!router.admin_state_up());
    assert!(router.availability_zone_hints().is_empty());
    assert!(router.availability_zones().is_empty());
    assert!(router.created_at().is_some());
    assert!(router.conntrack_helpers().is_empty());
    assert_eq!(
        router.description(),
        &Some(String::from("rust openstack integration"))
    );
    assert!(router.distributed().is_none());
    assert!(router.external_gateway().is_none());
    assert!(router.flavor_id().is_none());
    assert!(router.ha().is_none());
    assert_eq!(
        router.name().as_ref().unwrap(),
        "rust-openstack-integration"
    );
    assert!(router.project_id().is_some());
    assert!(router.revision_number().is_some());
    assert_eq!(*router.routes(), Some(vec![]));
    assert!(router.service_type_id().is_none());
    assert_eq!(router.status(), openstack::network::RouterStatus::Active);

    let network = os
        .new_network()
        .with_admin_state_up(false)
        .with_name("rust-openstack-integration-new")
        .with_description("New network for testing")
        .create()
        .await
        .expect("Could not create network");

    let cidr = ipnet::Ipv4Net::new(net::Ipv4Addr::new(192, 168, 1, 0), 24)
        .unwrap()
        .into();
    let subnet = os
        .new_subnet("rust-openstack-integration-new", cidr)
        .with_name("rust-openstack-integration-new")
        .with_dhcp_enabled(false)
        .with_dns_nameserver("8.8.8.8")
        .create()
        .await
        .expect("Could not create subnet");

    let ports = os.find_ports().with_device_id(router.id()).all().await;
    assert_eq!(ports.unwrap().len(), 0);
    let _ = router.add_router_interface(Some(subnet.id()), None).await;
    let ports = os.find_ports().with_device_id(router.id()).all().await;
    assert_eq!(ports.unwrap().len(), 1);
    let _ = router
        .remove_router_interface(Some(subnet.id()), None)
        .await;
    let ports = os.find_ports().with_device_id(router.id()).all().await;
    assert_eq!(ports.unwrap().len(), 0);

    let port = os.new_port(network.id().as_ref()).create().await.unwrap();
    let _ = router.add_router_interface(None, Some(port.id())).await;
    let ports = os.find_ports().with_device_id(router.id()).all().await;
    assert_eq!(ports.unwrap().len(), 1);
    let _ = router.remove_router_interface(None, Some(port.id())).await;
    let ports = os.find_ports().with_device_id(router.id()).all().await;
    assert_eq!(ports.unwrap().len(), 0);

    let routers = os
        .find_routers()
        .with_name("rust-openstack-integration")
        .all()
        .await
        .expect("Cannot find routers by router name.");
    assert_eq!(routers.len(), 1);
    assert_eq!(routers[0].id(), router.id());
    assert_eq!(routers[0].name(), router.name());

    subnet
        .delete()
        .await
        .expect("Cannot request subnet deletion")
        .wait()
        .await
        .expect("Subnet was not deleted");

    network
        .delete()
        .await
        .expect("Cannot request network deletion")
        .wait()
        .await
        .expect("Network was not deleted");

    router
        .delete()
        .await
        .expect("Cannot request router deletetion.")
        .wait()
        .await
        .expect("Router was not deleted.");
}

#[tokio::test]
async fn test_router_update() {
    let os = set_up().await;

    let mut router = os
        .new_router()
        .with_admin_state_up(false)
        .with_name("rust-openstack-integration-new")
        .with_description("New router for testing")
        .create()
        .await
        .expect("Could not create router");
    assert!(!router.is_dirty());

    router.set_admin_state_up(true);
    router.set_name("rust-openstack-integration-new2");
    router.set_description("Updated router for testing.");

    assert!(router.is_dirty());
    assert!(router.admin_state_up());
    assert_eq!(
        router.name().as_ref().unwrap(),
        "rust-openstack-integration-new2"
    );
    assert_eq!(
        router.description().as_ref().unwrap(),
        "Updated router for testing."
    );

    router.save().await.expect("Could not save router");

    assert!(!router.is_dirty());
    assert!(router.admin_state_up());
    assert_eq!(
        router.name().as_ref().unwrap(),
        "rust-openstack-integration-new2"
    );
    assert_eq!(
        router.description().as_ref().unwrap(),
        "Updated router for testing."
    );

    router.set_name("rust-openstack-integration-new3");
    router.set_description("This will be reverted by a refresh.");
    assert!(router.is_dirty());

    router.refresh().await.expect("Could not refresh router.");

    assert!(!router.is_dirty());
    assert!(router.admin_state_up());
    assert_eq!(
        router.name().as_ref().unwrap(),
        "rust-openstack-integration-new2"
    );
    assert_eq!(
        router.description().as_ref().unwrap(),
        "Updated router for testing."
    );

    router
        .delete()
        .await
        .expect("Cannot request router deletion.")
        .wait()
        .await
        .expect("Router was not deleted.");
}