From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 08F1F7D3BB for ; Mon, 8 Nov 2021 17:47:33 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id EA24D29037 for ; Mon, 8 Nov 2021 17:47:02 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id D9F722901C for ; Mon, 8 Nov 2021 17:47:01 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id AA3BE46064 for ; Mon, 8 Nov 2021 17:47:01 +0100 (CET) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Mon, 8 Nov 2021 17:46:51 +0100 Message-Id: <20211108164655.25913-2-h.laimer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211108164655.25913-1-h.laimer@proxmox.com> References: <20211108164655.25913-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.083 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH v3 proxmox-backup 1/5] pbs-api-types: add maintenance type X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Nov 2021 16:47:33 -0000 --- 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, + #[serde(skip_serializing_if="Option::is_none")] + pub maintenance_type: Option, } #[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-', 'offline-')", + ) + .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 { + 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