Skip to main content

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