Skip to main content

openstack_cli_compute/v2/server_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 ServerGroups command
19//!
20//! Wraps invoking of the `v2.1/os-server-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::compute::v2::server_group::list;
32use openstack_sdk::api::{Pagination, paged};
33use openstack_types::compute::v2::server_group::response;
34
35/// Lists all server groups for the tenant.
36///
37/// Administrative users can use the `all_projects` query parameter to list all
38/// server groups for all projects.
39///
40/// Normal response codes: 200
41///
42/// Error response codes: unauthorized(401), forbidden(403)
43#[derive(Args)]
44#[command(about = "List Server Groups")]
45pub struct ServerGroupsCommand {
46    /// Request Query parameters
47    #[command(flatten)]
48    query: QueryParameters,
49
50    /// Path parameters
51    #[command(flatten)]
52    path: PathParameters,
53
54    /// Total limit of entities count to return. Use this when there are too many entries.
55    #[arg(long, default_value_t = 10000)]
56    max_items: usize,
57}
58
59/// Query parameters
60#[derive(Args)]
61struct QueryParameters {
62    #[arg(help_heading = "Query parameters", long)]
63    all_projects: Option<String>,
64
65    /// Requests a page size of items. Returns a number of items up to a limit
66    /// value. Use the limit parameter to make an initial limited request and
67    /// use the ID of the last-seen item from the response as the marker
68    /// parameter value in a subsequent limited request.
69    #[arg(
70        help_heading = "Query parameters",
71        long("page-size"),
72        visible_alias("limit")
73    )]
74    limit: Option<u32>,
75
76    #[arg(help_heading = "Query parameters", long)]
77    offset: Option<u32>,
78}
79
80/// Path parameters
81#[derive(Args)]
82struct PathParameters {}
83
84impl ServerGroupsCommand {
85    /// Perform command action
86    pub async fn take_action<C: CliArgs>(
87        &self,
88        parsed_args: &C,
89        client: &mut AsyncOpenStack,
90    ) -> Result<(), OpenStackCliError> {
91        info!("List ServerGroups");
92
93        let op =
94            OutputProcessor::from_args(parsed_args, Some("compute.server_group"), Some("list"));
95        op.validate_args(parsed_args)?;
96
97        let mut ep_builder = list::Request::builder();
98
99        // Set query parameters
100        if let Some(val) = &self.query.all_projects {
101            ep_builder.all_projects(val);
102        }
103        if let Some(val) = &self.query.limit {
104            ep_builder.limit(*val);
105        }
106        if let Some(val) = &self.query.offset {
107            ep_builder.offset(*val);
108        }
109
110        let ep = ep_builder
111            .build()
112            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
113
114        let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
115            .query_async(client)
116            .await?;
117
118        op.output_list::<response::list_21::ServerGroupResponse>(data.clone())
119            .or_else(|_| op.output_list::<response::list_213::ServerGroupResponse>(data.clone()))
120            .or_else(|_| op.output_list::<response::list_215::ServerGroupResponse>(data.clone()))
121            .or_else(|_| op.output_list::<response::list_264::ServerGroupResponse>(data.clone()))?;
122        // Show command specific hints
123        op.show_command_hint()?;
124        Ok(())
125    }
126}