Compare commits
1 Commits
main
...
0e57073bdd
Author | SHA1 | Date | |
---|---|---|---|
0e57073bdd
|
34
plugins/module_utils/proxmox_api.py
Normal file
34
plugins/module_utils/proxmox_api.py
Normal file
@ -0,0 +1,34 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
|
||||
import requests
|
||||
|
||||
API_BASE_PATH: str = "/api2/json"
|
||||
|
||||
@dataclass()
|
||||
class ProxmoxAuthInfo:
|
||||
instance_url: str
|
||||
token_id: str
|
||||
secret: str
|
||||
verify_cert: bool = True
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProxmoxRealm:
|
||||
type: str
|
||||
realm: str
|
||||
comment: str = None
|
||||
default: bool = False
|
||||
|
||||
def get_realms(auth_info: ProxmoxAuthInfo) -> List['ProxmoxRealm']:
|
||||
realm_answer = requests.get(
|
||||
f"{auth_info.instance_url}{API_BASE_PATH}/access/domains",
|
||||
headers={"Authorization": _get_token_from_authinfo(auth_info)},
|
||||
verify=auth_info.verify_cert
|
||||
).json()
|
||||
return list(map(
|
||||
lambda r: ProxmoxRealm(r['type'], r['realm'], r.get('comment', None), r.get('default', False)),
|
||||
realm_answer['data']
|
||||
))
|
||||
|
||||
def _get_token_from_authinfo(auth_info: ProxmoxAuthInfo) -> str:
|
||||
return f"PVEAPIToken={auth_info.token_id}={auth_info.secret}"
|
117
plugins/modules/realm_info.py
Normal file
117
plugins/modules/realm_info.py
Normal file
@ -0,0 +1,117 @@
|
||||
#!/usr/bin/python
|
||||
# coding: utf-8
|
||||
|
||||
# (c) 2022, Johanna Dorothea Reichmann <transcaffeine@finally.coffee>
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import dataclasses
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
LIB_IMP_ERR = None
|
||||
try:
|
||||
from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_api import *
|
||||
HAS_LIB = True
|
||||
except:
|
||||
HAS_LIB = False
|
||||
LIB_IMP_ERR = traceback.format_exc()
|
||||
|
||||
|
||||
DOCUMENTATION = r'''
|
||||
---
|
||||
module: realm_info
|
||||
author:
|
||||
- Johanna Dorothea Reichmann (transcaffeine@finally.coffee)
|
||||
requirements:
|
||||
- python >= 3.9
|
||||
short_description: Get configured realms of a proxmox node
|
||||
description:
|
||||
- "Lists all configured realms in a proxmox instance"
|
||||
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
|
||||
realm:
|
||||
description: Realm to retrieve information about. If left omitted, return all realms
|
||||
type: str
|
||||
required: false
|
||||
'''
|
||||
|
||||
EXAMPLES = r'''
|
||||
- name:
|
||||
finallycoffee.proxmox.realm_info:
|
||||
proxmox_isntance: https://my.proxmox-node.local:8006
|
||||
promox_api_token_id: root@pam!token
|
||||
proxmox_api_secret: supersecuretokencontent
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
realms:
|
||||
description: The retrieved realms
|
||||
returned: When realms were found (matching the filter)
|
||||
type: list
|
||||
elements: dict[str, str, str, 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),
|
||||
realm=_(required=False, type='str'),
|
||||
),
|
||||
supports_check_mode=True
|
||||
)
|
||||
|
||||
result = _(
|
||||
changed=False,
|
||||
diff={},
|
||||
message=''
|
||||
)
|
||||
|
||||
realms = []
|
||||
try:
|
||||
realms = get_realms(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['realms'] = list(
|
||||
map(lambda r: dataclasses.asdict(r),
|
||||
filter(lambda r: r.realm == module.params['realm']
|
||||
if module.params['realm'] is not None
|
||||
else True,
|
||||
realms)
|
||||
)
|
||||
)
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user