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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
// 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.
//
// SPDX-License-Identifier: Apache-2.0
//
// WARNING: This file is automatically generated from OpenAPI schema using
// `openstack-codegenerator`.

//! Creates a server.
//!
//! The progress of this operation depends on the location of the requested
//! image, network I/O, host load, selected flavor, and other factors.
//!
//! To check the progress of the request, make a `GET /servers/{id}` request.
//! This call returns a progress attribute, which is a percentage value from 0
//! to 100.
//!
//! The `Location` header returns the full URL to the newly created server and
//! is available as a `self` and `bookmark` link in the server representation.
//!
//! When you create a server, the response shows only the server ID, its links,
//! and the admin password. You can get additional attributes through
//! subsequent `GET` requests on the server.
//!
//! Include the `block_device_mapping_v2` parameter in the create request body
//! to boot a server from a volume.
//!
//! Include the `key_name` parameter in the create request body to add a
//! keypair to the server when you create it. To create a keypair, make a
//! [create keypair](https://docs.openstack.org/api-ref/compute/#create-or-import-keypair)
//! request.
//!
//! **Preconditions**
//!
//! **Asynchronous postconditions**
//!
//! **Troubleshooting**
//!
//! Normal response codes: 202
//!
//! Error response codes: badRequest(400), unauthorized(401), forbidden(403),
//! itemNotFound(404), conflict(409)
//!
use derive_builder::Builder;
use http::{HeaderMap, HeaderName, HeaderValue};

use crate::api::rest_endpoint_prelude::*;

use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::borrow::Cow;
use std::collections::BTreeMap;

#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct Networks<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) fixed_ip: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) port: Option<Option<Cow<'a, str>>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) tag: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) uuid: Option<Cow<'a, str>>,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub enum NetworksStringEnum {
    #[serde(rename = "auto")]
    Auto,
    #[serde(rename = "none")]
    None,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(untagged)]
pub enum ServerNetworks<'a> {
    F1(Vec<Networks<'a>>),
    F2(NetworksStringEnum),
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub enum OsDcfDiskConfig {
    #[serde(rename = "AUTO")]
    Auto,
    #[serde(rename = "MANUAL")]
    Manual,
}

#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct BlockDeviceMapping<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) connection_info: Option<Cow<'a, str>>,

    /// Indicates whether a config drive enables metadata injection. The
    /// config_drive setting provides information about a drive that the
    /// instance can mount at boot time. The instance reads files from the
    /// drive to get information that is normally available through the
    /// metadata service. This metadata is different from the user data. Not
    /// all cloud providers enable the `config_drive`. Read more in the
    /// [OpenStack End User Guide](https://docs.openstack.org/nova/latest/user/config-drive.html).
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) delete_on_termination: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) device_name: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) no_device: Option<Value>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) snapshot_id: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) virtual_name: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) volume_id: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) volume_size: Option<i32>,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub enum SourceType {
    #[serde(rename = "blank")]
    Blank,
    #[serde(rename = "image")]
    Image,
    #[serde(rename = "snapshot")]
    Snapshot,
    #[serde(rename = "volume")]
    Volume,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub enum DestinationType {
    #[serde(rename = "local")]
    Local,
    #[serde(rename = "volume")]
    Volume,
}

#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct BlockDeviceMappingV2<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) boot_index: Option<Option<Cow<'a, str>>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) connection_info: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) delete_on_termination: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) destination_type: Option<DestinationType>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) device_name: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) device_type: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) disk_bus: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) guest_format: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) image_id: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) no_device: Option<Value>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) snapshot_id: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) source_type: Option<SourceType>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) tag: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) uuid: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) virtual_name: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) volume_id: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) volume_size: Option<i32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) volume_type: Option<Option<Cow<'a, str>>>,
}

#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct SecurityGroups<'a> {
    /// A target cell name. Schedule the server in a host in the cell
    /// specified. It is available when `TargetCellFilter` is available on
    /// cloud side that is cell v1 environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) name: Option<Cow<'a, str>>,
}

