Skip to main content

openstack_cli_placement/v1/allocation/
set_112.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 Allocation command [microversion = 1.12]
19//!
20//! Wraps invoking of the `allocations/{consumer_uuid}` with `PUT` 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_cli_core::common::parse_key_val;
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::placement::v1::allocation::set_112;
33use serde_json::Value;
34
35/// Create or update one or more allocation records representing the
36/// consumption of one or more classes of resources from one or more resource
37/// providers by the consumer identified by {consumer_uuid}. If allocations
38/// already exist for this consumer, they are replaced.
39///
40/// Normal Response Codes: 204
41///
42/// Error response codes: badRequest(400), itemNotFound(404), conflict(409)
43#[derive(Args)]
44#[command(about = "Update allocations (microversion = 1.12)")]
45pub struct AllocationCommand {
46    /// Request Query parameters
47    #[command(flatten)]
48    query: QueryParameters,
49
50    /// Path parameters
51    #[command(flatten)]
52    path: PathParameters,
53
54    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
55    allocations: Vec<(String, Value)>,
56
57    #[arg(help_heading = "Body parameters", long)]
58    project_id: String,
59
60    #[arg(help_heading = "Body parameters", long)]
61    user_id: String,
62}
63
64/// Query parameters
65#[derive(Args)]
66struct QueryParameters {}
67
68/// Path parameters
69#[derive(Args)]
70struct PathParameters {
71    /// consumer_uuid parameter for /allocations/{consumer_uuid} API
72    #[arg(
73        help_heading = "Path parameters",
74        id = "path_param_consumer_uuid",
75        value_name = "CONSUMER_UUID"
76    )]
77    consumer_uuid: String,
78}
79
80impl AllocationCommand {
81    /// Perform command action
82    pub async fn take_action<C: CliArgs>(
83        &self,
84        parsed_args: &C,
85        client: &mut AsyncOpenStack,
86    ) -> Result<(), OpenStackCliError> {
87        info!("Set Allocation");
88
89        let op = OutputProcessor::from_args(parsed_args, Some("placement.allocation"), Some("set"));
90        op.validate_args(parsed_args)?;
91
92        let mut ep_builder = set_112::Request::builder();
93        ep_builder.header(
94            http::header::HeaderName::from_static("openstack-api-version"),
95            http::header::HeaderValue::from_static("placement 1.12"),
96        );
97
98        ep_builder.consumer_uuid(&self.path.consumer_uuid);
99
100        // Set body parameters
101        // Set Request.allocations data
102
103        ep_builder.allocations(
104            self.allocations
105                .iter()
106                .map(|(k, v)| {
107                    serde_json::from_value(v.to_owned()).map(|v: set_112::AllocationsItem| (k, v))
108                })
109                .collect::<Result<Vec<_>, _>>()?
110                .into_iter(),
111        );
112
113        // Set Request.project_id data
114        ep_builder.project_id(&self.project_id);
115
116        // Set Request.user_id data
117        ep_builder.user_id(&self.user_id);
118
119        let ep = ep_builder
120            .build()
121            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
122        openstack_sdk::api::ignore(ep).query_async(client).await?;
123        // Show command specific hints
124        op.show_command_hint()?;
125        Ok(())
126    }
127}