2 use anyhow
::{bail, Error}
;
3 use serde
::{Deserialize, Serialize}
;
5 use proxmox_schema
::{api, ApiStringFormat, const_regex, Schema, StringSchema}
;
8 pub MAINTENANCE_MESSAGE_REGEX
= r
"^[[:^cntrl:]]*$";
11 pub const MAINTENANCE_MESSAGE_FORMAT
: ApiStringFormat
=
12 ApiStringFormat
::Pattern(&MAINTENANCE_MESSAGE_REGEX
);
15 pub const MAINTENANCE_MESSAGE_SCHEMA
: Schema
=
16 StringSchema
::new("Message describing the reason for the maintenance.")
17 .format(&MAINTENANCE_MESSAGE_FORMAT
)
21 #[derive(Clone, Copy, Debug)]
22 /// Operation requirements, used when checking for maintenance mode.
29 #[derive(Deserialize, Serialize, PartialEq)]
30 #[serde(rename_all="kebab-case")]
32 pub enum MaintenanceType
{
33 /// Only read operations are allowed on the datastore.
35 /// Neither read nor write operations are allowed on the datastore.
42 type: MaintenanceType
,
46 schema
: MAINTENANCE_MESSAGE_SCHEMA
,
51 #[derive(Deserialize, Serialize)]
53 pub struct MaintenanceMode
{
54 /// Type of maintenance ("read-only" or "offline").
55 #[serde(rename = "type")]
58 /// Reason for maintenance.
59 #[serde(skip_serializing_if = "Option::is_none")]
60 message
: Option
<String
>,
63 impl MaintenanceMode
{
64 pub fn check(&self, operation
: Option
<Operation
>) -> Result
<(), Error
> {
65 let message
= percent_encoding
::percent_decode_str(self.message
.as_deref().unwrap_or(""))
67 .unwrap_or(Cow
::Borrowed(""));
69 if self.ty
== MaintenanceType
::Offline
{
70 bail
!("offline maintenance mode: {}", message
);
71 } else if self.ty
== MaintenanceType
::ReadOnly
{
72 if let Some(Operation
::Write
) = operation
{
73 bail
!("read-only maintenance mode: {}", message
);