Skip to main content

openstack_cli_block_storage/v3/qos_spec/
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 QosSpec command
19//!
20//! Wraps invoking of the `v3/qos-specs/{id}` with `PUT` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_cli_core::cli::CliArgs;
26use openstack_cli_core::error::OpenStackCliError;
27use openstack_cli_core::output::OutputProcessor;
28use openstack_sdk::AsyncOpenStack;
29
30use openstack_cli_core::common::parse_key_val_opt;
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::block_storage::v3::qos_spec::find;
33use openstack_sdk::api::block_storage::v3::qos_spec::set;
34use openstack_sdk::api::find;
35use openstack_types::block_storage::v3::qos_spec::response;
36
37/// Command without description in OpenAPI
38#[derive(Args)]
39pub struct QosSpecCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47
48    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val_opt::<String, String>)]
49    qos_specs: Vec<(String, Option<String>)>,
50}
51
52/// Query parameters
53#[derive(Args)]
54struct QueryParameters {}
55
56/// Path parameters
57#[derive(Args)]
58struct PathParameters {
59    /// id parameter for /v3/qos-specs/{id} API
60    #[arg(
61        help_heading = "Path parameters",
62        id = "path_param_id",
63        value_name = "ID"
64    )]
65    id: String,
66}
67
68impl QosSpecCommand {
69    /// Perform command action
70    pub async fn take_action<C: CliArgs>(
71        &self,
72        parsed_args: &C,
73        client: &mut AsyncOpenStack,
74    ) -> Result<(), OpenStackCliError> {
75        info!("Set QosSpec");
76
77        let op =
78            OutputProcessor::from_args(parsed_args, Some("block-storage.qos_spec"), Some("set"));
79        op.validate_args(parsed_args)?;
80
81        let mut find_builder = find::Request::builder();
82
83        find_builder.id(&self.path.id);
84
85        let find_ep = find_builder
86            .build()
87            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
88        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
89
90        let mut ep_builder = set::Request::builder();
91
92        let resource_id = find_data["id"]
93            .as_str()
94            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
95            .to_string();
96        ep_builder.id(resource_id.clone());
97
98        // Set body parameters
99        // Set Request.qos_specs data
100
101        ep_builder.qos_specs(
102            self.qos_specs
103                .iter()
104                .cloned()
105                .map(|(k, v)| (k, v.map(Into::into))),
106        );
107
108        let ep = ep_builder
109            .build()
110            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
111
112        let data: serde_json::Value = ep.query_async(client).await?;
113
114        op.output_single::<response::set::QosSpecResponse>(data.clone())?;
115        // Show command specific hints
116        op.show_command_hint()?;
117        Ok(())
118    }
119}