chore(realms): refactor modules
This commit is contained in:
parent
de246c384c
commit
b3238ff47b
25
plugins/module_utils/common.py
Normal file
25
plugins/module_utils/common.py
Normal 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)
|
@ -1,17 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Tuple
|
||||
|
||||
import requests
|
||||
|
||||
API_BASE_PATH: str = "/api2/json"
|
||||
|
||||
|
||||
@dataclass()
|
||||
class ProxmoxAuthInfo:
|
||||
instance_url: str
|
||||
token_id: str
|
||||
secret: str
|
||||
verify_cert: bool = True
|
||||
from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import _proxmox_request, ProxmoxAuthInfo
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@ -63,14 +53,3 @@ def set_realm_data(auth_info: ProxmoxAuthInfo, realm: str, config: dict[str, str
|
||||
new_realm_data = None
|
||||
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)
|
@ -5,15 +5,17 @@
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import dataclasses
|
||||
import json
|
||||
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_realm import *
|
||||
|
||||
|
||||
LIB_IMP_ERR = None
|
||||
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
|
||||
except:
|
||||
HAS_LIB = False
|
||||
|
@ -10,13 +10,8 @@ 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()
|
||||
from ansible_collections.finallycoffee.proxmox.plugins.module_utils.common import *
|
||||
from ansible_collections.finallycoffee.proxmox.plugins.module_utils.proxmox_datacenter_realm import *
|
||||
|
||||
|
||||
DOCUMENTATION = r'''
|
||||
|
Loading…
Reference in New Issue
Block a user