Skip to main content

openstack_cli_network/v2/address_scope/
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 AddressScope command
19//!
20//! Wraps invoking of the `v2.0/address-scopes` 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::address_scope::create;
33use openstack_types::network::v2::address_scope::response;
34
35/// Creates an address scope.
36///
37/// Normal response codes: 201
38///
39/// Error response codes: 400, 401, 403, 404
40#[derive(Args)]
41#[command(about = "Create address scope")]
42pub struct AddressScopeCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50
51    /// An `address scope` object.
52    #[command(flatten)]
53    address_scope: AddressScope,
54}
55
56/// Query parameters
57#[derive(Args)]
58struct QueryParameters {}
59
60/// Path parameters
61#[derive(Args)]
62struct PathParameters {}
63/// AddressScope Body data
64#[derive(Args, Clone)]
65struct AddressScope {
66    /// The IP protocol version. Valid value is `4` or `6`.
67    #[arg(help_heading = "Body parameters", long)]
68    ip_version: Option<i32>,
69
70    /// Human-readable name of the resource. Default is an empty string.
71    #[arg(help_heading = "Body parameters", long)]
72    name: Option<String>,
73
74    /// Indicates whether this resource is shared across all projects. By
75    /// default, only administrative users can change this value.
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 AddressScopeCommand {
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 AddressScope");
94
95        let op =
96            OutputProcessor::from_args(parsed_args, Some("network.address_scope"), Some("create"));
97        op.validate_args(parsed_args)?;
98
99        let mut ep_builder = create::Request::builder();
100
101        // Set body parameters
102        // Set Request.address_scope data
103        let args = &self.address_scope;
104        let mut address_scope_builder = create::AddressScopeBuilder::default();
105        if let Some(val) = &args.ip_version {
106            address_scope_builder.ip_version(*val);
107        }
108
109        if let Some(val) = &args.name {
110            address_scope_builder.name(val);
111        }
112
113        if let Some(val) = &args.shared {
114            address_scope_builder.shared(*val);
115        }
116
117        if let Some(val) = &args.tenant_id {
118            address_scope_builder.tenant_id(val);
119        }
120
121        ep_builder.address_scope(
122            address_scope_builder
123                .build()
124                .wrap_err("error preparing the request data")?,
125        );
126
127        let ep = ep_builder
128            .build()
129            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
130
131        let data: serde_json::Value = ep.query_async(client).await?;
132
133        op.output_single::<response::create::AddressScopeResponse>(data.clone())?;
134        // Show command specific hints
135        op.show_command_hint()?;
136        Ok(())
137    }
138}