]> git.proxmox.com Git - proxmox-backup.git/blame - src/api2/types/mod.rs
client/remote: add support to specify port number
[proxmox-backup.git] / src / api2 / types / mod.rs
CommitLineData
e7cb4dc5
WB
1use anyhow::bail;
2use serde::{Deserialize, Serialize};
4ebf0eab 3
9ea4bce4
WB
4use proxmox::api::{api, schema::*};
5use proxmox::const_regex;
60b9b48e 6use proxmox::{IPRE, IPRE_BRACKET, IPV4RE, IPV6RE, IPV4OCTET, IPV6H16, IPV6LS32};
255f378a 7
f28d9088 8use crate::backup::CryptMode;
3b2046d2 9use crate::server::UPID;
f28d9088 10
e7cb4dc5
WB
11#[macro_use]
12mod macros;
13
14#[macro_use]
15mod userid;
16pub use userid::{Realm, RealmRef};
17pub use userid::{Username, UsernameRef};
18pub use userid::Userid;
19pub use userid::PROXMOX_GROUP_ID_SCHEMA;
20
255f378a
DM
21// File names: may not contain slashes, may not start with "."
22pub const FILENAME_FORMAT: ApiStringFormat = ApiStringFormat::VerifyFn(|name| {
23 if name.starts_with('.') {
24 bail!("file names may not start with '.'");
25 }
26 if name.contains('/') {
27 bail!("file names may not contain slashes");
28 }
29 Ok(())
30});
31
b25f313d
DM
32macro_rules! DNS_LABEL { () => (r"(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)") }
33macro_rules! DNS_NAME { () => (concat!(r"(?:", DNS_LABEL!() , r"\.)*", DNS_LABEL!())) }
255f378a 34
76cf5208
DM
35macro_rules! CIDR_V4_REGEX_STR { () => (concat!(r"(?:", IPV4RE!(), r"/\d{1,2})$")) }
36macro_rules! CIDR_V6_REGEX_STR { () => (concat!(r"(?:", IPV6RE!(), r"/\d{1,3})$")) }
37
255f378a 38const_regex!{
76cf5208
DM
39 pub IP_V4_REGEX = concat!(r"^", IPV4RE!(), r"$");
40 pub IP_V6_REGEX = concat!(r"^", IPV6RE!(), r"$");
41 pub IP_REGEX = concat!(r"^", IPRE!(), r"$");
42 pub CIDR_V4_REGEX = concat!(r"^", CIDR_V4_REGEX_STR!(), r"$");
43 pub CIDR_V6_REGEX = concat!(r"^", CIDR_V6_REGEX_STR!(), r"$");
44 pub CIDR_REGEX = concat!(r"^(?:", CIDR_V4_REGEX_STR!(), "|", CIDR_V6_REGEX_STR!(), r")$");
45
255f378a
DM
46 pub SHA256_HEX_REGEX = r"^[a-f0-9]{64}$"; // fixme: define in common_regex ?
47 pub SYSTEMD_DATETIME_REGEX = r"^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$"; // fixme: define in common_regex ?
d0adf270 48
da4a15a3
DM
49 pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
50
d0adf270
DM
51 /// Regex for safe identifiers.
52 ///
53 /// This
54 /// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
55 /// contains further information why it is reasonable to restict
56 /// names this way. This is not only useful for filenames, but for
57 /// any identifier command line tools work with.
163dc16c 58 pub PROXMOX_SAFE_ID_REGEX = concat!(r"^", PROXMOX_SAFE_ID_REGEX_STR!(), r"$");
454c13ed
DM
59
60 pub SINGLE_LINE_COMMENT_REGEX = r"^[[:^cntrl:]]*$";
b25f313d
DM
61
62 pub HOSTNAME_REGEX = r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)$";
63
ae62c4fe 64 pub DNS_NAME_REGEX = concat!(r"^", DNS_NAME!(), r"$");
b25f313d 65
ae62c4fe 66 pub DNS_NAME_OR_IP_REGEX = concat!(r"^", DNS_NAME!(), "|", IPRE!(), r"$");
163dc16c 67
ba20987a 68 pub BACKUP_REPO_URL_REGEX = concat!(r"^^(?:(?:(", USER_ID_REGEX_STR!(), ")@)?(", DNS_NAME!(), "|", IPRE_BRACKET!() ,"):)?(?:([0-9]{1,5}):)?(", PROXMOX_SAFE_ID_REGEX_STR!(), r")$");
090decbe 69
dcb8db66 70 pub CERT_FINGERPRINT_SHA256_REGEX = r"^(?:[0-9a-fA-F][0-9a-fA-F])(?::[0-9a-fA-F][0-9a-fA-F]){31}$";
ed3e60ae 71
9765092e 72 pub ACL_PATH_REGEX = concat!(r"^(?:/|", r"(?:/", PROXMOX_SAFE_ID_REGEX_STR!(), ")+", r")$");
9069debc
DM
73
74 pub BLOCKDEVICE_NAME_REGEX = r"^(:?(:?h|s|x?v)d[a-z]+)|(:?nvme\d+n\d+)$";
7957fabf
DC
75
76 pub ZPOOL_NAME_REGEX = r"^[a-zA-Z][a-z0-9A-Z\-_.:]+$";
255f378a 77}
4ebf0eab 78
255f378a
DM
79pub const SYSTEMD_DATETIME_FORMAT: ApiStringFormat =
80 ApiStringFormat::Pattern(&SYSTEMD_DATETIME_REGEX);
4ebf0eab 81
76cf5208
DM
82pub const IP_V4_FORMAT: ApiStringFormat =
83 ApiStringFormat::Pattern(&IP_V4_REGEX);
84
85pub const IP_V6_FORMAT: ApiStringFormat =
86 ApiStringFormat::Pattern(&IP_V6_REGEX);
87
255f378a 88pub const IP_FORMAT: ApiStringFormat =
76cf5208 89 ApiStringFormat::Pattern(&IP_REGEX);
bbf9e7e9 90
255f378a
DM
91pub const PVE_CONFIG_DIGEST_FORMAT: ApiStringFormat =
92 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
93
dcb8db66
DM
94pub const CERT_FINGERPRINT_SHA256_FORMAT: ApiStringFormat =
95 ApiStringFormat::Pattern(&CERT_FINGERPRINT_SHA256_REGEX);
96
d0adf270
DM
97pub const PROXMOX_SAFE_ID_FORMAT: ApiStringFormat =
98 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
99
454c13ed
DM
100pub const SINGLE_LINE_COMMENT_FORMAT: ApiStringFormat =
101 ApiStringFormat::Pattern(&SINGLE_LINE_COMMENT_REGEX);
102
b25f313d
DM
103pub const HOSTNAME_FORMAT: ApiStringFormat =
104 ApiStringFormat::Pattern(&HOSTNAME_REGEX);
105
106pub const DNS_NAME_FORMAT: ApiStringFormat =
107 ApiStringFormat::Pattern(&DNS_NAME_REGEX);
108
109pub const DNS_NAME_OR_IP_FORMAT: ApiStringFormat =
110 ApiStringFormat::Pattern(&DNS_NAME_OR_IP_REGEX);
111
7e7b781a
DM
112pub const PASSWORD_FORMAT: ApiStringFormat =
113 ApiStringFormat::Pattern(&PASSWORD_REGEX);
114
ed3e60ae
DM
115pub const ACL_PATH_FORMAT: ApiStringFormat =
116 ApiStringFormat::Pattern(&ACL_PATH_REGEX);
117
68da20bf
DM
118pub const NETWORK_INTERFACE_FORMAT: ApiStringFormat =
119 ApiStringFormat::Pattern(&PROXMOX_SAFE_ID_REGEX);
454c13ed 120
76cf5208
DM
121pub const CIDR_V4_FORMAT: ApiStringFormat =
122 ApiStringFormat::Pattern(&CIDR_V4_REGEX);
123
124pub const CIDR_V6_FORMAT: ApiStringFormat =
125 ApiStringFormat::Pattern(&CIDR_V6_REGEX);
126
127pub const CIDR_FORMAT: ApiStringFormat =
128 ApiStringFormat::Pattern(&CIDR_REGEX);
129
9069debc
DM
130pub const BLOCKDEVICE_NAME_FORMAT: ApiStringFormat =
131 ApiStringFormat::Pattern(&BLOCKDEVICE_NAME_REGEX);
76cf5208 132
685e1334
DM
133pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.")
134 .format(&PASSWORD_FORMAT)
135 .min_length(1)
b88f9c5b 136 .max_length(1024)
685e1334
DM
137 .schema();
138
139pub const PBS_PASSWORD_SCHEMA: Schema = StringSchema::new("User Password.")
140 .format(&PASSWORD_FORMAT)
141 .min_length(5)
142 .max_length(64)
143 .schema();
dcb8db66
DM
144
145pub const CERT_FINGERPRINT_SHA256_SCHEMA: Schema = StringSchema::new(
146 "X509 certificate fingerprint (sha256)."
147)
148 .format(&CERT_FINGERPRINT_SHA256_FORMAT)
149 .schema();
150
002a191a 151pub const PROXMOX_CONFIG_DIGEST_SCHEMA: Schema = StringSchema::new(r#"\
255f378a
DM
152Prevent changes if current configuration file has different SHA256 digest.
153This can be used to prevent concurrent modifications.
154"#
155)
156 .format(&PVE_CONFIG_DIGEST_FORMAT)
157 .schema();
158
159
160pub const CHUNK_DIGEST_FORMAT: ApiStringFormat =
161 ApiStringFormat::Pattern(&SHA256_HEX_REGEX);
162
163pub const CHUNK_DIGEST_SCHEMA: Schema = StringSchema::new("Chunk digest (SHA256).")
164 .format(&CHUNK_DIGEST_FORMAT)
165 .schema();
166
167pub const NODE_SCHEMA: Schema = StringSchema::new("Node name (or 'localhost')")
168 .format(&ApiStringFormat::VerifyFn(|node| {
169 if node == "localhost" || node == proxmox::tools::nodename() {
170 Ok(())
171 } else {
172 bail!("no such node '{}'", node);
173 }
174 }))
175 .schema();
176
177pub const SEARCH_DOMAIN_SCHEMA: Schema =
178 StringSchema::new("Search domain for host-name lookup.").schema();
179
180pub const FIRST_DNS_SERVER_SCHEMA: Schema =
181 StringSchema::new("First name server IP address.")
182 .format(&IP_FORMAT)
183 .schema();
184
185pub const SECOND_DNS_SERVER_SCHEMA: Schema =
186 StringSchema::new("Second name server IP address.")
187 .format(&IP_FORMAT)
188 .schema();
189
190pub const THIRD_DNS_SERVER_SCHEMA: Schema =
191 StringSchema::new("Third name server IP address.")
192 .format(&IP_FORMAT)
193 .schema();
194
76cf5208
DM
195pub const IP_V4_SCHEMA: Schema =
196 StringSchema::new("IPv4 address.")
197 .format(&IP_V4_FORMAT)
198 .max_length(15)
199 .schema();
200
201pub const IP_V6_SCHEMA: Schema =
202 StringSchema::new("IPv6 address.")
203 .format(&IP_V6_FORMAT)
204 .max_length(39)
205 .schema();
206
207pub const IP_SCHEMA: Schema =
208 StringSchema::new("IP (IPv4 or IPv6) address.")
209 .format(&IP_FORMAT)
210 .max_length(39)
211 .schema();
212
213pub const CIDR_V4_SCHEMA: Schema =
214 StringSchema::new("IPv4 address with netmask (CIDR notation).")
215 .format(&CIDR_V4_FORMAT)
216 .max_length(18)
217 .schema();
218
219pub const CIDR_V6_SCHEMA: Schema =
220 StringSchema::new("IPv6 address with netmask (CIDR notation).")
221 .format(&CIDR_V6_FORMAT)
222 .max_length(43)
223 .schema();
224
225pub const CIDR_SCHEMA: Schema =
226 StringSchema::new("IP address (IPv4 or IPv6) with netmask (CIDR notation).")
227 .format(&CIDR_FORMAT)
228 .max_length(43)
229 .schema();
230
4b40148c
DM
231pub const TIME_ZONE_SCHEMA: Schema = StringSchema::new(
232 "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names.")
233 .format(&SINGLE_LINE_COMMENT_FORMAT)
234 .min_length(2)
235 .max_length(64)
236 .schema();
237
ca257c80
DM
238pub const ACL_PATH_SCHEMA: Schema = StringSchema::new(
239 "Access control path.")
240 .format(&ACL_PATH_FORMAT)
241 .min_length(1)
242 .max_length(128)
243 .schema();
244
245pub const ACL_PROPAGATE_SCHEMA: Schema = BooleanSchema::new(
246 "Allow to propagate (inherit) permissions.")
247 .default(true)
248 .schema();
249
250pub const ACL_UGID_TYPE_SCHEMA: Schema = StringSchema::new(
251 "Type of 'ugid' property.")
ca257c80 252 .format(&ApiStringFormat::Enum(&[
bc0d0388
DM
253 EnumEntry::new("user", "User"),
254 EnumEntry::new("group", "Group")]))
ca257c80
DM
255 .schema();
256
255f378a
DM
257pub const BACKUP_ARCHIVE_NAME_SCHEMA: Schema =
258 StringSchema::new("Backup archive name.")
1ae5677d 259 .format(&PROXMOX_SAFE_ID_FORMAT)
255f378a
DM
260 .schema();
261
262pub const BACKUP_TYPE_SCHEMA: Schema =
263 StringSchema::new("Backup type.")
bc0d0388
DM
264 .format(&ApiStringFormat::Enum(&[
265 EnumEntry::new("vm", "Virtual Machine Backup"),
266 EnumEntry::new("ct", "Container Backup"),
267 EnumEntry::new("host", "Host Backup")]))
255f378a
DM
268 .schema();
269
270pub const BACKUP_ID_SCHEMA: Schema =
271 StringSchema::new("Backup ID.")
1ae5677d 272 .format(&PROXMOX_SAFE_ID_FORMAT)
255f378a
DM
273 .schema();
274
275pub const BACKUP_TIME_SCHEMA: Schema =
276 IntegerSchema::new("Backup time (Unix epoch.)")
277 .minimum(1_547_797_308)
278 .schema();
5830c205
DM
279
280pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task ID.")
281 .max_length(256)
282 .schema();
66c49c21
DM
283
284pub const DATASTORE_SCHEMA: Schema = StringSchema::new("Datastore name.")
d0adf270 285 .format(&PROXMOX_SAFE_ID_FORMAT)
688fbe07 286 .min_length(3)
66c49c21
DM
287 .max_length(32)
288 .schema();
fc189b19 289
2888b27f
DC
290pub const SYNC_SCHEDULE_SCHEMA: Schema = StringSchema::new(
291 "Run sync job at specified schedule.")
292 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
293 .schema();
294
42fdbe51
DM
295pub const GC_SCHEDULE_SCHEMA: Schema = StringSchema::new(
296 "Run garbage collection job at specified schedule.")
297 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
298 .schema();
299
67f7ffd0
DM
300pub const PRUNE_SCHEDULE_SCHEMA: Schema = StringSchema::new(
301 "Run prune job at specified schedule.")
302 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
303 .schema();
304
f37ef25b
HL
305pub const VERIFY_SCHEDULE_SCHEMA: Schema = StringSchema::new(
306 "Run verify job at specified schedule.")
307 .format(&ApiStringFormat::VerifyFn(crate::tools::systemd::time::verify_calendar_event))
308 .schema();
309
167971ed
DM
310pub const REMOTE_ID_SCHEMA: Schema = StringSchema::new("Remote ID.")
311 .format(&PROXMOX_SAFE_ID_FORMAT)
312 .min_length(3)
313 .max_length(32)
314 .schema();
315
b4900286
DM
316pub const JOB_ID_SCHEMA: Schema = StringSchema::new("Job ID.")
317 .format(&PROXMOX_SAFE_ID_FORMAT)
318 .min_length(3)
319 .max_length(32)
320 .schema();
321
322pub const REMOVE_VANISHED_BACKUPS_SCHEMA: Schema = BooleanSchema::new(
323 "Delete vanished backups. This remove the local copy if the remote backup was deleted.")
324 .default(true)
325 .schema();
326
454c13ed
DM
327pub const SINGLE_LINE_COMMENT_SCHEMA: Schema = StringSchema::new("Comment (single line).")
328 .format(&SINGLE_LINE_COMMENT_FORMAT)
329 .schema();
fc189b19 330
b25f313d
DM
331pub const HOSTNAME_SCHEMA: Schema = StringSchema::new("Hostname (as defined in RFC1123).")
332 .format(&HOSTNAME_FORMAT)
333 .schema();
334
335pub const DNS_NAME_OR_IP_SCHEMA: Schema = StringSchema::new("DNS name or IP address.")
336 .format(&DNS_NAME_OR_IP_FORMAT)
337 .schema();
338
9069debc
DM
339pub const BLOCKDEVICE_NAME_SCHEMA: Schema = StringSchema::new("Block device name (/sys/block/<name>).")
340 .format(&BLOCKDEVICE_NAME_FORMAT)
341 .min_length(3)
342 .max_length(64)
343 .schema();
fc189b19
DM
344
345// Complex type definitions
346
b31c8019
DM
347#[api(
348 properties: {
349 "backup-type": {
350 schema: BACKUP_TYPE_SCHEMA,
351 },
352 "backup-id": {
353 schema: BACKUP_ID_SCHEMA,
354 },
355 "last-backup": {
356 schema: BACKUP_TIME_SCHEMA,
357 },
358 "backup-count": {
359 type: Integer,
360 },
361 files: {
362 items: {
363 schema: BACKUP_ARCHIVE_NAME_SCHEMA
364 },
365 },
e7cb4dc5
WB
366 owner: {
367 type: Userid,
368 optional: true,
369 },
b31c8019
DM
370 },
371)]
372#[derive(Serialize, Deserialize)]
373#[serde(rename_all="kebab-case")]
374/// Basic information about a backup group.
375pub struct GroupListItem {
376 pub backup_type: String, // enum
377 pub backup_id: String,
378 pub last_backup: i64,
379 /// Number of contained snapshots
380 pub backup_count: u64,
381 /// List of contained archive files.
382 pub files: Vec<String>,
04b0ca8b
DC
383 /// The owner of group
384 #[serde(skip_serializing_if="Option::is_none")]
e7cb4dc5 385 pub owner: Option<Userid>,
b31c8019
DM
386}
387
d10332a1
SR
388#[api()]
389#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
390#[serde(rename_all = "lowercase")]
391/// Result of a verify operation.
392pub enum VerifyState {
393 /// Verification was successful
394 Ok,
395 /// Verification reported one or more errors
396 Failed,
397}
398
3b2046d2
TL
399#[api(
400 properties: {
401 upid: {
402 schema: UPID_SCHEMA
403 },
404 state: {
d10332a1 405 type: VerifyState
3b2046d2
TL
406 },
407 },
408)]
409#[derive(Serialize, Deserialize)]
410/// Task properties.
411pub struct SnapshotVerifyState {
412 /// UPID of the verify task
413 pub upid: UPID,
d10332a1
SR
414 /// State of the verification. Enum.
415 pub state: VerifyState,
3b2046d2
TL
416}
417
fc189b19 418#[api(
fc189b19
DM
419 properties: {
420 "backup-type": {
421 schema: BACKUP_TYPE_SCHEMA,
422 },
423 "backup-id": {
424 schema: BACKUP_ID_SCHEMA,
425 },
426 "backup-time": {
427 schema: BACKUP_TIME_SCHEMA,
428 },
5255e641
TL
429 comment: {
430 schema: SINGLE_LINE_COMMENT_SCHEMA,
431 optional: true,
432 },
3b2046d2
TL
433 verification: {
434 type: SnapshotVerifyState,
435 optional: true,
436 },
71da3d6a
DM
437 files: {
438 items: {
439 schema: BACKUP_ARCHIVE_NAME_SCHEMA
440 },
441 },
e7cb4dc5
WB
442 owner: {
443 type: Userid,
444 optional: true,
445 },
fc189b19
DM
446 },
447)]
448#[derive(Serialize, Deserialize)]
449#[serde(rename_all="kebab-case")]
71da3d6a 450/// Basic information about backup snapshot.
fc189b19
DM
451pub struct SnapshotListItem {
452 pub backup_type: String, // enum
453 pub backup_id: String,
454 pub backup_time: i64,
70030b43
DM
455 /// The first line from manifest "notes"
456 #[serde(skip_serializing_if="Option::is_none")]
457 pub comment: Option<String>,
3b2046d2
TL
458 /// The result of the last run verify task
459 #[serde(skip_serializing_if="Option::is_none")]
460 pub verification: Option<SnapshotVerifyState>,
71da3d6a 461 /// List of contained archive files.
1c090810 462 pub files: Vec<BackupContent>,
71da3d6a 463 /// Overall snapshot size (sum of all archive sizes).
fc189b19
DM
464 #[serde(skip_serializing_if="Option::is_none")]
465 pub size: Option<u64>,
04b0ca8b
DC
466 /// The owner of the snapshots group
467 #[serde(skip_serializing_if="Option::is_none")]
e7cb4dc5 468 pub owner: Option<Userid>,
fc189b19 469}
ff620a3d 470
db1e061d
DM
471#[api(
472 properties: {
473 "backup-type": {
474 schema: BACKUP_TYPE_SCHEMA,
475 },
476 "backup-id": {
477 schema: BACKUP_ID_SCHEMA,
478 },
479 "backup-time": {
480 schema: BACKUP_TIME_SCHEMA,
481 },
482 },
483)]
484#[derive(Serialize, Deserialize)]
485#[serde(rename_all="kebab-case")]
486/// Prune result.
487pub struct PruneListItem {
488 pub backup_type: String, // enum
489 pub backup_id: String,
490 pub backup_time: i64,
491 /// Keep snapshot
492 pub keep: bool,
493}
494
49ff1092
DM
495pub const PRUNE_SCHEMA_KEEP_DAILY: Schema = IntegerSchema::new(
496 "Number of daily backups to keep.")
497 .minimum(1)
498 .schema();
499
500pub const PRUNE_SCHEMA_KEEP_HOURLY: Schema = IntegerSchema::new(
501 "Number of hourly backups to keep.")
502 .minimum(1)
503 .schema();
504
505pub const PRUNE_SCHEMA_KEEP_LAST: Schema = IntegerSchema::new(
506 "Number of backups to keep.")
507 .minimum(1)
508 .schema();
509
510pub const PRUNE_SCHEMA_KEEP_MONTHLY: Schema = IntegerSchema::new(
511 "Number of monthly backups to keep.")
512 .minimum(1)
513 .schema();
514
515pub const PRUNE_SCHEMA_KEEP_WEEKLY: Schema = IntegerSchema::new(
516 "Number of weekly backups to keep.")
517 .minimum(1)
518 .schema();
519
520pub const PRUNE_SCHEMA_KEEP_YEARLY: Schema = IntegerSchema::new(
521 "Number of yearly backups to keep.")
522 .minimum(1)
523 .schema();
524
09b1f7b2
DM
525#[api(
526 properties: {
527 "filename": {
528 schema: BACKUP_ARCHIVE_NAME_SCHEMA,
529 },
f28d9088
WB
530 "crypt-mode": {
531 type: CryptMode,
532 optional: true,
533 },
09b1f7b2
DM
534 },
535)]
536#[derive(Serialize, Deserialize)]
537#[serde(rename_all="kebab-case")]
538/// Basic information about archive files inside a backup snapshot.
539pub struct BackupContent {
540 pub filename: String,
f28d9088 541 /// Info if file is encrypted, signed, or neither.
e181d2f6 542 #[serde(skip_serializing_if="Option::is_none")]
f28d9088 543 pub crypt_mode: Option<CryptMode>,
09b1f7b2
DM
544 /// Archive size (from backup manifest).
545 #[serde(skip_serializing_if="Option::is_none")]
546 pub size: Option<u64>,
547}
548
a92830dc
DM
549#[api(
550 properties: {
551 "upid": {
552 optional: true,
553 schema: UPID_SCHEMA,
554 },
555 },
556)]
557#[derive(Clone, Serialize, Deserialize)]
558#[serde(rename_all="kebab-case")]
559/// Garbage collection status.
560pub struct GarbageCollectionStatus {
561 pub upid: Option<String>,
562 /// Number of processed index files.
563 pub index_file_count: usize,
564 /// Sum of bytes referred by index files.
565 pub index_data_bytes: u64,
566 /// Bytes used on disk.
567 pub disk_bytes: u64,
568 /// Chunks used on disk.
569 pub disk_chunks: usize,
570 /// Sum of removed bytes.
571 pub removed_bytes: u64,
572 /// Number of removed chunks.
573 pub removed_chunks: usize,
cf459b19
DM
574 /// Sum of pending bytes (pending removal - kept for safety).
575 pub pending_bytes: u64,
576 /// Number of pending chunks (pending removal - kept for safety).
577 pub pending_chunks: usize,
a9767cf7
SR
578 /// Number of chunks marked as .bad by verify that have been removed by GC.
579 pub removed_bad: usize,
a92830dc
DM
580}
581
582impl Default for GarbageCollectionStatus {
583 fn default() -> Self {
584 GarbageCollectionStatus {
585 upid: None,
586 index_file_count: 0,
587 index_data_bytes: 0,
588 disk_bytes: 0,
589 disk_chunks: 0,
590 removed_bytes: 0,
591 removed_chunks: 0,
cf459b19
DM
592 pending_bytes: 0,
593 pending_chunks: 0,
a9767cf7 594 removed_bad: 0,
a92830dc
DM
595 }
596 }
597}
598
599
1dc117bb
DM
600#[api()]
601#[derive(Serialize, Deserialize)]
602/// Storage space usage information.
603pub struct StorageStatus {
604 /// Total space (bytes).
605 pub total: u64,
606 /// Used space (bytes).
607 pub used: u64,
608 /// Available space (bytes).
609 pub avail: u64,
610}
ff620a3d 611
99384f79
DM
612#[api(
613 properties: {
e7cb4dc5
WB
614 upid: { schema: UPID_SCHEMA },
615 user: { type: Userid },
99384f79
DM
616 },
617)]
618#[derive(Serialize, Deserialize)]
619/// Task properties.
620pub struct TaskListItem {
621 pub upid: String,
622 /// The node name where the task is running on.
623 pub node: String,
624 /// The Unix PID
625 pub pid: i64,
626 /// The task start time (Epoch)
627 pub pstart: u64,
628 /// The task start time (Epoch)
629 pub starttime: i64,
630 /// Worker type (arbitrary ASCII string)
631 pub worker_type: String,
632 /// Worker ID (arbitrary ASCII string)
633 pub worker_id: Option<String>,
634 /// The user who started the task
e7cb4dc5 635 pub user: Userid,
99384f79
DM
636 /// The task end time (Epoch)
637 #[serde(skip_serializing_if="Option::is_none")]
638 pub endtime: Option<i64>,
639 /// Task end status
640 #[serde(skip_serializing_if="Option::is_none")]
641 pub status: Option<String>,
642}
643
df528ee6
DC
644impl From<crate::server::TaskListInfo> for TaskListItem {
645 fn from(info: crate::server::TaskListInfo) -> Self {
646 let (endtime, status) = info
647 .state
77bd2a46 648 .map_or_else(|| (None, None), |a| (Some(a.endtime()), Some(a.to_string())));
df528ee6
DC
649
650 TaskListItem {
651 upid: info.upid_str,
652 node: "localhost".to_string(),
653 pid: info.upid.pid as i64,
654 pstart: info.upid.pstart,
655 starttime: info.upid.starttime,
656 worker_type: info.upid.worker_type,
657 worker_id: info.upid.worker_id,
e7cb4dc5 658 user: info.upid.userid,
df528ee6
DC
659 endtime,
660 status,
661 }
662 }
663}
664
ed751dc2
DM
665#[api()]
666#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
667#[serde(rename_all = "lowercase")]
668/// Node Power command type.
669pub enum NodePowerCommand {
670 /// Restart the server
671 Reboot,
672 /// Shutdown the server
673 Shutdown,
674}
675
c357260d
DM
676#[api()]
677#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
678#[serde(rename_all = "lowercase")]
679/// Interface configuration method
680pub enum NetworkConfigMethod {
681 /// Configuration is done manually using other tools
682 Manual,
683 /// Define interfaces with statically allocated addresses.
684 Static,
685 /// Obtain an address via DHCP
686 DHCP,
687 /// Define the loopback interface.
688 Loopback,
689}
690
bab5d18c
DM
691#[api()]
692#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
693#[serde(rename_all = "kebab-case")]
694#[allow(non_camel_case_types)]
695#[repr(u8)]
696/// Linux Bond Mode
697pub enum LinuxBondMode {
698 /// Round-robin policy
699 balance_rr = 0,
700 /// Active-backup policy
701 active_backup = 1,
702 /// XOR policy
703 balance_xor = 2,
704 /// Broadcast policy
705 broadcast = 3,
706 /// IEEE 802.3ad Dynamic link aggregation
8f2f3dd7 707 #[serde(rename = "802.3ad")]
bab5d18c
DM
708 ieee802_3ad = 4,
709 /// Adaptive transmit load balancing
710 balance_tlb = 5,
711 /// Adaptive load balancing
712 balance_alb = 6,
713}
714
8f2f3dd7
DC
715#[api()]
716#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
717#[serde(rename_all = "kebab-case")]
718#[allow(non_camel_case_types)]
719#[repr(u8)]
720/// Bond Transmit Hash Policy for LACP (802.3ad)
721pub enum BondXmitHashPolicy {
722 /// Layer 2
723 layer2 = 0,
724 /// Layer 2+3
725 #[serde(rename = "layer2+3")]
726 layer2_3 = 1,
727 /// Layer 3+4
728 #[serde(rename = "layer3+4")]
729 layer3_4 = 2,
730}
731
02269f3d
DM
732#[api()]
733#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
734#[serde(rename_all = "lowercase")]
735/// Network interface type
736pub enum NetworkInterfaceType {
737 /// Loopback
738 Loopback,
739 /// Physical Ethernet device
7b22acd0 740 Eth,
02269f3d
DM
741 /// Linux Bridge
742 Bridge,
743 /// Linux Bond
744 Bond,
745 /// Linux VLAN (eth.10)
746 Vlan,
747 /// Interface Alias (eth:1)
748 Alias,
749 /// Unknown interface type
750 Unknown,
751}
752
68da20bf
DM
753pub const NETWORK_INTERFACE_NAME_SCHEMA: Schema = StringSchema::new("Network interface name.")
754 .format(&NETWORK_INTERFACE_FORMAT)
755 .min_length(1)
756 .max_length(libc::IFNAMSIZ-1)
757 .schema();
758
3aedb738 759pub const NETWORK_INTERFACE_ARRAY_SCHEMA: Schema = ArraySchema::new(
1d9a68c2
DM
760 "Network interface list.", &NETWORK_INTERFACE_NAME_SCHEMA)
761 .schema();
762
3aedb738
DM
763pub const NETWORK_INTERFACE_LIST_SCHEMA: Schema = StringSchema::new(
764 "A list of network devices, comma separated.")
765 .format(&ApiStringFormat::PropertyString(&NETWORK_INTERFACE_ARRAY_SCHEMA))
766 .schema();
767
c357260d
DM
768#[api(
769 properties: {
770 name: {
68da20bf 771 schema: NETWORK_INTERFACE_NAME_SCHEMA,
c357260d 772 },
7b22acd0 773 "type": {
02269f3d
DM
774 type: NetworkInterfaceType,
775 },
7b22acd0 776 method: {
c357260d
DM
777 type: NetworkConfigMethod,
778 optional: true,
779 },
7b22acd0 780 method6: {
c357260d
DM
781 type: NetworkConfigMethod,
782 optional: true,
783 },
7b22acd0
DM
784 cidr: {
785 schema: CIDR_V4_SCHEMA,
786 optional: true,
787 },
788 cidr6: {
789 schema: CIDR_V6_SCHEMA,
790 optional: true,
791 },
792 gateway: {
793 schema: IP_V4_SCHEMA,
794 optional: true,
795 },
796 gateway6: {
797 schema: IP_V6_SCHEMA,
798 optional: true,
799 },
800 options: {
c357260d
DM
801 description: "Option list (inet)",
802 type: Array,
803 items: {
68da20bf 804 description: "Optional attribute line.",
c357260d
DM
805 type: String,
806 },
807 },
7b22acd0 808 options6: {
c357260d
DM
809 description: "Option list (inet6)",
810 type: Array,
811 items: {
68da20bf 812 description: "Optional attribute line.",
c357260d
DM
813 type: String,
814 },
815 },
7b22acd0 816 comments: {
8a6b86b8
DM
817 description: "Comments (inet, may span multiple lines)",
818 type: String,
819 optional: true,
5f60a58f 820 },
7b22acd0 821 comments6: {
8a6b86b8
DM
822 description: "Comments (inet6, may span multiple lines)",
823 type: String,
824 optional: true,
5f60a58f 825 },
1d9a68c2 826 bridge_ports: {
3aedb738 827 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
1d9a68c2
DM
828 optional: true,
829 },
bab5d18c 830 slaves: {
3aedb738 831 schema: NETWORK_INTERFACE_ARRAY_SCHEMA,
42fbe91a
DM
832 optional: true,
833 },
bab5d18c
DM
834 bond_mode: {
835 type: LinuxBondMode,
836 optional: true,
85959a99
DC
837 },
838 "bond-primary": {
839 schema: NETWORK_INTERFACE_NAME_SCHEMA,
840 optional: true,
841 },
8f2f3dd7
DC
842 bond_xmit_hash_policy: {
843 type: BondXmitHashPolicy,
844 optional: true,
845 },
c357260d
DM
846 }
847)]
848#[derive(Debug, Serialize, Deserialize)]
849/// Network Interface configuration
850pub struct Interface {
851 /// Autostart interface
7b22acd0
DM
852 #[serde(rename = "autostart")]
853 pub autostart: bool,
c357260d
DM
854 /// Interface is active (UP)
855 pub active: bool,
856 /// Interface name
857 pub name: String,
02269f3d 858 /// Interface type
7b22acd0 859 #[serde(rename = "type")]
02269f3d 860 pub interface_type: NetworkInterfaceType,
c357260d 861 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 862 pub method: Option<NetworkConfigMethod>,
c357260d 863 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 864 pub method6: Option<NetworkConfigMethod>,
c357260d 865 #[serde(skip_serializing_if="Option::is_none")]
8b57cd44 866 /// IPv4 address with netmask
7b22acd0 867 pub cidr: Option<String>,
c357260d
DM
868 #[serde(skip_serializing_if="Option::is_none")]
869 /// IPv4 gateway
7b22acd0 870 pub gateway: Option<String>,
c357260d 871 #[serde(skip_serializing_if="Option::is_none")]
8b57cd44 872 /// IPv6 address with netmask
7b22acd0 873 pub cidr6: Option<String>,
c357260d
DM
874 #[serde(skip_serializing_if="Option::is_none")]
875 /// IPv6 gateway
7b22acd0 876 pub gateway6: Option<String>,
3fce3bc3 877
c357260d 878 #[serde(skip_serializing_if="Vec::is_empty")]
7b22acd0 879 pub options: Vec<String>,
c357260d 880 #[serde(skip_serializing_if="Vec::is_empty")]
7b22acd0 881 pub options6: Vec<String>,
2c18efd9 882
8a6b86b8 883 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 884 pub comments: Option<String>,
8a6b86b8 885 #[serde(skip_serializing_if="Option::is_none")]
7b22acd0 886 pub comments6: Option<String>,
5f60a58f 887
2c18efd9
DM
888 #[serde(skip_serializing_if="Option::is_none")]
889 /// Maximum Transmission Unit
890 pub mtu: Option<u64>,
1d9a68c2
DM
891
892 #[serde(skip_serializing_if="Option::is_none")]
893 pub bridge_ports: Option<Vec<String>>,
7b22acd0
DM
894 /// Enable bridge vlan support.
895 #[serde(skip_serializing_if="Option::is_none")]
896 pub bridge_vlan_aware: Option<bool>,
42fbe91a
DM
897
898 #[serde(skip_serializing_if="Option::is_none")]
bab5d18c
DM
899 pub slaves: Option<Vec<String>>,
900 #[serde(skip_serializing_if="Option::is_none")]
901 pub bond_mode: Option<LinuxBondMode>,
85959a99
DC
902 #[serde(skip_serializing_if="Option::is_none")]
903 #[serde(rename = "bond-primary")]
904 pub bond_primary: Option<String>,
8f2f3dd7 905 pub bond_xmit_hash_policy: Option<BondXmitHashPolicy>,
c357260d
DM
906}
907
ff620a3d
DM
908// Regression tests
909
dcb8db66 910#[test]
ff329f97 911fn test_cert_fingerprint_schema() -> Result<(), anyhow::Error> {
dcb8db66
DM
912
913 let schema = CERT_FINGERPRINT_SHA256_SCHEMA;
914
915 let invalid_fingerprints = [
916 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
917 "88:7C:BE:26:77:a5:62:67:D9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
918 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8:ff",
919 "XX:88:7c:be:26:77:a5:62:67:d9:06:f5:e4::14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
920 "86:88:Y4:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
921 "86:88:0:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
922 ];
923
924 for fingerprint in invalid_fingerprints.iter() {
925 if let Ok(_) = parse_simple_value(fingerprint, &schema) {
add5861e 926 bail!("test fingerprint '{}' failed - got Ok() while exception an error.", fingerprint);
dcb8db66
DM
927 }
928 }
929
930 let valid_fingerprints = [
931 "86:88:7c:be:26:77:a5:62:67:d9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
932 "86:88:7C:BE:26:77:a5:62:67:D9:06:f5:e4:14:61:3e:20:dc:cd:43:92:07:7f:fb:65:54:6c:ff:d2:96:36:f8",
933 ];
934
935 for fingerprint in valid_fingerprints.iter() {
936 let v = match parse_simple_value(fingerprint, &schema) {
937 Ok(v) => v,
938 Err(err) => {
939 bail!("unable to parse fingerprint '{}' - {}", fingerprint, err);
940 }
941 };
942
943 if v != serde_json::json!(fingerprint) {
944 bail!("unable to parse fingerprint '{}' - got wrong value {:?}", fingerprint, v);
945 }
946 }
947
948 Ok(())
949}
950
ff620a3d 951#[test]
ff329f97 952fn test_proxmox_user_id_schema() -> Result<(), anyhow::Error> {
ff620a3d
DM
953 let invalid_user_ids = [
954 "x", // too short
955 "xx", // too short
956 "xxx", // no realm
957 "xxx@", // no realm
958 "xx x@test", // contains space
959 "xx\nx@test", // contains control character
960 "x:xx@test", // contains collon
961 "xx/x@test", // contains slash
962 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@test", // too long
963 ];
964
965 for name in invalid_user_ids.iter() {
e7cb4dc5 966 if let Ok(_) = parse_simple_value(name, &Userid::API_SCHEMA) {
add5861e 967 bail!("test userid '{}' failed - got Ok() while exception an error.", name);
ff620a3d
DM
968 }
969 }
970
971 let valid_user_ids = [
972 "xxx@y",
973 "name@y",
974 "xxx@test-it.com",
975 "xxx@_T_E_S_T-it.com",
976 "x_x-x.x@test-it.com",
977 ];
978
979 for name in valid_user_ids.iter() {
e7cb4dc5 980 let v = match parse_simple_value(name, &Userid::API_SCHEMA) {
ff620a3d
DM
981 Ok(v) => v,
982 Err(err) => {
983 bail!("unable to parse userid '{}' - {}", name, err);
984 }
985 };
986
987 if v != serde_json::json!(name) {
988 bail!("unable to parse userid '{}' - got wrong value {:?}", name, v);
989 }
990 }
991
992 Ok(())
993}
a2f862ee
DM
994
995#[api()]
996#[derive(Copy, Clone, Serialize, Deserialize)]
997#[serde(rename_all = "UPPERCASE")]
998pub enum RRDMode {
999 /// Maximum
1000 Max,
1001 /// Average
1002 Average,
1003}
1004
1005
1006#[api()]
1007#[repr(u64)]
1008#[derive(Copy, Clone, Serialize, Deserialize)]
1009#[serde(rename_all = "lowercase")]
1010pub enum RRDTimeFrameResolution {
1011 /// 1 min => last 70 minutes
1012 Hour = 60,
1013 /// 30 min => last 35 hours
1014 Day = 60*30,
1015 /// 3 hours => about 8 days
1016 Week = 60*180,
1017 /// 12 hours => last 35 days
1018 Month = 60*720,
1019 /// 1 week => last 490 days
1020 Year = 60*10080,
1021}
a4e86972
SR
1022
1023#[api()]
1024#[derive(Serialize, Deserialize)]
1025#[serde(rename_all = "PascalCase")]
1026/// Describes a package for which an update is available.
1027pub struct APTUpdateInfo {
1028 /// Package name
1029 pub package: String,
1030 /// Package title
1031 pub title: String,
1032 /// Package architecture
1033 pub arch: String,
1034 /// Human readable package description
1035 pub description: String,
1036 /// New version to be updated to
1037 pub version: String,
1038 /// Old version currently installed
1039 pub old_version: String,
1040 /// Package origin
1041 pub origin: String,
1042 /// Package priority in human-readable form
1043 pub priority: String,
1044 /// Package section
1045 pub section: String,
1046 /// URL under which the package's changelog can be retrieved
1047 pub change_log_url: String,
1048}