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 in units of /64. All projects that use the
85    /// subnet pool have the same prefix quota applied. If `default_quota` is
86    /// `0`, no quota will be enforced.
87    #[arg(help_heading = "Body parameters", long)]
88    default_quota: Option<u32>,
89
90    /// A human-readable description for the resource. Default is an empty
91    /// string.
92    #[arg(help_heading = "Body parameters", long)]
93    description: Option<String>,
94
95    /// The subnetpool is default pool or not.
96    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
97    is_default: Option<bool>,
98
99    /// The maximum prefix size that can be allocated from the subnet pool. For
100    /// IPv4 subnet pools, default is `32`. For IPv6 subnet pools, default is
101    /// `128`.
102    #[arg(help_heading = "Body parameters", long)]
103    max_prefixlen: Option<u32>,
104
105    /// The smallest prefix that can be allocated from a subnet pool. For IPv4
106    /// subnet pools, default is `8`. For IPv6 subnet pools, default is `64`.
107    #[arg(help_heading = "Body parameters", long)]
108    min_prefixlen: Option<u32>,
109
110    /// Human-readable name of the resource.
111    #[arg(help_heading = "Body parameters", long)]
112    name: Option<String>,
113
114    /// A list of subnet prefixes to assign to the subnet pool. The API merges
115    /// adjacent prefixes and treats them as a single prefix. Each subnet
116    /// prefix must be unique among all subnet prefixes in all subnet pools
117    /// that are associated with the address scope.
118    ///
119    /// Parameter is an array, may be provided multiple times.
120    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
121    prefixes: Option<Vec<String>>,
122
123    /// Indicates whether this resource is shared across all projects. By
124    /// default, only administrative users can change this value.
125    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
126    shared: Option<bool>,
127
128    /// The ID of the project.
129    #[arg(help_heading = "Body parameters", long)]
130    tenant_id: Option<String>,
131}
132
133impl SubnetpoolCommand {
134    /// Perform command action
135    pub async fn take_action<C: CliArgs>(
136        &self,
137        parsed_args: &C,
138        client: &mut AsyncOpenStack,
139    ) -> Result<(), OpenStackCliError> {
140        info!("Create Subnetpool");
141
142        let op =
143            OutputProcessor::from_args(parsed_args, Some("network.subnetpool"), Some("create"));
144        op.validate_args(parsed_args)?;
145
146        let mut ep_builder = create::Request::builder();
147
148        // Set body parameters
149        // Set Request.subnetpool data
150        let args = &self.subnetpool;
151        let mut subnetpool_builder = create::SubnetpoolBuilder::default();
152        if let Some(val) = &args.address_scope_id {
153            subnetpool_builder.address_scope_id(Some(val.into()));
154        } else if args.no_address_scope_id {
155            subnetpool_builder.address_scope_id(None);
156        }
157
158        if let Some(val) = &args.default_prefixlen {
159            subnetpool_builder.default_prefixlen(*val);
160        }
161
162        if let Some(val) = &args.default_quota {
163            subnetpool_builder.default_quota(*val);
164        }
165
166        if let Some(val) = &args.description {
167            subnetpool_builder.description(val);
168        }
169
170        if let Some(val) = &args.is_default {
171            subnetpool_builder.is_default(*val);
172        }
173
174        if let Some(val) = &args.max_prefixlen {
175            subnetpool_builder.max_prefixlen(*val);
176        }
177
178        if let Some(val) = &args.min_prefixlen {
179            subnetpool_builder.min_prefixlen(*val);
180        }
181
182        if let Some(val) = &args.name {
183            subnetpool_builder.name(val);
184        }
185
186        if let Some(val) = &args.prefixes {
187            subnetpool_builder.prefixes(val.iter().map(Into::into).collect::<Vec<_>>());
188        }
189
190        if let Some(val) = &args.shared {
191            subnetpool_builder.shared(*val);
192        }
193
194        if let Some(val) = &args.tenant_id {
195            subnetpool_builder.tenant_id(val);
196        }
197
198        ep_builder.subnetpool(
199            subnetpool_builder
200                .build()
201                .wrap_err("error preparing the request data")?,
202        );
203
204        let ep = ep_builder
205            .build()
206            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
207
208        let data: serde_json::Value = ep.query_async(client).await?;
209
210        op.output_single::<response::create::SubnetpoolResponse>(data.clone())?;
211        // Show command specific hints
212        op.show_command_hint()?;
213        Ok(())
214    }
215}