dinio_sdk/api/health_checks.rs
1//! `/api/v1/health-checks` (`../core/src/api/health_checks.ts`). A check is keyed by its
2//! record value's uuid; there is no listing per zone.
3
4use reqwest::Method;
5use uuid::Uuid;
6
7use crate::client::Client;
8use crate::error::Result;
9use crate::models::{HealthCheck, HealthCheckPatch, NewHealthCheck};
10
11/// Health check routes, from [`Client::health_checks`].
12///
13/// A check belongs to one record value and is addressed by that value's uuid
14/// (`record_id`). Access follows the value's zone.
15pub struct HealthChecks<'a> {
16 client: &'a Client,
17}
18
19impl<'a> HealthChecks<'a> {
20 pub(crate) fn new(client: &'a Client) -> Self {
21 Self { client }
22 }
23
24 /// The check of a record value, with its last result.
25 ///
26 /// `GET /api/v1/health-checks/:record_id`
27 ///
28 /// # Errors
29 ///
30 /// [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if the value has no check.
31 pub async fn get(&self, record_id: Uuid) -> Result<HealthCheck> {
32 let request = self
33 .client
34 .request(Method::GET, &["health-checks", &record_id.to_string()])?;
35 Ok(self.client.send(request).await?.0)
36 }
37
38 /// Adds a check to a record value and switches the value's `host_health` to
39 /// [`Auto`](crate::models::HostHealthMode::Auto), so Core starts running it right away.
40 /// The value counts as healthy until the first run says otherwise.
41 ///
42 /// `POST /api/v1/health-checks`
43 ///
44 /// # Errors
45 ///
46 /// * [`ErrorCode::Conflict`](crate::ErrorCode::Conflict) if the value already has a
47 /// check.
48 /// * [`ErrorCode::Validation`](crate::ErrorCode::Validation) for out-of-range timings
49 /// (see [`NewHealthCheck`]) or a bad config.
50 ///
51 /// # Examples
52 ///
53 /// ```no_run
54 /// use dinio_sdk::models::{HealthCheckConfig, NewHealthCheck, TcpCheck};
55 ///
56 /// # async fn example(client: &dinio_sdk::Client, value: &dinio_sdk::models::RecordValue) -> dinio_sdk::Result<()> {
57 /// let check = NewHealthCheck {
58 /// record_id: value.uuid,
59 /// config: HealthCheckConfig::Tcp(TcpCheck { host: "192.0.2.10".into(), port: 443 }),
60 /// name: Some("web".into()),
61 /// interval: 60,
62 /// timeout: 5,
63 /// retries: None,
64 /// retry_interval: None,
65 /// };
66 /// client.health_checks().create(&check).await?;
67 /// # Ok(())
68 /// # }
69 /// ```
70 pub async fn create(&self, check: &NewHealthCheck) -> Result<HealthCheck> {
71 let request = self
72 .client
73 .request(Method::POST, &["health-checks"])?
74 .json(check);
75 Ok(self.client.send(request).await?.0)
76 }
77
78 /// Changes a check; fields left `None` stay as they are.
79 ///
80 /// `PATCH /api/v1/health-checks/:record_id`
81 ///
82 /// # Errors
83 ///
84 /// * [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if the value has no check.
85 /// * [`ErrorCode::Validation`](crate::ErrorCode::Validation) if the result would have
86 /// `timeout >= interval`, or the config does not fit the check's type.
87 pub async fn update(&self, record_id: Uuid, patch: &HealthCheckPatch) -> Result<HealthCheck> {
88 let request = self
89 .client
90 .request(Method::PATCH, &["health-checks", &record_id.to_string()])?
91 .json(patch);
92 Ok(self.client.send(request).await?.0)
93 }
94
95 /// Removes the check of a record value. A value on `Auto` falls back to
96 /// [`None`](crate::models::HostHealthMode::None) (always healthy); `Up` and `Down` stay.
97 ///
98 /// `DELETE /api/v1/health-checks/:record_id`
99 ///
100 /// # Errors
101 ///
102 /// [`ErrorCode::NotFound`](crate::ErrorCode::NotFound) if the value has no check.
103 pub async fn delete(&self, record_id: Uuid) -> Result<()> {
104 let request = self
105 .client
106 .request(Method::DELETE, &["health-checks", &record_id.to_string()])?;
107 self.client.send_empty(request).await
108 }
109}