1 use std
::path
::PathBuf
;
6 use proxmox
::api
::{api, ApiMethod, Router, RpcEnvironment}
;
8 use crate::api2
::types
::*;
10 use crate::config
::datastore
;
17 description
: "List the configured datastores (with config digest).",
20 type: datastore
::DataStoreConfig
,
24 /// List all datastores
25 pub fn list_datastores(
28 _rpcenv
: &mut dyn RpcEnvironment
,
29 ) -> Result
<Value
, Error
> {
31 let (config
, digest
) = datastore
::config()?
;
33 Ok(config
.convert_to_array("name", Some(&digest
)))
41 schema
: DATASTORE_SCHEMA
,
45 schema
: SINGLE_LINE_COMMENT_SCHEMA
,
48 schema
: datastore
::DIR_NAME_SCHEMA
,
53 /// Create new datastore config.
54 pub fn create_datastore(name
: String
, param
: Value
) -> Result
<(), Error
> {
56 let _lock
= crate::tools
::open_file_locked(datastore
::DATASTORE_CFG_LOCKFILE
, std
::time
::Duration
::new(10, 0))?
;
58 let datastore
: datastore
::DataStoreConfig
= serde_json
::from_value(param
.clone())?
;
60 let (mut config
, _digest
) = datastore
::config()?
;
62 if let Some(_
) = config
.sections
.get(&name
) {
63 bail
!("datastore '{}' already exists.", name
);
66 let path
: PathBuf
= datastore
.path
.clone().into();
68 let backup_user
= crate::backup
::backup_user()?
;
69 let _store
= ChunkStore
::create(&name
, path
, backup_user
.uid
, backup_user
.gid
)?
;
71 config
.set_data(&name
, "datastore", &datastore
)?
;
73 datastore
::save_config(&config
)?
;
82 schema
: DATASTORE_SCHEMA
,
87 description
: "The datastore configuration (with config digest).",
88 type: datastore
::DataStoreConfig
,
91 /// Read a datastore configuration.
92 pub fn read_datastore(name
: String
) -> Result
<Value
, Error
> {
93 let (config
, digest
) = datastore
::config()?
;
94 let mut data
= config
.lookup_json("datastore", &name
)?
;
95 data
.as_object_mut().unwrap()
96 .insert("digest".into(), proxmox
::tools
::digest_to_hex(&digest
).into());
105 schema
: DATASTORE_SCHEMA
,
109 schema
: SINGLE_LINE_COMMENT_SCHEMA
,
113 schema
: datastore
::DIR_NAME_SCHEMA
,
118 /// Create new datastore config.
119 pub fn update_datastore(
121 comment
: Option
<String
>,
122 path
: Option
<String
>,
123 ) -> Result
<(), Error
> {
125 let _lock
= crate::tools
::open_file_locked(datastore
::DATASTORE_CFG_LOCKFILE
, std
::time
::Duration
::new(10, 0))?
;
127 // pass/compare digest
128 let (mut config
, _digest
) = datastore
::config()?
;
130 let mut data
: datastore
::DataStoreConfig
= config
.lookup("datastore", &name
)?
;
132 if let Some(comment
) = comment
{
133 let comment
= comment
.trim().to_string();
134 if comment
.is_empty() {
137 data
.comment
= Some(comment
);
140 if let Some(path
) = path { data.path = path; }
142 config
.set_data(&name
, "datastore", &data
)?
;
144 datastore
::save_config(&config
)?
;
154 schema
: DATASTORE_SCHEMA
,
159 /// Remove a datastore configuration.
160 pub fn delete_datastore(name
: String
) -> Result
<(), Error
> {
163 // fixme: check digest ?
165 let (mut config
, _digest
) = datastore
::config()?
;
167 match config
.sections
.get(&name
) {
168 Some(_
) => { config.sections.remove(&name); }
,
169 None
=> bail
!("datastore '{}' does not exist.", name
),
172 datastore
::save_config(&config
)?
;
177 const ITEM_ROUTER
: Router
= Router
::new()
178 .get(&API_METHOD_READ_DATASTORE
)
179 .put(&API_METHOD_UPDATE_DATASTORE
)
180 .delete(&API_METHOD_DELETE_DATASTORE
);
182 pub const ROUTER
: Router
= Router
::new()
183 .get(&API_METHOD_LIST_DATASTORES
)
184 .post(&API_METHOD_CREATE_DATASTORE
)
185 .match_all("name", &ITEM_ROUTER
);