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 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8439fd1
Create proxmox_backup_info.py
raoufnezhad Dec 28, 2024
2eb2e8a
Create test_proxmox_backup_info.py
raoufnezhad Dec 28, 2024
2b1c48f
Update plugins/modules/proxmox_backup_info.py
raoufnezhad Dec 29, 2024
469afec
check tests proxmox_backup_info.py
raoufnezhad Dec 29, 2024
5ede419
check tests test_proxmox_backup_info.py
raoufnezhad Dec 29, 2024
193a13c
Update check tests test_proxmox_backup_info.py
raoufnezhad Dec 29, 2024
f0f76c4
Update check tests proxmox_backup_info.py
raoufnezhad Dec 29, 2024
1fd1106
Update authors proxmox_backup_info.py
raoufnezhad Dec 29, 2024
0be21d8
Update active maintainers for proxmox_backup_info module
raoufnezhad Dec 29, 2024
deba349
Update add proxmox_backup_info module in proxmox group
raoufnezhad Dec 29, 2024
4fcdd00
edit timestamp to UTC test_proxmox_backup_info.py
raoufnezhad Dec 29, 2024
57791ab
Update vm name or vmid to VM name or VM id proxmox_backup_info.py
raoufnezhad Dec 30, 2024
8e8dfb0
update documentation in proxmox_backup_info.py
raoufnezhad Dec 30, 2024
87d99c8
Update backup_section decription in proxmox_backup_info.py
raoufnezhad Dec 30, 2024
95c5fdd
Update plugins/modules/proxmox_backup_info.py
raoufnezhad Dec 31, 2024
86dc5ed
change backup_section to backup_jobs
raoufnezhad Dec 31, 2024
cca7ead
change backup_section to backup_jobs
raoufnezhad Dec 31, 2024
b4d2fd8
remove whitespace in line 35 and 36
raoufnezhad Dec 31, 2024
68abb06
improve descriptions
raoufnezhad Dec 31, 2024
61d475a
check again proxmox_backup_info.py module
raoufnezhad Dec 31, 2024
7323293
change vmid type and some descriptions proxmox_backup_info.py
raoufnezhad Jan 5, 2025
e981db4
delete comment #if ...
raoufnezhad Jan 5, 2025
2b99f17
solve trailing whitespace error
raoufnezhad Jan 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/BOTMETA.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,8 @@ files:
maintainers: helldorado krauthosting
$modules/proxmox_backup.py:
maintainers: IamLunchbox
$modules/proxmox_backup_info.py:
maintainers: raoufnezhad mmayabi
$modules/proxmox_nic.py:
maintainers: Kogelvis krauthosting
$modules/proxmox_node_info.py:
Expand Down
1 change: 1 addition & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ action_groups:
proxmox:
- proxmox
- proxmox_backup
- proxmox_backup_info
- proxmox_disk
- proxmox_domain_info
- proxmox_group_info
Expand Down
227 changes: 227 additions & 0 deletions plugins/modules/proxmox_backup_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 Marzieh Raoufnezhad <raoufnezhad at gmail.com>
# Copyright (c) 2024 Maryam Mayabi <mayabi.ahm at gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = """
---
module: proxmox_backup_info

short_description: Proxmox Schedule Backup Info
felixfontein marked this conversation as resolved.
Show resolved Hide resolved

description:
- Retrieve information such as backup times, VM name, VM ID, mode, backup type, and backup schedule using the Proxmox Server API.

author:
- "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:

vm_name:
description: Proxmox vm name
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
required: False
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
type: str
vm_id:
description: Proxmox vm id
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
required: False
type: int
backup_section:
description: proxmox_backup_section
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
required: False
default: False
type: bool

extends_documentation_fragment:
- community.general.proxmox.documentation
- community.general.attributes
- community.general.attributes.info_module
- community.general.proxmox.actiongroup_proxmox
"""

EXAMPLES = """
- name: Print all backup information by vmid and vm name
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
proxmox_backup_info:
api_user: 'myUser@pam'
api_password: '*******'
api_host: '192.168.20.20'
raoufnezhad marked this conversation as resolved.
Show resolved Hide resolved

- name: Print proxmox backup information for a specific vm based on its name
proxmox_backup_info:
api_user: 'myUser@pam'
api_password: '*******'
api_host: '192.168.20.20'
vm_name: 'mailsrv'

