Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module proxmox_backup_info #9437

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

raoufnezhad
Copy link

@raoufnezhad raoufnezhad commented Dec 28, 2024

SUMMARY

This contribution proposes to add a module called proxmox_backup_info to the Ansible community.general collection. The module would retrieve information about backups in a Proxmox VE cluster, allowing users to query and gather details about existing backups.

ISSUE TYPE
  • New Module/Plugin Pull Request
COMPONENT NAME

proxmox_backup_info

ADDITIONAL INFORMATION

The module would use the Proxmox API to fetch information about backups in the cluster. It would allow users to specify parameters such as the API host, credentials, and optionally filter by VM ID, VM Name, or backup section. The result of the module would be a list of backups with details like backup type, backup id, mode, schedule, next-run time, storage, and associated VM.

The `proxmox_backup_info` module displays information such as backup times, VM name, VM ID, mode, backup type, and backup schedule using the Proxmox Server API.
create test for proxmox_backup_info.py module
@ansibullbot

This comment was marked as outdated.

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added module module needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor new_plugin New plugin plugins plugin (any type) tests tests unit tests/unit labels Dec 28, 2024
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-10 Automatically create a backport for the stable-10 branch labels Dec 28, 2024
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @raoufnezhad thanks for your contribution!

I see you have adjustments to make for the sanity tests. If I may suggest, give it a try to andebox (by yours truly):

https://pypi.org/project/andebox/

You can run this sanity test easily in your local machine by simply running:

$ andebox test -- sanity --docker default --python 3.10 plugins/modules/your_module.py

I left a couple of other comments below as well. Thanks again!

plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
Comment on lines 24 to 27
options:
api_user:
description: The user must have access to the proxmox server
required: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use consistent indentation. Here you started with 4 spaces then used 2 in the next level. We have been trying to use 2 at all times, that's not mandatory, but consistency is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your attention.

Comment on lines 82 to 83
description: The return value provided by proxmox_backup_info.
type: dict
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That description does not mean much. Please be either more specific or add notes to the documentation block referring to Proxmox API, and/or the return values provided by proxmoxer.

If the result value is not too big nor complex, please consider declaring contains block with a more finely grained level of details.

@ansibullbot

This comment was marked as outdated.

@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Dec 29, 2024
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
plugins/modules/proxmox_backup_info.py Show resolved Hide resolved
@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Dec 30, 2024
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Dec 30, 2024
required: false
type: int
backup_section:
description: The backup_section value is true or false. if this be true, this module just return all backup job information.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this option means that if it is set to true, then all backups for this 'section' (whatever that is, I don't know the proxmox terminology) will be listed. If set to false, what is listed depends on the other options.

Is that correct? A description that's closer to the above might be more helpful.

plugins/modules/proxmox_backup_info.py Outdated Show resolved Hide resolved
elements: dict
contains:
bktype:
description: backup type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The descriptions here also need to be full sentences.

Suggested change
description: backup type
description: The type of the backup.

Comment on lines 36 to 39
- The backup_jobs value is true or false.
- If set to true, this module just return all backup jobs information.
- If set to false, what is listed depends on the other options.
required: false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required: false is redundant, please remove it from both here and the argument_spec below.
Also, use semantic markup (such as V()), and boolean parameters may have, by definition, only the values true and false, no need to mention that ;-).

Suggested change
- The backup_jobs value is true or false.
- If set to true, this module just return all backup jobs information.
- If set to false, what is listed depends on the other options.
required: false
- Include backup jobs in the result.
- If V(true), this module just return all backup jobs information.
- If V(false), the result will not include backup jobs.

Or something like that.

Semantic markup is documented in:
https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#semantic-markup-within-module-documentation

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Dec 31, 2024
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Dec 31, 2024
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @raoufnezhad thanks again for your contribution!

There's a number of adjustments that need to be made. Please see below, feel free to question anything.

Comment on lines +40 to +42
- The backup_jobs value is true or false.
- If V(true), the module will return all backup jobs information.
- If V(false), the module will parse all backup jobs based on VM IDs and return a list of VMs' backup information.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first line is redundant:

Suggested change
- The backup_jobs value is true or false.
- If V(true), the module will return all backup jobs information.
- If V(false), the module will parse all backup jobs based on VM IDs and return a list of VMs' backup information.
- If V(true), the module will return all backup jobs information.
- If V(false), the module will parse all backup jobs based on VM IDs and return a list of VMs' backup information.

