Skip to main content

openstack_cli_compute/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.1/flavors/detail` 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::compute::v2::flavor::list_detailed;
32use openstack_sdk::api::{Pagination, paged};
33use openstack_types::compute::v2::flavor::response;
34
35/// Lists flavors with details.
36///
37/// Normal response codes: 200
38///
39/// Error response codes: badRequest(400), unauthorized(401), forbidden(403)
40#[derive(Args)]
41#[command(about = "List Flavors With Details")]
42pub struct FlavorsCommand {
43    /// Request Query parameters
44    #[command(flatten)]
45    query: QueryParameters,
46
47    /// Path parameters
48    #[command(flatten)]
49    path: PathParameters,
50
51    /// Total limit of entities count to return. Use this when there are too many entries.
52    #[arg(long, default_value_t = 10000)]
53    max_items: usize,
54}
55
56/// Query parameters
57#[derive(Args)]
58struct QueryParameters {
59    #[arg(help_heading = "Query parameters", long)]
60    is_public: Option<String>,
61
62    /// Requests a page size of items. Returns a number of items up to a limit
63    /// value. Use the limit parameter to make an initial limited request and
64    /// use the ID of the last-seen item from the response as the marker
65    /// parameter value in a subsequent limited request.
66    #[arg(
67        help_heading = "Query parameters",
68        long("page-size"),
69        visible_alias("limit")
70    )]
71    limit: Option<u32>,
72
73    /// The ID of the last-seen item. Use the limit parameter to make an
74    /// initial limited request and use the ID of the last-seen item from the
75    /// response as the marker parameter value in a subsequent limited request.
76    #[arg(help_heading = "Query parameters", long)]
77    marker: Option<String>,
78
79    #[arg(help_heading = "Query parameters", long)]
80    min_disk: Option<String>,
81
82    #[arg(help_heading = "Query parameters", long)]
83    min_ram: Option<String>,
84
85    #[arg(help_heading = "Query parameters", long)]
86    name: Option<String>,
87
88    #[arg(help_heading = "Query parameters", long, value_parser = ["asc","desc"])]
89    sort_dir: Option<String>,
90
91    #[arg(help_heading = "Query parameters", long, value_parser = ["created_at","description","disabled","ephemeral_gb","flavorid","id","is_public","memory_mb","name","root_gb","swap","updated_at","vcpu_weight","vcpus"])]
92    sort_key: Option<String>,
93}
94
95/// Path parameters
96#[derive(Args)]
97struct PathParameters {}
98
99impl FlavorsCommand {
100    /// Perform command action
101    pub async fn take_action<C: CliArgs>(
102        &self,
103        parsed_args: &C,
104        client: &mut AsyncOpenStack,
105    ) -> Result<(), OpenStackCliError> {
106        info!("List Flavors");
107
108        let op = OutputProcessor::from_args(parsed_args, Some("compute.flavor"), Some("list"));
109        op.validate_args(parsed_args)?;
110
111        let mut ep_builder = list_detailed::Request::builder();
112
113        // Set query parameters
114        if let Some(val) = &self.query.is_public {
115            ep_builder.is_public(val);
116        }
117        if let Some(val) = &self.query.limit {
118            ep_builder.limit(*val);
119        }
120        if let Some(val) = &self.query.marker {
121            ep_builder.marker(val);
122        }
123        if let Some(val) = &self.query.min_disk {
124            ep_builder.min_disk(val);
125        }
126        if let Some(val) = &self.query.min_ram {
127            ep_builder.min_ram(val);
128        }
129        if let Some(val) = &self.query.name {
130            ep_builder.name(val);
131        }
132        if let Some(val) = &self.query.sort_dir {
133            ep_builder.sort_dir(val);
134        }
135        if let Some(val) = &self.query.sort_key {
136            ep_builder.sort_key(val);
137        }
138
139        let ep = ep_builder
140            .build()
141            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
142
143        let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
144            .query_async(client)
145            .await?;
146
147        op.output_list::<response::list_detailed_20::FlavorResponse>(data.clone())
148            .or_else(|_| {
149                op.output_list::<response::list_detailed_2102::FlavorResponse>(data.clone())
150            })
151            .or_else(|_| {
152                op.output_list::<response::list_detailed_255::FlavorResponse>(data.clone())
153            })
154            .or_else(|_| {
155                op.output_list::<response::list_detailed_261::FlavorResponse>(data.clone())
156            })?;
157        // Show command specific hints
158        op.show_command_hint()?;
159        Ok(())
160    }
161}