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