Skip to main content

openstack_cli_compute/v2/aggregate/
create_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//! Create Aggregate command [microversion = 2.1]
19//!
20//! Wraps invoking of the `v2.1/os-aggregates` with `POST` 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::create_21;
33use openstack_types::compute::v2::aggregate::response;
34
35/// Creates an aggregate. If specifying an option availability_zone, the
36/// aggregate is created as an availability zone and the availability zone is
37/// visible to normal users.
38///
39/// Normal response codes: 200
40///
41/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
42/// conflict(409)
43#[derive(Args)]
44#[command(about = "Create Aggregate (microversion = 2.1)")]
45pub struct AggregateCommand {
46    /// Request Query parameters
47    #[command(flatten)]
48    query: QueryParameters,
49
50    /// Path parameters
51    #[command(flatten)]
52    path: PathParameters,
53
54    /// The host aggregate object.
55    #[command(flatten)]
56    aggregate: Aggregate,
57}
58
59/// Query parameters
60#[derive(Args)]
61struct QueryParameters {}
62
63/// Path parameters
64#[derive(Args)]
65struct PathParameters {}
66/// Aggregate Body data
67#[derive(Args, Clone)]
68struct Aggregate {
69    /// The availability zone of the host aggregate. You should use a custom
70    /// availability zone rather than the default returned by the
71    /// os-availability-zone API. The availability zone must not include ‘:’ in
72    /// its name.
73    #[arg(help_heading = "Body parameters", long)]
74    availability_zone: Option<String>,
75
76    /// Set explicit NULL for the availability_zone
77    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "availability_zone")]
78    no_availability_zone: bool,
79
80    /// The name of the host aggregate.
81    #[arg(help_heading = "Body parameters", long)]
82    name: String,
83}
84
85impl AggregateCommand {
86    /// Perform command action
87    pub async fn take_action<C: CliArgs>(
88        &self,
89        parsed_args: &C,
90        client: &mut AsyncOpenStack,
91    ) -> Result<(), OpenStackCliError> {
92        info!("Create Aggregate");
93
94        let op = OutputProcessor::from_args(parsed_args, Some("compute.aggregate"), Some("create"));
95        op.validate_args(parsed_args)?;
96
97        let mut ep_builder = create_21::Request::builder();
98        ep_builder.header(
99            http::header::HeaderName::from_static("openstack-api-version"),
100            http::header::HeaderValue::from_static("compute 2.1"),
101        );
102
103        // Set body parameters
104        // Set Request.aggregate data
105        let args = &self.aggregate;
106        let mut aggregate_builder = create_21::AggregateBuilder::default();
107        if let Some(val) = &args.availability_zone {
108            aggregate_builder.availability_zone(Some(val.into()));
109        } else if args.no_availability_zone {
110            aggregate_builder.availability_zone(None);
111        }
112
113        aggregate_builder.name(&args.name);
114
115        ep_builder.aggregate(
116            aggregate_builder
117                .build()
118                .wrap_err("error preparing the request data")?,
119        );
120
121        let ep = ep_builder
122            .build()
123            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
124
125        let data: serde_json::Value = ep.query_async(client).await?;
126
127        op.output_single::<response::create_21::AggregateResponse>(data.clone())?;
128        // Show command specific hints
129        op.show_command_hint()?;
130        Ok(())
131    }
132}