Skip to main content

openstack_cli_compute/v2/aggregate/
set_21.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//! Set Aggregate command [microversion = 2.1]
19//!
20//! Wraps invoking of the `v2.1/os-aggregates/{id}` with `PUT` method
21
22use clap::Args;
23use eyre::WrapErr;
24use tracing::info;
25
26use openstack_cli_core::cli::CliArgs;
27use openstack_cli_core::error::OpenStackCliError;
28use openstack_cli_core::output::OutputProcessor;
29use openstack_sdk::AsyncOpenStack;
30
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::compute::v2::aggregate::find;
33use openstack_sdk::api::compute::v2::aggregate::set_21;
34use openstack_sdk::api::find;
35use openstack_types::compute::v2::aggregate::response;
36
37/// Updates either or both the name and availability zone for an aggregate. If
38/// the aggregate to be updated has host that already in the given availability
39/// zone, the request will fail with 400 error.
40///
41/// Normal response codes: 200
42///
43/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
44/// itemNotFound(404), conflict(409)
45#[derive(Args)]
46#[command(about = "Update Aggregate (microversion = 2.1)")]
47pub struct AggregateCommand {
48    /// Request Query parameters
49    #[command(flatten)]
50    query: QueryParameters,
51
52    /// Path parameters
53    #[command(flatten)]
54    path: PathParameters,
55
56    /// The host aggregate object.
57    #[command(flatten)]
58    aggregate: Aggregate,
59}
60
61/// Query parameters
62#[derive(Args)]
63struct QueryParameters {}
64
65/// Path parameters
66#[derive(Args)]
67struct PathParameters {
68    /// id parameter for /v2.1/os-aggregates/{id} API
69    #[arg(
70        help_heading = "Path parameters",
71        id = "path_param_id",
72        value_name = "ID"
73    )]
74    id: String,
75}
76/// Aggregate Body data
77#[derive(Args, Clone)]
78struct Aggregate {
79    /// The availability zone of the host aggregate. You should use a custom
80    /// availability zone rather than the default returned by the
81    /// os-availability-zone API. The availability zone must not include ‘:’ in
82    /// its name.
83    ///
84    /// Warning
85    ///
86    /// You should not change or unset the availability zone of an aggregate
87    /// when that aggregate has hosts which contain servers in it since that
88    /// may impact the ability for those servers to move to another host.
89    #[arg(help_heading = "Body parameters", long)]
90    availability_zone: Option<String>,
91
92    /// Set explicit NULL for the availability_zone
93    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "availability_zone")]
94    no_availability_zone: bool,
95
96    /// The name of the host aggregate.
97    #[arg(help_heading = "Body parameters", long)]
98    name: Option<String>,
99}
100
101impl AggregateCommand {
102    /// Perform command action
103    pub async fn take_action<C: CliArgs>(
104        &self,
105        parsed_args: &C,
106        client: &mut AsyncOpenStack,
107    ) -> Result<(), OpenStackCliError> {
108        info!("Set Aggregate");
109
110        let op = OutputProcessor::from_args(parsed_args, Some("compute.aggregate"), Some("set"));
111        op.validate_args(parsed_args)?;
112
113        let mut find_builder = find::Request::builder();
114
115        find_builder.id(&self.path.id);
116        find_builder.header(
117            http::header::HeaderName::from_static("openstack-api-version"),
118            http::header::HeaderValue::from_static("compute 2.1"),
119        );
120
121        let find_ep = find_builder
122            .build()
123            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
124        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
125
126        let mut ep_builder = set_21::Request::builder();
127        ep_builder.header(
128            http::header::HeaderName::from_static("openstack-api-version"),
129            http::header::HeaderValue::from_static("compute 2.1"),
130        );
131
132        let resource_id = find_data["id"]
133            .as_str()
134            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
135            .to_string();
136        ep_builder.id(resource_id.clone());
137
138        // Set body parameters
139        // Set Request.aggregate data
140        let args = &self.aggregate;
141        let mut aggregate_builder = set_21::AggregateBuilder::default();
142        if let Some(val) = &args.availability_zone {
143            aggregate_builder.availability_zone(Some(val.into()));
144        } else if args.no_availability_zone {
145            aggregate_builder.availability_zone(None);
146        }
147
148        if let Some(val) = &args.name {
149            aggregate_builder.name(val);
150        }
151
152        ep_builder.aggregate(
153            aggregate_builder
154                .build()
155                .wrap_err("error preparing the request data")?,
156        );
157
158        let ep = ep_builder
159            .build()
160            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
161
162        let data: serde_json::Value = ep.query_async(client).await?;
163
164        op.output_single::<response::set_21::AggregateResponse>(data.clone())?;
165        // Show command specific hints
166        op.show_command_hint()?;
167        Ok(())
168    }
169}