openstack_sdk 0.22.5

OpenStack SDK
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
// 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`.

//! Create new cluster template.
//!
use derive_builder::Builder;
use http::{HeaderMap, HeaderName, HeaderValue};

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

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

#[derive(Debug, Deserialize, Clone, Serialize)]
pub enum Coe {
    #[serde(rename = "kubernetes")]
    Kubernetes,
}

/// A link representation.
#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
#[builder(setter(strip_option))]
pub struct Links<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[builder(default, setter(into))]
    pub(crate) created_at: Option<Cow<'a, str>>,

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

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

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

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

#[derive(Debug, Deserialize, Clone, Serialize)]
pub enum ServerType {
    #[serde(rename = "bm")]
    Bm,
    #[serde(rename = "vm")]
    Vm,
}

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

    #[builder(default, setter(into))]
    pub(crate) cluster_distro: Option<Cow<'a, str>>,

    /// Specify the Container Orchestration Engine to use. Supported COEs
    /// include `kubernetes`. If your environment has additional cluster
    /// drivers installed, refer to the cluster driver documentation for the
    /// new COE names.
    #[builder(default)]
    pub(crate) coe: Option<Coe>,

    #[builder(default, setter(into))]
    pub(crate) created_at: Option<Cow<'a, str>>,

    /// The DNS nameserver for the servers and containers in the cluster to
    /// use. This is configured in the private Neutron network for the cluster.
    /// The default is `8.8.8.8`.
    #[builder(default, setter(into))]
    pub(crate) dns_nameserver: Option<Cow<'a, str>>,

    /// The name of a driver to manage the storage for the images and the
    /// container’s writable layer. The default is `devicemapper`.
    #[builder(default, setter(into))]
    pub(crate) docker_storage_driver: Option<Cow<'a, str>>,

    /// The size in GB for the local storage on each server for the Docker
    /// daemon to cache the images and host the containers. Cinder volumes
    /// provide the storage. The default is 25 GB. For the `devicemapper`
    /// storage driver, the minimum value is 3GB. For the `overlay` storage
    /// driver, the minimum value is 1GB.
    #[builder(default, setter(into))]
    pub(crate) docker_volume_size: Option<i32>,

    #[builder(default, setter(into))]
    pub(crate) driver: Option<Cow<'a, str>>,

    /// The name or network ID of a Neutron network to provide connectivity to
    /// the external internet for the cluster. This network must be an external
    /// network, i.e. its attribute `router:external` must be `True`. The
    /// servers in the cluster will be connected to a private network and
    /// Magnum will create a router between this private network and the
    /// external network. This will allow the servers to download images,
    /// access discovery service, etc, and the containers to install packages,
    /// etc. In the opposite direction, floating IPs will be allocated from the
    /// external network to provide access from the external internet to
    /// servers and the container services hosted in the cluster.
    #[builder(default, setter(into))]
    pub(crate) external_network_id: Option<Cow<'a, str>>,

    /// The name or network ID of a Neutron network to provide connectivity to
    /// the internal network for the cluster.
    #[builder(default, setter(into))]
    pub(crate) fixed_network: Option<Cow<'a, str>>,

    /// Fixed subnet that are using to allocate network address for nodes in
    /// cluster.
    #[builder(default, setter(into))]
    pub(crate) fixed_subnet: Option<Cow<'a, str>>,

    /// The nova flavor ID or name for booting the node servers. The default is
    /// `m1.small`.
    #[builder(default, setter(into))]
    pub(crate) flavor_id: Option<Cow<'a, str>>,

    /// Whether enable or not using the floating IP of cloud provider. Some
    /// cloud providers used floating IP, some used public IP, thus Magnum
    /// provide this option for specifying the choice of using floating IP.
    #[builder(default, setter(into))]
    pub(crate) floating_ip_enabled: Option<Cow<'a, str>>,

    /// Indicates whether the ClusterTemplate is hidden or not, the default
    /// value is false.
    #[builder(default, setter(into))]
    pub(crate) hidden: Option<Cow<'a, str>>,

    /// The IP address for a proxy to use when direct http access from the
    /// servers to sites on the external internet is blocked. This may happen
    /// in certain countries or enterprises, and the proxy allows the servers
    /// and containers to access these sites. The format is a URL including a
    /// port number. The default is `None`.
    #[builder(default, setter(into))]
    pub(crate) http_proxy: Option<Cow<'a, str>>,

    /// The IP address for a proxy to use when direct https access from the
    /// servers to sites on the external internet is blocked. This may happen
    /// in certain countries or enterprises, and the proxy allows the servers
    /// and containers to access these sites. The format is a URL including a
    /// port number. The default is `None`.
    #[builder(default, setter(into))]
    pub(crate) https_proxy: Option<Cow<'a, str>>,

    /// The name or UUID of the base image in Glance to boot the servers for
    /// the cluster. The image must have the attribute `os_distro` defined as
    /// appropriate for the cluster driver.
    #[builder(setter(into))]
    pub(crate) image_id: Cow<'a, str>,

    /// The URL pointing to users’s own private insecure docker registry to
    /// deploy and run docker containers.
    #[builder(default, setter(into))]
    pub(crate) insecure_registry: Option<Cow<'a, str>>,

    /// The name of the SSH keypair to configure in the cluster servers for ssh
    /// access. Users will need the key to be able to ssh to the servers in the
    /// cluster. The login name is specific to the cluster driver, for example
    /// with fedora-atomic image, default login name is `fedora`.
    #[builder(default, setter(into))]
    pub(crate) keypair_id: Option<Cow<'a, str>>,

    /// Arbitrary labels in the form of `key=value` pairs. The accepted keys
    /// and valid values are defined in the cluster drivers. They are used as a
    /// way to pass additional parameters that are specific to a cluster
    /// driver.
    #[builder(default, private, setter(into, name = "_labels"))]
    pub(crate) labels: Option<BTreeMap<Cow<'a, str>, Cow<'a, str>>>,

    #[builder(default, setter(into))]
    pub(crate) links: Option<Vec<Links<'a>>>,

    /// The flavor of the master node for this cluster template.
    #[builder(default, setter(into))]
    pub(crate) master_flavor_id: Option<Cow<'a, str>>,

    /// Since multiple masters may exist in a cluster, a Neutron load balancer
    /// is created to provide the API endpoint for the cluster and to direct
    /// requests to the masters. In some cases, such as when the LBaaS service
    /// is not available, this option can be set to `false` to create a cluster
    /// without the load balancer. In this case, one of the masters will serve
    /// as the API endpoint. The default is `true`, i.e. to create the load
    /// balancer for the cluster.
    #[builder(default, setter(into))]
    pub(crate) master_lb_enabled: Option<Cow<'a, str>>,

    /// Name of the resource.
    #[builder(default, setter(into))]
    pub(crate) name: Option<Cow<'a, str>>,

    /// The name of a network driver for providing the networks for the
    /// containers. Note that this is different and separate from the Neutron
    /// network for the cluster. The operation and networking model are
    /// specific to the particular driver.
    #[builder(default, setter(into))]
    pub(crate) network_driver: Option<Cow<'a, str>>,

    /// When a proxy server is used, some sites should not go through the proxy
    /// and should be accessed normally. In this case, users can specify these
    /// sites as a comma separated list of IPs. The default is `None`.
    #[builder(default, setter(into))]
    pub(crate) no_proxy: Option<Cow<'a, str>>,

    #[builder(default, setter(into))]
    pub(crate) project_id: Option<Cow<'a, str>>,

    /// Access to a cluster template is normally limited to the admin, owner or
    /// users within the same tenant as the owners. Setting this flag makes the
    /// cluster template public and accessible by other users. The default is
    /// not public.
    #[builder(default, setter(into))]
    pub(crate) public: Option<Cow<'a, str>>,

    /// Docker images by default are pulled from the public Docker registry,
    /// but in some cases, users may want to use a private registry. This
    /// option provides an alternative registry based on the Registry V2:
    /// Magnum will create a local registry in the cluster backed by swift to
    /// host the images. The default is to use the public registry.
    #[builder(default, setter(into))]
    pub(crate) registry_enabled: Option<Cow<'a, str>>,

    /// The servers in the cluster can be `vm` or `baremetal`. This parameter
    /// selects the type of server to create for the cluster. The default is
    /// `vm`.
    #[builder(default)]
    pub(crate) server_type: Option<ServerType>,

    /// Administrator tags for the cluster template.
    #[builder(default, setter(into))]
    pub(crate) tags: Option<Cow<'a, str>>,

    /// Transport Layer Security (TLS) is normally enabled to secure the
    /// cluster. In some cases, users may want to disable TLS in the cluster,
    /// for instance during development or to troubleshoot certain problems.
    /// Specifying this parameter will disable TLS so that users can access the
    /// COE endpoints without a certificate. The default is TLS enabled.
    #[builder(default, setter(into))]
    pub(crate) tls_disabled: Option<Cow<'a, str>>,

    #[builder(default, setter(into))]
    pub(crate) updated_at: Option<Cow<'a, str>>,

    #[builder(default, setter(into))]
    pub(crate) user_id: Option<Cow<'a, str>>,

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

    /// The name of a volume driver for managing the persistent storage for the
    /// containers. The functionality supported are specific to the driver.
    #[builder(default, setter(into))]
    pub(crate) volume_driver: Option<Cow<'a, str>>,

    #[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> {
    /// Arbitrary labels in the form of `key=value` pairs. The accepted keys
    /// and valid values are defined in the cluster drivers. They are used as a
    /// way to pass additional parameters that are specific to a cluster
    /// driver.
    pub fn labels<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.labels
            .get_or_insert(None)
            .get_or_insert_with(BTreeMap::new)
            .extend(iter.map(|(k, v)| (k.into(), v.into())));
        self
    }

    /// Add a single header to the Clustertemplate.
    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
    where
        K: Into<HeaderName>,
        V: Into<HeaderValue>,
    {
        self._headers
            .get_or_insert(None)
            .get_or_insert_with(HeaderMap::new)
            .insert(header_name.into(), header_value.into());
        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 RestEndpoint for Request<'_> {
    fn method(&self) -> http::Method {
        http::Method::POST
    }

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

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

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

        if let Some(val) = &self.apiserver_port {
            params.push("apiserver_port", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.cluster_distro {
            params.push("cluster_distro", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.coe {
            params.push("coe", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.created_at {
            params.push("created_at", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.dns_nameserver {
            params.push("dns_nameserver", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.docker_storage_driver {
            params.push("docker_storage_driver", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.docker_volume_size {
            params.push("docker_volume_size", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.driver {
            params.push("driver", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.external_network_id {
            params.push("external_network_id", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.fixed_network {
            params.push("fixed_network", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.fixed_subnet {
            params.push("fixed_subnet", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.flavor_id {
            params.push("flavor_id", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.floating_ip_enabled {
            params.push("floating_ip_enabled", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.hidden {
            params.push("hidden", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.http_proxy {
            params.push("http_proxy", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.https_proxy {
            params.push("https_proxy", serde_json::to_value(val)?);
        }
        params.push("image_id", serde_json::to_value(&self.image_id)?);
        if let Some(val) = &self.insecure_registry {
            params.push("insecure_registry", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.keypair_id {
            params.push("keypair_id", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.labels {
            params.push("labels", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.links {
            params.push("links", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.master_flavor_id {
            params.push("master_flavor_id", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.master_lb_enabled {
            params.push("master_lb_enabled", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.name {
            params.push("name", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.network_driver {
            params.push("network_driver", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.no_proxy {
            params.push("no_proxy", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.project_id {
            params.push("project_id", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.public {
            params.push("public", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.registry_enabled {
            params.push("registry_enabled", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.server_type {
            params.push("server_type", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.tags {
            params.push("tags", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.tls_disabled {
            params.push("tls_disabled", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.updated_at {
            params.push("updated_at", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.user_id {
            params.push("user_id", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.uuid {
            params.push("uuid", serde_json::to_value(val)?);
        }
        if let Some(val) = &self.volume_driver {
            params.push("volume_driver", serde_json::to_value(val)?);
        }

        params.into_body()
    }

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

    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(1, 0))
    }
}

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

    #[test]
    fn test_service_type() {
        assert_eq!(
            Request::builder()
                .image_id("foo")
                .build()
                .unwrap()
                .service_type(),
            ServiceType::ContainerInfrastructureManagement
        );
    }

    #[test]
    fn test_response_key() {
        assert!(Request::builder()
            .image_id("foo")
            .build()
            .unwrap()
            .response_key()
            .is_none())
    }

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

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

        let endpoint = Request::builder().image_id("foo").build().unwrap();
        let _: serde_json::Value = endpoint.query(&client).unwrap();
        mock.assert();
    }

    #[cfg(feature = "sync")]
    #[test]
    fn endpoint_headers() {
        let server = MockServer::start();
        let client = FakeOpenStackClient::new(server.base_url());
        let mock = server.mock(|when, then| {
            when.method(httpmock::Method::POST)
                .path("/clustertemplates".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()
            .image_id("foo")
            .headers(
                [(
                    Some(HeaderName::from_static("foo")),
                    HeaderValue::from_static("bar"),
                )]
                .into_iter(),
            )
            .header(
                HeaderName::from_static("not_foo"),
                HeaderValue::from_static("not_bar"),
            )
            .build()
            .unwrap();
        let _: serde_json::Value = endpoint.query(&client).unwrap();
        mock.assert();
    }
}