Skip to main content

openstack_cli_block_storage/v3/quota_set/
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 QuotaSet command
19//!
20//! Wraps invoking of the `v3/os-quota-sets/{project_id}` with `GET` method
21
22use clap::Args;
23use eyre::OptionExt;
24use tracing::info;
25
26use openstack_cli_core::cli::CliArgs;
27use openstack_cli_core::error::OpenStackCliError;
28use openstack_cli_core::output::OutputProcessor;
29use openstack_sdk::AsyncOpenStack;
30
31use eyre::eyre;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::block_storage::v3::quota_set::get;
34use openstack_sdk::api::find_by_name;
35use openstack_sdk::api::identity::v3::project::find as find_project;
36use openstack_types::block_storage::v3::quota_set::response;
37use tracing::warn;
38
39/// Show quota for a particular tenant
40#[derive(Args)]
41#[command(about = "Show quota for a particular tenant")]
42pub struct QuotaSetCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50}
51
52/// Query parameters
53#[derive(Args)]
54struct QueryParameters {
55    /// Show project’s quota usage information. Default is false.
56    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
57    usage: Option<bool>,
58}
59
60/// Path parameters
61#[derive(Args)]
62struct PathParameters {
63    /// Project resource for which the operation should be performed.
64    #[command(flatten)]
65    project: ProjectInput,
66}
67
68/// Project input select group
69#[derive(Args)]
70#[group(required = true, multiple = false)]
71struct ProjectInput {
72    /// Project Name.
73    #[arg(long, help_heading = "Path parameters", value_name = "PROJECT_NAME")]
74    project_name: Option<String>,
75    /// Project ID.
76    #[arg(long, help_heading = "Path parameters", value_name = "PROJECT_ID")]
77    project_id: Option<String>,
78    /// Current project.
79    #[arg(long, help_heading = "Path parameters", action = clap::ArgAction::SetTrue)]
80    current_project: bool,
81}
82
83impl QuotaSetCommand {
84    /// Perform command action
85    pub async fn take_action<C: CliArgs>(
86        &self,
87        parsed_args: &C,
88        client: &mut AsyncOpenStack,
89    ) -> Result<(), OpenStackCliError> {
90        info!("Show QuotaSet");
91
92        let op =
93            OutputProcessor::from_args(parsed_args, Some("block-storage.quota_set"), Some("show"));
94        op.validate_args(parsed_args)?;
95
96        let mut ep_builder = get::Request::builder();
97
98        // Process path parameter `project_id`
99        if let Some(id) = &self.path.project.project_id {
100            // project_id is passed. No need to lookup
101            ep_builder.project_id(id);
102        } else if let Some(name) = &self.path.project.project_name {
103            // project_name is passed. Need to lookup resource
104            let mut sub_find_builder = find_project::Request::builder();
105            warn!(
106                "Querying project by name (because of `--project-name` parameter passed) may not be definite. This may fail in which case parameter `--project-id` should be used instead."
107            );
108
109            sub_find_builder.id(name);
110            let find_ep = sub_find_builder
111                .build()
112                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
113            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
114            // Try to extract resource id
115            match find_data.get("id") {
116                Some(val) => match val.as_str() {
117                    Some(id_str) => {
118                        ep_builder.project_id(id_str.to_owned());
119                    }
120                    None => {
121                        return Err(OpenStackCliError::ResourceAttributeNotString(
122                            serde_json::to_string(&val)?,
123                        ));
124                    }
125                },
126                None => {
127                    return Err(OpenStackCliError::ResourceAttributeMissing(
128                        "id".to_string(),
129                    ));
130                }
131            };
132        } else if self.path.project.current_project {
133            let token = client
134                .get_auth_info()
135                .ok_or_eyre("Cannot determine current authentication information")?
136                .token;
137            if let Some(project) = token.project {
138                ep_builder.project_id(
139                    project
140                        .id
141                        .ok_or_eyre("Project ID is missing in the project auth info")?,
142                );
143            } else {
144                return Err(eyre!("Current project information can not be identified").into());
145            }
146        }
147        // Set query parameters
148        if let Some(val) = &self.query.usage {
149            ep_builder.usage(*val);
150        }
151
152        let ep = ep_builder
153            .build()
154            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
155
156        let data: serde_json::Value = ep.query_async(client).await?;
157
158        op.output_single::<response::get::QuotaSetResponse>(data.clone())?;
159        // Show command specific hints
160        op.show_command_hint()?;
161        Ok(())
162    }
163}