Skip to main content

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