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())?; 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 members = inventory.compute_media_set_members(&media_set_uuid)?;
let media_list = members.media_list(); 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 { if let Some(fingerprint) = encryption_key_fingerprint {
task_log!(worker, "Encryption key fingerprint: {}", fingerprint); task_log!(worker, "Encryption key fingerprint: {}", fingerprint);
} }
task_log!(worker, "Pool: {}", pool);
task_log!( task_log!(
worker, worker,
"Datastore(s): {}", "Datastore(s): {}",
@ -322,7 +369,7 @@ pub fn restore(
.join(", "), .join(", "),
); );
task_log!(worker, "Drive: {}", drive); task_log!(worker, "Drive: {}", drive_name);
task_log!( task_log!(
worker, worker,
"Required media list: {}", "Required media list: {}",
@ -348,34 +395,16 @@ pub fn restore(
worker.clone(), worker.clone(),
media_id, media_id,
&drive_config, &drive_config,
&drive, drive_name,
&store_map, &store_map,
&mut checked_chunks_map, &mut checked_chunks_map,
&auth_id, restore_owner,
&notify_user, &email,
&owner,
)?; )?;
} }
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(())
} }
)?;
Ok(upid_str.into())
}
/// Request and restore complete media without using existing catalog (create catalog instead) /// Request and restore complete media without using existing catalog (create catalog instead)
pub fn request_and_restore_media( pub fn request_and_restore_media(
@ -385,21 +414,15 @@ pub fn request_and_restore_media(
drive_name: &str, drive_name: &str,
store_map: &DataStoreMap, store_map: &DataStoreMap,
checked_chunks_map: &mut HashMap<String, HashSet<[u8;32]>>, checked_chunks_map: &mut HashMap<String, HashSet<[u8;32]>>,
authid: &Authid, restore_owner: &Authid,
notify_user: &Option<Userid>, email: &Option<String>,
owner: &Option<Authid>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let media_set_uuid = match media_id.media_set_label { let media_set_uuid = match media_id.media_set_label {
None => bail!("restore_media: no media set - internal error"), None => bail!("restore_media: no media set - internal error"),
Some(ref set) => &set.uuid, Some(ref set) => &set.uuid,
}; };
let email = notify_user let (mut drive, info) = request_and_load_media(&worker, &drive_config, &drive_name, &media_id.label, email)?;
.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)?;
match info.media_set_label { match info.media_set_label {
None => { None => {
@ -419,8 +442,6 @@ pub fn request_and_restore_media(
} }
} }
let restore_owner = owner.as_ref().unwrap_or(authid);
restore_media( restore_media(
worker, worker,
&mut drive, &mut drive,