- name: Print proxmox backup information for a specific vm based on its vmid
proxmox_backup_info:
api_user: 'myUser@pam'
api_password: '*******'
api_host: '192.168.20.20'
vm_id: '150'

- name: Print proxmox all backup job information
proxmox_backup_info:
api_user: 'myUser@pam'
api_password: '*******'
api_host: '192.168.20.20'
backup_section: True
"""

RETURN = """
---
backup_info:
description: The return value provided by proxmox_backup_info.
returned: always, but can be empty
type: list
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.

returned: on success
type: str
enabled:
description: 1 if backup is enabled else 0.
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
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).

returned: on success
type: bool
id:
description: backup job id
returned: on success
type: str
mode:
description: backup job mode such as snapshot
returned: on success
type: str
next-run:
description: next backup time
returned: on success
type: str
schedule:
description: backup job schedule
returned: on success
type: str
storage:
description: backup storage location
returned: on success
type: str
vm_name:
description: VM name
returned: on success
type: str
vmid:
description: vm id
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

"""

from datetime import datetime
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible_collections.community.general.plugins.module_utils.proxmox import (
proxmox_auth_argument_spec, ProxmoxAnsible, HAS_PROXMOXER, PROXMOXER_IMP_ERR)


class ProxmoxBackupInfoAnsible(ProxmoxAnsible):

# Get all backup information
def getSectionsList(self):
try:
backupSections = self.proxmox_api.cluster.backup.get()
except Exception as e:
self.module.fail_json(msg="Getting backup sections failed: %s" % e)
return backupSections

# Get vm information
def getVmsList(self):
try:
vms = self.proxmox_api.cluster.resources.get(type='vm')
except Exception as e:
self.module.fail_json(msg="Getting vms info from cluster failed: %s" % e)
return vms

# Get all backup information by vmid and vm name
def vmsBackupInfo(self):
backupList = self.getSectionsList()
vmInfo = self.getVmsList()
bkInfo = []
for backupItem in backupList:
nextrun = datetime.fromtimestamp(backupItem['next-run'])
vmids = backupItem['vmid'].split(',')
for vmid in vmids:
for vm in vmInfo:
if vm['vmid'] == int(vmid):
vmName = vm['name']
break
bkInfoData = {'id': backupItem['id'],
'schedule': backupItem['schedule'],
'storage': backupItem['storage'],
'mode': backupItem['mode'],
'next-run': nextrun.strftime("%Y-%m-%d %H:%M:%S"),
'enabled': backupItem['enabled'],
'bktype': backupItem['type'],
'vmid': vmid,
'vm_name': vmName}
bkInfo.append(bkInfoData)
return bkInfo

# Get proxmox backup information for a specific vm based on its vmid or vm name
def specificVmBackupInfo(self, vm_name_id):
fullBackupInfo = self.vmsBackupInfo()
vmBackupSections = []
for vm in fullBackupInfo:
if (vm["vm_name"] == vm_name_id or int(vm["vmid"]) == vm_name_id):
vmBackupSections.append(vm)
return vmBackupSections


def main():
# Define module args
args = proxmox_auth_argument_spec()
backup_info_args = dict(
vm_id=dict(type='int', required=False),
vm_name=dict(type='str', required=False),
backup_section=dict(type='bool', default=False, required=False)
)
args.update(backup_info_args)

module = AnsibleModule(
argument_spec=args,
mutually_exclusive=[('backup_section', 'vm_id', 'vm_name')],
supports_check_mode=True
)

# Define (init) result value
result = dict(
changed=False
)

# Check if proxmoxer exist
if not HAS_PROXMOXER:
module.fail_json(msg=missing_required_lib('proxmoxer'), exception=PROXMOXER_IMP_ERR)

# Start to connect to proxmox to get backup data
proxmox = ProxmoxBackupInfoAnsible(module)
vm_id = module.params['vm_id']
vm_name = module.params['vm_name']
backup_section = module.params['backup_section']

# Update result value based on what requested (module args)
if backup_section:
result['backup_info'] = proxmox.getSectionsList()
elif vm_id:
result['backup_info'] = proxmox.specificVmBackupInfo(vm_id)
elif vm_name:
result['backup_info'] = proxmox.specificVmBackupInfo(vm_name)
else:
result['backup_info'] = proxmox.vmsBackupInfo()

# Return result value
module.exit_json(**result)


if __name__ == '__main__':
main()
Loading
Loading