diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index 576099eb..1c9063bf 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -3,7 +3,10 @@ use serde::{Deserialize, Serialize}; use proxmox::api::api; -use proxmox::api::schema::{ApiStringFormat, EnumEntry, IntegerSchema, Schema, StringSchema}; +use proxmox::api::schema::{ + ApiStringFormat, ApiType, ArraySchema, EnumEntry, IntegerSchema, ReturnType, Schema, + StringSchema, +}; use proxmox::const_regex; use proxmox::{IPRE, IPRE_BRACKET, IPV4OCTET, IPV4RE, IPV6H16, IPV6LS32, IPV6RE}; @@ -564,3 +567,74 @@ impl std::convert::TryFrom> for RsaPubK }) } } + +#[api( + properties: { + upid: { schema: UPID::API_SCHEMA }, + }, +)] +#[derive(Serialize, Deserialize)] +/// Task properties. +pub struct TaskListItem { + pub upid: String, + /// The node name where the task is running on. + pub node: String, + /// The Unix PID + pub pid: i64, + /// The task start time (Epoch) + pub pstart: u64, + /// The task start time (Epoch) + pub starttime: i64, + /// Worker type (arbitrary ASCII string) + pub worker_type: String, + /// Worker ID (arbitrary ASCII string) + pub worker_id: Option, + /// The authenticated entity who started the task + pub user: Authid, + /// The task end time (Epoch) + #[serde(skip_serializing_if="Option::is_none")] + pub endtime: Option, + /// Task end status + #[serde(skip_serializing_if="Option::is_none")] + pub status: Option, +} + +pub const ADMIN_DATASTORE_LIST_SNAPSHOTS_RETURN_TYPE: ReturnType = ReturnType { + optional: false, + schema: &ArraySchema::new( + "Returns the list of snapshots.", + &SnapshotListItem::API_SCHEMA, + ).schema(), +}; + +pub const ADMIN_DATASTORE_LIST_SNAPSHOT_FILES_RETURN_TYPE: ReturnType = ReturnType { + optional: false, + schema: &ArraySchema::new( + "Returns the list of archive files inside a backup snapshots.", + &BackupContent::API_SCHEMA, + ).schema(), +}; + +pub const ADMIN_DATASTORE_LIST_GROUPS_RETURN_TYPE: ReturnType = ReturnType { + optional: false, + schema: &ArraySchema::new( + "Returns the list of backup groups.", + &GroupListItem::API_SCHEMA, + ).schema(), +}; + +pub const ADMIN_DATASTORE_PRUNE_RETURN_TYPE: ReturnType = ReturnType { + optional: false, + schema: &ArraySchema::new( + "Returns the list of snapshots and a flag indicating if there are kept or removed.", + &PruneListItem::API_SCHEMA, + ).schema(), +}; + +pub const NODE_TASKS_LIST_TASKS_RETURN_TYPE: ReturnType = ReturnType { + optional: false, + schema: &ArraySchema::new( + "A list of tasks.", + &TaskListItem::API_SCHEMA, + ).schema(), +}; diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index f3c52413..0998d659 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -134,13 +134,7 @@ fn get_all_snapshot_files( }, }, }, - returns: { - type: Array, - description: "Returns the list of backup groups.", - items: { - type: GroupListItem, - } - }, + returns: pbs_api_types::ADMIN_DATASTORE_LIST_GROUPS_RETURN_TYPE, access: { permission: &Permission::Privilege( &["datastore", "{store}"], @@ -282,13 +276,7 @@ pub fn delete_group( }, }, }, - returns: { - type: Array, - description: "Returns the list of archive files inside a backup snapshots.", - items: { - type: BackupContent, - } - }, + returns: pbs_api_types::ADMIN_DATASTORE_LIST_SNAPSHOT_FILES_RETURN_TYPE, access: { permission: &Permission::Privilege( &["datastore", "{store}"], @@ -382,13 +370,7 @@ pub fn delete_snapshot( }, }, }, - returns: { - type: Array, - description: "Returns the list of snapshots.", - items: { - type: SnapshotListItem, - } - }, + returns: pbs_api_types::ADMIN_DATASTORE_LIST_SNAPSHOTS_RETURN_TYPE, access: { permission: &Permission::Privilege( &["datastore", "{store}"], @@ -809,13 +791,7 @@ pub fn verify( }, }, }, - returns: { - type: Array, - description: "Returns the list of snapshots and a flag indicating if there are kept or removed.", - items: { - type: PruneListItem, - }, - }, + returns: pbs_api_types::ADMIN_DATASTORE_PRUNE_RETURN_TYPE, access: { permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_MODIFY | PRIV_DATASTORE_PRUNE, true), }, diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs index 326dbf7a..89665c68 100644 --- a/src/api2/node/tasks.rs +++ b/src/api2/node/tasks.rs @@ -412,11 +412,7 @@ fn stop_task( }, }, }, - returns: { - description: "A list of tasks.", - type: Array, - items: { type: TaskListItem }, - }, + returns: pbs_api_types::NODE_TASKS_LIST_TASKS_RETURN_TYPE, access: { description: "Users can only see their own tasks, unless they have Sys.Audit on /system/tasks.", permission: &Permission::Anybody, diff --git a/src/api2/types/mod.rs b/src/api2/types/mod.rs index bd3c7ac5..2b5e3f71 100644 --- a/src/api2/types/mod.rs +++ b/src/api2/types/mod.rs @@ -379,59 +379,6 @@ pub struct DataStoreStatus { pub counts: Option, } -#[api( - properties: { - upid: { schema: UPID_SCHEMA }, - user: { type: Authid }, - }, -)] -#[derive(Serialize, Deserialize)] -/// Task properties. -pub struct TaskListItem { - pub upid: String, - /// The node name where the task is running on. - pub node: String, - /// The Unix PID - pub pid: i64, - /// The task start time (Epoch) - pub pstart: u64, - /// The task start time (Epoch) - pub starttime: i64, - /// Worker type (arbitrary ASCII string) - pub worker_type: String, - /// Worker ID (arbitrary ASCII string) - pub worker_id: Option, - /// The authenticated entity who started the task - pub user: Authid, - /// The task end time (Epoch) - #[serde(skip_serializing_if="Option::is_none")] - pub endtime: Option, - /// Task end status - #[serde(skip_serializing_if="Option::is_none")] - pub status: Option, -} - -impl From for TaskListItem { - fn from(info: crate::server::TaskListInfo) -> Self { - let (endtime, status) = info - .state - .map_or_else(|| (None, None), |a| (Some(a.endtime()), Some(a.to_string()))); - - TaskListItem { - upid: info.upid_str, - node: "localhost".to_string(), - pid: info.upid.pid as i64, - pstart: info.upid.pstart, - starttime: info.upid.starttime, - worker_type: info.upid.worker_type, - worker_id: info.upid.worker_id, - user: info.upid.auth_id, - endtime, - status, - } - } -} - #[api()] #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index be9a4794..075aaa6c 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -314,6 +314,27 @@ pub struct TaskListInfo { pub state: Option, // endtime, status } +impl Into for TaskListInfo { + fn into(self) -> pbs_api_types::TaskListItem { + let (endtime, status) = self + .state + .map_or_else(|| (None, None), |a| (Some(a.endtime()), Some(a.to_string()))); + + pbs_api_types::TaskListItem { + upid: self.upid_str, + node: "localhost".to_string(), + pid: self.upid.pid as i64, + pstart: self.upid.pstart, + starttime: self.upid.starttime, + worker_type: self.upid.worker_type, + worker_id: self.upid.worker_id, + user: self.upid.auth_id, + endtime, + status, + } + } +} + fn lock_task_list_files(exclusive: bool) -> Result { open_backup_lockfile(PROXMOX_BACKUP_TASK_LOCK_FN, None, exclusive) }