Skip to main content

openstack_cli_placement/v1/allocation/
set_18.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.8]
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_sdk::api::QueryAsync;
31use openstack_sdk::api::placement::v1::allocation::set_18;
32use serde_json::Value;
33
34/// Create or update one or more allocation records representing the
35/// consumption of one or more classes of resources from one or more resource
36/// providers by the consumer identified by {consumer_uuid}. If allocations
37/// already exist for this consumer, they are replaced.
38///
39/// Normal Response Codes: 204
40///
41/// Error response codes: badRequest(400), itemNotFound(404), conflict(409)
42#[derive(Args)]
43#[command(about = "Update allocations (microversion = 1.8)")]
44pub struct AllocationCommand {
45    /// Request Query parameters
46    #[command(flatten)]
47    query: QueryParameters,
48
49    /// Path parameters
50    #[command(flatten)]
51    path: PathParameters,
52
53    /// Parameter is an array, may be provided multiple times.
54    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
55    allocations: Vec<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_18::Request::builder();
93        ep_builder.header(
94            http::header::HeaderName::from_static("openstack-api-version"),
95            http::header::HeaderValue::from_static("placement 1.8"),
96        );
97
98        ep_builder.consumer_uuid(&self.path.consumer_uuid);
99
100        // Set body parameters
101        // Set Request.allocations data
102
103        let allocations_builder: Vec<set_18::Allocations> = self
104            .allocations
105            .iter()
106            .flat_map(|v| serde_json::from_value::<set_18::Allocations>(v.to_owned()))
107            .collect::<Vec<set_18::Allocations>>();
108        ep_builder.allocations(allocations_builder);
109
110        // Set Request.project_id data
111        ep_builder.project_id(&self.project_id);
112
113        // Set Request.user_id data
114        ep_builder.user_id(&self.user_id);
115
116        let ep = ep_builder
117            .build()
118            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
119        openstack_sdk::api::ignore(ep).query_async(client).await?;
120        // Show command specific hints
121        op.show_command_hint()?;
122        Ok(())
123    }
124}