Skip to main content

openstack_cli_network/v2/subnetpool/
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 Subnetpool command
19//!
20//! Wraps invoking of the `v2.0/subnetpools` 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::subnetpool::create;
33use openstack_types::network::v2::subnetpool::response;
34
35/// Creates a subnet pool.
36///
37/// Normal response codes: 201
38///
39/// Error response codes: 400, 401, 403, 404
40#[derive(Args)]
41#[command(about = "Create subnet pool")]
42pub struct SubnetpoolCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50
51    /// A `subnetpool` object.
52    #[command(flatten)]
53    subnetpool: Subnetpool,
54}
55
56/// Query parameters
57#[derive(Args)]
58struct QueryParameters {}
59
60/// Path parameters
61#[derive(Args)]
62struct PathParameters {}
63/// Subnetpool Body data
64#[derive(Args, Clone)]
65struct Subnetpool {
66    /// An address scope to assign to the subnet pool.
67    #[arg(help_heading = "Body parameters", long)]
68    address_scope_id: Option<String>,
69
70    /// Set explicit NULL for the address_scope_id
71    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "address_scope_id")]
72    no_address_scope_id: bool,
73
74    /// The size of the prefix to allocate when the `cidr` or `prefixlen`
75    /// attributes are omitted when you create the subnet. Default is
76    /// `min_prefixlen`.
77    #[arg(help_heading = "Body parameters", long)]
78    default_prefixlen: Option<u32>,
79
80    /// A per-project quota on the prefix space that can be allocated from the
81    /// subnet pool for project subnets. Default is no quota is enforced on
82    /// allocations from the subnet pool. For IPv4 subnet pools,
83    /// `default_quota` is measured in units of /32. For IPv6 subnet pools,
84    /// `default_quota` is measured units of /64. All projects that use the
85    /// subnet pool have the same prefix quota applied.
86    #[arg(help_heading = "Body parameters", long)]
87    default_quota: Option<u32>,
88
89    /// A human-readable description for the resource. Default is an empty
90    /// string.
91    #[arg(help_heading = "Body parameters", long)]
92    description: Option<String>,
93
94    /// The subnetpool is default pool or not.
95    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
96    is_default: Option<bool>,
97
98    /// The maximum prefix size that can be allocated from the subnet pool. For
99    /// IPv4 subnet pools, default is `32`. For IPv6 subnet pools, default is
100    /// `128`.
101    #[arg(help_heading = "Body parameters", long)]
102    max_prefixlen: Option<u32>,
103
104    /// The smallest prefix that can be allocated from a subnet pool. For IPv4
105    /// subnet pools, default is `8`. For IPv6 subnet pools, default is `64`.
106    #[arg(help_heading = "Body parameters", long)]
107    min_prefixlen: Option<u32>,
108
109    /// Human-readable name of the resource.
110    #[arg(help_heading = "Body parameters", long)]
111    name: Option<String>,
112
113    /// A list of subnet prefixes to assign to the subnet pool. The API merges
114    /// adjacent prefixes and treats them as a single prefix. Each subnet
115    /// prefix must be unique among all subnet prefixes in all subnet pools
116    /// that are associated with the address scope.
117    ///
118    /// Parameter is an array, may be provided multiple times.
119    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
120    prefixes: Option<Vec<String>>,
121
122    /// Indicates whether this resource is shared across all projects. By
123    /// default, only administrative users can change this value.
124    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
125    shared: Option<bool>,
126
127    /// The ID of the project.
128    #[arg(help_heading = "Body parameters", long)]
129    tenant_id: Option<String>,
130}
131
132impl SubnetpoolCommand {
133    /// Perform command action
134    pub async fn take_action<C: CliArgs>(
135        &self,
136        parsed_args: &C,
137        client: &mut AsyncOpenStack,
138    ) -> Result<(), OpenStackCliError> {
139        info!("Create Subnetpool");
140
141        let op =
142            OutputProcessor::from_args(parsed_args, Some("network.subnetpool"), Some("create"));
143        op.validate_args(parsed_args)?;
144
145        let mut ep_builder = create::Request::builder();
146
147        // Set body parameters
148        // Set Request.subnetpool data
149        let args = &self.subnetpool;
150        let mut subnetpool_builder = create::SubnetpoolBuilder::default();
151        if let Some(val) = &args.address_scope_id {
152            subnetpool_builder.address_scope_id(Some(val.into()));
153        } else if args.no_address_scope_id {
154            subnetpool_builder.address_scope_id(None);
155        }
156
157        if let Some(val) = &args.default_prefixlen {
158            subnetpool_builder.default_prefixlen(*val);
159        }
160
161        if let Some(val) = &args.default_quota {
162            subnetpool_builder.default_quota(*val);
163        }
164
165        if let Some(val) = &args.description {
166            subnetpool_builder.description(val);
167        }
168
169        if let Some(val) = &args.is_default {
170            subnetpool_builder.is_default(*val);
171        }
172
173        if let Some(val) = &args.max_prefixlen {
174            subnetpool_builder.max_prefixlen(*val);
175        }
176
177        if let Some(val) = &args.min_prefixlen {
178            subnetpool_builder.min_prefixlen(*val);
179        }
180
181        if let Some(val) = &args.name {
182            subnetpool_builder.name(val);
183        }
184
185        if let Some(val) = &args.prefixes {
186            subnetpool_builder.prefixes(val.iter().map(Into::into).collect::<Vec<_>>());
187        }
188
189        if let Some(val) = &args.shared {
190            subnetpool_builder.shared(*val);
191        }
192
193        if let Some(val) = &args.tenant_id {
194            subnetpool_builder.tenant_id(val);
195        }
196
197        ep_builder.subnetpool(
198            subnetpool_builder
199                .build()
200                .wrap_err("error preparing the request data")?,
201        );
202
203        let ep = ep_builder
204            .build()
205            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
206
207        let data: serde_json::Value = ep.query_async(client).await?;
208
209        op.output_single::<response::create::SubnetpoolResponse>(data.clone())?;
210        // Show command specific hints
211        op.show_command_hint()?;
212        Ok(())
213    }
214}