/// A `server` object.
///
#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct Server<'a> {
    /// IPv4 address that should be used to access this server.
    ///
    #[serde(rename = "accessIPv4", skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) access_ipv4: Option<Cow<'a, str>>,

    /// IPv6 address that should be used to access this server.
    ///
    #[serde(rename = "accessIPv6", skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) access_ipv6: Option<Cow<'a, str>>,

    /// The administrative password of the server. If you omit this parameter,
    /// the operation generates a new password.
    ///
    #[serde(rename = "adminPass", skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) admin_pass: Option<Cow<'a, str>>,

    /// A target cell name. Schedule the server in a host in the cell
    /// specified. It is available when `TargetCellFilter` is available on
    /// cloud side that is cell v1 environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) availability_zone: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) block_device_mapping: Option<Vec<BlockDeviceMapping<'a>>>,

    /// Enables fine grained control of the block device mapping for an
    /// instance. This is typically used for booting servers from volumes. An
    /// example format would look as follows:
    ///
    /// > ```text
    /// > "block_device_mapping_v2": [{
    /// >     "boot_index": "0",
    /// >     "uuid": "ac408821-c95a-448f-9292-73986c790911",
    /// >     "source_type": "image",
    /// >     "volume_size": "25",
    /// >     "destination_type": "volume",
    /// >     "delete_on_termination": true,
    /// >     "tag": "disk1",
    /// >     "disk_bus": "scsi"}]
    /// >
    /// > ```
    ///
    /// In microversion 2.32, `tag` is an optional string attribute that can be
    /// used to assign a tag to the block device. This tag is then exposed to
    /// the guest in the metadata API and the config drive and is associated to
    /// hardware metadata for that block device, such as bus (ex: SCSI), bus
    /// address (ex: 1:0:2:0), and serial.
    ///
    /// A bug has caused the `tag` attribute to no longer be accepted starting
    /// with version 2.33. It has been restored in version 2.42.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) block_device_mapping_v2: Option<Vec<BlockDeviceMappingV2<'a>>>,

    /// Indicates whether a config drive enables metadata injection. The
    /// config_drive setting provides information about a drive that the
    /// instance can mount at boot time. The instance reads files from the
    /// drive to get information that is normally available through the
    /// metadata service. This metadata is different from the user data. Not
    /// all cloud providers enable the `config_drive`. Read more in the
    /// [OpenStack End User Guide](https://docs.openstack.org/nova/latest/user/config-drive.html).
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) config_drive: Option<bool>,

    /// A free form description of the server. Limited to 255 characters in
    /// length. Before microversion 2.19 this was set to the server name.
    ///
    /// **New in version 2.19**
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) description: Option<Option<Cow<'a, str>>>,

    /// The flavor reference, as an ID (including a UUID) or full URL, for the
    /// flavor for your server instance.
    ///
    #[serde(rename = "flavorRef")]
    #[builder(setter(into))]
    pub(crate) flavor_ref: Cow<'a, str>,

    /// The hostname of the hypervisor on which the server is to be created.
    /// The API will return 400 if no hypervisors are found with the given
    /// hostname. By default, it can be specified by administrators only.
    ///
    /// **New in version 2.74**
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) host: Option<Cow<'a, str>>,

    /// The hostname to configure for the instance in the metadata service.
    ///
    /// Starting with microversion 2.94, this can be a Fully Qualified Domain
    /// Name (FQDN) of up to 255 characters in length.
    ///
    /// Note
    ///
    /// This information is published via the metadata service and requires
    /// application such as `cloud-init` to propagate it through to the
    /// instance.
    ///
    /// **New in version 2.90**
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) hostname: Option<Cow<'a, str>>,

    /// The hostname of the hypervisor on which the server is to be created.
    /// The API will return 400 if no hypervisors are found with the given
    /// hostname. By default, it can be specified by administrators only.
    ///
    /// **New in version 2.74**
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) hypervisor_hostname: Option<Cow<'a, str>>,

    /// The UUID of the image to use for your server instance. This is not
    /// required in case of boot from volume. In all other cases it is required
    /// and must be a valid UUID otherwise API will return 400.
    ///
    #[serde(rename = "imageRef", skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) image_ref: Option<Cow<'a, str>>,

    /// A target cell name. Schedule the server in a host in the cell
    /// specified. It is available when `TargetCellFilter` is available on
    /// cloud side that is cell v1 environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) key_name: Option<Cow<'a, str>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) max_count: Option<i32>,

    /// Metadata key and value pairs. The maximum size of the metadata key and
    /// value is 255 bytes each.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, private, setter(name = "_metadata"))]
    pub(crate) metadata: Option<BTreeMap<Cow<'a, str>, Cow<'a, str>>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) min_count: Option<i32>,

    /// A target cell name. Schedule the server in a host in the cell
    /// specified. It is available when `TargetCellFilter` is available on
    /// cloud side that is cell v1 environment.
    ///
    #[serde()]
    #[builder(setter(into))]
    pub(crate) name: Cow<'a, str>,

    /// A list of `network` object. Required parameter when there are multiple
    /// networks defined for the tenant. When you do not specify the networks
    /// parameter, the server attaches to the only network created for the
    /// current tenant. Optionally, you can create one or more NICs on the
    /// server. To provision the server instance with a NIC for a network,
    /// specify the UUID of the network in the `uuid` attribute in a `networks`
    /// object. To provision the server instance with a NIC for an already
    /// existing port, specify the port-id in the `port` attribute in a
    /// `networks` object.
    ///
    /// If multiple networks are defined, the order in which they appear in the
    /// guest operating system will not necessarily reflect the order in which
    /// they are given in the server boot request. Guests should therefore not
    /// depend on device order to deduce any information about their network
    /// devices. Instead, device role tags should be used: introduced in 2.32,
    /// broken in 2.37, and re-introduced and fixed in 2.42, the `tag` is an
    /// optional, string attribute that can be used to assign a tag to a
    /// virtual network interface. This tag is then exposed to the guest in the
    /// metadata API and the config drive and is associated to hardware
    /// metadata for that network interface, such as bus (ex: PCI), bus address
    /// (ex: 0000:00:02.0), and MAC address.
    ///
    /// A bug has caused the `tag` attribute to no longer be accepted starting
    /// with version 2.37. Therefore, network interfaces could only be tagged
    /// in versions 2.32 to 2.36 inclusively. Version 2.42 has restored the
    /// `tag` attribute.
    ///
    /// Starting with microversion 2.37, this field is required and the special
    /// string values *auto* and *none* can be specified for networks. *auto*
    /// tells the Compute service to use a network that is available to the
    /// project, if one exists. If one does not exist, the Compute service will
    /// attempt to automatically allocate a network for the project (if
    /// possible). *none* tells the Compute service to not allocate a network
    /// for the instance. The *auto* and *none* values cannot be used with any
    /// other network values, including other network uuids, ports, fixed IPs
    /// or device tags. These are requested as strings for the networks value,
    /// not in a list. See the associated example.
    ///
    #[serde()]
    #[builder(setter(into))]
    pub(crate) networks: ServerNetworks<'a>,

    /// Controls how the API partitions the disk when you create, rebuild, or
    /// resize servers. A server inherits the `OS-DCF:diskConfig` value from
    /// the image from which it was created, and an image inherits the
    /// `OS-DCF:diskConfig` value from the server from which it was created. To
    /// override the inherited setting, you can include this attribute in the
    /// request body of a server create, rebuild, or resize request. If the
    /// `OS-DCF:diskConfig` value for an image is `MANUAL`, you cannot create a
    /// server from that image and set its `OS-DCF:diskConfig` value to `AUTO`.
    /// A valid value is:
    ///
    /// - `AUTO`. The API builds the server with a single partition the size of
    ///   the target flavor disk. The API automatically adjusts the file system
    ///   to fit the entire partition.
    /// - `MANUAL`. The API builds the server by using whatever partition
    ///   scheme and file system is in the source image. If the target flavor
    ///   disk is larger, the API does not partition the remaining disk space.
    ///
    #[serde(rename = "OS-DCF:diskConfig", skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) os_dcf_disk_config: Option<OsDcfDiskConfig>,

    /// Indicates whether a config drive enables metadata injection. The
    /// config_drive setting provides information about a drive that the
    /// instance can mount at boot time. The instance reads files from the
    /// drive to get information that is normally available through the
    /// metadata service. This metadata is different from the user data. Not
    /// all cloud providers enable the `config_drive`. Read more in the
    /// [OpenStack End User Guide](https://docs.openstack.org/nova/latest/user/config-drive.html).
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default)]
    pub(crate) return_reservation_id: Option<bool>,

    /// One or more security groups. Specify the name of the security group in
    /// the `name` attribute. If you omit this attribute, the API creates the
    /// server in the `default` security group. Requested security groups are
    /// not applied to pre-existing ports.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) security_groups: Option<Vec<SecurityGroups<'a>>>,

    /// A list of tags. Tags have the following restrictions:
    ///
    /// - Tag is a Unicode bytestring no longer than 60 characters.
    /// - Tag is a non-empty string.
    /// - ‘/’ is not allowed to be in a tag name
    /// - Comma is not allowed to be in a tag name in order to simplify
    ///   requests that specify lists of tags
    /// - All other characters are allowed to be in a tag name
    /// - Each server can have up to 50 tags.
    ///
    /// **New in version 2.52**
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) tags: Option<Vec<Cow<'a, str>>>,

    /// A list of trusted certificate IDs, which are used during image
    /// signature verification to verify the signing certificate. The list is
    /// restricted to a maximum of 50 IDs. This parameter is optional in server
    /// create requests if allowed by policy, and is not supported for
    /// volume-backed instances.
    ///
    /// **New in version 2.63**
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) trusted_image_certificates: Option<Vec<Cow<'a, str>>>,

    /// Configuration information or scripts to use upon launch. Must be Base64
    /// encoded. Restricted to 65535 bytes.
    ///
    /// Note
    ///
    /// The `null` value allowed in Nova legacy v2 API, but due to the strict
    /// input validation, it isn’t allowed in Nova v2.1 API.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) user_data: Option<Cow<'a, str>>,
}

