Skip to main content

openstack_cli_compute/v2/server/
reboot.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
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_sdk::api::QueryAsync;
33use openstack_sdk::api::compute::v2::server::reboot;
34
35/// Reboots a server.
36///
37/// Specify the `reboot` action in the request body.
38///
39/// **Preconditions**
40///
41/// The preconditions for rebooting a server depend on the type of reboot.
42///
43/// You can only *SOFT* reboot a server when its status is `ACTIVE`.
44///
45/// You can only *HARD* reboot a server when its status is one of:
46///
47/// If the server is locked, you must have administrator privileges to reboot
48/// the server.
49///
50/// **Asynchronous Postconditions**
51///
52/// After you successfully reboot a server, its status changes to `ACTIVE`.
53///
54/// Normal response codes: 202
55///
56/// Error response codes: unauthorized(401), forbidden(403), itemNotFound(404),
57/// conflict(409)
58#[derive(Args)]
59#[command(about = "Reboot Server (reboot Action)")]
60pub struct ServerCommand {
61    /// Request Query parameters
62    #[command(flatten)]
63    query: QueryParameters,
64
65    /// Path parameters
66    #[command(flatten)]
67    path: PathParameters,
68
69    /// The action to reboot a server.
70    #[command(flatten)]
71    reboot: Reboot,
72}
73
74/// Query parameters
75#[derive(Args)]
76struct QueryParameters {}
77
78/// Path parameters
79#[derive(Args)]
80struct PathParameters {
81    /// id parameter for /v2.1/servers/{id}/action API
82    #[arg(
83        help_heading = "Path parameters",
84        id = "path_param_id",
85        value_name = "ID"
86    )]
87    id: String,
88}
89
90#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
91enum Type {
92    Hard,
93    Soft,
94}
95
96/// Reboot Body data
97#[derive(Args, Clone)]
98struct Reboot {
99    /// The type of the reboot action. The valid values are `HARD` and `SOFT`.
100    /// A `SOFT` reboot attempts a graceful shutdown and restart of the server.
101    /// A `HARD` reboot attempts a forced shutdown and restart of the server.
102    /// The `HARD` reboot corresponds to the power cycles of the server.
103    #[arg(help_heading = "Body parameters", long)]
104    _type: Type,
105}
106
107impl ServerCommand {
108    /// Perform command action
109    pub async fn take_action<C: CliArgs>(
110        &self,
111        parsed_args: &C,
112        client: &mut AsyncOpenStack,
113    ) -> Result<(), OpenStackCliError> {
114        info!("Action Server");
115
116        let op = OutputProcessor::from_args(parsed_args, Some("compute.server"), Some("reboot"));
117        op.validate_args(parsed_args)?;
118
119        let mut ep_builder = reboot::Request::builder();
120
121        ep_builder.id(&self.path.id);
122
123        // Set body parameters
124        // Set Request.reboot data
125        let args = &self.reboot;
126        let mut reboot_builder = reboot::RebootBuilder::default();
127
128        let tmp = match &args._type {
129            Type::Hard => reboot::Type::Hard,
130            Type::Soft => reboot::Type::Soft,
131        };
132        reboot_builder._type(tmp);
133
134        ep_builder.reboot(
135            reboot_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        openstack_sdk::api::ignore(ep).query_async(client).await?;
144        // Show command specific hints
145        op.show_command_hint()?;
146        Ok(())
147    }
148}