Skip to main content

openstack_cli_placement/v1/resource_provider/inventory/
create.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 Inventory command
19//!
20//! Wraps invoking of the `resource_providers/{uuid}/inventories` with `POST` 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::resource_provider::inventory::create;
33use openstack_types::placement::v1::resource_provider::inventory::response;
34use serde_json::Value;
35
36/// POST to create one inventory.
37///
38/// On success return a 201 response, a location header pointing to the newly
39/// created inventory and an application/json representation of the inventory.
40#[derive(Args)]
41pub struct InventoryCommand {
42    /// Request Query parameters
43    #[command(flatten)]
44    query: QueryParameters,
45
46    /// Path parameters
47    #[command(flatten)]
48    path: PathParameters,
49
50    /// A dictionary of inventories keyed by resource classes.
51    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
52    inventories: Vec<(String, Value)>,
53
54    /// A consistent view marker that assists with the management of concurrent
55    /// resource provider updates.
56    #[arg(help_heading = "Body parameters", long)]
57    resource_provider_generation: i32,
58}
59
60/// Query parameters
61#[derive(Args)]
62struct QueryParameters {}
63
64/// Path parameters
65#[derive(Args)]
66struct PathParameters {
67    /// uuid parameter for
68    /// /resource_providers/{uuid}/inventories/{resource_class} API
69    #[arg(
70        help_heading = "Path parameters",
71        id = "path_param_uuid",
72        value_name = "UUID"
73    )]
74    uuid: String,
75}
76
77impl InventoryCommand {
78    /// Perform command action
79    pub async fn take_action<C: CliArgs>(
80        &self,
81        parsed_args: &C,
82        client: &mut AsyncOpenStack,
83    ) -> Result<(), OpenStackCliError> {
84        info!("Create Inventory");
85
86        let op = OutputProcessor::from_args(
87            parsed_args,
88            Some("placement.resource_provider/inventory"),
89            Some("create"),
90        );
91        op.validate_args(parsed_args)?;
92
93        let mut ep_builder = create::Request::builder();
94
95        ep_builder.uuid(&self.path.uuid);
96
97        // Set body parameters
98        // Set Request.inventories data
99
100        ep_builder.inventories(
101            self.inventories
102                .iter()
103                .map(|(k, v)| {
104                    serde_json::from_value(v.to_owned()).map(|v: create::InventoriesItem| (k, v))
105                })
106                .collect::<Result<Vec<_>, _>>()?
107                .into_iter(),
108        );
109
110        // Set Request.resource_provider_generation data
111        ep_builder.resource_provider_generation(self.resource_provider_generation);
112
113        let ep = ep_builder
114            .build()
115            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
116
117        let data: serde_json::Value = ep.query_async(client).await?;
118
119        op.output_single::<response::create::InventoryResponse>(data.clone())?;
120        // Show command specific hints
121        op.show_command_hint()?;
122        Ok(())
123    }
124}