api2/tape/restore: refactor restore code into its own function

and create the 'email' and 'restore_owner' variable at the beginning,
so that we can reuse them and do not have to pass the sources of those
through too many functions

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-05-11 12:50:03 +02:00 committed by Dietmar Maurer
parent 1bff50afea
commit fa9507020a

View File

@ -281,6 +281,54 @@ pub fn restore(
set_tape_device_state(&drive, &worker.upid().to_string())?;
let restore_owner = owner.as_ref().unwrap_or(&auth_id);
let email = notify_user
.as_ref()
.and_then(|userid| lookup_user_email(userid))
.or_else(|| lookup_user_email(&auth_id.clone().into()));
task_log!(worker, "Restore mediaset '{}'", media_set);
task_log!(worker, "Pool: {}", pool);
let res = restore_worker(
worker.clone(),
inventory,
media_set_uuid,
drive_config,
&drive,
store_map,
restore_owner,
email
);
task_log!(worker, "Restore mediaset '{}' done", media_set);
if let Err(err) = set_tape_device_state(&drive, "") {
task_log!(
worker,
"could not unset drive state for {}: {}",
drive,
err
);
}
res
}
)?;
Ok(upid_str.into())
}
fn restore_worker(
worker: Arc<WorkerTask>,
inventory: Inventory,
media_set_uuid: Uuid,
drive_config: SectionConfigData,
drive_name: &str,
store_map: DataStoreMap,
restore_owner: &Authid,
email: Option<String>,
) -> Result<(), Error> {
let members = inventory.compute_media_set_members(&media_set_uuid)?;
let media_list = members.media_list();
@ -306,11 +354,10 @@ pub fn restore(
}
}
task_log!(worker, "Restore mediaset '{}'", media_set);
if let Some(fingerprint) = encryption_key_fingerprint {
task_log!(worker, "Encryption key fingerprint: {}", fingerprint);
}
task_log!(worker, "Pool: {}", pool);
task_log!(
worker,
"Datastore(s): {}",
@ -322,7 +369,7 @@ pub fn restore(
.join(", "),
);
task_log!(worker, "Drive: {}", drive);
task_log!(worker, "Drive: {}", drive_name);
task_log!(
worker,
"Required media list: {}",
@ -348,33 +395,15 @@ pub fn restore(
worker.clone(),
media_id,
&drive_config,
&drive,
drive_name,
&store_map,
&mut checked_chunks_map,
&auth_id,
&notify_user,
&owner,
restore_owner,
&email,
)?;
}
drop(datastore_locks);
task_log!(worker, "Restore mediaset '{}' done", media_set);
if let Err(err) = set_tape_device_state(&drive, "") {
task_log!(
worker,
"could not unset drive state for {}: {}",
drive,
err
);
}
Ok(())
}
)?;
Ok(upid_str.into())
}
/// Request and restore complete media without using existing catalog (create catalog instead)
@ -385,21 +414,15 @@ pub fn request_and_restore_media(
drive_name: &str,
store_map: &DataStoreMap,
checked_chunks_map: &mut HashMap<String, HashSet<[u8;32]>>,
authid: &Authid,
notify_user: &Option<Userid>,
owner: &Option<Authid>,
restore_owner: &Authid,
email: &Option<String>,
) -> Result<(), Error> {
let media_set_uuid = match media_id.media_set_label {
None => bail!("restore_media: no media set - internal error"),
Some(ref set) => &set.uuid,
};
let email = notify_user
.as_ref()
.and_then(|userid| lookup_user_email(userid))
.or_else(|| lookup_user_email(&authid.clone().into()));
let (mut drive, info) = request_and_load_media(&worker, &drive_config, &drive_name, &media_id.label, &email)?;
let (mut drive, info) = request_and_load_media(&worker, &drive_config, &drive_name, &media_id.label, email)?;
match info.media_set_label {
None => {
@ -419,8 +442,6 @@ pub fn request_and_restore_media(
}
}
let restore_owner = owner.as_ref().unwrap_or(authid);
restore_media(
worker,
&mut drive,