Skip to main content

openstack_cli_compute/v2/flavor/
set_255.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 [microversion = 2.55]
19//!
20//! Wraps invoking of the `v2.1/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::compute::v2::flavor::find;
33use openstack_sdk::api::compute::v2::flavor::set_255;
34use openstack_sdk::api::find;
35use openstack_types::compute::v2::flavor::response;
36
37/// Updates a flavor description.
38///
39/// This API is available starting with microversion 2.55.
40///
41/// Policy defaults enable only users with the administrative role to perform
42/// this operation. Cloud providers can change these permissions through the
43/// `policy.yaml` file.
44///
45/// Normal response codes: 200
46///
47/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
48/// itemNotFound(404)
49#[derive(Args)]
50#[command(about = "Update Flavor Description (microversion = 2.55)")]
51pub struct FlavorCommand {
52    /// Request Query parameters
53    #[command(flatten)]
54    query: QueryParameters,
55
56    /// Path parameters
57    #[command(flatten)]
58    path: PathParameters,
59
60    /// The ID and links for the flavor for your server instance. A flavor is a
61    /// combination of memory, disk size, and CPUs.
62    #[command(flatten)]
63    flavor: Flavor,
64}
65
66/// Query parameters
67#[derive(Args)]
68struct QueryParameters {}
69
70/// Path parameters
71#[derive(Args)]
72struct PathParameters {
73    /// id parameter for /v2.1/flavors/{id} API
74    #[arg(
75        help_heading = "Path parameters",
76        id = "path_param_id",
77        value_name = "ID"
78    )]
79    id: String,
80}
81/// Flavor Body data
82#[derive(Args, Clone)]
83struct Flavor {
84    /// A free form description of the flavor. Limited to 65535 characters in
85    /// length. Only printable characters are allowed.
86    #[arg(help_heading = "Body parameters", long)]
87    description: String,
88}
89
90impl FlavorCommand {
91    /// Perform command action
92    pub async fn take_action<C: CliArgs>(
93        &self,
94        parsed_args: &C,
95        client: &mut AsyncOpenStack,
96    ) -> Result<(), OpenStackCliError> {
97        info!("Set Flavor");
98
99        let op = OutputProcessor::from_args(parsed_args, Some("compute.flavor"), Some("set"));
100        op.validate_args(parsed_args)?;
101
102        let mut find_builder = find::Request::builder();
103
104        find_builder.id(&self.path.id);
105        find_builder.header(
106            http::header::HeaderName::from_static("openstack-api-version"),
107            http::header::HeaderValue::from_static("compute 2.55"),
108        );
109
110        let find_ep = find_builder
111            .build()
112            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
113        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
114
115        let mut ep_builder = set_255::Request::builder();
116        ep_builder.header(
117            http::header::HeaderName::from_static("openstack-api-version"),
118            http::header::HeaderValue::from_static("compute 2.55"),
119        );
120
121        let resource_id = find_data["id"]
122            .as_str()
123            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
124            .to_string();
125        ep_builder.id(resource_id.clone());
126
127        // Set body parameters
128        // Set Request.flavor data
129        let args = &self.flavor;
130        let mut flavor_builder = set_255::FlavorBuilder::default();
131
132        flavor_builder.description(args.description.clone());
133
134        ep_builder.flavor(
135            flavor_builder
136                .build()
137                .wrap_err("error preparing the request data")?,
138        );
139
140        let ep = ep_builder
141            .build()
142            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
143
144        let data: serde_json::Value = ep.query_async(client).await?;
145
146        op.output_single::<response::set_255::FlavorResponse>(data.clone())?;
147        // Show command specific hints
148        op.show_command_hint()?;
149        Ok(())
150    }
151}