diff --git a/.gitignore b/.gitignore index f952889..6f72014 100644 --- a/.gitignore +++ b/.gitignore @@ -193,5 +193,3 @@ pip-selfcheck.json # http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ # End of https://www.toptal.com/developers/gitignore/api/ansible,python,venv,virtualenv - -!test/lib/ansible_test/data/requirements/ diff --git a/plugins/inventory/netbird.py b/plugins/inventory/netbird.py index f5db27c..f0a963a 100644 --- a/plugins/inventory/netbird.py +++ b/plugins/inventory/netbird.py @@ -6,6 +6,7 @@ # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) + __metaclass__ = type DOCUMENTATION = r""" @@ -66,17 +67,19 @@ DOCUMENTATION = r""" EXAMPLES = r""" """ + from ansible.errors import AnsibleError -# TODO: Re-enable if necessary -# , AnsibleParserError -from ansible.utils.display import Display + from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable # Specific for the NetbirdAPI Class -import requests +try: + import requests # import json - -display = Display() +except ImportError: + HAS_NETBIRD_API_LIBS = False +else: + HAS_NETBIRD_API_LIBS = True class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): @@ -85,19 +88,19 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def _build_client(self, loader): """Build the Netbird API Client""" - access_token = self.get_option('api_key') + api_key = self.get_option('api_key') api_url = self.get_option('api_url') if self.templar.is_template(access_token): access_token = self.templar.template(access_token) if self.templar.is_template(api_url): api_url = self.templar.template(api_url) - if access_token is None: + if api_key is None: raise AnsibleError("Could not retrieve the Netbird API Key from the configuration sources.") if api_url is None: raise AnsibleError("Could not retrieve the Netbird API URL from the configuration sources.") - self.client = NetbirdApi(access_token, api_url) + self.client = NetbirdApi(api_key, api_url) def _get_peer_inventory(self): """Get the inventory from the Netbird API""" @@ -106,6 +109,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def parse(self, inventory, loader, path, cache=True): """Dynamically parse the inventory from the Netbird API""" super(InventoryModule, self).parse(inventory, loader, path) + if not HAS_NETBIRD_API_LIBS: + raise AnsibleError("the Netbird Dynamic inventory requires Requests.") + self.peers = None self._read_config_data(path) diff --git a/test/lib/ansible_test/data/requirements/sanity.import-plugins.txt b/test/lib/ansible_test/data/requirements/sanity.import-plugins.txt deleted file mode 100644 index 661d8e7..0000000 --- a/test/lib/ansible_test/data/requirements/sanity.import-plugins.txt +++ /dev/null @@ -1,11 +0,0 @@ -ansible>=9.2.0 -cffi==1.16.0 -cryptography==42.0.2 -Jinja2==3.1.3 -MarkupSafe==2.1.5 -packaging==23.2 -pycparser==2.21 -PyYAML==6.0.1 -requests>=2.31.0 -resolvelib==1.0.1 -pytest==8.0.0 diff --git a/tests/unit/plugins/inventory/test_netbird.py b/tests/unit/plugins/inventory/test_netbird.py index 3bc96ee..41c7fee 100644 --- a/tests/unit/plugins/inventory/test_netbird.py +++ b/tests/unit/plugins/inventory/test_netbird.py @@ -25,7 +25,7 @@ def inventory(): def test_missing_access_token_lookup(inventory): loader = DataLoader() - inventory._options = {'api_key': None} + inventory._options = {'api_key': None, 'api_url': None} with pytest.raises(AnsibleError) as error_message: inventory._build_client(loader) assert 'Could not retrieve Netbird access token' in error_message