mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-21 19:29:05 +00:00
Add basic typing for module_utils (#11222)
* Add basic typing for module_utils.
* Apply some suggestions.
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
* Make pass again.
* Add more types as suggested.
* Normalize extra imports.
* Add more type hints.
* Improve typing.
* Add changelog fragment.
* Reduce changelog.
* Apply suggestions from code review.
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
* Fix typo.
* Cleanup.
* Improve types and make type checking happy.
* Let's see whether older Pythons barf on this.
* Revert "Let's see whether older Pythons barf on this."
This reverts commit 9973af3dbe.
* Add noqa.
---------
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
fb2f34ba85
commit
c7f6a28d89
56 changed files with 725 additions and 469 deletions
|
|
@ -6,10 +6,11 @@ from __future__ import annotations
|
|||
|
||||
|
||||
import http.client as http_client
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import ssl
|
||||
import json
|
||||
import typing as t
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from ansible.module_utils.urls import generic_urlparse
|
||||
|
|
@ -20,7 +21,7 @@ HTTPSConnection = http_client.HTTPSConnection
|
|||
|
||||
|
||||
class UnixHTTPConnection(HTTPConnection):
|
||||
def __init__(self, path):
|
||||
def __init__(self, path: str) -> None:
|
||||
HTTPConnection.__init__(self, "localhost")
|
||||
self.path = path
|
||||
|
||||
|
|
@ -31,33 +32,34 @@ class UnixHTTPConnection(HTTPConnection):
|
|||
|
||||
|
||||
class LXDClientException(Exception):
|
||||
def __init__(self, msg, **kwargs):
|
||||
def __init__(self, msg: str, **kwargs) -> None:
|
||||
self.msg = msg
|
||||
self.kwargs = kwargs
|
||||
|
||||
|
||||
class LXDClient:
|
||||
def __init__(
|
||||
self, url, key_file=None, cert_file=None, debug=False, server_cert_file=None, server_check_hostname=True
|
||||
):
|
||||
self,
|
||||
url: str,
|
||||
key_file: str | None = None,
|
||||
cert_file: str | None = None,
|
||||
debug: bool = False,
|
||||
server_cert_file: str | None = None,
|
||||
server_check_hostname: bool = True,
|
||||
) -> None:
|
||||
"""LXD Client.
|
||||
|
||||
:param url: The URL of the LXD server. (e.g. unix:/var/lib/lxd/unix.socket or https://127.0.0.1)
|
||||
:type url: ``str``
|
||||
:param key_file: The path of the client certificate key file.
|
||||
:type key_file: ``str``
|
||||
:param cert_file: The path of the client certificate file.
|
||||
:type cert_file: ``str``
|
||||
:param debug: The debug flag. The request and response are stored in logs when debug is true.
|
||||
:type debug: ``bool``
|
||||
:param server_cert_file: The path of the server certificate file.
|
||||
:type server_cert_file: ``str``
|
||||
:param server_check_hostname: Whether to check the server's hostname as part of TLS verification.
|
||||
:type debug: ``bool``
|
||||
"""
|
||||
self.url = url
|
||||
self.debug = debug
|
||||
self.logs = []
|
||||
self.logs: list[dict[str, t.Any]] = []
|
||||
self.connection: UnixHTTPConnection | HTTPSConnection
|
||||
if url.startswith("https:"):
|
||||
self.cert_file = cert_file
|
||||
self.key_file = key_file
|
||||
|
|
@ -67,7 +69,7 @@ class LXDClient:
|
|||
# Check that the received cert is signed by the provided server_cert_file
|
||||
ctx.load_verify_locations(cafile=server_cert_file)
|
||||
ctx.check_hostname = server_check_hostname
|
||||
ctx.load_cert_chain(cert_file, keyfile=key_file)
|
||||
ctx.load_cert_chain(cert_file, keyfile=key_file) # type: ignore # TODO!
|
||||
self.connection = HTTPSConnection(parts.get("netloc"), context=ctx)
|
||||
elif url.startswith("unix:"):
|
||||
unix_socket_path = url[len("unix:") :]
|
||||
|
|
@ -75,7 +77,7 @@ class LXDClient:
|
|||
else:
|
||||
raise LXDClientException("URL scheme must be unix: or https:")
|
||||
|
||||
def do(self, method, url, body_json=None, ok_error_codes=None, timeout=None, wait_for_container=None):
|
||||
def do(self, method: str, url: str, body_json=None, ok_error_codes=None, timeout=None, wait_for_container=None):
|
||||
resp_json = self._send_request(method, url, body_json=body_json, ok_error_codes=ok_error_codes, timeout=timeout)
|
||||
if resp_json["type"] == "async":
|
||||
url = f"{resp_json['operation']}/wait"
|
||||
|
|
@ -91,7 +93,7 @@ class LXDClient:
|
|||
body_json = {"type": "client", "password": trust_password}
|
||||
return self._send_request("POST", "/1.0/certificates", body_json=body_json)
|
||||
|
||||
def _send_request(self, method, url, body_json=None, ok_error_codes=None, timeout=None):
|
||||
def _send_request(self, method: str, url: str, body_json=None, ok_error_codes=None, timeout=None):
|
||||
try:
|
||||
body = json.dumps(body_json)
|
||||
self.connection.request(method, url, body=body)
|
||||
|
|
@ -133,9 +135,9 @@ class LXDClient:
|
|||
return err
|
||||
|
||||
|
||||
def default_key_file():
|
||||
def default_key_file() -> str:
|
||||
return os.path.expanduser("~/.config/lxc/client.key")
|
||||
|
||||
|
||||
def default_cert_file():
|
||||
def default_cert_file() -> str:
|
||||
return os.path.expanduser("~/.config/lxc/client.crt")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue