tape: cleanup MediaLocation type for direct use with API
This commit is contained in:
@ -4,26 +4,13 @@ use proxmox::api::api;
|
||||
|
||||
use super::{
|
||||
MediaStatus,
|
||||
MediaLocation,
|
||||
};
|
||||
|
||||
#[api()]
|
||||
#[derive(Serialize,Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
/// Media location
|
||||
pub enum MediaLocationKind {
|
||||
/// Ready for use (inside tape library)
|
||||
Online,
|
||||
/// Local available, but need to be mounted (insert into tape
|
||||
/// drive)
|
||||
Offline,
|
||||
/// Media is inside a Vault
|
||||
Vault,
|
||||
}
|
||||
|
||||
#[api(
|
||||
properties: {
|
||||
location: {
|
||||
type: MediaLocationKind,
|
||||
type: MediaLocation,
|
||||
},
|
||||
status: {
|
||||
type: MediaStatus,
|
||||
@ -38,9 +25,7 @@ pub struct MediaListEntry {
|
||||
pub changer_id: String,
|
||||
/// Media Uuid
|
||||
pub uuid: String,
|
||||
pub location: MediaLocationKind,
|
||||
/// Media location hint (vault name, changer name)
|
||||
pub location_hint: Option<String>,
|
||||
pub location: MediaLocation,
|
||||
pub status: MediaStatus,
|
||||
/// Expired flag
|
||||
pub expired: bool,
|
||||
|
91
src/api2/types/tape/media_location.rs
Normal file
91
src/api2/types/tape/media_location.rs
Normal file
@ -0,0 +1,91 @@
|
||||
use anyhow::{bail, Error};
|
||||
|
||||
use proxmox::api::{
|
||||
schema::{
|
||||
Schema,
|
||||
StringSchema,
|
||||
ApiStringFormat,
|
||||
parse_simple_value,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::api2::types::{
|
||||
PROXMOX_SAFE_ID_FORMAT,
|
||||
CHANGER_NAME_SCHEMA,
|
||||
};
|
||||
|
||||
pub const VAULT_NAME_SCHEMA: Schema = StringSchema::new("Vault name.")
|
||||
.format(&PROXMOX_SAFE_ID_FORMAT)
|
||||
.min_length(3)
|
||||
.max_length(32)
|
||||
.schema();
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
/// Media location
|
||||
pub enum MediaLocation {
|
||||
/// Ready for use (inside tape library)
|
||||
Online(String),
|
||||
/// Local available, but need to be mounted (insert into tape
|
||||
/// drive)
|
||||
Offline,
|
||||
/// Media is inside a Vault
|
||||
Vault(String),
|
||||
}
|
||||
|
||||
proxmox::forward_deserialize_to_from_str!(MediaLocation);
|
||||
proxmox::forward_serialize_to_display!(MediaLocation);
|
||||
|
||||
impl MediaLocation {
|
||||
pub const API_SCHEMA: Schema = StringSchema::new(
|
||||
"Media location (e.g. 'offline', 'online-<changer_name>', 'vault-<vault_name>')")
|
||||
.format(&ApiStringFormat::VerifyFn(|text| {
|
||||
let location: MediaLocation = text.parse()?;
|
||||
match location {
|
||||
MediaLocation::Online(ref changer) => {
|
||||
parse_simple_value(changer, &CHANGER_NAME_SCHEMA)?;
|
||||
}
|
||||
MediaLocation::Vault(ref vault) => {
|
||||
parse_simple_value(vault, &VAULT_NAME_SCHEMA)?;
|
||||
}
|
||||
MediaLocation::Offline => { /* OK */}
|
||||
}
|
||||
Ok(())
|
||||
}))
|
||||
.schema();
|
||||
}
|
||||
|
||||
|
||||
impl std::fmt::Display for MediaLocation {
|
||||
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
MediaLocation::Offline => {
|
||||
write!(f, "offline")
|
||||
}
|
||||
MediaLocation::Online(changer) => {
|
||||
write!(f, "online-{}", changer)
|
||||
}
|
||||
MediaLocation::Vault(vault) => {
|
||||
write!(f, "vault-{}", vault)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for MediaLocation {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s == "offline" {
|
||||
return Ok(MediaLocation::Offline);
|
||||
}
|
||||
if let Some(changer) = s.strip_prefix("online-") {
|
||||
return Ok(MediaLocation::Online(changer.to_string()));
|
||||
}
|
||||
if let Some(vault) = s.strip_prefix("vault-") {
|
||||
return Ok(MediaLocation::Online(vault.to_string()));
|
||||
}
|
||||
|
||||
bail!("MediaLocation parse error");
|
||||
}
|
||||
}
|
@ -15,5 +15,8 @@ pub use media_pool::*;
|
||||
mod media_status;
|
||||
pub use media_status::*;
|
||||
|
||||
mod media_location;
|
||||
pub use media_location::*;
|
||||
|
||||
mod media;
|
||||
pub use media::*;
|
||||
|
Reference in New Issue
Block a user