From 58aa521f682b7ab02b23077a1477dcd5df6b653c Mon Sep 17 00:00:00 2001
From: Felix Fontein <felix@fontein.de>
Date: Sat, 20 Apr 2024 10:28:39 +0200
Subject: [PATCH] Use pools or ranges depending on the package version.

---
 changelogs/fragments/8248-linode-rename.yml | 4 ++++
 plugins/inventory/linode.py                 | 9 ++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/fragments/8248-linode-rename.yml

diff --git a/changelogs/fragments/8248-linode-rename.yml b/changelogs/fragments/8248-linode-rename.yml
new file mode 100644
index 0000000000..507073b0b0
--- /dev/null
+++ b/changelogs/fragments/8248-linode-rename.yml
@@ -0,0 +1,4 @@
+bugfixes:
+  - "linode inventory plugin - linode_api4 renamed the IPv6 ``pools`` to ``ranges`` in version 5.4.1. The plugin now checks the package
+     version to use the appropriate name (https://github.com/ansible-collections/community.general/issues/8213,
+     https://github.com/ansible-collections/community.general/pull/8248)."
diff --git a/plugins/inventory/linode.py b/plugins/inventory/linode.py
index e161e086e5..6619bf15e4 100644
--- a/plugins/inventory/linode.py
+++ b/plugins/inventory/linode.py
@@ -124,10 +124,12 @@ from ansible.errors import AnsibleError
 from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
 
 from ansible_collections.community.general.plugins.plugin_utils.unsafe import make_unsafe
+from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
 
 
 try:
     from linode_api4 import LinodeClient
+    from linode_api4.linode_client import package_version
     from linode_api4.objects.linode import Instance
     from linode_api4.errors import ApiError as LinodeApiError
     HAS_LINODE = True
@@ -219,7 +221,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
             if ip_style == 'api':
                 ips = instance.ips.ipv4.public + instance.ips.ipv4.private
                 ips += [instance.ips.ipv6.slaac, instance.ips.ipv6.link_local]
-                ips += instance.ips.ipv6.pools
+                # linode_api4 renamed pools to ranges in 5.4.1 (https://github.com/linode/linode_api4-python/releases/tag/v5.4.1,
+                # https://github.com/linode/linode_api4-python/commit/dc592c706518abc1c6d4b4c88b074970e5375d5f)
+                if LooseVersion(package_version) < LooseVersion("5.4.1"):
+                    ips += instance.ips.ipv6.pools
+                else:
+                    ips += instance.ips.ipv6.ranges
 
                 for ip_type in set(ip.type for ip in ips):
                     self.inventory.set_variable(