Skip to main content

openstack_cli_compute/v2/server/
set_219.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 Server command [microversion = 2.19]
19//!
20//! Wraps invoking of the `v2.1/servers/{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 clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::compute::v2::server::find;
34use openstack_sdk::api::compute::v2::server::set_219;
35use openstack_sdk::api::find;
36use openstack_types::compute::v2::server::response;
37
38/// Updates the editable attributes of an existing server.
39///
40/// Normal response codes: 200
41///
42/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
43/// itemNotFound(404)
44#[derive(Args)]
45#[command(about = "Update Server (microversion = 2.19)")]
46pub struct ServerCommand {
47    /// Request Query parameters
48    #[command(flatten)]
49    query: QueryParameters,
50
51    /// Path parameters
52    #[command(flatten)]
53    path: PathParameters,
54
55    /// A `server` object.
56    #[command(flatten)]
57    server: Server,
58}
59
60/// Query parameters
61#[derive(Args)]
62struct QueryParameters {}
63
64/// Path parameters
65#[derive(Args)]
66struct PathParameters {
67    /// id parameter for /v2.1/servers/{id} API
68    #[arg(
69        help_heading = "Path parameters",
70        id = "path_param_id",
71        value_name = "ID"
72    )]
73    id: String,
74}
75
76#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
77enum OsDcfDiskConfig {
78    Auto,
79    Manual,
80}
81
82/// Server Body data
83#[derive(Args, Clone)]
84struct Server {
85    /// IPv4 address that should be used to access this server.
86    #[arg(help_heading = "Body parameters", long)]
87    access_ipv4: Option<String>,
88
89    /// IPv6 address that should be used to access this server.
90    #[arg(help_heading = "Body parameters", long)]
91    access_ipv6: Option<String>,
92
93    /// A free form description of the server. Limited to 255 characters in
94    /// length. Before microversion 2.19 this was set to the server name.
95    ///
96    /// **New in version 2.19**
97    #[arg(help_heading = "Body parameters", long)]
98    description: Option<String>,
99
100    /// Set explicit NULL for the description
101    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
102    no_description: bool,
103
104    /// The server name.
105    #[arg(help_heading = "Body parameters", long)]
106    name: Option<String>,
107
108    /// Controls how the API partitions the disk when you create, rebuild, or
109    /// resize servers. A server inherits the `OS-DCF:diskConfig` value from
110    /// the image from which it was created, and an image inherits the
111    /// `OS-DCF:diskConfig` value from the server from which it was created. To
112    /// override the inherited setting, you can include this attribute in the
113    /// request body of a server create, rebuild, or resize request. If the
114    /// `OS-DCF:diskConfig` value for an image is `MANUAL`, you cannot create a
115    /// server from that image and set its `OS-DCF:diskConfig` value to `AUTO`.
116    /// A valid value is:
117    ///
118    /// - `AUTO`. The API builds the server with a single partition the size of
119    ///   the target flavor disk. The API automatically adjusts the file system
120    ///   to fit the entire partition.
121    /// - `MANUAL`. The API builds the server by using whatever partition
122    ///   scheme and file system is in the source image. If the target flavor
123    ///   disk is larger, the API does not partition the remaining disk space.
124    #[arg(help_heading = "Body parameters", long)]
125    os_dcf_disk_config: Option<OsDcfDiskConfig>,
126}
127
128impl ServerCommand {
129    /// Perform command action
130    pub async fn take_action<C: CliArgs>(
131        &self,
132        parsed_args: &C,
133        client: &mut AsyncOpenStack,
134    ) -> Result<(), OpenStackCliError> {
135        info!("Set Server");
136
137        let op = OutputProcessor::from_args(parsed_args, Some("compute.server"), Some("set"));
138        op.validate_args(parsed_args)?;
139
140        let mut find_builder = find::Request::builder();
141
142        find_builder.id(&self.path.id);
143        find_builder.header(
144            http::header::HeaderName::from_static("openstack-api-version"),
145            http::header::HeaderValue::from_static("compute 2.19"),
146        );
147
148        let find_ep = find_builder
149            .build()
150            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
151        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
152
153        let mut ep_builder = set_219::Request::builder();
154        ep_builder.header(
155            http::header::HeaderName::from_static("openstack-api-version"),
156            http::header::HeaderValue::from_static("compute 2.19"),
157        );
158
159        let resource_id = find_data["id"]
160            .as_str()
161            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
162            .to_string();
163        ep_builder.id(resource_id.clone());
164
165        // Set body parameters
166        // Set Request.server data
167        let args = &self.server;
168        let mut server_builder = set_219::ServerBuilder::default();
169        if let Some(val) = &args.os_dcf_disk_config {
170            let tmp = match val {
171                OsDcfDiskConfig::Auto => set_219::OsDcfDiskConfig::Auto,
172                OsDcfDiskConfig::Manual => set_219::OsDcfDiskConfig::Manual,
173            };
174            server_builder.os_dcf_disk_config(tmp);
175        }
176
177        if let Some(val) = &args.access_ipv4 {
178            server_builder.access_ipv4(val);
179        }
180
181        if let Some(val) = &args.access_ipv6 {
182            server_builder.access_ipv6(val);
183        }
184
185        if let Some(val) = &args.description {
186            server_builder.description(Some(val.into()));
187        } else if args.no_description {
188            server_builder.description(None);
189        }
190
191        if let Some(val) = &args.name {
192            server_builder.name(val);
193        }
194
195        ep_builder.server(
196            server_builder
197                .build()
198                .wrap_err("error preparing the request data")?,
199        );
200
201        let ep = ep_builder
202            .build()
203            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
204
205        let data: serde_json::Value = ep.query_async(client).await?;
206
207        op.output_single::<response::set_219::ServerResponse>(data.clone())?;
208        // Show command specific hints
209        op.show_command_hint()?;
210        Ok(())
211    }
212}