From 455e5f71105e2c9e67aa4043378f654d37960a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Thu, 5 Nov 2020 12:12:22 +0100 Subject: [PATCH] types: extract DataStoreListItem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit for reuse in remote scan API call Signed-off-by: Fabian Grünbichler --- src/api2/admin/datastore.rs | 23 ++++++++--------------- src/api2/types/mod.rs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index a5d3e979..b051e8dd 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -902,15 +902,7 @@ pub fn garbage_collection_status( type: Array, items: { description: "Datastore name and description.", - properties: { - store: { - schema: DATASTORE_SCHEMA, - }, - comment: { - optional: true, - schema: SINGLE_LINE_COMMENT_SCHEMA, - }, - }, + type: DataStoreListItem, }, }, access: { @@ -922,7 +914,7 @@ fn get_datastore_list( _param: Value, _info: &ApiMethod, rpcenv: &mut dyn RpcEnvironment, -) -> Result { +) -> Result, Error> { let (config, _digest) = datastore::config()?; @@ -935,11 +927,12 @@ fn get_datastore_list( let user_privs = user_info.lookup_privs(&auth_id, &["datastore", &store]); let allowed = (user_privs & (PRIV_DATASTORE_AUDIT| PRIV_DATASTORE_BACKUP)) != 0; if allowed { - let mut entry = json!({ "store": store }); - if let Some(comment) = data["comment"].as_str() { - entry["comment"] = comment.into(); - } - list.push(entry); + list.push( + DataStoreListItem { + store: store.clone(), + comment: data["comment"].as_str().map(String::from), + } + ); } } diff --git a/src/api2/types/mod.rs b/src/api2/types/mod.rs index 2380e36a..b1dac2f8 100644 --- a/src/api2/types/mod.rs +++ b/src/api2/types/mod.rs @@ -370,6 +370,25 @@ pub const BLOCKDEVICE_NAME_SCHEMA: Schema = StringSchema::new("Block device name // Complex type definitions +#[api( + properties: { + store: { + schema: DATASTORE_SCHEMA, + }, + comment: { + optional: true, + schema: SINGLE_LINE_COMMENT_SCHEMA, + }, + }, +)] +#[derive(Serialize, Deserialize)] +#[serde(rename_all="kebab-case")] +/// Basic information about a datastore. +pub struct DataStoreListItem { + pub store: String, + pub comment: Option, +} + #[api( properties: { "backup-type": {