openstack_cli_network/v2/floatingip/create.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//! Create Floatingip command
19//!
20//! Wraps invoking of the `v2.0/floatingips` 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::network::v2::floatingip::create;
33use openstack_types::network::v2::floatingip::response;
34
35/// Creates a floating IP, and, if you specify port information, associates the
36/// floating IP with an internal port.
37///
38/// To associate the floating IP with an internal port, specify the port ID
39/// attribute in the request body. If you do not specify a port ID in the
40/// request, you can issue a PUT request instead of a POST request.
41///
42/// Default policy settings enable only administrative users to set floating IP
43/// addresses and some non-administrative users might require a floating IP
44/// address. If you do not specify a floating IP address in the request, the
45/// operation automatically allocates one.
46///
47/// By default, this operation associates the floating IP address with a single
48/// fixed IP address that is configured on an OpenStack Networking port. If a
49/// port has multiple IP addresses, you must specify the `fixed_ip_address`
50/// attribute in the request body to associate a fixed IP address with the
51/// floating IP address.
52///
53/// You can create floating IPs on only external networks. When you create a
54/// floating IP, you must specify the ID of the network on which you want to
55/// create the floating IP. Alternatively, you can create a floating IP on a
56/// subnet in the external network, based on the costs and quality of that
57/// subnet.
58///
59/// You must configure an IP address with the internal OpenStack Networking
60/// port that is associated with the floating IP address.
61///
62/// The operation returns the `Bad Request (400)` response code for one of
63/// reasons:
64///
65/// If the port ID is not valid, this operation returns `404` response code.
66///
67/// The operation returns the `Conflict (409)` response code for one of
68/// reasons:
69///
70/// Normal response codes: 201
71///
72/// Error response codes: 400, 401, 404, 409
73#[derive(Args)]
74#[command(about = "Create floating IP")]
75pub struct FloatingipCommand {
76 /// Request Query parameters
77 #[command(flatten)]
78 query: QueryParameters,
79
80 /// Path parameters
81 #[command(flatten)]
82 path: PathParameters,
83
84 /// A `floatingip` object. When you associate a floating IP address with a
85 /// VM, the instance has the same public IP address each time that it
86 /// boots, basically to maintain a consistent IP address for maintaining
87 /// DNS assignment.
88 #[command(flatten)]
89 floatingip: Floatingip,
90}
91
92/// Query parameters
93#[derive(Args)]
94struct QueryParameters {}
95
96/// Path parameters
97#[derive(Args)]
98struct PathParameters {}
99/// Floatingip Body data
100#[derive(Args, Clone)]
101struct Floatingip {
102 /// A human-readable description for the resource. Default is an empty
103 /// string.
104 #[arg(help_heading = "Body parameters", long)]
105 description: Option<String>,
106
107 /// A valid DNS domain.
108 #[arg(help_heading = "Body parameters", long)]
109 dns_domain: Option<String>,
110
111 /// A valid DNS name.
112 #[arg(help_heading = "Body parameters", long)]
113 dns_name: Option<String>,
114
115 /// The fixed IP address that is associated with the floating IP. If an
116 /// internal port has multiple associated IP addresses, the service chooses
117 /// the first IP address unless you explicitly define a fixed IP address in
118 /// the `fixed_ip_address` parameter.
119 #[arg(help_heading = "Body parameters", long)]
120 fixed_ip_address: Option<String>,
121
122 /// Set explicit NULL for the fixed_ip_address
123 #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "fixed_ip_address")]
124 no_fixed_ip_address: bool,
125
126 /// The floating IP address.
127 #[arg(help_heading = "Body parameters", long)]
128 floating_ip_address: Option<String>,
129
130 /// Set explicit NULL for the floating_ip_address
131 #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "floating_ip_address")]
132 no_floating_ip_address: bool,
133
134 /// The ID of the network associated with the floating IP.
135 #[arg(help_heading = "Body parameters", long)]
136 floating_network_id: String,
137
138 /// The ID of a port associated with the floating IP. To associate the
139 /// floating IP with a fixed IP at creation time, you must specify the
140 /// identifier of the internal port.
141 #[arg(help_heading = "Body parameters", long)]
142 port_id: Option<String>,
143
144 /// Set explicit NULL for the port_id
145 #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "port_id")]
146 no_port_id: bool,
147
148 /// The ID of the QoS policy associated with the floating IP.
149 #[arg(help_heading = "Body parameters", long)]
150 qos_policy_id: Option<String>,
151
152 /// Set explicit NULL for the qos_policy_id
153 #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "qos_policy_id")]
154 no_qos_policy_id: bool,
155
156 /// The subnet ID on which you want to create the floating IP.
157 #[arg(help_heading = "Body parameters", long)]
158 subnet_id: Option<String>,
159
160 /// Set explicit NULL for the subnet_id
161 #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "subnet_id")]
162 no_subnet_id: bool,
163
164 /// The ID of the project.
165 #[arg(help_heading = "Body parameters", long)]
166 tenant_id: Option<String>,
167}
168
169impl FloatingipCommand {
170 /// Perform command action
171 pub async fn take_action<C: CliArgs>(
172 &self,
173 parsed_args: &C,
174 client: &mut AsyncOpenStack,
175 ) -> Result<(), OpenStackCliError> {
176 info!("Create Floatingip");
177
178 let op =
179 OutputProcessor::from_args(parsed_args, Some("network.floatingip"), Some("create"));
180 op.validate_args(parsed_args)?;
181
182 let mut ep_builder = create::Request::builder();
183
184 // Set body parameters
185 // Set Request.floatingip data
186 let args = &self.floatingip;
187 let mut floatingip_builder = create::FloatingipBuilder::default();
188 if let Some(val) = &args.description {
189 floatingip_builder.description(val);
190 }
191
192 if let Some(val) = &args.dns_domain {
193 floatingip_builder.dns_domain(val);
194 }
195
196 if let Some(val) = &args.dns_name {
197 floatingip_builder.dns_name(val);
198 }
199
200 if let Some(val) = &args.fixed_ip_address {
201 floatingip_builder.fixed_ip_address(Some(val.into()));
202 } else if args.no_fixed_ip_address {
203 floatingip_builder.fixed_ip_address(None);
204 }
205
206 if let Some(val) = &args.floating_ip_address {
207 floatingip_builder.floating_ip_address(Some(val.into()));
208 } else if args.no_floating_ip_address {
209 floatingip_builder.floating_ip_address(None);
210 }
211
212 floatingip_builder.floating_network_id(&args.floating_network_id);
213
214 if let Some(val) = &args.port_id {
215 floatingip_builder.port_id(Some(val.into()));
216 } else if args.no_port_id {
217 floatingip_builder.port_id(None);
218 }
219
220 if let Some(val) = &args.qos_policy_id {
221 floatingip_builder.qos_policy_id(Some(val.into()));
222 } else if args.no_qos_policy_id {
223 floatingip_builder.qos_policy_id(None);
224 }
225
226 if let Some(val) = &args.subnet_id {
227 floatingip_builder.subnet_id(Some(val.into()));
228 } else if args.no_subnet_id {
229 floatingip_builder.subnet_id(None);
230 }
231
232 if let Some(val) = &args.tenant_id {
233 floatingip_builder.tenant_id(val);
234 }
235
236 ep_builder.floatingip(
237 floatingip_builder
238 .build()
239 .wrap_err("error preparing the request data")?,
240 );
241
242 let ep = ep_builder
243 .build()
244 .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
245
246 let data: serde_json::Value = ep.query_async(client).await?;
247
248 op.output_single::<response::create::FloatingipResponse>(data.clone())?;
249 // Show command specific hints
250 op.show_command_hint()?;
251 Ok(())
252 }
253}