Skip to main content

openstack_cli_network/v2/metering/metering_label/
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 MeteringLabel command
19//!
20//! Wraps invoking of the `v2.0/metering/metering-labels` 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::metering::metering_label::create;
33use openstack_types::network::v2::metering::metering_label::response;
34
35/// Creates an L3 metering label.
36///
37/// Normal response codes: 201
38///
39/// Error response codes: 400, 401, 403
40#[derive(Args)]
41#[command(about = "Create metering label")]
42pub struct MeteringLabelCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50
51    /// A `metering_label` object.
52    #[command(flatten)]
53    metering_label: MeteringLabel,
54}
55
56/// Query parameters
57#[derive(Args)]
58struct QueryParameters {}
59
60/// Path parameters
61#[derive(Args)]
62struct PathParameters {}
63/// MeteringLabel Body data
64#[derive(Args, Clone)]
65struct MeteringLabel {
66    /// A human-readable description for the resource. Default is an empty
67    /// string.
68    #[arg(help_heading = "Body parameters", long)]
69    description: Option<String>,
70
71    /// Human-readable name of the resource. Default is an empty string.
72    #[arg(help_heading = "Body parameters", long)]
73    name: Option<String>,
74
75    /// Indicates whether this metering label is shared across all projects.
76    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
77    shared: Option<bool>,
78
79    /// The ID of the project that owns the resource. Only administrative and
80    /// users with advsvc role can specify a project ID other than their own.
81    /// You cannot change this value through authorization policies.
82    #[arg(help_heading = "Body parameters", long)]
83    tenant_id: Option<String>,
84}
85
86impl MeteringLabelCommand {
87    /// Perform command action
88    pub async fn take_action<C: CliArgs>(
89        &self,
90        parsed_args: &C,
91        client: &mut AsyncOpenStack,
92    ) -> Result<(), OpenStackCliError> {
93        info!("Create MeteringLabel");
94
95        let op = OutputProcessor::from_args(
96            parsed_args,
97            Some("network.metering/metering_label"),
98            Some("create"),
99        );
100        op.validate_args(parsed_args)?;
101
102        let mut ep_builder = create::Request::builder();
103
104        // Set body parameters
105        // Set Request.metering_label data
106        let args = &self.metering_label;
107        let mut metering_label_builder = create::MeteringLabelBuilder::default();
108        if let Some(val) = &args.description {
109            metering_label_builder.description(val);
110        }
111
112        if let Some(val) = &args.name {
113            metering_label_builder.name(val);
114        }
115
116        if let Some(val) = &args.shared {
117            metering_label_builder.shared(*val);
118        }
119
120        if let Some(val) = &args.tenant_id {
121            metering_label_builder.tenant_id(val);
122        }
123
124        ep_builder.metering_label(
125            metering_label_builder
126                .build()
127                .wrap_err("error preparing the request data")?,
128        );
129
130        let ep = ep_builder
131            .build()
132            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
133
134        let data: serde_json::Value = ep.query_async(client).await?;
135
136        op.output_single::<response::create::MeteringLabelResponse>(data.clone())?;
137        // Show command specific hints
138        op.show_command_hint()?;
139        Ok(())
140    }
141}