openstack_cli/load_balancer/v2/healthmonitor/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 Healthmonitor command
19//!
20//! Wraps invoking of the `v2/lbaas/healthmonitors` with `POST` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_sdk::AsyncOpenStack;
26
27use crate::Cli;
28use crate::OpenStackCliError;
29use crate::output::OutputProcessor;
30
31use clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::load_balancer::v2::healthmonitor::create;
34use openstack_types::load_balancer::v2::healthmonitor::response::create::HealthmonitorResponse;
35
36/// Creates a health monitor on a pool.
37///
38/// Health monitors define how the load balancer monitors backend servers to
39/// determine if they are available to service requests.
40///
41/// This operation provisions a new health monitor by using the configuration
42/// that you define in the request object. After the API validates the request
43/// and starts the provisioning process, the API returns a response object that
44/// contains a unique ID and the status of provisioning the health monitor.
45///
46/// In the response, the health monitor [provisioning status](#prov-status) is
47/// `ACTIVE`, `PENDING_CREATE`, or `ERROR`.
48///
49/// If the status is `PENDING_CREATE`, issue GET
50/// `/v2/lbaas/healthmonitors/{healthmonitor_id}` to view the progress of the
51/// provisioning operation. When the health monitor status changes to `ACTIVE`,
52/// the health monitor is successfully provisioned and is ready for further
53/// configuration.
54///
55/// If the API cannot fulfill the request due to insufficient data or data that
56/// is not valid, the service returns the HTTP `Bad Request (400)` response
57/// code with information about the failure in the response body. Validation
58/// errors require that you correct the error and submit the request again.
59///
60/// Specifying a project_id is deprecated. The health monitor will inherit the
61/// project_id of the parent load balancer.
62///
63/// At a minimum, you must specify these health monitor attributes:
64///
65/// Some attributes receive default values if you omit them from the request:
66///
67/// To create a health monitor, the parent load balancer must have an `ACTIVE`
68/// provisioning status.
69#[derive(Args)]
70#[command(about = "Create Health Monitor")]
71pub struct HealthmonitorCommand {
72 /// Request Query parameters
73 #[command(flatten)]
74 query: QueryParameters,
75
76 /// Path parameters
77 #[command(flatten)]
78 path: PathParameters,
79
80 /// Defines mandatory and optional attributes of a POST request.
81 #[command(flatten)]
82 healthmonitor: Healthmonitor,
83}
84
85/// Query parameters
86#[derive(Args)]
87struct QueryParameters {}
88
89/// Path parameters
90#[derive(Args)]
91struct PathParameters {}
92
93#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
94enum HttpMethod {
95 Connect,
96 Delete,
97 Get,
98 Head,
99 Options,
100 Patch,
101 Post,
102 Put,
103 Trace,
104}
105
106#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
107enum Type {
108 Http,
109 Https,
110 Ping,
111 Sctp,
112 Tcp,
113 TlsHello,
114 UdpConnect,
115}
116
117/// Healthmonitor Body data
118#[derive(Args, Clone)]
119struct Healthmonitor {
120 /// The administrative state of the resource, which is up (`true`) or down
121 /// (`false`). Default is `true`.
122 #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
123 admin_state_up: Option<bool>,
124
125 /// The time, in seconds, between sending probes to members.
126 #[arg(help_heading = "Body parameters", long)]
127 delay: i32,
128
129 /// The domain name, which be injected into the HTTP Host Header to the
130 /// backend server for HTTP health check.
131 ///
132 /// **New in version 2.10**
133 #[arg(help_heading = "Body parameters", long)]
134 domain_name: Option<String>,
135
136 /// The list of HTTP status codes expected in response from the member to
137 /// declare it healthy. Specify one of the following values:
138 ///
139 /// - A single value, such as `200`
140 /// - A list, such as `200, 202`
141 /// - A range, such as `200-204`
142 ///
143 /// The default is 200.
144 #[arg(help_heading = "Body parameters", long)]
145 expected_codes: Option<String>,
146
147 /// The HTTP method that the health monitor uses for requests. One of
148 /// `CONNECT`, `DELETE`, `GET`, `HEAD`, `OPTIONS`, `PATCH`, `POST`, `PUT`,
149 /// or `TRACE`. The default is `GET`.
150 #[arg(help_heading = "Body parameters", long)]
151 http_method: Option<HttpMethod>,
152
153 /// The HTTP version. One of `1.0` or `1.1`. The default is `1.0`.
154 ///
155 /// **New in version 2.10**
156 #[arg(help_heading = "Body parameters", long)]
157 http_version: Option<f32>,
158
159 /// The number of successful checks before changing the `operating status`
160 /// of the member to `ONLINE`. A valid value is from `1` to `10`.
161 #[arg(help_heading = "Body parameters", long)]
162 max_retries: i32,
163
164 /// The number of allowed check failures before changing the
165 /// `operating status` of the member to `ERROR`. A valid value is from `1`
166 /// to `10`. The default is `3`.
167 #[arg(help_heading = "Body parameters", long)]
168 max_retries_down: Option<i32>,
169
170 /// Human-readable name of the resource.
171 #[arg(help_heading = "Body parameters", long)]
172 name: Option<String>,
173
174 /// The ID of the pool.
175 #[arg(help_heading = "Body parameters", long)]
176 pool_id: String,
177
178 /// The ID of the project owning this resource. (deprecated)
179 #[arg(help_heading = "Body parameters", long)]
180 project_id: Option<String>,
181
182 /// A list of simple strings assigned to the resource.
183 ///
184 /// **New in version 2.5**
185 ///
186 /// Parameter is an array, may be provided multiple times.
187 #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
188 tags: Option<Vec<String>>,
189
190 #[arg(help_heading = "Body parameters", long)]
191 tenant_id: Option<String>,
192
193 /// The maximum time, in seconds, that a monitor waits to connect before it
194 /// times out. This value must be less than the delay value.
195 #[arg(help_heading = "Body parameters", long)]
196 timeout: i32,
197
198 /// The type of health monitor. One of `HTTP`, `HTTPS`, `PING`, `SCTP`,
199 /// `TCP`, `TLS-HELLO`, or `UDP-CONNECT`.
200 #[arg(help_heading = "Body parameters", long)]
201 _type: Type,
202
203 /// The HTTP URL path of the request sent by the monitor to test the health
204 /// of a backend member. Must be a string that begins with a forward slash
205 /// (`/`). The default URL path is `/`.
206 #[arg(help_heading = "Body parameters", long)]
207 url_path: Option<String>,
208}
209
210impl HealthmonitorCommand {
211 /// Perform command action
212 pub async fn take_action(
213 &self,
214 parsed_args: &Cli,
215 client: &mut AsyncOpenStack,
216 ) -> Result<(), OpenStackCliError> {
217 info!("Create Healthmonitor");
218
219 let op = OutputProcessor::from_args(
220 parsed_args,
221 Some("load-balancer.healthmonitor"),
222 Some("create"),
223 );
224 op.validate_args(parsed_args)?;
225
226 let mut ep_builder = create::Request::builder();
227
228 // Set body parameters
229 // Set Request.healthmonitor data
230 let args = &self.healthmonitor;
231 let mut healthmonitor_builder = create::HealthmonitorBuilder::default();
232 if let Some(val) = &args.admin_state_up {
233 healthmonitor_builder.admin_state_up(*val);
234 }
235
236 healthmonitor_builder.delay(args.delay);
237
238 if let Some(val) = &args.domain_name {
239 healthmonitor_builder.domain_name(val);
240 }
241
242 if let Some(val) = &args.expected_codes {
243 healthmonitor_builder.expected_codes(val);
244 }
245
246 if let Some(val) = &args.http_method {
247 let tmp = match val {
248 HttpMethod::Connect => create::HttpMethod::Connect,
249 HttpMethod::Delete => create::HttpMethod::Delete,
250 HttpMethod::Get => create::HttpMethod::Get,
251 HttpMethod::Head => create::HttpMethod::Head,
252 HttpMethod::Options => create::HttpMethod::Options,
253 HttpMethod::Patch => create::HttpMethod::Patch,
254 HttpMethod::Post => create::HttpMethod::Post,
255 HttpMethod::Put => create::HttpMethod::Put,
256 HttpMethod::Trace => create::HttpMethod::Trace,
257 };
258 healthmonitor_builder.http_method(tmp);
259 }
260
261 if let Some(val) = &args.http_version {
262 healthmonitor_builder.http_version(*val);
263 }
264
265 healthmonitor_builder.max_retries(args.max_retries);
266
267 if let Some(val) = &args.max_retries_down {
268 healthmonitor_builder.max_retries_down(*val);
269 }
270
271 if let Some(val) = &args.name {
272 healthmonitor_builder.name(val);
273 }
274
275 healthmonitor_builder.pool_id(&args.pool_id);
276
277 if let Some(val) = &args.project_id {
278 healthmonitor_builder.project_id(val);
279 }
280
281 if let Some(val) = &args.tags {
282 healthmonitor_builder.tags(val.iter().map(Into::into).collect::<Vec<_>>());
283 }
284
285 if let Some(val) = &args.tenant_id {
286 healthmonitor_builder.tenant_id(val);
287 }
288
289 healthmonitor_builder.timeout(args.timeout);
290
291 let tmp = match &args._type {
292 Type::Http => create::Type::Http,
293 Type::Https => create::Type::Https,
294 Type::Ping => create::Type::Ping,
295 Type::Sctp => create::Type::Sctp,
296 Type::Tcp => create::Type::Tcp,
297 Type::TlsHello => create::Type::TlsHello,
298 Type::UdpConnect => create::Type::UdpConnect,
299 };
300 healthmonitor_builder._type(tmp);
301
302 if let Some(val) = &args.url_path {
303 healthmonitor_builder.url_path(val);
304 }
305
306 ep_builder.healthmonitor(healthmonitor_builder.build().unwrap());
307
308 let ep = ep_builder
309 .build()
310 .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
311
312 let data = ep.query_async(client).await?;
313 op.output_single::<HealthmonitorResponse>(data)?;
314 // Show command specific hints
315 op.show_command_hint()?;
316 Ok(())
317 }
318}