chore(realms): refactor modules

This commit is contained in:
transcaffeine 2022-06-18 11:41:59 +02:00
parent de246c384c
commit b3238ff47b
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
4 changed files with 33 additions and 32 deletions

View File

@ -0,0 +1,25 @@
from dataclasses import dataclass
import requests
API_BASE_PATH: str = "/api2/json"
@dataclass()
class ProxmoxAuthInfo:
instance_url: str
token_id: str
secret: str
verify_cert: bool = True
def _get_token_from_auth_info(auth_info: ProxmoxAuthInfo) -> str:
return f"PVEAPIToken={auth_info.token_id}={auth_info.secret}"
def _proxmox_request(method: str, endpoint: str, auth_info: ProxmoxAuthInfo, **params):
return requests.request(method,
f"{auth_info.instance_url}{API_BASE_PATH}{endpoint}",
headers={'Authorization': _get_token_from_auth_info(auth_info)},
verify=auth_info.verify_cert,
**params)

View File

@ -1,17 +1,7 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import List, Tuple from typing import List, Tuple
import requests from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import _proxmox_request, ProxmoxAuthInfo
API_BASE_PATH: str = "/api2/json"
@dataclass()
class ProxmoxAuthInfo:
instance_url: str
token_id: str
secret: str
verify_cert: bool = True
@dataclass(frozen=True) @dataclass(frozen=True)
@ -63,14 +53,3 @@ def set_realm_data(auth_info: ProxmoxAuthInfo, realm: str, config: dict[str, str
new_realm_data = None new_realm_data = None
return existing_realm_data, new_realm_data return existing_realm_data, new_realm_data
def _get_token_from_auth_info(auth_info: ProxmoxAuthInfo) -> str:
return f"PVEAPIToken={auth_info.token_id}={auth_info.secret}"
def _proxmox_request(method: str, endpoint: str, auth_info: ProxmoxAuthInfo, **params):
return requests.request(method,
f"{auth_info.instance_url}{API_BASE_PATH}{endpoint}",
headers={'Authorization': _get_token_from_auth_info(auth_info)},
verify=auth_info.verify_cert,
**params)

View File

@ -5,15 +5,17 @@
__metaclass__ = type __metaclass__ = type
import dataclasses
import json
import traceback import traceback
from ansible.module_utils.basic import AnsibleModule 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_realm import *
LIB_IMP_ERR = None LIB_IMP_ERR = None
try: try:
from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_api import * from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_datacenter_realm import set_realm_data
HAS_LIB = True HAS_LIB = True
except: except:
HAS_LIB = False HAS_LIB = False

View File

@ -10,13 +10,8 @@ import traceback
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
LIB_IMP_ERR = None from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import *
try: from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_datacenter_realm import *
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''' DOCUMENTATION = r'''