Skip to main content

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