Skip to main content

openstack_cli_compute/v2/server/
rebuild_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//! Action Server command [microversion = 2.19]
19//!
20//! Wraps invoking of the `v2.1/servers/{id}/action` with `POST` 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_cli_core::common::parse_key_val;
33use openstack_sdk::api::QueryAsync;
34use openstack_sdk::api::compute::v2::server::rebuild_219;
35use openstack_types::compute::v2::server::response;
36use serde_json::Value;
37
38/// Command without description in OpenAPI
39#[derive(Args)]
40#[command(about = "Rebuild Server (rebuild Action) (microversion = 2.19)")]
41pub struct ServerCommand {
42    /// Request Query parameters
43    #[command(flatten)]
44    query: QueryParameters,
45
46    /// Path parameters
47    #[command(flatten)]
48    path: PathParameters,
49
50    /// The action to rebuild a server.
51    #[command(flatten)]
52    rebuild: Rebuild,
53}
54
55/// Query parameters
56#[derive(Args)]
57struct QueryParameters {}
58
59/// Path parameters
60#[derive(Args)]
61struct PathParameters {
62    /// id parameter for /v2.1/servers/{id}/action API
63    #[arg(
64        help_heading = "Path parameters",
65        id = "path_param_id",
66        value_name = "ID"
67    )]
68    id: String,
69}
70
71#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
72enum OsDcfDiskConfig {
73    Auto,
74    Manual,
75}
76
77/// Rebuild Body data
78#[derive(Args, Clone)]
79struct Rebuild {
80    /// IPv4 address that should be used to access this server.
81    #[arg(help_heading = "Body parameters", long)]
82    access_ipv4: Option<String>,
83
84    /// IPv6 address that should be used to access this server.
85    #[arg(help_heading = "Body parameters", long)]
86    access_ipv6: Option<String>,
87
88    /// The administrative password of the server. If you omit this parameter,
89    /// the operation generates a new password.
90    #[arg(help_heading = "Body parameters", long)]
91    admin_pass: 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 UUID of the image to rebuild for your server instance. It must be a
105    /// valid UUID otherwise API will return 400. To rebuild a volume-backed
106    /// server with a new image, at least microversion 2.93 needs to be
107    /// provided in the request else the request will fall back to old
108    /// behaviour i.e. the API will return 400 (for an image different from the
109    /// image used when creating the volume). For non-volume-backed servers,
110    /// specifying a new image will result in validating that the image is
111    /// acceptable for the current compute host on which the server exists. If
112    /// the new image is not valid, the server will go into `ERROR` status.
113    #[arg(help_heading = "Body parameters", long)]
114    image_ref: String,
115
116    /// Metadata key and value pairs. The maximum size of the metadata key and
117    /// value is 255 bytes each.
118    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, String>)]
119    metadata: Option<Vec<(String, String)>>,
120
121    /// The server name.
122    #[arg(help_heading = "Body parameters", long)]
123    name: Option<String>,
124
125    /// Controls how the API partitions the disk when you create, rebuild, or
126    /// resize servers. A server inherits the `OS-DCF:diskConfig` value from
127    /// the image from which it was created, and an image inherits the
128    /// `OS-DCF:diskConfig` value from the server from which it was created. To
129    /// override the inherited setting, you can include this attribute in the
130    /// request body of a server create, rebuild, or resize request. If the
131    /// `OS-DCF:diskConfig` value for an image is `MANUAL`, you cannot create a
132    /// server from that image and set its `OS-DCF:diskConfig` value to `AUTO`.
133    /// A valid value is:
134    ///
135    /// - `AUTO`. The API builds the server with a single partition the size of
136    ///   the target flavor disk. The API automatically adjusts the file system
137    ///   to fit the entire partition.
138    /// - `MANUAL`. The API builds the server by using whatever partition
139    ///   scheme and file system is in the source image. If the target flavor
140    ///   disk is larger, the API does not partition the remaining disk space.
141    #[arg(help_heading = "Body parameters", long)]
142    os_dcf_disk_config: Option<OsDcfDiskConfig>,
143
144    /// The file path and contents, text only, to inject into the server at
145    /// launch. The maximum size of the file path data is 255 bytes. The
146    /// maximum limit is the number of allowed bytes in the decoded, rather
147    /// than encoded, data.
148    ///
149    /// **Available until version 2.56**
150    ///
151    /// Parameter is an array, may be provided multiple times.
152    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
153    personality: Option<Vec<Value>>,
154
155    /// Indicates whether the server is rebuilt with the preservation of the
156    /// ephemeral partition (`true`).
157    ///
158    /// Note
159    ///
160    /// This only works with baremetal servers provided by Ironic. Passing it
161    /// to any other server instance results in a fault and will prevent the
162    /// rebuild from happening.
163    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
164    preserve_ephemeral: Option<bool>,
165}
166
167impl ServerCommand {
168    /// Perform command action
169    pub async fn take_action<C: CliArgs>(
170        &self,
171        parsed_args: &C,
172        client: &mut AsyncOpenStack,
173    ) -> Result<(), OpenStackCliError> {
174        info!("Action Server");
175
176        let op = OutputProcessor::from_args(parsed_args, Some("compute.server"), Some("rebuild"));
177        op.validate_args(parsed_args)?;
178
179        let mut ep_builder = rebuild_219::Request::builder();
180        ep_builder.header(
181            http::header::HeaderName::from_static("openstack-api-version"),
182            http::header::HeaderValue::from_static("compute 2.19"),
183        );
184
185        ep_builder.id(&self.path.id);
186
187        // Set body parameters
188        // Set Request.rebuild data
189        let args = &self.rebuild;
190        let mut rebuild_builder = rebuild_219::RebuildBuilder::default();
191        if let Some(val) = &args.os_dcf_disk_config {
192            let tmp = match val {
193                OsDcfDiskConfig::Auto => rebuild_219::OsDcfDiskConfig::Auto,
194                OsDcfDiskConfig::Manual => rebuild_219::OsDcfDiskConfig::Manual,
195            };
196            rebuild_builder.os_dcf_disk_config(tmp);
197        }
198
199        if let Some(val) = &args.access_ipv4 {
200            rebuild_builder.access_ipv4(val);
201        }
202
203        if let Some(val) = &args.access_ipv6 {
204            rebuild_builder.access_ipv6(val);
205        }
206
207        if let Some(val) = &args.admin_pass {
208            rebuild_builder.admin_pass(val);
209        }
210
211        if let Some(val) = &args.description {
212            rebuild_builder.description(Some(val.into()));
213        } else if args.no_description {
214            rebuild_builder.description(None);
215        }
216
217        rebuild_builder.image_ref(&args.image_ref);
218
219        if let Some(val) = &args.metadata {
220            rebuild_builder.metadata(val.iter().cloned());
221        }
222
223        if let Some(val) = &args.name {
224            rebuild_builder.name(val);
225        }
226
227        if let Some(val) = &args.personality {
228            let personality_builder: Vec<rebuild_219::Personality> = val
229                .iter()
230                .flat_map(|v| serde_json::from_value::<rebuild_219::Personality>(v.to_owned()))
231                .collect::<Vec<rebuild_219::Personality>>();
232            rebuild_builder.personality(personality_builder);
233        }
234
235        if let Some(val) = &args.preserve_ephemeral {
236            rebuild_builder.preserve_ephemeral(*val);
237        }
238
239        ep_builder.rebuild(
240            rebuild_builder
241                .build()
242                .wrap_err("error preparing the request data")?,
243        );
244
245        let ep = ep_builder
246            .build()
247            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
248
249        let data: serde_json::Value = ep.query_async(client).await?;
250
251        op.output_single::<response::rebuild_219::ServerResponse>(data.clone())?;
252        // Show command specific hints
253        op.show_command_hint()?;
254        Ok(())
255    }
256}