Skip to main content

openstack_cli_compute/v2/hypervisor/
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 Hypervisors command
19//!
20//! Wraps invoking of the `v2.1/os-hypervisors/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::hypervisor::list_detailed;
32use openstack_sdk::api::{Pagination, paged};
33use openstack_types::compute::v2::hypervisor::response;
34
35/// Lists hypervisors details.
36///
37/// Policy defaults enable only users with the administrative role to perform
38/// this operation. Cloud providers can change these permissions through the
39/// `policy.yaml` file.
40///
41/// Normal response codes: 200
42///
43/// Error response codes: badRequest(400), unauthorized(401), forbidden(403)
44#[derive(Args)]
45#[command(about = "List Hypervisors Details")]
46pub struct HypervisorsCommand {
47    /// Request Query parameters
48    #[command(flatten)]
49    query: QueryParameters,
50
51    /// Path parameters
52    #[command(flatten)]
53    path: PathParameters,
54
55    /// Total limit of entities count to return. Use this when there are too many entries.
56    #[arg(long, default_value_t = 10000)]
57    max_items: usize,
58}
59
60/// Query parameters
61#[derive(Args)]
62struct QueryParameters {
63    #[arg(help_heading = "Query parameters", long)]
64    hypervisor_hostname_pattern: Option<String>,
65
66    /// Requests a page size of items. Returns a number of items up to a limit
67    /// value. Use the limit parameter to make an initial limited request and
68    /// use the ID of the last-seen item from the response as the marker
69    /// parameter value in a subsequent limited request.
70    #[arg(
71        help_heading = "Query parameters",
72        long("page-size"),
73        visible_alias("limit")
74    )]
75    limit: Option<u32>,
76
77    /// The ID of the last-seen item. Use the limit parameter to make an
78    /// initial limited request and use the ID of the last-seen item from the
79    /// response as the marker parameter value in a subsequent limited request.
80    #[arg(help_heading = "Query parameters", long)]
81    marker: Option<String>,
82
83    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
84    with_servers: Option<bool>,
85}
86
87/// Path parameters
88#[derive(Args)]
89struct PathParameters {}
90
91impl HypervisorsCommand {
92    /// Perform command action
93    pub async fn take_action<C: CliArgs>(
94        &self,
95        parsed_args: &C,
96        client: &mut AsyncOpenStack,
97    ) -> Result<(), OpenStackCliError> {
98        info!("List Hypervisors");
99
100        let op = OutputProcessor::from_args(parsed_args, Some("compute.hypervisor"), Some("list"));
101        op.validate_args(parsed_args)?;
102
103        let mut ep_builder = list_detailed::Request::builder();
104
105        // Set query parameters
106        if let Some(val) = &self.query.hypervisor_hostname_pattern {
107            ep_builder.hypervisor_hostname_pattern(val);
108        }
109        if let Some(val) = &self.query.limit {
110            ep_builder.limit(*val);
111        }
112        if let Some(val) = &self.query.marker {
113            ep_builder.marker(val);
114        }
115        if let Some(val) = &self.query.with_servers {
116            ep_builder.with_servers(*val);
117        }
118
119        let ep = ep_builder
120            .build()
121            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
122
123        let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
124            .query_async(client)
125            .await?;
126
127        op.output_list::<response::list_detailed_21::HypervisorResponse>(data.clone())
128            .or_else(|_| {
129                op.output_list::<response::list_detailed_228::HypervisorResponse>(data.clone())
130            })
131            .or_else(|_| {
132                op.output_list::<response::list_detailed_233::HypervisorResponse>(data.clone())
133            })
134            .or_else(|_| {
135                op.output_list::<response::list_detailed_253::HypervisorResponse>(data.clone())
136            })
137            .or_else(|_| {
138                op.output_list::<response::list_detailed_288::HypervisorResponse>(data.clone())
139            })?;
140        // Show command specific hints
141        op.show_command_hint()?;
142        Ok(())
143    }
144}