With the other two it is already clear what the option does.

vm_id:
description:
- The ID of the Proxmox VM.
- If vm_id is defined, the returned list will contain backup jobs that have been parsed and filtered based on vm_id value.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- If vm_id is defined, the returned list will contain backup jobs that have been parsed and filtered based on vm_id value.
- If defined, the returned list will contain backup jobs that have been parsed and filtered based on O(vm_id) value.

vm_name:
description:
- The name of the Proxmox VM.
- If vm_name is defined, the returned list will contain backup jobs that have been parsed and filtered based on vm_name value.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- If vm_name is defined, the returned list will contain backup jobs that have been parsed and filtered based on vm_name value.
- If defined, the returned list will contain backup jobs that have been parsed and filtered based on O(vm_name) value.

- "Marzieh Raoufnezhad (@raoufnezhad) <[email protected]>"
- "Maryam Mayabi (@mmayabi) <[email protected]>"

options:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options:
version_added: 10.3.0
options:

returned: on success
type: str
enabled:
description: 1 if backup is enabled else 0.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: 1 if backup is enabled else 0.
description: V(1) if backup is enabled else V(0).

vmid:
description: The VM vmid.
returned: on success
type: int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the Proxmox VMID can be an UUID, in which case it is not an int but a str. Could you please double check and confirm here? Thx

class ProxmoxBackupInfoAnsible(ProxmoxAnsible):

# Get all backup information
def getJobsList(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very pythonic. Please use:

Suggested change
def getJobsList(self):
def get_jobs_list(self):

instead. Same for other methods and variables. Python only uses CamelCase in class names.

Personally, I would even remove the "list" part in the name, because if we are getting "jobs" (plural), I think it is clear that it is a list of them.


def test_get_specific_backup_information_by_vmid(self):
with pytest.raises(AnsibleExitJson) as exc_info:
vmid = "101"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The vmid parameter is int, so this should be:

Suggested change
vmid = "101"
vmid = 101

Comment on lines +179 to +185
def specificVmBackupInfo(self, vm_name_id):
fullBackupInfo = self.vmsBackupInfo()
vmBackupJobs = []
for vm in fullBackupInfo:
if (vm["vm_name"] == vm_name_id or int(vm["vmid"]) == vm_name_id):
vmBackupJobs.append(vm)
return vmBackupJobs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. If vm_name_id="100" (str), then int(vm["vmid"]) == vm_named_id is always going to be False, even then int(vm["vmid"]) == 100.

>>> 3 == "3"
False

Additionally, there is the naming convention (camelCase vs. snake_case), and the more pythonic way of doing this would be:

Suggested change
def specificVmBackupInfo(self, vm_name_id):
fullBackupInfo = self.vmsBackupInfo()
vmBackupJobs = []
for vm in fullBackupInfo:
if (vm["vm_name"] == vm_name_id or int(vm["vmid"]) == vm_name_id):
vmBackupJobs.append(vm)
return vmBackupJobs
return [vm for vm in full_backup_info if vm["vm_name"] == vm_name_id or int(vm["vmid"]) == vm_name_id]

(it won't work like that because of the type issue mentioned above - needs adjustments)

Not goin to be a huge problem here, but from a performance point of view it is much better to do the test outside the loop than inside it.

if is int
  return [... if vm[vmid] == param]
if is str
  return [... if vm[name] == param]

Or, make two separate functions, one for the ID another for the name, which is cleaner to read, IMHO.

Comment on lines +192 to +194
vm_id=dict(type='int', required=False),
vm_name=dict(type='str', required=False),
backup_jobs=dict(type='bool', default=False, required=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant

Suggested change
vm_id=dict(type='int', required=False),
vm_name=dict(type='str', required=False),
backup_jobs=dict(type='bool', default=False, required=False)
vm_id=dict(type='int'),
vm_name=dict(type='str'),
backup_jobs=dict(type='bool', default=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-10 Automatically create a backport for the stable-10 branch check-before-release PR will be looked at again shortly before release and merged if possible. module module new_contributor Help guide this first time contributor new_plugin New plugin plugins plugin (any type) tests tests unit tests/unit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants