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
: PROXMOX_CONFIG_DIGEST_SCHEMA
,
118 /// Create new datastore config.
119 pub fn update_datastore(
121 comment
: Option
<String
>,
122 digest
: 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
, expected_digest
) = datastore
::config()?
;
130 if let Some(ref digest
) = digest
{
131 let digest
= proxmox
::tools
::hex_to_digest(digest
)?
;
132 crate::tools
::detect_modified_configuration_file(&digest
, &expected_digest
)?
;
135 let mut data
: datastore
::DataStoreConfig
= config
.lookup("datastore", &name
)?
;
137 if let Some(comment
) = comment
{
138 let comment
= comment
.trim().to_string();
139 if comment
.is_empty() {
142 data
.comment
= Some(comment
);
146 config
.set_data(&name
, "datastore", &data
)?
;
148 datastore
::save_config(&config
)?
;
158 schema
: DATASTORE_SCHEMA
,
163 /// Remove a datastore configuration.
164 pub fn delete_datastore(name
: String
) -> Result
<(), Error
> {
167 // fixme: check digest ?
169 let (mut config
, _digest
) = datastore
::config()?
;
171 match config
.sections
.get(&name
) {
172 Some(_
) => { config.sections.remove(&name); }
,
173 None
=> bail
!("datastore '{}' does not exist.", name
),
176 datastore
::save_config(&config
)?
;
181 const ITEM_ROUTER
: Router
= Router
::new()
182 .get(&API_METHOD_READ_DATASTORE
)
183 .put(&API_METHOD_UPDATE_DATASTORE
)
184 .delete(&API_METHOD_DELETE_DATASTORE
);
186 pub const ROUTER
: Router
= Router
::new()
187 .get(&API_METHOD_LIST_DATASTORES
)
188 .post(&API_METHOD_CREATE_DATASTORE
)
189 .match_all("name", &ITEM_ROUTER
);