Skip to main content

openstack_cli_network/v2/qos/policy/
create.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//! Create Policy command
19//!
20//! Wraps invoking of the `v2.0/qos/policies` with `POST` 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::network::v2::qos::policy::create;
33use openstack_types::network::v2::qos::policy::response;
34
35/// Creates a QoS policy.
36///
37/// Creates a QoS policy by using the configuration that you define in the
38/// request object. A response object is returned. The object contains a unique
39/// ID.
40///
41/// By the default policy configuration, if the caller is not an administrative
42/// user, this call returns the HTTP `Forbidden (403)` response code.
43///
44/// Users with an administrative role can create policies on behalf of other
45/// projects by specifying a project ID that is different than their own.
46///
47/// Normal response codes: 201
48///
49/// Error response codes: 401, 403, 404, 409
50#[derive(Args)]
51#[command(about = "Create QoS policy")]
52pub struct PolicyCommand {
53    /// Request Query parameters
54    #[command(flatten)]
55    query: QueryParameters,
56
57    /// Path parameters
58    #[command(flatten)]
59    path: PathParameters,
60
61    /// A QoS `policy` object.
62    #[command(flatten)]
63    policy: Policy,
64}
65
66/// Query parameters
67#[derive(Args)]
68struct QueryParameters {}
69
70/// Path parameters
71#[derive(Args)]
72struct PathParameters {}
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    /// The ID of the project that owns the resource. Only administrative and
95    /// users with advsvc role can specify a project ID other than their own.
96    /// You cannot change this value through authorization policies.
97    #[arg(help_heading = "Body parameters", long)]
98    tenant_id: Option<String>,
99}
100
101impl PolicyCommand {
102    /// Perform command action
103    pub async fn take_action<C: CliArgs>(
104        &self,
105        parsed_args: &C,
106        client: &mut AsyncOpenStack,
107    ) -> Result<(), OpenStackCliError> {
108        info!("Create Policy");
109
110        let op =
111            OutputProcessor::from_args(parsed_args, Some("network.qos/policy"), Some("create"));
112        op.validate_args(parsed_args)?;
113
114        let mut ep_builder = create::Request::builder();
115
116        // Set body parameters
117        // Set Request.policy data
118        let args = &self.policy;
119        let mut policy_builder = create::PolicyBuilder::default();
120        if let Some(val) = &args.description {
121            policy_builder.description(val);
122        }
123
124        if let Some(val) = &args.is_default {
125            policy_builder.is_default(*val);
126        }
127
128        if let Some(val) = &args.name {
129            policy_builder.name(val);
130        }
131
132        if let Some(val) = &args.shared {
133            policy_builder.shared(*val);
134        }
135
136        if let Some(val) = &args.tenant_id {
137            policy_builder.tenant_id(val);
138        }
139
140        ep_builder.policy(
141            policy_builder
142                .build()
143                .wrap_err("error preparing the request data")?,
144        );
145
146        let ep = ep_builder
147            .build()
148            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
149
150        let data: serde_json::Value = ep.query_async(client).await?;
151
152        op.output_single::<response::create::PolicyResponse>(data.clone())?;
153        // Show command specific hints
154        op.show_command_hint()?;
155        Ok(())
156    }
157}