Skip to main content

openstack_cli_compute/v2/server/
set_21.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.1]
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_21;
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.1)")]
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    /// The server name.
94    #[arg(help_heading = "Body parameters", long)]
95    name: Option<String>,
96
97    /// Controls how the API partitions the disk when you create, rebuild, or
98    /// resize servers. A server inherits the `OS-DCF:diskConfig` value from
99    /// the image from which it was created, and an image inherits the
100    /// `OS-DCF:diskConfig` value from the server from which it was created. To
101    /// override the inherited setting, you can include this attribute in the
102    /// request body of a server create, rebuild, or resize request. If the
103    /// `OS-DCF:diskConfig` value for an image is `MANUAL`, you cannot create a
104    /// server from that image and set its `OS-DCF:diskConfig` value to `AUTO`.
105    /// A valid value is:
106    ///
107    /// - `AUTO`. The API builds the server with a single partition the size of
108    ///   the target flavor disk. The API automatically adjusts the file system
109    ///   to fit the entire partition.
110    /// - `MANUAL`. The API builds the server by using whatever partition
111    ///   scheme and file system is in the source image. If the target flavor
112    ///   disk is larger, the API does not partition the remaining disk space.
113    #[arg(help_heading = "Body parameters", long)]
114    os_dcf_disk_config: Option<OsDcfDiskConfig>,
115}
116
117impl ServerCommand {
118    /// Perform command action
119    pub async fn take_action<C: CliArgs>(
120        &self,
121        parsed_args: &C,
122        client: &mut AsyncOpenStack,
123    ) -> Result<(), OpenStackCliError> {
124        info!("Set Server");
125
126        let op = OutputProcessor::from_args(parsed_args, Some("compute.server"), Some("set"));
127        op.validate_args(parsed_args)?;
128
129        let mut find_builder = find::Request::builder();
130
131        find_builder.id(&self.path.id);
132        find_builder.header(
133            http::header::HeaderName::from_static("openstack-api-version"),
134            http::header::HeaderValue::from_static("compute 2.1"),
135        );
136
137        let find_ep = find_builder
138            .build()
139            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
140        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;
141
142        let mut ep_builder = set_21::Request::builder();
143        ep_builder.header(
144            http::header::HeaderName::from_static("openstack-api-version"),
145            http::header::HeaderValue::from_static("compute 2.1"),
146        );
147
148        let resource_id = find_data["id"]
149            .as_str()
150            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
151            .to_string();
152        ep_builder.id(resource_id.clone());
153
154        // Set body parameters
155        // Set Request.server data
156        let args = &self.server;
157        let mut server_builder = set_21::ServerBuilder::default();
158        if let Some(val) = &args.os_dcf_disk_config {
159            let tmp = match val {
160                OsDcfDiskConfig::Auto => set_21::OsDcfDiskConfig::Auto,
161                OsDcfDiskConfig::Manual => set_21::OsDcfDiskConfig::Manual,
162            };
163            server_builder.os_dcf_disk_config(tmp);
164        }
165
166        if let Some(val) = &args.access_ipv4 {
167            server_builder.access_ipv4(val);
168        }
169
170        if let Some(val) = &args.access_ipv6 {
171            server_builder.access_ipv6(val);
172        }
173
174        if let Some(val) = &args.name {
175            server_builder.name(val);
176        }
177
178        ep_builder.server(
179            server_builder
180                .build()
181                .wrap_err("error preparing the request data")?,
182        );
183
184        let ep = ep_builder
185            .build()
186            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
187
188        let data: serde_json::Value = ep.query_async(client).await?;
189
190        op.output_single::<response::set_20::ServerResponse>(data.clone())?;
191        // Show command specific hints
192        op.show_command_hint()?;
193        Ok(())
194    }
195}