Skip to main content

openstack_cli_compute/v2/quota_set/
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 QuotaSet command
19//!
20//! Wraps invoking of the `v2.1/os-quota-sets/{id}` with `DELETE` 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 openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::compute::v2::quota_set::delete;
33use openstack_sdk::api::find_by_name;
34use openstack_sdk::api::identity::v3::user::find as find_user;
35use tracing::warn;
36
37/// Reverts the quotas to default values for a project or a project and a user.
38///
39/// To revert quotas for a project and a user, specify the `user_id` query
40/// parameter.
41///
42/// Normal response codes: 202
43///
44/// Error response codes: unauthorized(401), forbidden(403)
45#[derive(Args)]
46#[command(about = "Revert Quotas To Defaults")]
47pub struct QuotaSetCommand {
48    /// Request Query parameters
49    #[command(flatten)]
50    query: QueryParameters,
51
52    /// Path parameters
53    #[command(flatten)]
54    path: PathParameters,
55}
56
57/// Query parameters
58#[derive(Args)]
59struct QueryParameters {
60    /// User resource for which the operation should be performed.
61    #[command(flatten)]
62    user: UserInput,
63}
64
65/// User input select group
66#[derive(Args)]
67#[group(required = false, multiple = false)]
68struct UserInput {
69    /// User Name.
70    #[arg(long, help_heading = "Path parameters", value_name = "USER_NAME")]
71    user_name: Option<String>,
72    /// User ID.
73    #[arg(long, help_heading = "Path parameters", value_name = "USER_ID")]
74    user_id: Option<String>,
75    /// Current authenticated user.
76    #[arg(long, help_heading = "Path parameters", action = clap::ArgAction::SetTrue)]
77    current_user: bool,
78}
79
80/// Path parameters
81#[derive(Args)]
82struct PathParameters {
83    /// id parameter for /v2.1/os-quota-sets/{id} API
84    #[arg(
85        help_heading = "Path parameters",
86        id = "path_param_id",
87        value_name = "ID"
88    )]
89    id: String,
90}
91
92impl QuotaSetCommand {
93    /// Perform command action
94    pub async fn take_action<C: CliArgs>(
95        &self,
96        parsed_args: &C,
97        client: &mut AsyncOpenStack,
98    ) -> Result<(), OpenStackCliError> {
99        info!("Delete QuotaSet");
100
101        let op = OutputProcessor::from_args(parsed_args, Some("compute.quota_set"), Some("delete"));
102        op.validate_args(parsed_args)?;
103
104        let mut ep_builder = delete::Request::builder();
105
106        ep_builder.id(&self.path.id);
107        // Set query parameters
108        if let Some(id) = &self.query.user.user_id {
109            // user_id is passed. No need to lookup
110            ep_builder.user_id(id);
111        } else if let Some(name) = &self.query.user.user_name {
112            // user_name is passed. Need to lookup resource
113            let mut sub_find_builder = find_user::Request::builder();
114            warn!(
115                "Querying user by name (because of `--user-name` parameter passed) may not be definite. This may fail in which case parameter `--user-id` should be used instead."
116            );
117
118            sub_find_builder.id(name);
119            let find_ep = sub_find_builder
120                .build()
121                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
122            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
123            // Try to extract resource id
124            match find_data.get("id") {
125                Some(val) => match val.as_str() {
126                    Some(id_str) => {
127                        ep_builder.user_id(id_str.to_owned());
128                    }
129                    None => {
130                        return Err(OpenStackCliError::ResourceAttributeNotString(
131                            serde_json::to_string(&val)?,
132                        ));
133                    }
134                },
135                None => {
136                    return Err(OpenStackCliError::ResourceAttributeMissing(
137                        "id".to_string(),
138                    ));
139                }
140            };
141        } else if self.query.user.current_user {
142            ep_builder.user_id(
143                client
144                    .get_auth_info()
145                    .ok_or_eyre("Cannot determine current authentication information")?
146                    .token
147                    .user
148                    .id,
149            );
150        }
151
152        let ep = ep_builder
153            .build()
154            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
155        openstack_sdk::api::ignore(ep).query_async(client).await?;
156        // Show command specific hints
157        op.show_command_hint()?;
158        Ok(())
159    }
160}