Skip to main content

openstack_cli_load_balancer/v2/quota/
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 Quota command
19//!
20//! Wraps invoking of the `v2/lbaas/quotas/{project_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 eyre::eyre;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::find_by_name;
34use openstack_sdk::api::identity::v3::project::find as find_project;
35use openstack_sdk::api::load_balancer::v2::quota::delete;
36use tracing::warn;
37
38/// Resets a project quota to use the deployment default quota.
39#[derive(Args)]
40#[command(about = "Reset a Quota")]
41pub struct QuotaCommand {
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 QuotaCommand {
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!("Delete Quota");
86
87        let op =
88            OutputProcessor::from_args(parsed_args, Some("load-balancer.quota"), Some("delete"));
89        op.validate_args(parsed_args)?;
90
91        let mut ep_builder = delete::Request::builder();
92
93        // Process path parameter `project_id`
94        if let Some(id) = &self.path.project.project_id {
95            // project_id is passed. No need to lookup
96            ep_builder.project_id(id);
97        } else if let Some(name) = &self.path.project.project_name {
98            // project_name is passed. Need to lookup resource
99            let mut sub_find_builder = find_project::Request::builder();
100            warn!(
101                "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."
102            );
103
104            sub_find_builder.id(name);
105            let find_ep = sub_find_builder
106                .build()
107                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
108            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
109            // Try to extract resource id
110            match find_data.get("id") {
111                Some(val) => match val.as_str() {
112                    Some(id_str) => {
113                        ep_builder.project_id(id_str.to_owned());
114                    }
115                    None => {
116                        return Err(OpenStackCliError::ResourceAttributeNotString(
117                            serde_json::to_string(&val)?,
118                        ));
119                    }
120                },
121                None => {
122                    return Err(OpenStackCliError::ResourceAttributeMissing(
123                        "id".to_string(),
124                    ));
125                }
126            };
127        } else if self.path.project.current_project {
128            let token = client
129                .get_auth_info()
130                .ok_or_eyre("Cannot determine current authentication information")?
131                .token;
132            if let Some(project) = token.project {
133                ep_builder.project_id(
134                    project
135                        .id
136                        .ok_or_eyre("Project ID is missing in the project auth info")?,
137                );
138            } else {
139                return Err(eyre!("Current project information can not be identified").into());
140            }
141        }
142
143        let ep = ep_builder
144            .build()
145            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
146        openstack_sdk::api::ignore(ep).query_async(client).await?;
147        // Show command specific hints
148        op.show_command_hint()?;
149        Ok(())
150    }
151}