Skip to content

Commit c0072d2

Browse files
authored
ansible-core 2.20: avoid deprecated functionality (#953)
* Avoid deprecated functionality. * Lint. * Fix typing. * Python 3.7/3.8 compat.
1 parent 1bfee3c commit c0072d2

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "Avoid deprecated functionality in ansible-core 2.20 (https://github.com/ansible-collections/community.crypto/pull/953)."

plugins/module_utils/_acme/account.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from __future__ import annotations
1010

1111
import typing as t
12+
from collections.abc import Mapping
1213

13-
from ansible.module_utils.common._collections_compat import Mapping
1414
from ansible_collections.community.crypto.plugins.module_utils._acme.errors import (
1515
ACMEProtocolException,
1616
ModuleFailException,
@@ -22,6 +22,11 @@
2222
ACMEClient,
2323
)
2424

25+
_JsonMapping = Mapping[str, t.Any]
26+
else:
27+
# Since we need to pass this to t.cast(), we need a version that doesn't break with Python 3.7 and 3.8
28+
_JsonMapping = Mapping
29+
2530

2631
class ACMEAccount:
2732
"""
@@ -42,7 +47,7 @@ def _new_reg(
4247
terms_agreed: bool = False,
4348
allow_creation: bool = True,
4449
external_account_binding: dict[str, t.Any] | None = None,
45-
) -> tuple[bool, dict[str, t.Any] | None]:
50+
) -> tuple[bool, Mapping[str, t.Any] | None]:
4651
"""
4752
Registers a new ACME account. Returns a pair ``(created, data)``.
4853
Here, ``created`` is ``True`` if the account was created and
@@ -132,7 +137,7 @@ def _new_reg(
132137
# Account did not exist
133138
if "location" in info:
134139
self.client.set_account_uri(info["location"])
135-
return True, result
140+
return True, t.cast(_JsonMapping, result)
136141
if info["status"] == 200:
137142
# Account did exist
138143
if result.get("status") == "deactivated":
@@ -147,7 +152,7 @@ def _new_reg(
147152
raise ModuleFailException("Account is deactivated")
148153
if "location" in info:
149154
self.client.set_account_uri(info["location"])
150-
return False, result
155+
return False, t.cast(_JsonMapping, result)
151156
if (
152157
info["status"] in (400, 404)
153158
and result["type"] == "urn:ietf:params:acme:error:accountDoesNotExist"
@@ -236,7 +241,7 @@ def setup_account(
236241
allow_creation: t.Literal[True] = True,
237242
remove_account_uri_if_not_exists: bool = False,
238243
external_account_binding: dict[str, t.Any] | None = None,
239-
) -> tuple[bool, dict[str, t.Any]]: ...
244+
) -> tuple[bool, Mapping[str, t.Any]]: ...
240245

241246
@t.overload
242247
def setup_account(
@@ -247,7 +252,7 @@ def setup_account(
247252
allow_creation: bool = True,
248253
remove_account_uri_if_not_exists: bool = False,
249254
external_account_binding: dict[str, t.Any] | None = None,
250-
) -> tuple[bool, dict[str, t.Any] | None]: ...
255+
) -> tuple[bool, Mapping[str, t.Any] | None]: ...
251256

252257
def setup_account(
253258
self,
@@ -257,7 +262,7 @@ def setup_account(
257262
allow_creation: bool = True,
258263
remove_account_uri_if_not_exists: bool = False,
259264
external_account_binding: dict[str, t.Any] | None = None,
260-
) -> tuple[bool, dict[str, t.Any] | None]:
265+
) -> tuple[bool, Mapping[str, t.Any] | None]:
261266
"""
262267
Detect or create an account on the ACME server. For ACME v1,
263268
as the only way (without knowing an account URI) to test if an
@@ -288,7 +293,7 @@ def setup_account(
288293
created = False
289294
# Verify that the account key belongs to the URI.
290295
# (If update_contact is True, this will be done below.)
291-
account_data = self.get_account_data()
296+
account_data: Mapping[str, t.Any] | None = self.get_account_data()
292297
if account_data is None:
293298
if remove_account_uri_if_not_exists and not allow_creation:
294299
self.client.account_uri = None
@@ -314,7 +319,7 @@ def setup_account(
314319

315320
def update_account(
316321
self, *, account_data: dict[str, t.Any], contact: list[str] | None = None
317-
) -> tuple[bool, dict[str, t.Any]]:
322+
) -> tuple[bool, Mapping[str, t.Any]]:
318323
"""
319324
Update an account on the ACME server. Check mode is fully respected.
320325
@@ -340,9 +345,11 @@ def update_account(
340345
return False, dict(account_data)
341346

342347
# Apply change
348+
account_data_res: Mapping[str, t.Any]
343349
if self.client.module.check_mode:
344-
account_data = dict(account_data)
345-
account_data.update(update_request)
350+
account_data_dict = dict(account_data)
351+
account_data_dict.update(update_request)
352+
account_data_res = account_data_dict
346353
else:
347354
raw_account_data, info = self.client.send_signed_request(
348355
self.client.account_uri, update_request
@@ -354,9 +361,9 @@ def update_account(
354361
info=info,
355362
content_json=account_data,
356363
)
357-
account_data = raw_account_data
364+
account_data_res = raw_account_data
358365

359-
return True, account_data
366+
return True, account_data_res
360367

361368

362369
__all__ = ("ACMEAccount",)

plugins/modules/acme_account_info.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,12 @@ def main() -> t.NoReturn:
320320
result["account_uri"] = client.account_uri
321321
result["exists"] = True
322322
# Make sure promised data is there
323+
account_data_dict = dict(account_data)
323324
if "contact" not in account_data:
324-
account_data["contact"] = []
325+
account_data_dict["contact"] = []
325326
if client.account_key_data:
326-
account_data["public_account_key"] = client.account_key_data["jwk"]
327-
result["account"] = account_data
327+
account_data_dict["public_account_key"] = client.account_key_data["jwk"]
328+
result["account"] = account_data_dict
328329
# Retrieve orders list
329330
if (
330331
account_data.get("orders")

plugins/plugin_utils/_action_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import copy
2020
import traceback
2121
import typing as t
22+
from collections.abc import Mapping
2223

2324
from ansible.errors import AnsibleError
2425
from ansible.module_utils.basic import SEQUENCETYPE, remove_values
25-
from ansible.module_utils.common._collections_compat import Mapping
2626
from ansible.module_utils.common.arg_spec import ArgumentSpecValidator
2727
from ansible.module_utils.errors import UnsupportedError
2828
from ansible.plugins.action import ActionBase

0 commit comments

Comments
 (0)