Skip to main content

openstack_cli_load_balancer/v2/flavor/
set.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 Flavor command
19//!
20//! Wraps invoking of the `v2/lbaas/flavors/{flavor_id}` with `PUT` method
21
22use clap::Args;
23use eyre::WrapErr;
24use tracing::info;
25
26use openstack_cli_core::cli::CliArgs;
27use openstack_cli_core::error::OpenStackCliError;
28use openstack_cli_core::output::OutputProcessor;
29use openstack_sdk::AsyncOpenStack;
30
31use openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::find;
33use openstack_sdk::api::load_balancer::v2::flavor::find;
34use openstack_sdk::api::load_balancer::v2::flavor::set;
35use openstack_types::load_balancer::v2::flavor::response;
36
37/// Command without description in OpenAPI
38#[derive(Args)]
39pub struct FlavorCommand {
40    /// Request Query parameters
41    #[command(flatten)]
42    query: QueryParameters,
43
44    /// Path parameters
45    #[command(flatten)]
46    path: PathParameters,
47
48    /// Defines the attributes of a PUT request.
49    #[command(flatten)]
50    flavor: Flavor,
51}
52
53/// Query parameters
54#[derive(Args)]
55struct QueryParameters {}
56
57/// Path parameters
58#[derive(Args)]
59struct PathParameters {
60    /// flavor_id parameter for /v2/lbaas/flavors/{flavor_id} API
61    #[arg(
62        help_heading = "Path parameters",
63        id = "path_param_id",
64        value_name = "ID"
65    )]
66    id: String,
67}
68/// Flavor Body data
69#[derive(Args, Clone)]
70struct Flavor {
71    #[arg(help_heading = "Body parameters", long)]
72    description: Option<String>,
73
74    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
75    enabled: Option<bool>,
76
77    #[arg(help_heading = "Body parameters", long)]
78    name: Option<String>,
79}
80
81impl FlavorCommand {
82    /// Perform command action
83    pub async fn take_action<C: CliArgs>(
84        &self,
85        parsed_args: &C,
86        client: &mut AsyncOpenStack,
87    ) -> Result<(), OpenStackCliError> {
88        info!("Set Flavor");
89
90        let op = OutputProcessor::from_args(parsed_args, Some("load-balancer.flavor"), Some("set"));
91        op.validate_args(parsed_args)?;
92
93        let mut find_builder = find::Request::builder();
94
95        find_builder.id(&self.path.id);
96
97        let find_ep = find_builder
98            .build()
99            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
100        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
101
102        let mut ep_builder = set::Request::builder();
103
104        let resource_id = find_data["id"]
105            .as_str()
106            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
107            .to_string();
108        ep_builder.id(resource_id.clone());
109
110        // Set body parameters
111        // Set Request.flavor data
112        let args = &self.flavor;
113        let mut flavor_builder = set::FlavorBuilder::default();
114        if let Some(val) = &args.description {
115            flavor_builder.description(val);
116        }
117
118        if let Some(val) = &args.enabled {
119            flavor_builder.enabled(*val);
120        }
121
122        if let Some(val) = &args.name {
123            flavor_builder.name(val);
124        }
125
126        ep_builder.flavor(
127            flavor_builder
128                .build()
129                .wrap_err("error preparing the request data")?,
130        );
131
132        let ep = ep_builder
133            .build()
134            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
135
136        let data: serde_json::Value = ep.query_async(client).await?;
137
138        op.output_single::<response::set::FlavorResponse>(data.clone())?;
139        // Show command specific hints
140        op.show_command_hint()?;
141        Ok(())
142    }
143}