Skip to main content

openstack_cli_network/v2/metering/metering_label_rule/
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 MeteringLabelRule command
19//!
20//! Wraps invoking of the `v2.0/metering/metering-label-rules` 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 clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::network::v2::metering::metering_label_rule::create;
34use openstack_types::network::v2::metering::metering_label_rule::response;
35
36/// Creates an L3 metering label rule.
37///
38/// Normal response codes: 201
39///
40/// Error response codes: 400, 401, 403, 404, 409
41#[derive(Args)]
42#[command(about = "Create metering label rule")]
43pub struct MeteringLabelRuleCommand {
44    /// Request Query parameters
45    #[command(flatten)]
46    query: QueryParameters,
47
48    /// Path parameters
49    #[command(flatten)]
50    path: PathParameters,
51
52    /// A `metering_label_rule` object.
53    #[command(flatten)]
54    metering_label_rule: MeteringLabelRule,
55}
56
57/// Query parameters
58#[derive(Args)]
59struct QueryParameters {}
60
61/// Path parameters
62#[derive(Args)]
63struct PathParameters {}
64
65#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
66enum Direction {
67    Egress,
68    Ingress,
69}
70
71/// MeteringLabelRule Body data
72#[derive(Args, Clone)]
73struct MeteringLabelRule {
74    #[arg(help_heading = "Body parameters", long)]
75    destination_ip_prefix: Option<String>,
76
77    /// Ingress or egress, which is the direction in which the metering rule is
78    /// applied.
79    #[arg(help_heading = "Body parameters", long)]
80    direction: Option<Direction>,
81
82    /// Indicates whether to count the traffic of a specific IP address with
83    /// the `remote_ip_prefix`, `source_ip_prefix`, or `destination_ip_prefix`
84    /// values. Default is `false`.
85    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
86    excluded: Option<bool>,
87
88    /// The metering label ID associated with this metering rule.
89    #[arg(help_heading = "Body parameters", long)]
90    metering_label_id: Option<String>,
91
92    /// (deprecated) The source IP prefix that is matched by this metering
93    /// rule. By source IP prefix, one should read the internal/private IPs
94    /// used in OpenStack.
95    #[arg(help_heading = "Body parameters", long)]
96    remote_ip_prefix: Option<String>,
97
98    #[arg(help_heading = "Body parameters", long)]
99    source_ip_prefix: Option<String>,
100
101    #[arg(help_heading = "Body parameters", long)]
102    tenant_id: Option<String>,
103}
104
105impl MeteringLabelRuleCommand {
106    /// Perform command action
107    pub async fn take_action<C: CliArgs>(
108        &self,
109        parsed_args: &C,
110        client: &mut AsyncOpenStack,
111    ) -> Result<(), OpenStackCliError> {
112        info!("Create MeteringLabelRule");
113
114        let op = OutputProcessor::from_args(
115            parsed_args,
116            Some("network.metering/metering_label_rule"),
117            Some("create"),
118        );
119        op.validate_args(parsed_args)?;
120
121        let mut ep_builder = create::Request::builder();
122
123        // Set body parameters
124        // Set Request.metering_label_rule data
125        let args = &self.metering_label_rule;
126        let mut metering_label_rule_builder = create::MeteringLabelRuleBuilder::default();
127        if let Some(val) = &args.destination_ip_prefix {
128            metering_label_rule_builder.destination_ip_prefix(val);
129        }
130
131        if let Some(val) = &args.direction {
132            let tmp = match val {
133                Direction::Egress => create::Direction::Egress,
134                Direction::Ingress => create::Direction::Ingress,
135            };
136            metering_label_rule_builder.direction(tmp);
137        }
138
139        if let Some(val) = &args.excluded {
140            metering_label_rule_builder.excluded(*val);
141        }
142
143        if let Some(val) = &args.metering_label_id {
144            metering_label_rule_builder.metering_label_id(val);
145        }
146
147        if let Some(val) = &args.remote_ip_prefix {
148            metering_label_rule_builder.remote_ip_prefix(val);
149        }
150
151        if let Some(val) = &args.source_ip_prefix {
152            metering_label_rule_builder.source_ip_prefix(val);
153        }
154
155        if let Some(val) = &args.tenant_id {
156            metering_label_rule_builder.tenant_id(val);
157        }
158
159        ep_builder.metering_label_rule(
160            metering_label_rule_builder
161                .build()
162                .wrap_err("error preparing the request data")?,
163        );
164
165        let ep = ep_builder
166            .build()
167            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
168
169        let data: serde_json::Value = ep.query_async(client).await?;
170
171        op.output_single::<response::create::MeteringLabelRuleResponse>(data.clone())?;
172        // Show command specific hints
173        op.show_command_hint()?;
174        Ok(())
175    }
176}