Skip to main content

openstack_cli_dns/v2/zone/recordset/
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 Recordset command
19//!
20//! Wraps invoking of the `v2/zones/{zone_id}/recordsets` 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_sdk::api::QueryAsync;
32use openstack_sdk::api::dns::v2::zone::find as find_zone;
33use openstack_sdk::api::dns::v2::zone::recordset::create;
34use openstack_sdk::api::find_by_name;
35use openstack_types::dns::v2::zone::recordset::response;
36use tracing::warn;
37
38/// Create a recordset in a zone
39#[derive(Args)]
40#[command(about = "Create Recordset")]
41pub struct RecordsetCommand {
42    /// Request Query parameters
43    #[command(flatten)]
44    query: QueryParameters,
45
46    /// Path parameters
47    #[command(flatten)]
48    path: PathParameters,
49
50    /// Description for this recordset
51    #[arg(help_heading = "Body parameters", long)]
52    description: Option<String>,
53
54    /// DNS Name for the recordset
55    #[arg(help_heading = "Body parameters", long)]
56    name: Option<String>,
57
58    /// A list of data for this recordset. Each item will be a separate record
59    /// in Designate These items should conform to the DNS spec for the record
60    /// type - e.g. A records must be IPv4 addresses, CNAME records must be a
61    /// hostname.
62    ///
63    /// Parameter is an array, may be provided multiple times.
64    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
65    records: Option<Vec<String>>,
66
67    /// TTL (Time to Live) for the recordset.
68    #[arg(help_heading = "Body parameters", long)]
69    ttl: Option<i32>,
70
71    /// They RRTYPE of the recordset.
72    #[arg(help_heading = "Body parameters", long)]
73    _type: Option<Type>,
74}
75
76/// Query parameters
77#[derive(Args)]
78struct QueryParameters {}
79
80/// Path parameters
81#[derive(Args)]
82struct PathParameters {
83    /// Zone resource for which the operation should be performed.
84    #[command(flatten)]
85    zone: ZoneInput,
86}
87
88/// Zone input select group
89#[derive(Args)]
90#[group(required = true, multiple = false)]
91struct ZoneInput {
92    /// Zone Name.
93    #[arg(long, help_heading = "Path parameters", value_name = "ZONE_NAME")]
94    zone_name: Option<String>,
95    /// Zone ID.
96    #[arg(long, help_heading = "Path parameters", value_name = "ZONE_ID")]
97    zone_id: Option<String>,
98}
99
100#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
101enum Type {
102    A,
103    Aaaa,
104    Caa,
105    Cert,
106    Cname,
107    Mx,
108    Naptr,
109    Ns,
110    Ptr,
111    Soa,
112    Spf,
113    Srv,
114    Sshfp,
115    Txt,
116}
117
118impl RecordsetCommand {
119    /// Perform command action
120    pub async fn take_action<C: CliArgs>(
121        &self,
122        parsed_args: &C,
123        client: &mut AsyncOpenStack,
124    ) -> Result<(), OpenStackCliError> {
125        info!("Create Recordset");
126
127        let op =
128            OutputProcessor::from_args(parsed_args, Some("dns.zone/recordset"), Some("create"));
129        op.validate_args(parsed_args)?;
130
131        let mut ep_builder = create::Request::builder();
132
133        // Process path parameter `zone_id`
134        if let Some(id) = &self.path.zone.zone_id {
135            // zone_id is passed. No need to lookup
136            ep_builder.zone_id(id);
137        } else if let Some(name) = &self.path.zone.zone_name {
138            // zone_name is passed. Need to lookup resource
139            let mut sub_find_builder = find_zone::Request::builder();
140            warn!(
141                "Querying zone by name (because of `--zone-name` parameter passed) may not be definite. This may fail in which case parameter `--zone-id` should be used instead."
142            );
143
144            sub_find_builder.id(name);
145            let find_ep = sub_find_builder
146                .build()
147                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
148            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
149            // Try to extract resource id
150            match find_data.get("id") {
151                Some(val) => match val.as_str() {
152                    Some(id_str) => {
153                        ep_builder.zone_id(id_str.to_owned());
154                    }
155                    None => {
156                        return Err(OpenStackCliError::ResourceAttributeNotString(
157                            serde_json::to_string(&val)?,
158                        ));
159                    }
160                },
161                None => {
162                    return Err(OpenStackCliError::ResourceAttributeMissing(
163                        "id".to_string(),
164                    ));
165                }
166            };
167        }
168
169        // Set body parameters
170        // Set Request.description data
171        if let Some(arg) = &self.description {
172            ep_builder.description(arg);
173        }
174
175        // Set Request.name data
176        if let Some(arg) = &self.name {
177            ep_builder.name(arg);
178        }
179
180        // Set Request.records data
181        if let Some(arg) = &self.records {
182            ep_builder.records(arg.iter().map(Into::into).collect::<Vec<_>>());
183        }
184
185        // Set Request.ttl data
186        if let Some(arg) = &self.ttl {
187            ep_builder.ttl(*arg);
188        }
189
190        // Set Request._type data
191        if let Some(arg) = &self._type {
192            let tmp = match arg {
193                Type::A => create::Type::A,
194                Type::Aaaa => create::Type::Aaaa,
195                Type::Caa => create::Type::Caa,
196                Type::Cert => create::Type::Cert,
197                Type::Cname => create::Type::Cname,
198                Type::Mx => create::Type::Mx,
199                Type::Naptr => create::Type::Naptr,
200                Type::Ns => create::Type::Ns,
201                Type::Ptr => create::Type::Ptr,
202                Type::Soa => create::Type::Soa,
203                Type::Spf => create::Type::Spf,
204                Type::Srv => create::Type::Srv,
205                Type::Sshfp => create::Type::Sshfp,
206                Type::Txt => create::Type::Txt,
207            };
208            ep_builder._type(tmp);
209        }
210
211        let ep = ep_builder
212            .build()
213            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
214
215        let data: serde_json::Value = ep.query_async(client).await?;
216
217        op.output_single::<response::create::RecordsetResponse>(data.clone())?;
218        // Show command specific hints
219        op.show_command_hint()?;
220        Ok(())
221    }
222}