src/api2/admin/datastore.rs: allow to list all snapshots

This commit is contained in:
Dietmar Maurer 2019-07-18 09:11:43 +02:00
parent 0f73ee73bb
commit 15c847f136
2 changed files with 21 additions and 94 deletions

View File

@ -135,20 +135,25 @@ fn list_snapshots (
) -> Result<Value, Error> {
let store = tools::required_string_param(&param, "store")?;
let backup_type = tools::required_string_param(&param, "backup-type")?;
let backup_id = tools::required_string_param(&param, "backup-id")?;
let group = BackupGroup::new(backup_type, backup_id);
let backup_type = param["backup-type"].as_str();
let backup_id = param["backup-id"].as_str();
let datastore = DataStore::lookup_datastore(store)?;
let base_path = datastore.base_path();
let backup_list = group.list_backups(&base_path)?;
let backup_list = BackupInfo::list_backups(&base_path)?;
let mut snapshots = vec![];
for info in backup_list {
let group = info.backup_dir.group();
if let Some(backup_type) = backup_type {
if backup_type != group.backup_type() { continue; }
}
if let Some(backup_id) = backup_id {
if backup_id != group.backup_id() { continue; }
}
snapshots.push(json!({
"backup-type": group.backup_type(),
"backup-id": group.backup_id(),
@ -391,36 +396,6 @@ pub fn api_method_garbage_collection_status() -> ApiMethod {
)
}
fn get_backup_list(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
//let config = datastore::config()?;
let store = param["store"].as_str().unwrap();
let datastore = DataStore::lookup_datastore(store)?;
let mut list = vec![];
let backup_list = BackupInfo::list_backups(&datastore.base_path())?;
for info in backup_list {
list.push(json!({
"backup-type": info.backup_dir.group().backup_type(),
"backup-id": info.backup_dir.group().backup_id(),
"backup-time": info.backup_dir.backup_time().timestamp(),
"files": info.files,
}));
}
let result = json!(list);
Ok(result)
}
fn get_datastore_list(
_param: Value,
_info: &ApiMethod,
@ -499,13 +474,6 @@ pub fn router() -> Router {
);
let datastore_info = Router::new()
.subdir(
"backups",
Router::new()
.get(ApiMethod::new(
get_backup_list,
ObjectSchema::new("List backups.")
.required("store", store_schema.clone()))))
.subdir(
"download",
Router::new()
@ -546,8 +514,8 @@ pub fn router() -> Router {
list_snapshots,
ObjectSchema::new("List backup groups.")
.required("store", store_schema.clone())
.required("backup-type", StringSchema::new("Backup type."))
.required("backup-id", StringSchema::new("Backup ID."))
.optional("backup-type", StringSchema::new("Backup type."))
.optional("backup-id", StringSchema::new("Backup ID."))
)
)
.delete(

View File

@ -219,48 +219,6 @@ fn strip_server_file_expenstions(list: Vec<String>) -> Vec<String> {
result
}
/* not used:
fn list_backups(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let repo = extract_repository_from_value(&param)?;
let mut client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/backups", repo.store());
let result = client.get(&path, None)?;
record_repository(&repo);
// fixme: implement and use output formatter instead ..
let list = result["data"].as_array().unwrap();
for item in list {
let id = item["backup-id"].as_str().unwrap();
let btype = item["backup-type"].as_str().unwrap();
let epoch = item["backup-time"].as_i64().unwrap();
let backup_dir = BackupDir::new(btype, id, epoch);
let files = item["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect();
let files = strip_server_file_expenstions(files);
for filename in files {
let path = backup_dir.relative_path().to_str().unwrap().to_owned();
println!("{} | {}/{}", backup_dir.backup_time().format("%c"), path, filename);
}
}
//Ok(result)
Ok(Value::Null)
}
*/
fn list_backup_groups(
param: Value,
_info: &ApiMethod,
@ -340,19 +298,20 @@ fn list_snapshots(
let repo = extract_repository_from_value(&param)?;
let path = tools::required_string_param(&param, "group")?;
let group = BackupGroup::parse(path)?;
let output_format = param["output-format"].as_str().unwrap_or("text").to_owned();
let client = HttpClient::new(repo.host(), repo.user())?;
let path = format!("api2/json/admin/datastore/{}/snapshots", repo.store());
let result = client.get(&path, Some(json!({
"backup-type": group.backup_type(),
"backup-id": group.backup_id(),
}))).wait()?;
let mut args = json!({});
if let Some(path) = param["group"].as_str() {
let group = BackupGroup::parse(path)?;
args["backup-type"] = group.backup_type().into();
args["backup-id"] = group.backup_id().into();
}
let result = client.get(&path, Some(args)).wait()?;
record_repository(&repo);
@ -1291,7 +1250,7 @@ fn main() {
ApiMethod::new(
list_snapshots,
ObjectSchema::new("List backup snapshots.")
.required("group", StringSchema::new("Backup group."))
.optional("group", StringSchema::new("Backup group."))
.optional("repository", REPO_URL_SCHEMA.clone())
.optional("output-format", OUTPUT_FORMAT.clone())
))