Skip to main content

openstack_cli_network/v2/security_group/
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 SecurityGroups command
19//!
20//! Wraps invoking of the `v2.0/security-groups` 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::security_group::list;
32use openstack_sdk::api::{Pagination, paged};
33use openstack_types::network::v2::security_group::response;
34
35/// Lists OpenStack Networking security groups to which the project has access.
36///
37/// The response is an array of `security_group` objects which contains a list
38/// of `security_group_rules` objects.
39///
40/// Standard query parameters are supported on the URI. For more information,
41/// see [Filtering and Column Selection](#filtering).
42///
43/// Use the `fields` query parameter to control which fields are returned in
44/// the response body. For more information, see [Fields](#fields).
45///
46/// Pagination query parameters are supported if Neutron configuration supports
47/// it by overriding `allow_pagination=false`. For more information, see
48/// [Pagination](#pagination).
49///
50/// Sorting query parameters are supported if Neutron configuration supports it
51/// with `allow_sorting=true`. For more information, see [Sorting](#sorting).
52///
53/// Normal response codes: 200
54///
55/// Error response codes: 401
56#[derive(Args)]
57#[command(about = "List security groups")]
58pub struct SecurityGroupsCommand {
59    /// Request Query parameters
60    #[command(flatten)]
61    query: QueryParameters,
62
63    /// Path parameters
64    #[command(flatten)]
65    path: PathParameters,
66
67    /// Total limit of entities count to return. Use this when there are too many entries.
68    #[arg(long, default_value_t = 10000)]
69    max_items: usize,
70}
71
72/// Query parameters
73#[derive(Args)]
74struct QueryParameters {
75    /// description query parameter for /v2.0/security-groups API
76    #[arg(help_heading = "Query parameters", long)]
77    description: Option<String>,
78
79    /// id query parameter for /v2.0/security-groups API
80    #[arg(help_heading = "Query parameters", long)]
81    id: Option<String>,
82
83    /// Requests a page size of items. Returns a number of items up to a limit
84    /// value. Use the limit parameter to make an initial limited request and
85    /// use the ID of the last-seen item from the response as the marker
86    /// parameter value in a subsequent limited request.
87    #[arg(
88        help_heading = "Query parameters",
89        long("page-size"),
90        visible_alias("limit")
91    )]
92    limit: Option<u32>,
93
94    /// The ID of the last-seen item. Use the limit parameter to make an
95    /// initial limited request and use the ID of the last-seen item from the
96    /// response as the marker parameter value in a subsequent limited request.
97    #[arg(help_heading = "Query parameters", long)]
98    marker: Option<String>,
99
100    /// name query parameter for /v2.0/security-groups API
101    #[arg(help_heading = "Query parameters", long)]
102    name: Option<String>,
103
104    /// not-tags query parameter for /v2.0/security-groups API
105    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
106    not_tags: Option<Vec<String>>,
107
108    /// not-tags-any query parameter for /v2.0/security-groups API
109    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
110    not_tags_any: Option<Vec<String>>,
111
112    /// Reverse the page direction
113    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
114    page_reverse: Option<bool>,
115
116    /// revision_number query parameter for /v2.0/security-groups API
117    #[arg(help_heading = "Query parameters", long)]
118    revision_number: Option<String>,
119
120    /// shared query parameter for /v2.0/security-groups API
121    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
122    shared: Option<bool>,
123
124    /// Sort direction. This is an optional feature and may be silently ignored
125    /// by the server.
126    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
127    sort_dir: Option<Vec<String>>,
128
129    /// Sort results by the attribute. This is an optional feature and may be
130    /// silently ignored by the server.
131    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
132    sort_key: Option<Vec<String>>,
133
134    /// tags query parameter for /v2.0/security-groups API
135    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
136    tags: Option<Vec<String>>,
137
138    /// tags-any query parameter for /v2.0/security-groups API
139    #[arg(action=clap::ArgAction::Append, help_heading = "Query parameters", long)]
140    tags_any: Option<Vec<String>>,
141
142    /// tenant_id query parameter for /v2.0/security-groups API
143    #[arg(help_heading = "Query parameters", long)]
144    tenant_id: Option<String>,
145}
146
147/// Path parameters
148#[derive(Args)]
149struct PathParameters {}
150
151impl SecurityGroupsCommand {
152    /// Perform command action
153    pub async fn take_action<C: CliArgs>(
154        &self,
155        parsed_args: &C,
156        client: &mut AsyncOpenStack,
157    ) -> Result<(), OpenStackCliError> {
158        info!("List SecurityGroups");
159
160        let op =
161            OutputProcessor::from_args(parsed_args, Some("network.security_group"), Some("list"));
162        op.validate_args(parsed_args)?;
163
164        let mut ep_builder = list::Request::builder();
165
166        // Set query parameters
167        if let Some(val) = &self.query.limit {
168            ep_builder.limit(*val);
169        }
170        if let Some(val) = &self.query.marker {
171            ep_builder.marker(val);
172        }
173        if let Some(val) = &self.query.page_reverse {
174            ep_builder.page_reverse(*val);
175        }
176        if let Some(val) = &self.query.description {
177            ep_builder.description(val);
178        }
179        if let Some(val) = &self.query.id {
180            ep_builder.id(val);
181        }
182        if let Some(val) = &self.query.name {
183            ep_builder.name(val);
184        }
185        if let Some(val) = &self.query.not_tags {
186            ep_builder.not_tags(val.iter());
187        }
188        if let Some(val) = &self.query.not_tags_any {
189            ep_builder.not_tags_any(val.iter());
190        }
191        if let Some(val) = &self.query.revision_number {
192            ep_builder.revision_number(val);
193        }
194        if let Some(val) = &self.query.shared {
195            ep_builder.shared(*val);
196        }
197        if let Some(val) = &self.query.tags {
198            ep_builder.tags(val.iter());
199        }
200        if let Some(val) = &self.query.tags_any {
201            ep_builder.tags_any(val.iter());
202        }
203        if let Some(val) = &self.query.tenant_id {
204            ep_builder.tenant_id(val);
205        }
206        if let Some(val) = &self.query.sort_dir {
207            ep_builder.sort_dir(val.iter());
208        }
209        if let Some(val) = &self.query.sort_key {
210            ep_builder.sort_key(val.iter());
211        }
212
213        let ep = ep_builder
214            .build()
215            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
216
217        let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
218            .query_async(client)
219            .await?;
220
221        op.output_list::<response::list::SecurityGroupResponse>(data.clone())?;
222        // Show command specific hints
223        op.show_command_hint()?;
224        Ok(())
225    }
226}