impl<'a> ServerBuilder<'a> {
    /// Metadata key and value pairs. The maximum size of the metadata key and
    /// value is 255 bytes each.
    ///
    pub fn metadata<I, K, V>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = (K, V)>,
        K: Into<Cow<'a, str>>,
        V: Into<Cow<'a, str>>,
    {
        self.metadata
            .get_or_insert(None)
            .get_or_insert_with(BTreeMap::new)
            .extend(iter.map(|(k, v)| (k.into(), v.into())));
        self
    }
}

/// The dictionary of data to send to the scheduler. Alternatively, you can
/// specify `OS-SCH-HNT:scheduler_hints` as the key in the request body.
///
/// Note
///
/// This is a top-level key in the request body, not part of the server portion
/// of the request body.
///
/// There are a few caveats with scheduler hints:
///
/// - The request validation schema is per hint. For example, some require a
///   single string value, and some accept a list of values.
/// - Hints are only used based on the cloud scheduler configuration, which
///   varies per deployment.
/// - Hints are pluggable per deployment, meaning that a cloud can have custom
///   hints which may not be available in another cloud.
///
/// For these reasons, it is important to consult each cloud’s user
/// documentation to know what is available for scheduler hints.
///
#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct OsSchedulerHints<'a> {
    /// Schedule the server on a host in the network specified with this
    /// parameter and a cidr (`os:scheduler_hints.cidr`). It is available when
    /// `SimpleCIDRAffinityFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) build_near_host_ip: Option<Cow<'a, str>>,

    /// Schedule the server on a host in the network specified with an IP
    /// address (`os:scheduler_hints:build_near_host_ip`) and this parameter.
    /// If `os:scheduler_hints:build_near_host_ip` is specified and this
    /// parameter is omitted, `/24` is used. It is available when
    /// `SimpleCIDRAffinityFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) cidr: Option<Cow<'a, str>>,

    /// A list of cell routes or a cell route (string). Schedule the server in
    /// a cell that is not specified. It is available when
    /// `DifferentCellFilter` is available on cloud side that is cell v1
    /// environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) different_cell: Option<Vec<Cow<'a, str>>>,

    /// A list of server UUIDs or a server UUID. Schedule the server on a
    /// different host from a set of servers. It is available when
    /// `DifferentHostFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) different_host: Option<Vec<Cow<'a, str>>>,

    /// The server group UUID. Schedule the server according to a policy of the
    /// server group (`anti-affinity`, `affinity`, `soft-anti-affinity` or
    /// `soft-affinity`). It is available when `ServerGroupAffinityFilter`,
    /// `ServerGroupAntiAffinityFilter`, `ServerGroupSoftAntiAffinityWeigher`,
    /// `ServerGroupSoftAffinityWeigher` are available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) group: Option<Cow<'a, str>>,

    /// Schedule the server by using a custom filter in JSON format. For
    /// example:
    ///
    /// ```text
    /// "query": "[\">=\",\"$free_ram_mb\",1024]"
    ///
    /// ```
    ///
    /// It is available when `JsonFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) query: Option<Value>,

    /// A list of server UUIDs or a server UUID. Schedule the server on the
    /// same host as another server in a set of servers. It is available when
    /// `SameHostFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) same_host: Option<Vec<Cow<'a, str>>>,

    /// A target cell name. Schedule the server in a host in the cell
    /// specified. It is available when `TargetCellFilter` is available on
    /// cloud side that is cell v1 environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) target_cell: Option<Cow<'a, str>>,

    #[builder(setter(name = "_properties"), default, private)]
    #[serde(flatten)]
    _properties: BTreeMap<Cow<'a, str>, Value>,
}

