Skip to main content

openstack_cli_block_storage/v3/quota_set/
defaults.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}/defaults` 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::defaults;
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/// Command without description in OpenAPI
40#[derive(Args)]
41pub struct QuotaSetCommand {
42    /// Request Query parameters
43    #[command(flatten)]
44    query: QueryParameters,
45
46    /// Path parameters
47    #[command(flatten)]
48    path: PathParameters,
49}
50
51/// Query parameters
52#[derive(Args)]
53struct QueryParameters {}
54
55/// Path parameters
56#[derive(Args)]
57struct PathParameters {
58    /// Project resource for which the operation should be performed.
59    #[command(flatten)]
60    project: ProjectInput,
61}
62
63/// Project input select group
64#[derive(Args)]
65#[group(required = true, multiple = false)]
66struct ProjectInput {
67    /// Project Name.
68    #[arg(long, help_heading = "Path parameters", value_name = "PROJECT_NAME")]
69    project_name: Option<String>,
70    /// Project ID.
71    #[arg(long, help_heading = "Path parameters", value_name = "PROJECT_ID")]
72    project_id: Option<String>,
73    /// Current project.
74    #[arg(long, help_heading = "Path parameters", action = clap::ArgAction::SetTrue)]
75    current_project: bool,
76}
77
78impl QuotaSetCommand {
79    /// Perform command action
80    pub async fn take_action<C: CliArgs>(
81        &self,
82        parsed_args: &C,
83        client: &mut AsyncOpenStack,
84    ) -> Result<(), OpenStackCliError> {
85        info!("Show QuotaSet");
86
87        let op = OutputProcessor::from_args(
88            parsed_args,
89            Some("block-storage.quota_set"),
90            Some("defaults"),
91        );
92        op.validate_args(parsed_args)?;
93
94        let mut ep_builder = defaults::Request::builder();
95
96        // Process path parameter `project_id`
97        if let Some(id) = &self.path.project.project_id {
98            // project_id is passed. No need to lookup
99            ep_builder.project_id(id);
100        } else if let Some(name) = &self.path.project.project_name {
101            // project_name is passed. Need to lookup resource
102            let mut sub_find_builder = find_project::Request::builder();
103            warn!(
104                "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."
105            );
106
107            sub_find_builder.id(name);
108            let find_ep = sub_find_builder
109                .build()
110                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
111            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
112            // Try to extract resource id
113            match find_data.get("id") {
114                Some(val) => match val.as_str() {
115                    Some(id_str) => {
116                        ep_builder.project_id(id_str.to_owned());
117                    }
118                    None => {
119                        return Err(OpenStackCliError::ResourceAttributeNotString(
120                            serde_json::to_string(&val)?,
121                        ));
122                    }
123                },
124                None => {
125                    return Err(OpenStackCliError::ResourceAttributeMissing(
126                        "id".to_string(),
127                    ));
128                }
129            };
130        } else if self.path.project.current_project {
131            let token = client
132                .get_auth_info()
133                .ok_or_eyre("Cannot determine current authentication information")?
134                .token;
135            if let Some(project) = token.project {
136                ep_builder.project_id(
137                    project
138                        .id
139                        .ok_or_eyre("Project ID is missing in the project auth info")?,
140                );
141            } else {
142                return Err(eyre!("Current project information can not be identified").into());
143            }
144        }
145
146        let ep = ep_builder
147            .build()
148            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
149
150        let data: serde_json::Value = ep.query_async(client).await?;
151
152        op.output_single::<response::defaults::QuotaSetResponse>(data.clone())?;
153        // Show command specific hints
154        op.show_command_hint()?;
155        Ok(())
156    }
157}