mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Add basic typing for module_utils.
This commit is contained in:
@@ -5,11 +5,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
from ansible.module_utils.basic import missing_required_lib, AnsibleModule
|
||||
|
||||
import traceback
|
||||
|
||||
REDIS_IMP_ERR = None
|
||||
REDIS_IMP_ERR: str | None = None
|
||||
try:
|
||||
from redis import Redis
|
||||
from redis import __version__ as redis_version
|
||||
@@ -20,25 +20,25 @@ except ImportError:
|
||||
REDIS_IMP_ERR = traceback.format_exc()
|
||||
HAS_REDIS_PACKAGE = False
|
||||
|
||||
CERTIFI_IMPORT_ERROR: str | None = None
|
||||
try:
|
||||
import certifi
|
||||
|
||||
HAS_CERTIFI_PACKAGE = True
|
||||
CERTIFI_IMPORT_ERROR = None
|
||||
except ImportError:
|
||||
CERTIFI_IMPORT_ERROR = traceback.format_exc()
|
||||
HAS_CERTIFI_PACKAGE = False
|
||||
|
||||
|
||||
def fail_imports(module, needs_certifi=True):
|
||||
errors = []
|
||||
traceback = []
|
||||
def fail_imports(module: AnsibleModule, needs_certifi: bool = True) -> None:
|
||||
errors: list[str] = []
|
||||
traceback: list[str] = []
|
||||
if not HAS_REDIS_PACKAGE:
|
||||
errors.append(missing_required_lib("redis"))
|
||||
traceback.append(REDIS_IMP_ERR)
|
||||
traceback.append(REDIS_IMP_ERR) # type: ignore
|
||||
if not HAS_CERTIFI_PACKAGE and needs_certifi:
|
||||
errors.append(missing_required_lib("certifi"))
|
||||
traceback.append(CERTIFI_IMPORT_ERROR)
|
||||
traceback.append(CERTIFI_IMPORT_ERROR) # type: ignore
|
||||
if errors:
|
||||
module.fail_json(msg="\n".join(errors), traceback="\n".join(traceback))
|
||||
|
||||
@@ -60,7 +60,7 @@ def redis_auth_argument_spec(tls_default=True):
|
||||
)
|
||||
|
||||
|
||||
def redis_auth_params(module):
|
||||
def redis_auth_params(module: AnsibleModule):
|
||||
login_host = module.params["login_host"]
|
||||
login_user = module.params["login_user"]
|
||||
login_password = module.params["login_password"]
|
||||
@@ -92,7 +92,7 @@ def redis_auth_params(module):
|
||||
class RedisAnsible:
|
||||
"""Base class for Redis module"""
|
||||
|
||||
def __init__(self, module):
|
||||
def __init__(self, module: AnsibleModule) -> None:
|
||||
self.module = module
|
||||
self.connection = self._connect()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user