impl<'a> OsSchedulerHintsBuilder<'a> {
    pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = (K, V)>,
        K: Into<Cow<'a, str>>,
        V: Into<Value>,
    {
        self._properties
            .get_or_insert_with(BTreeMap::new)
            .extend(iter.map(|(k, v)| (k.into(), v.into())));
        self
    }
}

#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct OsSchHntSchedulerHints<'a> {
    /// Schedule the server on a host in the network specified with this
    /// parameter and a cidr (`os:scheduler_hints.cidr`). It is available when
    /// `SimpleCIDRAffinityFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) build_near_host_ip: Option<Cow<'a, str>>,

    /// Schedule the server on a host in the network specified with an IP
    /// address (`os:scheduler_hints:build_near_host_ip`) and this parameter.
    /// If `os:scheduler_hints:build_near_host_ip` is specified and this
    /// parameter is omitted, `/24` is used. It is available when
    /// `SimpleCIDRAffinityFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) cidr: Option<Cow<'a, str>>,

    /// A list of cell routes or a cell route (string). Schedule the server in
    /// a cell that is not specified. It is available when
    /// `DifferentCellFilter` is available on cloud side that is cell v1
    /// environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) different_cell: Option<Vec<Cow<'a, str>>>,

    /// A list of server UUIDs or a server UUID. Schedule the server on a
    /// different host from a set of servers. It is available when
    /// `DifferentHostFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) different_host: Option<Vec<Cow<'a, str>>>,

    /// The server group UUID. Schedule the server according to a policy of the
    /// server group (`anti-affinity`, `affinity`, `soft-anti-affinity` or
    /// `soft-affinity`). It is available when `ServerGroupAffinityFilter`,
    /// `ServerGroupAntiAffinityFilter`, `ServerGroupSoftAntiAffinityWeigher`,
    /// `ServerGroupSoftAffinityWeigher` are available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) group: Option<Cow<'a, str>>,

    /// Schedule the server by using a custom filter in JSON format. For
    /// example:
    ///
    /// ```text
    /// "query": "[\">=\",\"$free_ram_mb\",1024]"
    ///
    /// ```
    ///
    /// It is available when `JsonFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) query: Option<Value>,

    /// A list of server UUIDs or a server UUID. Schedule the server on the
    /// same host as another server in a set of servers. It is available when
    /// `SameHostFilter` is available on cloud side.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) same_host: Option<Vec<Cow<'a, str>>>,

    /// A target cell name. Schedule the server in a host in the cell
    /// specified. It is available when `TargetCellFilter` is available on
    /// cloud side that is cell v1 environment.
    ///
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) target_cell: Option<Cow<'a, str>>,

    #[builder(setter(name = "_properties"), default, private)]
    #[serde(flatten)]
    _properties: BTreeMap<Cow<'a, str>, Value>,
}

