Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/module_utils/_deprecation.py

raw
1 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
2 # Do not use this from other collections or standalone plugins/modules!
3
4 from __future__ import annotations
5
6 import warnings
7 from datetime import datetime, timezone
8
9 from ansible.module_utils.basic import AnsibleModule
10
11 from ._vendor.hcloud.locations import BoundLocation
12 from ._vendor.hcloud.server_types import BoundServerType, ServerTypeLocation
13
14 DEPRECATED_EXISTING_SERVERS = """
15 Existing servers of that type will continue to work as before and no action is \
16 required on your part.
17 """.strip()
18
19
20 def deprecated_server_type_warning(
21 module: AnsibleModule,
22 server_type: BoundServerType,
23 location: BoundLocation | None = None,
24 ) -> bool:
25 with warnings.catch_warnings():
26 warnings.simplefilter("ignore")
27 if server_type.deprecation is not None:
28 if server_type.deprecation.unavailable_after < datetime.now(timezone.utc):
29 module.warn(
30 str.format(
31 "Server type {server_type} is unavailable in all locations and can no longer be ordered. ",
32 server_type=server_type.name,
33 )
34 + DEPRECATED_EXISTING_SERVERS,
35 )
36 else:
37 module.warn(
38 str.format(
39 "Server type {server_type} is deprecated in all locations and will no longer be available "
40 "for order as of {unavailable_after}. ",
41 server_type=server_type.name,
42 unavailable_after=server_type.deprecation.unavailable_after.strftime("%Y-%m-%d"),
43 )
44 + DEPRECATED_EXISTING_SERVERS,
45 )
46 return True
47
48 deprecated_locations: list[ServerTypeLocation] = []
49 unavailable_locations: list[ServerTypeLocation] = []
50
51 for o in server_type.locations or []:
52 if o.deprecation is not None:
53 deprecated_locations.append(o)
54 if o.deprecation.unavailable_after < datetime.now(timezone.utc):
55 unavailable_locations.append(o)
56
57 if not deprecated_locations:
58 return False
59
60 # Warn when the server type is deprecated in the given location
61 if location:
62 found = [o for o in deprecated_locations if location.name == o.location.name]
63 if not found:
64 return False
65
66 deprecated_location = found[0]
67
68 if deprecated_location in unavailable_locations:
69 module.warn(
70 str.format(
71 "Server type {server_type} is unavailable in {location} and can no longer be ordered. ",
72 server_type=server_type.name,
73 location=deprecated_location.location.name,
74 )
75 + DEPRECATED_EXISTING_SERVERS,
76 )
77 else:
78 module.warn(
79 str.format(
80 "Server type {server_type} is deprecated in {location} and will no longer be available "
81 "for order as of {unavailable_after}. ",
82 server_type=server_type.name,
83 location=deprecated_location.location.name,
84 unavailable_after=deprecated_location.deprecation.unavailable_after.strftime("%Y-%m-%d"),
85 )
86 + DEPRECATED_EXISTING_SERVERS,
87 )
88
89 return True
90
91 # No location given, only warn when all locations are deprecated
92 if len(server_type.locations) != len(deprecated_locations):
93 return False
94
95 if unavailable_locations:
96
97 if len(deprecated_locations) != len(unavailable_locations):
98 module.warn(
99 str.format(
100 "Server type {server_type} is deprecated in all locations ({deprecated_locations}) and can no "
101 "longer be ordered in some locations ({unavailable_locations}). ",
102 server_type=server_type.name,
103 deprecated_locations=",".join(o.location.name for o in deprecated_locations),
104 unavailable_locations=",".join(o.location.name for o in unavailable_locations),
105 )
106 + DEPRECATED_EXISTING_SERVERS,
107 )
108 else:
109 module.warn(
110 str.format(
111 "Server type {server_type} is unavailable in all locations ({unavailable_locations}) and can no "
112 "longer be ordered. ",
113 server_type=server_type.name,
114 unavailable_locations=",".join(o.location.name for o in unavailable_locations),
115 )
116 + DEPRECATED_EXISTING_SERVERS,
117 )
118 else:
119 module.warn(
120 str.format(
121 "Server type {server_type} is deprecated in all locations ({deprecated_locations}) and will no "
122 "longer be available for order. ",
123 server_type=server_type.name,
124 deprecated_locations=",".join(o.location.name for o in deprecated_locations),
125 )
126 + DEPRECATED_EXISTING_SERVERS,
127 )
128
129 return True