Skip to main content

openstack_cli_network/v2/qos/policy/
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 Policy command
19//!
20//! Wraps invoking of the `v2.0/qos/policies/{id}` with `PUT` method
21
22use clap::Args;
23use eyre::WrapErr;
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::find;
33use openstack_sdk::api::network::v2::qos::policy::find;
34use openstack_sdk::api::network::v2::qos::policy::set;
35use openstack_types::network::v2::qos::policy::response;
36
37/// Updates a QoS policy.
38///
39/// Normal response codes: 200
40///
41/// Error response codes: 400, 401, 404, 412
42#[derive(Args)]
43#[command(about = "Update QoS policy")]
44pub struct PolicyCommand {
45    /// Request Query parameters
46    #[command(flatten)]
47    query: QueryParameters,
48
49    /// Path parameters
50    #[command(flatten)]
51    path: PathParameters,
52
53    /// A QoS `policy` object.
54    #[command(flatten)]
55    policy: Policy,
56}
57
58/// Query parameters
59#[derive(Args)]
60struct QueryParameters {}
61
62/// Path parameters
63#[derive(Args)]
64struct PathParameters {
65    /// id parameter for /v2.0/qos/policies/{id} API
66    #[arg(
67        help_heading = "Path parameters",
68        id = "path_param_id",
69        value_name = "ID"
70    )]
71    id: String,
72}
73/// Policy Body data
74#[derive(Args, Clone)]
75struct Policy {
76    /// A human-readable description for the resource. Default is an empty
77    /// string.
78    #[arg(help_heading = "Body parameters", long)]
79    description: Option<String>,
80
81    /// If `true`, the QoS `policy` is the default policy.
82    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
83    is_default: Option<bool>,
84
85    /// Human-readable name of the resource.
86    #[arg(help_heading = "Body parameters", long)]
87    name: Option<String>,
88
89    /// Set to `true` to share this policy with other projects. Default is
90    /// `false`.
91    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
92    shared: Option<bool>,
93}
94
95impl PolicyCommand {
96    /// Perform command action
97    pub async fn take_action<C: CliArgs>(
98        &self,
99        parsed_args: &C,
100        client: &mut AsyncOpenStack,
101    ) -> Result<(), OpenStackCliError> {
102        info!("Set Policy");
103
104        let op = OutputProcessor::from_args(parsed_args, Some("network.qos/policy"), Some("set"));
105        op.validate_args(parsed_args)?;
106
107        let mut find_builder = find::Request::builder();
108
109        find_builder.id(&self.path.id);
110
111        let find_ep = find_builder
112            .build()
113            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
114        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
115
116        let mut ep_builder = set::Request::builder();
117
118        let resource_id = find_data["id"]
119            .as_str()
120            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
121            .to_string();
122        ep_builder.id(resource_id.clone());
123
124        // Set body parameters
125        // Set Request.policy data
126        let args = &self.policy;
127        let mut policy_builder = set::PolicyBuilder::default();
128        if let Some(val) = &args.description {
129            policy_builder.description(val);
130        }
131
132        if let Some(val) = &args.is_default {
133            policy_builder.is_default(*val);
134        }
135
136        if let Some(val) = &args.name {
137            policy_builder.name(val);
138        }
139
140        if let Some(val) = &args.shared {
141            policy_builder.shared(*val);
142        }
143
144        ep_builder.policy(
145            policy_builder
146                .build()
147                .wrap_err("error preparing the request data")?,
148        );
149
150        let ep = ep_builder
151            .build()
152            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
153
154        let data: serde_json::Value = ep.query_async(client).await?;
155
156        op.output_single::<response::set::PolicyResponse>(data.clone())?;
157        // Show command specific hints
158        op.show_command_hint()?;
159        Ok(())
160    }
161}