impl<'a> OsSchHntSchedulerHintsBuilder<'a> {
    pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = (K, V)>,
        K: Into<Cow<'a, str>>,
        V: Into<Value>,
    {
        self._properties
            .get_or_insert_with(BTreeMap::new)
            .extend(iter.map(|(k, v)| (k.into(), v.into())));
        self
    }
}

#[derive(Builder, Debug, Clone)]
#[builder(setter(strip_option))]
pub struct Request<'a> {
    #[builder(default, setter(into))]
    pub(crate) os_sch_hnt_scheduler_hints: Option<OsSchHntSchedulerHints<'a>>,

    /// The dictionary of data to send to the scheduler. Alternatively, you can
    /// specify `OS-SCH-HNT:scheduler_hints` as the key in the request body.
    ///
    /// Note
    ///
    /// This is a top-level key in the request body, not part of the server
    /// portion of the request body.
    ///
    /// There are a few caveats with scheduler hints:
    ///
    /// - The request validation schema is per hint. For example, some require
    ///   a single string value, and some accept a list of values.
    /// - Hints are only used based on the cloud scheduler configuration, which
    ///   varies per deployment.
    /// - Hints are pluggable per deployment, meaning that a cloud can have
    ///   custom hints which may not be available in another cloud.
    ///
    /// For these reasons, it is important to consult each cloud’s user
    /// documentation to know what is available for scheduler hints.
    ///
    #[builder(default, setter(into))]
    pub(crate) os_scheduler_hints: Option<OsSchedulerHints<'a>>,

    /// A `server` object.
    ///
    #[builder(setter(into))]
    pub(crate) server: Server<'a>,

    #[builder(setter(name = "_headers"), default, private)]
    _headers: Option<HeaderMap>,
}
impl<'a> Request<'a> {
    /// Create a builder for the endpoint.
    pub fn builder() -> RequestBuilder<'a> {
        RequestBuilder::default()
    }
}

