[pbs-devel] [PATCH proxmox-backup v8 1/6] api-types: add maintenance type

Hannes Laimer h.laimer at proxmox.com
Tue Feb 15 13:10:34 CET 2022


Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
 pbs-api-types/src/datastore.rs   | 26 +++++++++++---
 pbs-api-types/src/lib.rs         |  3 ++
 pbs-api-types/src/maintenance.rs | 59 ++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 4 deletions(-)
 create mode 100644 pbs-api-types/src/maintenance.rs

diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs
index 36279b3a..e765aed3 100644
--- a/pbs-api-types/src/datastore.rs
+++ b/pbs-api-types/src/datastore.rs
@@ -6,10 +6,9 @@ use proxmox_schema::{
 };
 
 use crate::{
-    PROXMOX_SAFE_ID_FORMAT, SHA256_HEX_REGEX, SINGLE_LINE_COMMENT_SCHEMA, CryptMode, UPID,
-    Fingerprint, Userid, Authid,
-    GC_SCHEDULE_SCHEMA, DATASTORE_NOTIFY_STRING_SCHEMA, PRUNE_SCHEDULE_SCHEMA,
-
+    Authid, CryptMode, Fingerprint, MaintenanceMode, Userid, DATASTORE_NOTIFY_STRING_SCHEMA,
+    GC_SCHEDULE_SCHEMA, PROXMOX_SAFE_ID_FORMAT, PRUNE_SCHEDULE_SCHEMA, SHA256_HEX_REGEX,
+    SINGLE_LINE_COMMENT_SCHEMA, UPID,
 };
 
 const_regex!{
@@ -224,6 +223,11 @@ pub struct PruneOptions {
             optional: true,
             type: bool,
         },
+        "maintenance-mode": {
+            optional: true,
+            format: &ApiStringFormat::PropertyString(&MaintenanceMode::API_SCHEMA),
+            type: String,
+        },
     }
 )]
 #[derive(Serialize,Deserialize,Updater)]
@@ -261,6 +265,20 @@ pub struct DataStoreConfig {
     /// Send notification only for job errors
     #[serde(skip_serializing_if="Option::is_none")]
     pub notify: Option<String>,
+    /// Maintenance mode, type is either 'offline' or 'read-only', message has to be URL encoded
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub maintenance_mode: Option<String>,
+}
+
+impl DataStoreConfig {
+    pub fn get_maintenance_mode(&self) -> Option<MaintenanceMode> {
+        self.maintenance_mode
+            .as_ref()
+            .map(|str| MaintenanceMode::API_SCHEMA.parse_property_string(str).ok())
+            .flatten()
+            .map(|value| MaintenanceMode::deserialize(value).ok())
+            .flatten()
+    }
 }
 
 #[api(
diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs
index 754e7b22..efb01c3e 100644
--- a/pbs-api-types/src/lib.rs
+++ b/pbs-api-types/src/lib.rs
@@ -49,6 +49,9 @@ pub use jobs::*;
 mod key_derivation;
 pub use key_derivation::{Kdf, KeyInfo};
 
+mod maintenance;
+pub use maintenance::*;
+
 mod network;
 pub use network::*;
 
diff --git a/pbs-api-types/src/maintenance.rs b/pbs-api-types/src/maintenance.rs
new file mode 100644
index 00000000..2926d32d
--- /dev/null
+++ b/pbs-api-types/src/maintenance.rs
@@ -0,0 +1,59 @@
+use serde::{Deserialize, Serialize};
+
+use proxmox_schema::{api, ApiStringFormat, const_regex, Schema, StringSchema};
+
+const_regex!{
+    pub MAINTENANCE_MESSAGE_REGEX = r"^[a-zA-Z0-9%]*$";
+}
+
+pub const MAINTENANCE_MESSAGE_FORMAT: ApiStringFormat =
+    ApiStringFormat::Pattern(&MAINTENANCE_MESSAGE_REGEX);
+
+
+pub const MAINTENANCE_MESSAGE_SCHEMA: Schema =
+    StringSchema::new("URL encoded message describing the reason for the maintenance.")
+        .format(&MAINTENANCE_MESSAGE_FORMAT)
+        .max_length(32)
+        .schema();
+
+#[derive(Clone, Copy, Debug)]
+/// Operation requirements, used when checking for maintenance mode.
+pub enum Operation {
+    Read,
+    Write,
+}
+
+#[api]
+#[derive(Deserialize, Serialize)]
+#[serde(rename_all="kebab-case")]
+/// Maintenance type.
+pub enum MaintenanceType {
+    /// Only reading operations are allowed on the datastore.
+    ReadOnly,
+    /// Neither reading nor writing operations are allowed on the datastore.
+    Offline,
+}
+
+#[api(
+    properties: {
+        type: {
+            type: MaintenanceType,
+        },
+        message: {
+            optional: true,
+            schema: MAINTENANCE_MESSAGE_SCHEMA,
+        }
+    },
+    default_key: "type",
+)]
+#[derive(Deserialize, Serialize)]
+/// Maintenance mode
+pub struct MaintenanceMode {
+    /// Type of the maintenance
+    #[serde(rename = "type")]
+    ty: MaintenanceType,
+
+    /// Reason for the maintenance mode.
+    #[serde(skip_serializing_if = "Option::is_none")]
+    message: Option<String>,
+}
-- 
2.30.2






More information about the pbs-devel mailing list