Skip to main content

openstack_cli_placement/v1/allocation/
set_138.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.38]
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_138;
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.38)")]
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    consumer_generation: Option<i32>,
59
60    #[arg(help_heading = "Body parameters", long)]
61    consumer_type: String,
62
63    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
64    mappings: Option<Vec<(String, Value)>>,
65
66    #[arg(help_heading = "Body parameters", long)]
67    project_id: String,
68
69    #[arg(help_heading = "Body parameters", long)]
70    user_id: String,
71}
72
73/// Query parameters
74#[derive(Args)]
75struct QueryParameters {}
76
77/// Path parameters
78#[derive(Args)]
79struct PathParameters {
80    /// consumer_uuid parameter for /allocations/{consumer_uuid} API
81    #[arg(
82        help_heading = "Path parameters",
83        id = "path_param_consumer_uuid",
84        value_name = "CONSUMER_UUID"
85    )]
86    consumer_uuid: String,
87}
88
89impl AllocationCommand {
90    /// Perform command action
91    pub async fn take_action<C: CliArgs>(
92        &self,
93        parsed_args: &C,
94        client: &mut AsyncOpenStack,
95    ) -> Result<(), OpenStackCliError> {
96        info!("Set Allocation");
97
98        let op = OutputProcessor::from_args(parsed_args, Some("placement.allocation"), Some("set"));
99        op.validate_args(parsed_args)?;
100
101        let mut ep_builder = set_138::Request::builder();
102        ep_builder.header(
103            http::header::HeaderName::from_static("openstack-api-version"),
104            http::header::HeaderValue::from_static("placement 1.38"),
105        );
106
107        ep_builder.consumer_uuid(&self.path.consumer_uuid);
108
109        // Set body parameters
110        // Set Request.allocations data
111
112        ep_builder.allocations(
113            self.allocations
114                .iter()
115                .map(|(k, v)| {
116                    serde_json::from_value(v.to_owned()).map(|v: set_138::AllocationsItem| (k, v))
117                })
118                .collect::<Result<Vec<_>, _>>()?
119                .into_iter(),
120        );
121
122        // Set Request.consumer_generation data
123        if let Some(val) = &self.consumer_generation {
124            ep_builder.consumer_generation(*val);
125        }
126
127        // Set Request.consumer_type data
128        ep_builder.consumer_type(&self.consumer_type);
129
130        // Set Request.mappings data
131        if let Some(arg) = &self.mappings {
132            ep_builder.mappings(
133                arg.iter()
134                    .map(|(k, v)| {
135                        serde_json::from_value::<Vec<String>>(v.to_owned())
136                            .map(|v| (k, v.into_iter()))
137                    })
138                    .collect::<Result<Vec<_>, _>>()?
139                    .into_iter(),
140            );
141        }
142
143        // Set Request.project_id data
144        ep_builder.project_id(&self.project_id);
145
146        // Set Request.user_id data
147        ep_builder.user_id(&self.user_id);
148
149        let ep = ep_builder
150            .build()
151            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
152        openstack_sdk::api::ignore(ep).query_async(client).await?;
153        // Show command specific hints
154        op.show_command_hint()?;
155        Ok(())
156    }
157}