Skip to main content

openstack_cli_dns/v2/zone/recordset/
delete.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//! Delete Recordset command
19//!
20//! Wraps invoking of the `v2/zones/{zone_id}/recordsets/{recordset_id}` with `DELETE` 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 openstack_sdk::api::QueryAsync;
31use openstack_sdk::api::dns::v2::zone::find as find_zone;
32use openstack_sdk::api::dns::v2::zone::recordset::delete;
33use openstack_sdk::api::find_by_name;
34use tracing::warn;
35
36/// Delete a recordset
37#[derive(Args)]
38#[command(about = "Delete a Recordset")]
39pub struct RecordsetCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47}
48
49/// Query parameters
50#[derive(Args)]
51struct QueryParameters {}
52
53/// Path parameters
54#[derive(Args)]
55struct PathParameters {
56    /// recordset_id parameter for
57    /// /v2/zones/{zone_id}/recordsets/{recordset_id} API
58    #[arg(
59        help_heading = "Path parameters",
60        id = "path_param_id",
61        value_name = "ID"
62    )]
63    id: String,
64
65    /// Zone resource for which the operation should be performed.
66    #[command(flatten)]
67    zone: ZoneInput,
68}
69
70/// Zone input select group
71#[derive(Args)]
72#[group(required = true, multiple = false)]
73struct ZoneInput {
74    /// Zone Name.
75    #[arg(long, help_heading = "Path parameters", value_name = "ZONE_NAME")]
76    zone_name: Option<String>,
77    /// Zone ID.
78    #[arg(long, help_heading = "Path parameters", value_name = "ZONE_ID")]
79    zone_id: Option<String>,
80}
81
82impl RecordsetCommand {
83    /// Perform command action
84    pub async fn take_action<C: CliArgs>(
85        &self,
86        parsed_args: &C,
87        client: &mut AsyncOpenStack,
88    ) -> Result<(), OpenStackCliError> {
89        info!("Delete Recordset");
90
91        let op =
92            OutputProcessor::from_args(parsed_args, Some("dns.zone/recordset"), Some("delete"));
93        op.validate_args(parsed_args)?;
94
95        let mut ep_builder = delete::Request::builder();
96
97        ep_builder.id(&self.path.id);
98
99        // Process path parameter `zone_id`
100        if let Some(id) = &self.path.zone.zone_id {
101            // zone_id is passed. No need to lookup
102            ep_builder.zone_id(id);
103        } else if let Some(name) = &self.path.zone.zone_name {
104            // zone_name is passed. Need to lookup resource
105            let mut sub_find_builder = find_zone::Request::builder();
106            warn!(
107                "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."
108            );
109
110            sub_find_builder.id(name);
111            let find_ep = sub_find_builder
112                .build()
113                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
114            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
115            // Try to extract resource id
116            match find_data.get("id") {
117                Some(val) => match val.as_str() {
118                    Some(id_str) => {
119                        ep_builder.zone_id(id_str.to_owned());
120                    }
121                    None => {
122                        return Err(OpenStackCliError::ResourceAttributeNotString(
123                            serde_json::to_string(&val)?,
124                        ));
125                    }
126                },
127                None => {
128                    return Err(OpenStackCliError::ResourceAttributeMissing(
129                        "id".to_string(),
130                    ));
131                }
132            };
133        }
134
135        let ep = ep_builder
136            .build()
137            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
138        openstack_sdk::api::ignore(ep).query_async(client).await?;
139        // Show command specific hints
140        op.show_command_hint()?;
141        Ok(())
142    }
143}