Skip to main content

openstack_cli_network/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.0/flavors/{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::network::v2::flavor::find;
34use openstack_sdk::api::network::v2::flavor::set;
35use openstack_types::network::v2::flavor::response;
36
37/// Updates a flavor.
38///
39/// The service_type cannot be updated as there may be associated service
40/// profiles and consumers depending on the value.
41///
42/// Normal response codes: 200
43///
44/// Error response codes: 400, 401, 403, 404
45#[derive(Args)]
46#[command(about = "Update flavor")]
47pub struct FlavorCommand {
48    /// Request Query parameters
49    #[command(flatten)]
50    query: QueryParameters,
51
52    /// Path parameters
53    #[command(flatten)]
54    path: PathParameters,
55
56    /// A `flavor` object.
57    #[command(flatten)]
58    flavor: Flavor,
59}
60
61/// Query parameters
62#[derive(Args)]
63struct QueryParameters {}
64
65/// Path parameters
66#[derive(Args)]
67struct PathParameters {
68    /// id parameter for /v2.0/flavors/{id} API
69    #[arg(
70        help_heading = "Path parameters",
71        id = "path_param_id",
72        value_name = "ID"
73    )]
74    id: String,
75}
76/// Flavor Body data
77#[derive(Args, Clone)]
78struct Flavor {
79    /// The human-readable description for the flavor.
80    #[arg(help_heading = "Body parameters", long)]
81    description: Option<String>,
82
83    /// Set explicit NULL for the description
84    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
85    no_description: bool,
86
87    /// Indicates whether the flavor is enabled or not. Default is true.
88    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
89    enabled: Option<Option<bool>>,
90
91    /// Name of the flavor.
92    #[arg(help_heading = "Body parameters", long)]
93    name: Option<String>,
94
95    /// Parameter is an array, may be provided multiple times.
96    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
97    service_profiles: Option<Vec<String>>,
98}
99
100impl FlavorCommand {
101    /// Perform command action
102    pub async fn take_action<C: CliArgs>(
103        &self,
104        parsed_args: &C,
105        client: &mut AsyncOpenStack,
106    ) -> Result<(), OpenStackCliError> {
107        info!("Set Flavor");
108
109        let op = OutputProcessor::from_args(parsed_args, Some("network.flavor"), Some("set"));
110        op.validate_args(parsed_args)?;
111
112        let mut find_builder = find::Request::builder();
113
114        find_builder.id(&self.path.id);
115
116        let find_ep = find_builder
117            .build()
118            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
119        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
120
121        let mut ep_builder = set::Request::builder();
122
123        let resource_id = find_data["id"]
124            .as_str()
125            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
126            .to_string();
127        ep_builder.id(resource_id.clone());
128
129        // Set body parameters
130        // Set Request.flavor data
131        let args = &self.flavor;
132        let mut flavor_builder = set::FlavorBuilder::default();
133        if let Some(val) = &args.description {
134            flavor_builder.description(Some(val.into()));
135        } else if args.no_description {
136            flavor_builder.description(None);
137        }
138
139        if let Some(val) = &args.enabled {
140            flavor_builder.enabled(*val);
141        }
142
143        if let Some(val) = &args.name {
144            flavor_builder.name(val);
145        }
146
147        if let Some(val) = &args.service_profiles {
148            flavor_builder.service_profiles(val.iter().map(Into::into).collect::<Vec<_>>());
149        }
150
151        ep_builder.flavor(
152            flavor_builder
153                .build()
154                .wrap_err("error preparing the request data")?,
155        );
156
157        let ep = ep_builder
158            .build()
159            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
160
161        let data: serde_json::Value = ep.query_async(client).await?;
162
163        op.output_single::<response::set::FlavorResponse>(data.clone())?;
164        // Show command specific hints
165        op.show_command_hint()?;
166        Ok(())
167    }
168}