From 54029e17259a1842d0fc7982a295b97c0ec1ef2a Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 17 Dec 2025 14:25:29 +0100 Subject: [PATCH] feat: deprecated datacenter in inventory --- plugins/inventory/hcloud.py | 27 +++++++++++++++++++-------- tests/unit/inventory/test_hcloud.py | 29 +++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/plugins/inventory/hcloud.py b/plugins/inventory/hcloud.py index 0fb8981..93603f3 100644 --- a/plugins/inventory/hcloud.py +++ b/plugins/inventory/hcloud.py @@ -171,9 +171,12 @@ plugin: hetzner.hcloud.hcloud # hcloud_image_id: 114690387 # hcloud_image_name: "debian-12" # hcloud_image_os_flavor: "debian" -## Datacenter -# hcloud_datacenter: "hel1-dc2" +## Location # hcloud_location: "hel1" +# # The hcloud_datacenter variable is deprecated and will be removed after 1 July 2026. +# # Please use the hcloud_location variable instead. +# # See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters. +# hcloud_datacenter: "hel1-dc2" ## Network # hcloud_ipv4: "65.109.140.95" # Value is optional! # hcloud_ipv6: "2a01:4f9:c011:b83f::1" # Value is optional! @@ -185,10 +188,11 @@ plugin: hetzner.hcloud.hcloud # name: "my-private-network" # ip: "10.0.0.3" # -hostname: "my-prefix-{{ hcloud_datacenter }}-{{ hcloud_name }}-{{ hcloud_server_type }}" +hostname: "my-prefix-{{ hcloud_location }}-{{ hcloud_name }}-{{ hcloud_server_type }}" """ import sys +import warnings from ipaddress import IPv6Network from ansible.errors import AnsibleError @@ -230,7 +234,7 @@ if sys.version_info >= (3, 11): architecture: str # Datacenter - datacenter: str + datacenter: str # Deprecated! location: str # Labels @@ -322,7 +326,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if self.get_option("locations"): locations: list[str] = self.get_option("locations") - servers = [s for s in servers if s.datacenter.location.name in locations] + servers = [s for s in servers if s.location.name in locations] if self.get_option("types"): server_types: list[str] = self.get_option("types") @@ -365,9 +369,16 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): server_dict["private_ipv4"] = private_net.ip break - # Datacenter - server_dict["datacenter"] = server.datacenter.name - server_dict["location"] = server.datacenter.location.name + # Location + server_dict["location"] = server.location.name + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=DeprecationWarning) + self.display.warning( + "The `hcloud_datacenter` variable is deprecated and will be removed " + "after 1 July 2026. Please use the `hcloud_location` variable instead. " + "See https://docs.hetzner.cloud/changelog#2025-12-16-phasing-out-datacenters." + ) + server_dict["datacenter"] = server.datacenter and server.datacenter.name # Image if server.image is not None: diff --git a/tests/unit/inventory/test_hcloud.py b/tests/unit/inventory/test_hcloud.py index 9c5b5c9..9e58903 100644 --- a/tests/unit/inventory/test_hcloud.py +++ b/tests/unit/inventory/test_hcloud.py @@ -33,18 +33,39 @@ def test_build_inventory_server(): "blocked": False, "dns_ptr": "static.1.0.0.127.clients.your-server.de", }, - "ipv6": {"id": 56583279, "ip": "2001:db8::/64", "blocked": False, "dns_ptr": []}, + "ipv6": { + "id": 56583279, + "ip": "2001:db8::/64", + "blocked": False, + "dns_ptr": [], + }, "floating_ips": [], "firewalls": [], }, "private_net": [], - "server_type": {"id": 109, "name": "cpx22", "architecture": "x86"}, + "server_type": { + "id": 109, + "name": "cpx22", + "architecture": "x86", + }, + "location": { + "id": 3, + "name": "hel1", + }, "datacenter": { "id": 3, "name": "hel1-dc2", - "location": {"id": 3, "name": "hel1"}, + "location": { + "id": 3, + "name": "hel1", + }, + }, + "image": { + "id": 114690387, + "name": "debian-12", + "os_flavor": "debian", + "os_version": "12", }, - "image": {"id": 114690387, "name": "debian-12", "os_flavor": "debian", "os_version": "12"}, }, ) # pylint: disable=protected-access