impl<'a> RequestBuilder<'a> {
    /// Add a single header to the Server.
    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
where {
        self._headers
            .get_or_insert(None)
            .get_or_insert_with(HeaderMap::new)
            .insert(header_name, HeaderValue::from_static(header_value));
        self
    }

    /// Add multiple headers.
    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
    where
        I: Iterator<Item = T>,
        T: Into<(Option<HeaderName>, HeaderValue)>,
    {
        self._headers
            .get_or_insert(None)
            .get_or_insert_with(HeaderMap::new)
            .extend(iter.map(Into::into));
        self
    }
}

impl<'a> RestEndpoint for Request<'a> {
    fn method(&self) -> http::Method {
        http::Method::POST
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "servers".to_string().into()
    }

    fn parameters(&self) -> QueryParams {
        QueryParams::default()
    }

    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
        let mut params = JsonBodyParams::default();

        params.push("server", serde_json::to_value(&self.server)?);
        if let Some(val) = &self.os_scheduler_hints {
            params.push("os:scheduler_hints", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.os_sch_hnt_scheduler_hints {
            params.push("OS-SCH-HNT:scheduler_hints", serde_json::to_value(val)?);
        }

        params.into_body()
    }

    fn service_type(&self) -> ServiceType {
        ServiceType::Compute
    }

    fn response_key(&self) -> Option<Cow<'static, str>> {
        None
    }

