From 551d0639cc9054694f3b84ca7ac3d78d9072df35 Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Sun, 14 Aug 2022 17:01:40 +0200 Subject: [PATCH] feat(group): add module for retrieving groups present in proxmox node --- .../module_utils/proxmox_datacenter_group.py | 19 +++ plugins/modules/group_info.py | 127 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 plugins/module_utils/proxmox_datacenter_group.py create mode 100644 plugins/modules/group_info.py diff --git a/plugins/module_utils/proxmox_datacenter_group.py b/plugins/module_utils/proxmox_datacenter_group.py new file mode 100644 index 0000000..8ce358d --- /dev/null +++ b/plugins/module_utils/proxmox_datacenter_group.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass, field +from typing import List, Tuple + +from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import _proxmox_request, ProxmoxAuthInfo + + +@dataclass(frozen=True) +class ProxmoxGroup: + name: str + members: List = field(default_factory=lambda: []) + comment: str = None + + +def get_groups(auth_info: ProxmoxAuthInfo) -> List['ProxmoxGroups']: + group_answer = _proxmox_request('get', '/access/groups', auth_info).json() + return list(map( + lambda r: ProxmoxGroup(r['groupid'], r['users'].split(','), r.get('comment', '')), + group_answer['data'] + )) diff --git a/plugins/modules/group_info.py b/plugins/modules/group_info.py new file mode 100644 index 0000000..8c4bdb7 --- /dev/null +++ b/plugins/modules/group_info.py @@ -0,0 +1,127 @@ +#!/usr/bin/python +# coding: utf-8 + +# (c) 2022, Johanna Dorothea Reichmann + +__metaclass__ = type + +import dataclasses +import traceback + +from ansible.module_utils.basic import AnsibleModule + +from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import * +from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_datacenter_group import * + + +DOCUMENTATION = r''' +--- +module: group_info +author: +- Johanna Dorothea Reichmann (transcaffeine@finally.coffee) +requirements: + - python >= 3.9 +short_description: Get all groups of a proxmox +description: + - "Lists all groups in proxmos" +options: + proxmox_instance: + description: Location of the proxmox API with scheme, domain name/ip and port, e.g. https://localhost:8006 + type: str + required: true + proxmox_api_token_id: + description: The token ID containing username, realm and token name (format: user@realm!name) + type: str + required: true + proxmox_api_secret: + description: The secret + type: str + required: true + proxmox_api_verify_cert: + description: If the certificate presented for `proxmox_instance_url` should be verified + type: bool + required: false + default: true + group: + description: Group to retrieve information about. If left omitted, return all groups + type: str + required: false +''' + +EXAMPLES = r''' +- name: Retrieve all groups + finallycoffee.proxmox.group_info: + proxmox_instance: https://my.proxmox-node.local:8006 + promox_api_token_id: root@pam!token + proxmox_api_secret: supersecuretokencontent +- name: Retrieve group information for group developers-realm + finallycoffee.proxmox.group_info: + proxmox_instance: https://my.proxmox-node.local:8006 + promox_api_token_id: root@pam!token + proxmox_api_secret: supersecuretokencontent + group: developers-realm +''' + +RETURN = r''' +groups: + description: The retrieved groups + returned: When groups were found (matching the filter) + type: list + elements: dict[str, list[str]] +group: + description: The groups's name (`.name`) and members (`.members`) + returned: When a single group was queried + type: dict[str, list[str]] + +''' + + +def main(): + _ = dict + module = AnsibleModule( + argument_spec=_( + proxmox_instance=_(required=True, type='str'), + proxmox_api_token_id=_(required=True, type='str'), + proxmox_api_secret=_(type='str', required=True, no_log=True), + proxmox_api_verify_cert=_(type='bool', required=False, default=True), + group=_(required=False, type='str'), + ), + supports_check_mode=True + ) + + result = _( + changed=False, + diff={}, + message='' + ) + + groups = [] + try: + groups = get_groups(ProxmoxAuthInfo( + module.params['proxmox_instance'], + module.params['proxmox_api_token_id'], + module.params['proxmox_api_secret'], + module.params['proxmox_api_verify_cert'], + )) + except IOError as owie: + result['msg'] = owie + module.exit_json(**result) + + result['groups'] = list( + map(lambda r: dataclasses.asdict(r), + filter(lambda r: r.name == module.params['group'] + if module.params['group'] is not None + else True, + groups) + ) + ) + if module.params['group'] is not None: + result['group'] = dataclasses.asdict( + list(filter(lambda r: r.name == module.params['group'], groups))[0] + ) + + module.exit_json(**result) + + +if __name__ == '__main__': + main()