Skip to main content

openstack_cli_dns/v2/zone/
create.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Create Zone command
19//!
20//! Wraps invoking of the `v2/zones` with `POST` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_cli_core::cli::CliArgs;
26use openstack_cli_core::error::OpenStackCliError;
27use openstack_cli_core::output::OutputProcessor;
28use openstack_sdk::AsyncOpenStack;
29
30use clap::ValueEnum;
31use openstack_cli_core::common::parse_key_val;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::dns::v2::zone::create;
34use openstack_types::dns::v2::zone::response;
35
36/// Create a zone
37#[derive(Args)]
38#[command(about = "Create Zone")]
39pub struct ZoneCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47
48    /// Key:Value pairs of information about this zone, and the pool the user
49    /// would like to place the zone in. This information can be used by the
50    /// scheduler to place zones on the correct pool.
51    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
52    attributes: Option<Vec<(String, String)>>,
53
54    /// Description for this zone
55    #[arg(help_heading = "Body parameters", long)]
56    description: Option<String>,
57
58    /// e-mail for the zone. Used in SOA records for the zone. Mandatory for
59    /// PRIMARY zones, forbidden for SECONDARY zones.
60    #[arg(help_heading = "Body parameters", long)]
61    email: Option<String>,
62
63    /// Mandatory for secondary zones. The servers to slave from to get DNS
64    /// information
65    ///
66    /// Parameter is an array, may be provided multiple times.
67    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
68    masters: Option<Vec<String>>,
69
70    /// DNS Name for the zone
71    #[arg(help_heading = "Body parameters", long)]
72    name: Option<String>,
73
74    /// TTL (Time to Live) for the zone.
75    #[arg(help_heading = "Body parameters", long)]
76    ttl: Option<i32>,
77
78    /// Type of zone. PRIMARY is controlled by Designate, SECONDARY zones are
79    /// slaved from another DNS Server. Defaults to PRIMARY
80    #[arg(help_heading = "Body parameters", long)]
81    _type: Option<Type>,
82}
83
84/// Query parameters
85#[derive(Args)]
86struct QueryParameters {}
87
88/// Path parameters
89#[derive(Args)]
90struct PathParameters {}
91
92#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
93enum Type {
94    Catalog,
95    Primary,
96    Secondary,
97}
98
99impl ZoneCommand {
100    /// Perform command action
101    pub async fn take_action<C: CliArgs>(
102        &self,
103        parsed_args: &C,
104        client: &mut AsyncOpenStack,
105    ) -> Result<(), OpenStackCliError> {
106        info!("Create Zone");
107
108        let op = OutputProcessor::from_args(parsed_args, Some("dns.zone"), Some("create"));
109        op.validate_args(parsed_args)?;
110
111        let mut ep_builder = create::Request::builder();
112
113        // Set body parameters
114        // Set Request.attributes data
115        if let Some(arg) = &self.attributes {
116            ep_builder.attributes(arg.iter().cloned());
117        }
118
119        // Set Request.description data
120        if let Some(arg) = &self.description {
121            ep_builder.description(arg);
122        }
123
124        // Set Request.email data
125        if let Some(arg) = &self.email {
126            ep_builder.email(arg);
127        }
128
129        // Set Request.masters data
130        if let Some(arg) = &self.masters {
131            ep_builder.masters(arg.iter().map(Into::into).collect::<Vec<_>>());
132        }
133
134        // Set Request.name data
135        if let Some(arg) = &self.name {
136            ep_builder.name(arg);
137        }
138
139        // Set Request.ttl data
140        if let Some(arg) = &self.ttl {
141            ep_builder.ttl(*arg);
142        }
143
144        // Set Request._type data
145        if let Some(arg) = &self._type {
146            let tmp = match arg {
147                Type::Catalog => create::Type::Catalog,
148                Type::Primary => create::Type::Primary,
149                Type::Secondary => create::Type::Secondary,
150            };
151            ep_builder._type(tmp);
152        }
153
154        let ep = ep_builder
155            .build()
156            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
157
158        let data: serde_json::Value = ep.query_async(client).await?;
159
160        op.output_single::<response::create::ZoneResponse>(data.clone())?;
161        // Show command specific hints
162        op.show_command_hint()?;
163        Ok(())
164    }
165}