    /// Returns headers to be set into the request
    fn request_headers(&self) -> Option<&HeaderMap> {
        self._headers.as_ref()
    }

    /// Returns required API version
    fn api_version(&self) -> Option<ApiVersion> {
        Some(ApiVersion::new(2, 94))
    }
}

#[cfg(test)]
mod tests {
    #![allow(unused_imports)]
    use super::*;
    #[cfg(feature = "sync")]
    use crate::api::Query;
    #[cfg(feature = "sync")]
    use crate::test::client::MockServerClient;
    use crate::types::ServiceType;
    use http::{HeaderName, HeaderValue};
    use serde_json::json;

    #[test]
    fn test_service_type() {
        assert_eq!(
            Request::builder()
                .server(
                    ServerBuilder::default()
                        .flavor_ref("foo")
                        .name("foo")
                        .networks(ServerNetworks::F1(Vec::from([NetworksBuilder::default()
                            .build()
                            .unwrap()])))
                        .build()
                        .unwrap()
                )
                .build()
                .unwrap()
                .service_type(),
            ServiceType::Compute
        );
    }

    #[test]
    fn test_response_key() {
        assert!(Request::builder()
            .server(
                ServerBuilder::default()
                    .flavor_ref("foo")
                    .name("foo")
                    .networks(ServerNetworks::F1(Vec::from([NetworksBuilder::default()
                        .build()
                        .unwrap()])))
                    .build()
                    .unwrap()
            )
            .build()
            .unwrap()
            .response_key()
            .is_none())
    }

    #[cfg(feature = "sync")]
    #[test]
    fn endpoint() {
        let client = MockServerClient::new();
        let mock = client.server.mock(|when, then| {
            when.method(httpmock::Method::POST)
                .path("/servers".to_string());

            then.status(200)
                .header("content-type", "application/json")
                .json_body(json!({ "dummy": {} }));
        });

        let endpoint = Request::builder()
            .server(
                ServerBuilder::default()
                    .flavor_ref("foo")
                    .name("foo")
                    .networks(ServerNetworks::F1(Vec::from([NetworksBuilder::default()
                        .build()
                        .unwrap()])))
                    .build()
                    .unwrap(),
            )
            .build()
            .unwrap();
        let _: serde_json::Value = endpoint.query(&client).unwrap();
        mock.assert();
    }

    #[cfg(feature = "sync")]
    #[test]
    fn endpoint_headers() {
        let client = MockServerClient::new();
        let mock = client.server.mock(|when, then| {
            when.method(httpmock::Method::POST)
                .path("/servers".to_string())
                .header("foo", "bar")
                .header("not_foo", "not_bar");
            then.status(200)
                .header("content-type", "application/json")
                .json_body(json!({ "dummy": {} }));
        });

        let endpoint = Request::builder()
            .server(
                ServerBuilder::default()
                    .flavor_ref("foo")
                    .name("foo")
                    .networks(ServerNetworks::F1(Vec::from([NetworksBuilder::default()
                        .build()
                        .unwrap()])))
                    .build()
                    .unwrap(),
            )
            .headers(
                [(
                    Some(HeaderName::from_static("foo")),
                    HeaderValue::from_static("bar"),
                )]
                .into_iter(),
            )
            .header("not_foo", "not_bar")
            .build()
            .unwrap();
        let _: serde_json::Value = endpoint.query(&client).unwrap();
        mock.assert();
    }
}