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