all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v3 proxmox-backup 1/5] pbs-api-types: add maintenance type
Date: Mon,  8 Nov 2021 17:46:51 +0100	[thread overview]
Message-ID: <20211108164655.25913-2-h.laimer@proxmox.com> (raw)
In-Reply-To: <20211108164655.25913-1-h.laimer@proxmox.com>

---
 pbs-api-types/src/datastore.rs   |  8 +++-
 pbs-api-types/src/lib.rs         |  3 ++
 pbs-api-types/src/maintenance.rs | 82 ++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+), 1 deletion(-)
 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 77c1258f..a12e20b3 100644
--- a/pbs-api-types/src/datastore.rs
+++ b/pbs-api-types/src/datastore.rs
@@ -7,7 +7,7 @@ use proxmox_schema::{
 
 use crate::{
     PROXMOX_SAFE_ID_FORMAT, SHA256_HEX_REGEX, SINGLE_LINE_COMMENT_SCHEMA, CryptMode, UPID,
-    Fingerprint, Userid, Authid,
+    Fingerprint, Userid, Authid, MaintenanceType,
     GC_SCHEDULE_SCHEMA, DATASTORE_NOTIFY_STRING_SCHEMA, PRUNE_SCHEDULE_SCHEMA,
 
 };
@@ -219,6 +219,10 @@ pub struct PruneOptions {
             optional: true,
             type: bool,
         },
+        "maintenance-type": {
+            optional: true,
+            type: MaintenanceType,
+        },
     }
 )]
 #[derive(Serialize,Deserialize,Updater)]
@@ -256,6 +260,8 @@ pub struct DataStoreConfig {
     /// Send notification only for job errors
     #[serde(skip_serializing_if="Option::is_none")]
     pub notify: Option<String>,
+    #[serde(skip_serializing_if="Option::is_none")]
+    pub maintenance_type: Option<MaintenanceType>,
 }
 
 #[api(
diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs
index 96ac657b..23b29c80 100644
--- a/pbs-api-types/src/lib.rs
+++ b/pbs-api-types/src/lib.rs
@@ -44,6 +44,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..f816b279
--- /dev/null
+++ b/pbs-api-types/src/maintenance.rs
@@ -0,0 +1,82 @@
+use anyhow::{bail, Error};
+
+use proxmox_schema::{parse_simple_value, ApiStringFormat, Schema, StringSchema, UpdaterType};
+
+use crate::{PROXMOX_SAFE_ID_FORMAT, SINGLE_LINE_COMMENT_FORMAT};
+
+pub const MAINTENANCE_MODE_SCHEMA: Schema = StringSchema::new("Maintenance mode.")
+    .format(&PROXMOX_SAFE_ID_FORMAT)
+    .min_length(3)
+    .max_length(32)
+    .schema();
+
+pub const MAINTENANCE_MESSAGE_SCHEMA: Schema =
+    StringSchema::new("Message describing the reason for the maintenance.")
+        .format(&SINGLE_LINE_COMMENT_FORMAT)
+        .max_length(32)
+        .schema();
+
+#[derive(UpdaterType)]
+/// Maintenance type and message.
+pub enum MaintenanceType {
+    /// Only reading operations are allowed on the datastore.
+    ReadOnly(String),
+    /// Neither reading nor writing operations are allowed on the datastore.
+    Offline(String),
+}
+
+/// Operation requirments, used when checking for maintenance mode.
+pub enum Operation {
+    Read,
+    Write,
+}
+
+proxmox::forward_deserialize_to_from_str!(MaintenanceType);
+proxmox::forward_serialize_to_display!(MaintenanceType);
+
+impl proxmox_schema::ApiType for MaintenanceType {
+    const API_SCHEMA: Schema = StringSchema::new(
+        "Maintenance type (e.g. 'read-only-<message>', 'offline-<message>')",
+    )
+    .format(&ApiStringFormat::VerifyFn(|text| {
+        let location: MaintenanceType = text.parse()?;
+        match location {
+            MaintenanceType::ReadOnly(ref message) => {
+                parse_simple_value(message, &MAINTENANCE_MESSAGE_SCHEMA)?;
+            }
+            MaintenanceType::Offline(ref message) => {
+                parse_simple_value(message, &MAINTENANCE_MESSAGE_SCHEMA)?;
+            }
+        }
+        Ok(())
+    }))
+    .schema();
+}
+
+impl std::fmt::Display for MaintenanceType {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            MaintenanceType::ReadOnly(message) => {
+                write!(f, "read-only-{}", message)
+            }
+            MaintenanceType::Offline(message) => {
+                write!(f, "offline-{}", message)
+            }
+        }
+    }
+}
+
+impl std::str::FromStr for MaintenanceType {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if let Some(message) = s.strip_prefix("read-only-") {
+            return Ok(MaintenanceType::ReadOnly(message.to_string()));
+        }
+        if let Some(message) = s.strip_prefix("offline-") {
+            return Ok(MaintenanceType::Offline(message.to_string()));
+        }
+
+        bail!("Maintenance type parse error");
+    }
+}
-- 
2.30.2





  reply	other threads:[~2021-11-08 16:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-08 16:46 [pbs-devel] [PATCH v3 proxmox-backup 0/5] closes #3071: maintenance mode for datastore Hannes Laimer
2021-11-08 16:46 ` Hannes Laimer [this message]
2021-11-08 16:46 ` [pbs-devel] [PATCH v3 proxmox-backup 2/5] pbs-datastore: add check for maintenance in lookup Hannes Laimer
2021-11-08 16:46 ` [pbs-devel] [PATCH v3 proxmox-backup 3/5] pbs-datastore: add active operations tracking Hannes Laimer
2021-11-08 16:46 ` [pbs-devel] [PATCH v3 proxmox-backup 4/5] api2: make maintenance_type updatable Hannes Laimer
2021-11-09  8:05   ` Thomas Lamprecht
2021-11-08 16:46 ` [pbs-devel] [PATCH v3 proxmox-backup 5/5] ui: add option to change the maintenance type Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211108164655.25913-2-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal