Skip to main content

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