]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/api2/config/datastore.rs: improve api, implement update and read
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 14 Jan 2020 13:45:56 +0000 (14:45 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 14 Jan 2020 13:45:56 +0000 (14:45 +0100)
src/api2/config/datastore.rs

index ef979de697b3dcfa88dce449a0c91aaf5cb15dd8..2408a2b5e34ec7d747c56cff6c96265cb513ad1b 100644 (file)
@@ -75,6 +75,72 @@ pub fn create_datastore(name: String, param: Value) -> Result<(), Error> {
     Ok(())
 }
 
+#[api(
+   input: {
+        properties: {
+            name: {
+                schema: DATASTORE_SCHEMA,
+            },
+        },
+    },
+)]
+/// Read a datastore configuration.
+pub fn read_datastore(name: String) -> Result<Value, Error> {
+    let (config, digest) = datastore::config()?;
+    let mut data = config.lookup_json("datastore", &name)?;
+    data.as_object_mut().unwrap()
+        .insert("digest".into(), proxmox::tools::digest_to_hex(&digest).into());
+    Ok(data)
+}
+
+#[api(
+    protected: true,
+    input: {
+        properties: {
+            name: {
+                schema: DATASTORE_SCHEMA,
+            },
+            comment: {
+                optional: true,
+                schema: SINGLE_LINE_COMMENT_SCHEMA,
+            },
+            path: {
+                optional: true,
+                schema: datastore::DIR_NAME_SCHEMA,
+            },
+        },
+    },
+)]
+/// Create new datastore config.
+pub fn update_datastore(
+    name: String,
+    comment: Option<String>,
+    path: Option<String>,
+) -> Result<(), Error> {
+
+    // fixme: locking ?
+    // pass/compare digest
+    let (mut config, _digest) = datastore::config()?;
+
+    let mut data: datastore::DataStoreConfig = config.lookup("datastore", &name)?;
+
+    if let Some(comment) = comment {
+        let comment = comment.trim().to_string();
+        if comment.is_empty() {
+            data.comment = None;
+        } else {
+            data.comment = Some(comment);
+        }
+    }
+    if let Some(path) = path { data.path = path; }
+
+    config.set_data(&name, "datastore", &data)?;
+
+    datastore::save_config(&config)?;
+
+    Ok(())
+}
+
 #[api(
     protected: true,
     input: {
@@ -103,7 +169,12 @@ pub fn delete_datastore(name: String) -> Result<(), Error> {
     Ok(())
 }
 
+const ITEM_ROUTER: Router = Router::new()
+    .get(&API_METHOD_READ_DATASTORE)
+    .put(&API_METHOD_UPDATE_DATASTORE)
+    .delete(&API_METHOD_DELETE_DATASTORE);
+
 pub const ROUTER: Router = Router::new()
     .get(&API_METHOD_LIST_DATASTORES)
     .post(&API_METHOD_CREATE_DATASTORE)
-    .delete(&API_METHOD_DELETE_DATASTORE);
+    .match_all("name", &ITEM_ROUTER);