Skip to main content

openstack_cli_network/v2/subnet/
list.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//! List Subnets command
19//!
20//! Wraps invoking of the `v2.0/subnets` with `GET` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_cli_core::cli::CliArgs;
26use openstack_cli_core::error::OpenStackCliError;
27use openstack_cli_core::output::OutputProcessor;
28use openstack_sdk::AsyncOpenStack;
29
30use openstack_sdk::api::QueryAsync;
31use openstack_sdk::api::network::v2::subnet::list;
32use openstack_sdk::api::{Pagination, paged};
33use openstack_types::network::v2::subnet::response;
34
35/// Lists subnets that the project has access to.
36///
37/// Default policy settings return only subnets owned by the project of the
38/// user submitting the request, unless the user has administrative role. You
39/// can control which attributes are returned by using the fields query
40/// parameter. You can filter results by using query string parameters.
41///
42/// Standard query parameters are supported on the URI. For more information,
43/// see [Filtering and Column Selection](#filtering).
44///
45/// Use the `fields` query parameter to control which fields are returned in
46/// the response body. For more information, see [Fields](#fields).
47///
48/// Pagination query parameters are supported if Neutron configuration supports
49/// it by overriding `allow_pagination=false`. For more information, see
50/// [Pagination](#pagination).
51///
52/// Sorting query parameters are supported if Neutron configuration supports it
53/// with `allow_sorting=true`. For more information, see [Sorting](#sorting).
54///
55/// Normal response codes: 200
56///
57/// Error response codes: 401
58#[derive(Args)]
59#[command(about = "List subnets")]
60pub struct SubnetsCommand {
61    /// Request Query parameters
62    #[command(flatten)]
63    query: QueryParameters,
64
65    /// Path parameters
66    #[command(flatten)]
67    path: PathParameters,
68
69    /// Total limit of entities count to return. Use this when there are too many entries.
70    #[arg(long, default_value_t = 10000)]
71    max_items: usize,
72}
73
74/// Query parameters
75#[derive(Args)]
76struct QueryParameters {
77    /// cidr query parameter for /v2.0/subnets API
78    #[arg(help_heading = "Query parameters", long)]
79    cidr: Option<String>,
80
81    /// description query parameter for /v2.0/subnets API
82    #[arg(help_heading = "Query parameters", long)]
83    description: Option<String>,
84
85    /// enable_dhcp query parameter for /v2.0/subnets API
86    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
87    enable_dhcp: Option<bool>,
88
89    /// gateway_ip query parameter for /v2.0/subnets API
90    #[arg(help_heading = "Query parameters", long)]
91    gateway_ip: Option<String>,
92
93    /// id query parameter for /v2.0/subnets API
94    #[arg(help_heading = "Query parameters", long)]
95    id: Option<String>,
96
97    /// ip_version query parameter for /v2.0/subnets API
98    #[arg(help_heading = "Query parameters", long)]
99    ip_version: Option<i32>,
100
101    /// ipv6_address_mode query parameter for /v2.0/subnets API
102    #[arg(help_heading = "Query parameters", long, value_parser = ["dhcpv6-stateful","dhcpv6-stateless","slaac"])]
103    ipv6_address_mode: Option<String>,
104
105    /// ipv6_ra_mode query parameter for /v2.0/subnets API
106    #[arg(help_heading = "Query parameters", long, value_parser = ["dhcpv6-stateful","dhcpv6-stateless","slaac"])]
107    ipv6_ra_mode: Option<String>,
108
109    /// Requests a page size of items. Returns a number of items up to a limit
110    /// value. Use the limit parameter to make an initial limited request and
111    /// use the ID of the last-seen item from the response as the marker
112    /// parameter value in a subsequent limited request.
113    #[arg(
114        help_heading = "Query parameters",
115        long("page-size"),
116        visible_alias("limit")
117    )]
118    limit: Option<u32>,
119
120    /// The ID of the last-seen item. Use the limit parameter to make an
121    /// initial limited request and use the ID of the last-seen item from the
122    /// response as the marker parameter value in a subsequent limited request.
123    #[arg(help_heading = "Query parameters", long)]
124    marker: Option<String>,
125
126    /// name query parameter for /v2.0/subnets API
127    #[arg(help_heading = "Query parameters", long)]
128    name: Option<String>,
129
130    /// network_id query parameter for /v2.0/subnets API
131    #[arg(help_heading = "Query parameters", long)]
132    network_id: Option<String>,
133
134    /// not-tags query parameter for /v2.0/subnets API
135    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
136    not_tags: Option<Vec<String>>,
137
138    /// not-tags-any query parameter for /v2.0/subnets API
139    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
140    not_tags_any: Option<Vec<String>>,
141
142    /// Reverse the page direction
143    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
144    page_reverse: Option<bool>,
145
146    /// revision_number query parameter for /v2.0/subnets API
147    #[arg(help_heading = "Query parameters", long)]
148    revision_number: Option<String>,
149
150    /// The membership of a subnet to an external network.
151    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
152    router_external: Option<bool>,
153
154    /// segment_id query parameter for /v2.0/subnets API
155    #[arg(help_heading = "Query parameters", long)]
156    segment_id: Option<String>,
157
158    /// shared query parameter for /v2.0/subnets API
159    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
160    shared: Option<bool>,
161
162    /// Sort direction. This is an optional feature and may be silently ignored
163    /// by the server.
164    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
165    sort_dir: Option<Vec<String>>,
166
167    /// Sort results by the attribute. This is an optional feature and may be
168    /// silently ignored by the server.
169    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
170    sort_key: Option<Vec<String>>,
171
172    /// subnetpool_id query parameter for /v2.0/subnets API
173    #[arg(help_heading = "Query parameters", long)]
174    subnetpool_id: Option<String>,
175
176    /// tags query parameter for /v2.0/subnets API
177    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
178    tags: Option<Vec<String>>,
179
180    /// tags-any query parameter for /v2.0/subnets API
181    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
182    tags_any: Option<Vec<String>>,
183
184    /// tenant_id query parameter for /v2.0/subnets API
185    #[arg(help_heading = "Query parameters", long)]
186    tenant_id: Option<String>,
187}
188
189/// Path parameters
190#[derive(Args)]
191struct PathParameters {}
192
193impl SubnetsCommand {
194    /// Perform command action
195    pub async fn take_action<C: CliArgs>(
196        &self,
197        parsed_args: &C,
198        client: &mut AsyncOpenStack,
199    ) -> Result<(), OpenStackCliError> {
200        info!("List Subnets");
201
202        let op = OutputProcessor::from_args(parsed_args, Some("network.subnet"), Some("list"));
203        op.validate_args(parsed_args)?;
204
205        let mut ep_builder = list::Request::builder();
206
207        // Set query parameters
208        if let Some(val) = &self.query.limit {
209            ep_builder.limit(*val);
210        }
211        if let Some(val) = &self.query.marker {
212            ep_builder.marker(val);
213        }
214        if let Some(val) = &self.query.page_reverse {
215            ep_builder.page_reverse(*val);
216        }
217        if let Some(val) = &self.query.sort_dir {
218            ep_builder.sort_dir(val.iter());
219        }
220        if let Some(val) = &self.query.sort_key {
221            ep_builder.sort_key(val.iter());
222        }
223        if let Some(val) = &self.query.cidr {
224            ep_builder.cidr(val);
225        }
226        if let Some(val) = &self.query.description {
227            ep_builder.description(val);
228        }
229        if let Some(val) = &self.query.enable_dhcp {
230            ep_builder.enable_dhcp(*val);
231        }
232        if let Some(val) = &self.query.gateway_ip {
233            ep_builder.gateway_ip(val);
234        }
235        if let Some(val) = &self.query.id {
236            ep_builder.id(val);
237        }
238        if let Some(val) = &self.query.ip_version {
239            ep_builder.ip_version(*val);
240        }
241        if let Some(val) = &self.query.ipv6_address_mode {
242            ep_builder.ipv6_address_mode(val);
243        }
244        if let Some(val) = &self.query.ipv6_ra_mode {
245            ep_builder.ipv6_ra_mode(val);
246        }
247        if let Some(val) = &self.query.name {
248            ep_builder.name(val);
249        }
250        if let Some(val) = &self.query.network_id {
251            ep_builder.network_id(val);
252        }
253        if let Some(val) = &self.query.not_tags {
254            ep_builder.not_tags(val.iter());
255        }
256        if let Some(val) = &self.query.not_tags_any {
257            ep_builder.not_tags_any(val.iter());
258        }
259        if let Some(val) = &self.query.revision_number {
260            ep_builder.revision_number(val);
261        }
262        if let Some(val) = &self.query.router_external {
263            ep_builder.router_external(*val);
264        }
265        if let Some(val) = &self.query.segment_id {
266            ep_builder.segment_id(val);
267        }
268        if let Some(val) = &self.query.shared {
269            ep_builder.shared(*val);
270        }
271        if let Some(val) = &self.query.subnetpool_id {
272            ep_builder.subnetpool_id(val);
273        }
274        if let Some(val) = &self.query.tags {
275            ep_builder.tags(val.iter());
276        }
277        if let Some(val) = &self.query.tags_any {
278            ep_builder.tags_any(val.iter());
279        }
280        if let Some(val) = &self.query.tenant_id {
281            ep_builder.tenant_id(val);
282        }
283
284        let ep = ep_builder
285            .build()
286            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
287
288        let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
289            .query_async(client)
290            .await?;
291
292        op.output_list::<response::list::SubnetResponse>(data.clone())?;
293        // Show command specific hints
294        op.show_command_hint()?;
295        Ok(())
296    }
297}