Skip to main content

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