openstack_cli_compute/v2/server/add_floating_ip_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//! Action Server command [microversion = 2.1]
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 openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::compute::v2::server::add_floating_ip_21;
33
34/// Adds a floating IP address to a server, which associates that address with
35/// the server.
36///
37/// A pool of floating IP addresses, configured by the cloud administrator, is
38/// available in OpenStack Compute. The project quota defines the maximum
39/// number of floating IP addresses that you can allocate to the project. After
40/// you
41/// [create (allocate) a floating IPaddress](https://docs.openstack.org/api-ref/compute/#create-allocate-floating-ip-address)
42/// for a project, you can associate that address with the server. Specify the
43/// `addFloatingIp` action in the request body.
44///
45/// If an instance is connected to multiple networks, you can associate a
46/// floating IP address with a specific fixed IP address by using the optional
47/// `fixed_address` parameter.
48///
49/// **Preconditions**
50///
51/// The server must exist.
52///
53/// You can only add a floating IP address to the server when its status is
54/// `ACTIVE` or `STOPPED`
55///
56/// Normal response codes: 202
57///
58/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
59/// itemNotFound(404)
60#[derive(Args)]
61#[command(
62 about = "Add (Associate) Floating Ip (addFloatingIp Action) (DEPRECATED) (microversion = 2.1)"
63)]
64pub struct ServerCommand {
65 /// Request Query parameters
66 #[command(flatten)]
67 query: QueryParameters,
68
69 /// Path parameters
70 #[command(flatten)]
71 path: PathParameters,
72
73 /// The action. Contains required floating IP `address` and optional
74 /// `fixed_address`.
75 #[command(flatten)]
76 add_floating_ip: AddFloatingIp,
77}
78
79/// Query parameters
80#[derive(Args)]
81struct QueryParameters {}
82
83/// Path parameters
84#[derive(Args)]
85struct PathParameters {
86 /// id parameter for /v2.1/servers/{id}/action API
87 #[arg(
88 help_heading = "Path parameters",
89 id = "path_param_id",
90 value_name = "ID"
91 )]
92 id: String,
93}
94/// AddFloatingIp Body data
95#[derive(Args, Clone)]
96struct AddFloatingIp {
97 /// The fixed IP address with which you want to associate the floating IP
98 /// address.
99 #[arg(help_heading = "Body parameters", long)]
100 address: String,
101
102 /// The fixed IP address with which you want to associate the floating IP
103 /// address.
104 #[arg(help_heading = "Body parameters", long)]
105 fixed_address: Option<String>,
106}
107
108impl ServerCommand {
109 /// Perform command action
110 pub async fn take_action<C: CliArgs>(
111 &self,
112 parsed_args: &C,
113 client: &mut AsyncOpenStack,
114 ) -> Result<(), OpenStackCliError> {
115 info!("Action Server");
116
117 let op = OutputProcessor::from_args(
118 parsed_args,
119 Some("compute.server"),
120 Some("add_floating_ip"),
121 );
122 op.validate_args(parsed_args)?;
123
124 let mut ep_builder = add_floating_ip_21::Request::builder();
125 ep_builder.header(
126 http::header::HeaderName::from_static("openstack-api-version"),
127 http::header::HeaderValue::from_static("compute 2.1"),
128 );
129
130 ep_builder.id(&self.path.id);
131
132 // Set body parameters
133 // Set Request.add_floating_ip data
134 let args = &self.add_floating_ip;
135 let mut add_floating_ip_builder = add_floating_ip_21::AddFloatingIpBuilder::default();
136
137 add_floating_ip_builder.address(&args.address);
138
139 if let Some(val) = &args.fixed_address {
140 add_floating_ip_builder.fixed_address(val);
141 }
142
143 ep_builder.add_floating_ip(
144 add_floating_ip_builder
145 .build()
146 .wrap_err("error preparing the request data")?,
147 );
148
149 let ep = ep_builder
150 .build()
151 .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
152 openstack_sdk::api::ignore(ep).query_async(client).await?;
153 // Show command specific hints
154 op.show_command_hint()?;
155 Ok(())
156 }
157}