]> git.proxmox.com Git - proxmox-backup.git/blob - src/client/backup_repo.rs
dc9b8ec84fab77478688e1c26472b7176a5e24b9
[proxmox-backup.git] / src / client / backup_repo.rs
1 use std::convert::TryFrom;
2 use std::fmt;
3
4 use anyhow::{format_err, Error};
5
6 use pbs_api_types::{BACKUP_REPO_URL_REGEX, IP_V6_REGEX, Authid, Userid};
7
8 /// Reference remote backup locations
9 ///
10
11 #[derive(Debug)]
12 pub struct BackupRepository {
13 /// The user name used for Authentication
14 auth_id: Option<Authid>,
15 /// The host name or IP address
16 host: Option<String>,
17 /// The port
18 port: Option<u16>,
19 /// The name of the datastore
20 store: String,
21 }
22
23 impl BackupRepository {
24
25 pub fn new(auth_id: Option<Authid>, host: Option<String>, port: Option<u16>, store: String) -> Self {
26 let host = match host {
27 Some(host) if (IP_V6_REGEX.regex_obj)().is_match(&host) => {
28 Some(format!("[{}]", host))
29 },
30 other => other,
31 };
32 Self { auth_id, host, port, store }
33 }
34
35 pub fn auth_id(&self) -> &Authid {
36 if let Some(ref auth_id) = self.auth_id {
37 return auth_id;
38 }
39
40 &Authid::root_auth_id()
41 }
42
43 pub fn user(&self) -> &Userid {
44 if let Some(auth_id) = &self.auth_id {
45 return auth_id.user();
46 }
47
48 Userid::root_userid()
49 }
50
51 pub fn host(&self) -> &str {
52 if let Some(ref host) = self.host {
53 return host;
54 }
55 "localhost"
56 }
57
58 pub fn port(&self) -> u16 {
59 if let Some(port) = self.port {
60 return port;
61 }
62 8007
63 }
64
65 pub fn store(&self) -> &str {
66 &self.store
67 }
68 }
69
70 impl fmt::Display for BackupRepository {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 match (&self.auth_id, &self.host, self.port) {
73 (Some(auth_id), _, _) => write!(f, "{}@{}:{}:{}", auth_id, self.host(), self.port(), self.store),
74 (None, Some(host), None) => write!(f, "{}:{}", host, self.store),
75 (None, _, Some(port)) => write!(f, "{}:{}:{}", self.host(), port, self.store),
76 (None, None, None) => write!(f, "{}", self.store),
77 }
78 }
79 }
80
81 impl std::str::FromStr for BackupRepository {
82 type Err = Error;
83
84 /// Parse a repository URL.
85 ///
86 /// This parses strings like `user@host:datastore`. The `user` and
87 /// `host` parts are optional, where `host` defaults to the local
88 /// host, and `user` defaults to `root@pam`.
89 fn from_str(url: &str) -> Result<Self, Self::Err> {
90
91 let cap = (BACKUP_REPO_URL_REGEX.regex_obj)().captures(url)
92 .ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?;
93
94 Ok(Self {
95 auth_id: cap.get(1).map(|m| Authid::try_from(m.as_str().to_owned())).transpose()?,
96 host: cap.get(2).map(|m| m.as_str().to_owned()),
97 port: cap.get(3).map(|m| m.as_str().parse::<u16>()).transpose()?,
98 store: cap[4].to_owned(),
99 })
100 }
101 }