Skip to main content

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