Skip to content

Commit 280eaf6

Browse files
committed
Move prune_backups validation into method, add comments to clarify option format, fix documentation format issues
1 parent 4591a38 commit 280eaf6

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

library/proxmox_storage.py

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
elements: str
4747
choices: [ "images", "rootdir", "vztmpl", "backup", "iso", "snippets" ]
4848
description:
49-
- Contents supported by the storage, not all storage
50-
types support all content types.
49+
- Contents supported by the storage, not all storage types support all content types.
5150
nodes:
5251
required: false
5352
type: list
@@ -114,13 +113,13 @@
114113
- keep-yearly
115114
description:
116115
- The retention option to use.
117-
- C(keep-all): Keep all backups. This option is mutually exclusive with the other options.
118-
- C(keep-last): Keep the last n backups.
119-
- C(keep-hourly): Keep backups for the last n hours. If there is more than one backup for a single hour, only the latest is kept.
120-
- C(keep-daily): Keep backups for the last n days. If there is more than one backup for a single day, only the latest is kept.
121-
- C(keep-weekly): Keep backups for the last n weeks. If there is more than one backup for a single week, only the latest is kept. Weeks start on Monday and end on Sunday. The software uses the ISO week date-system and handles weeks at the end of the year correctly.
122-
- C(keep-monthly): Keep backups for the last n months. If there is more than one backup for a single month, only the latest is kept.
123-
- C(keep-yearly): Keep backups for the last n years. If there is more than one backup for a single year, only the latest is kept.
116+
- "C(keep-all): Keep all backups. This option is mutually exclusive with the other options."
117+
- "C(keep-last): Keep the last n backups."
118+
- "C(keep-hourly): Keep backups for the last n hours. If there is more than one backup for a single hour, only the latest is kept."
119+
- "C(keep-daily): Keep backups for the last n days. If there is more than one backup for a single day, only the latest is kept."
120+
- "C(keep-weekly): Keep backups for the last n weeks. If there is more than one backup for a single week, only the latest is kept. Weeks start on Monday and end on Sunday. The software uses the ISO week date-system and handles weeks at the end of the year correctly."
121+
- "C(keep-monthly): Keep backups for the last n months. If there is more than one backup for a single month, only the latest is kept."
122+
- "C(keep-yearly): Keep backups for the last n years. If there is more than one backup for a single year, only the latest is kept."
124123
value:
125124
required: true
126125
description:
@@ -426,47 +425,57 @@ def prepare_storage_args(self):
426425
# end cifs
427426
if self.maxfiles is not None:
428427
self.module.warn("'maxfiles' parameter is deprecated, use 'prune_backups' parameter instead")
429-
if "backup" not in self.content:
428+
if 'backup' not in self.content:
430429
self.module.fail_json(
431430
msg="'maxfiles' parameter is not allowed when there is no 'backup' in 'content' parameter"
432431
)
433432
if self.prune_backups is not None:
434-
if "backup" not in self.content:
435-
self.module.fail_json(
436-
msg="'prune_backups' parameter is not allowed when there is no 'backup' in 'content' parameter"
437-
)
438-
439-
if len(self.prune_backups) != len(set(cfg["option"] for cfg in self.prune_backups)):
440-
self.module.fail_json(msg="'prune_backups' parameter has duplicate entries")
441-
442-
keep_all_entries = [cfg for cfg in self.prune_backups if cfg["option"] == "keep-all"]
443-
keep_all_entry = keep_all_entries[0] if len(keep_all_entries) > 0 else None
444-
other_entries = [cfg for cfg in self.prune_backups if cfg["option"] != "keep-all"]
445-
if keep_all_entry and len(other_entries) > 0:
446-
self.module.fail_json(
447-
msg="'keep-all' is mutually exclusive with other options in 'prune_backups' parameter"
448-
)
449-
450-
if keep_all_entry and type(keep_all_entry["value"]) is not bool:
451-
self.module.fail_json(msg="value of 'keep-all' option must be a boolean in 'prune_backups' parameter")
452-
if any(type(cfg["value"]) is not int for cfg in other_entries):
453-
self.module.fail_json(
454-
msg="all values except for the 'keep-all' option must be integers in 'prune_backups' parameter"
455-
)
456-
457-
args["prune-backups"] = (
458-
"keep-all={value}".format(value=(1 if keep_all_entry["value"] else 0))
433+
# order is important for prune_backups, hence we accept a list of options instead of a dict
434+
keep_all_entry, other_entries = self.validate_storage_prune_backups_option()
435+
436+
# the format for the prune-backups argument is (see https://pve.proxmox.com/pve-docs/api-viewer/index.html#/storage/{storage}):
437+
# [keep-all=<1|0>][,keep-daily=<N>][,keep-hourly=<N>][,keep-last=<N>][,keep-monthly=<N>][,keep-weekly=<N>][,keep-yearly=<N>]
438+
args['prune-backups'] = (
439+
# keep-all is mutually exclusive with the other options, we checked that earlier
440+
# example: "keep-all=1"
441+
'keep-all={}'.format(1 if keep_all_entry['value'] else 0)
459442
if keep_all_entry
443+
# example: "keep-last=3,keep-hourly=6"
460444
else ",".join(
461-
map(lambda cfg: "{}={}".format(cfg["option"], cfg["value"]), other_entries)
445+
map(lambda cfg: '{}={}'.format(cfg['option'], cfg['value']), other_entries)
462446
)
463447
)
464-
465448
if self.krbd is not None and self.type != 'rbd':
466449
self.module.fail_json(msg="krbd is only allowed with 'rbd' storage type")
467450

468451
return args
469452

453+
def validate_storage_prune_backups_option(self):
454+
if 'backup' not in self.content:
455+
self.module.fail_json(
456+
msg="'prune_backups' parameter is not allowed when there is no 'backup' in 'content' parameter"
457+
)
458+
459+
if len(self.prune_backups) != len(set(cfg['option'] for cfg in self.prune_backups)):
460+
self.module.fail_json(msg="'prune_backups' parameter has duplicate entries")
461+
462+
keep_all_entries = [cfg for cfg in self.prune_backups if cfg['option'] == 'keep-all']
463+
keep_all_entry = keep_all_entries[0] if len(keep_all_entries) > 0 else None
464+
other_entries = [cfg for cfg in self.prune_backups if cfg['option'] != 'keep-all']
465+
if keep_all_entry and len(other_entries) > 0:
466+
self.module.fail_json(
467+
msg="'keep-all' is mutually exclusive with other options in 'prune_backups' parameter"
468+
)
469+
470+
if keep_all_entry and type(keep_all_entry['value']) is not bool:
471+
self.module.fail_json(msg="value of 'keep-all' option must be a boolean in 'prune_backups' parameter")
472+
if any(type(cfg['value']) is not int for cfg in other_entries):
473+
self.module.fail_json(
474+
msg="all values except for the 'keep-all' option must be integers in 'prune_backups' parameter"
475+
)
476+
477+
return keep_all_entry, other_entries
478+
470479
def create_storage(self):
471480
new_storage = self.prepare_storage_args()
472481
try:

0 commit comments

Comments
 (0)