Skip to main content

openstack_cli_dns/v2/zone/recordset/
show.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//! Show Recordset command
19//!
20//! Wraps invoking of the `v2/zones/{zone_id}/recordsets/{recordset_id}` with `GET` 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::find;
33use openstack_sdk::api::find;
34use openstack_sdk::api::find_by_name;
35use openstack_types::dns::v2::zone::recordset::response;
36use tracing::warn;
37
38/// Show an single recordset
39#[derive(Args)]
40#[command(about = "Show a Recordset")]
41pub struct RecordsetCommand {
42    /// Request Query parameters
43    #[command(flatten)]
44    query: QueryParameters,
45
46    /// Request Headers parameters
47    #[command(flatten)]
48    headers: HeaderParameters,
49
50    /// Path parameters
51    #[command(flatten)]
52    path: PathParameters,
53}
54
55/// Query parameters
56#[derive(Args)]
57struct QueryParameters {}
58
59/// Header parameters
60#[derive(Args)]
61struct HeaderParameters {
62    /// If enabled this will show results from all projects in Designate
63    #[arg(long)]
64    x_auth_all_projects: Option<bool>,
65
66    /// This allows a user to impersonate another project
67    #[arg(long)]
68    x_auth_sudo_project_id: Option<String>,
69}
70
71/// Path parameters
72#[derive(Args)]
73struct PathParameters {
74    /// recordset_id parameter for
75    /// /v2/zones/{zone_id}/recordsets/{recordset_id} API
76    #[arg(
77        help_heading = "Path parameters",
78        id = "path_param_id",
79        value_name = "ID"
80    )]
81    id: String,
82
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
100impl RecordsetCommand {
101    /// Perform command action
102    pub async fn take_action<C: CliArgs>(
103        &self,
104        parsed_args: &C,
105        client: &mut AsyncOpenStack,
106    ) -> Result<(), OpenStackCliError> {
107        info!("Show Recordset");
108
109        let op = OutputProcessor::from_args(parsed_args, Some("dns.zone/recordset"), Some("show"));
110        op.validate_args(parsed_args)?;
111
112        let mut find_builder = find::Request::builder();
113
114        find_builder.id(&self.path.id);
115
116        // Process path parameter `zone_id`
117        if let Some(id) = &self.path.zone.zone_id {
118            // zone_id is passed. No need to lookup
119            find_builder.zone_id(id);
120        } else if let Some(name) = &self.path.zone.zone_name {
121            // zone_name is passed. Need to lookup resource
122            let mut sub_find_builder = find_zone::Request::builder();
123            warn!(
124                "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."
125            );
126
127            sub_find_builder.id(name);
128            let find_ep = sub_find_builder
129                .build()
130                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
131            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
132            // Try to extract resource id
133            match find_data.get("id") {
134                Some(val) => match val.as_str() {
135                    Some(id_str) => {
136                        find_builder.zone_id(id_str.to_owned());
137                    }
138                    None => {
139                        return Err(OpenStackCliError::ResourceAttributeNotString(
140                            serde_json::to_string(&val)?,
141                        ));
142                    }
143                },
144                None => {
145                    return Err(OpenStackCliError::ResourceAttributeMissing(
146                        "id".to_string(),
147                    ));
148                }
149            };
150        }
151
152        if let Some(val) = &self.headers.x_auth_all_projects {
153            find_builder.header(
154                http::header::HeaderName::from_static("x-auth-all-projects"),
155                http::header::HeaderValue::from_static(if *val { "true" } else { "false" }),
156            );
157        }
158        if let Some(val) = &self.headers.x_auth_sudo_project_id {
159            find_builder.header(
160                http::header::HeaderName::from_static("x-auth-sudo-project-id"),
161                http::header::HeaderValue::from_str(val)?,
162            );
163        }
164        let find_ep = find_builder
165            .build()
166            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
167        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
168
169        op.output_single::<response::get::RecordsetResponse>(find_data.clone())?;
170        // Show command specific hints
171        op.show_command_hint()?;
172        Ok(())
173    }
174}