diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml index 8fa85ce..314ef62 100644 --- a/.github/ISSUE_TEMPLATE/bug.yml +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -28,23 +28,23 @@ body: attributes: label: Package Version description: What version of our Package are you running? Please be as specific as possible - placeholder: 2.0.0 + placeholder: 0.1.5 validations: required: true - type: input - id: php-version + id: python-version attributes: - label: PHP Version - description: What version of PHP are you running? Please be as specific as possible - placeholder: 8.2.0 + label: Python Version + description: What version of Python are you running? Please be as specific as possible + placeholder: 3.10.12 validations: required: true - type: input - id: laravel-version + id: ansible-version attributes: - label: Laravel Version + label: Ansible Version description: What version of Laravel are you running? Please be as specific as possible - placeholder: 9.0.0 + placeholder: 2.16.4 validations: required: true - type: dropdown diff --git a/galaxy.yml b/galaxy.yml index fe90f5e..a36ae81 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -8,7 +8,7 @@ namespace: dominion_solutions name: netbird # The version of the collection. Must be compatible with semantic versioning -version: 0.1.4 +version: 0.1.5 # The path to the Markdown (.md) readme file. This path is relative to the root of the collection readme: README.md diff --git a/plugins/inventory/netbird.py b/plugins/inventory/netbird.py index de607e5..3eead23 100644 --- a/plugins/inventory/netbird.py +++ b/plugins/inventory/netbird.py @@ -93,6 +93,7 @@ from ansible.utils.display import Display # Specific for the NetbirdAPI Class import json +import re try: import requests @@ -148,7 +149,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): def _get_peer_inventory(self): """Get the inventory from the Netbird API""" - self.peers = self.client.ListPeers() + try: + self.peers = self.client.ListPeers() + except Exception as e: + raise AnsibleError(f"Could not retrieve the Netbird inventory: {e}") def _filter_by_config(self): """Filter peers by user specified configuration.""" @@ -286,6 +290,12 @@ class NetbirdApi: } peers = [] response = requests.request("GET", url, headers=headers) + if response.status_code in [401]: + raise ConnectionRefusedError(f"{response.status_code}: {response.text}\nPlease check the API Key and URL.") + + elif re.match('4\\d\\d', response.status_code): + raise ConnectionError(f"{response.status_code}: {response.text}\nPlease check the API Key and URL.") + peer_json = json.loads(response.text) for current_peer_map in peer_json: current_peer = Peer(current_peer_map["hostname"], current_peer_map['dns_label'], current_peer_map["id"], current_peer_map) diff --git a/tests/unit/module_utils/inventories/fixtures/ip_address.netbird.yml b/tests/unit/module_utils/inventories/fixtures/ip_address.netbird.yml new file mode 100644 index 0000000..4206ae4 --- /dev/null +++ b/tests/unit/module_utils/inventories/fixtures/ip_address.netbird.yml @@ -0,0 +1,13 @@ +--- +plugin: dominion_solutions.netbird.netbird +api_key: nbp_this_is_a_fake_api_key +api_url: https://netbird.example.com/api/v1 +ip_style: plain +strict: No +netbird_connected: No +netbird_groups: +groups: +keyed_groups: +compose: + ansible_ssh_host: ip + ansible_ssh_port: 22 diff --git a/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml b/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml index 814a64b..3fb5665 100644 --- a/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml +++ b/tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml @@ -9,3 +9,4 @@ groups: strict: No keyed_groups: compose: + ansible_ssh_host: ip diff --git a/tests/unit/plugins/inventory/test_netbird.py b/tests/unit/plugins/inventory/test_netbird.py index bf33704..8ac4ad2 100644 --- a/tests/unit/plugins/inventory/test_netbird.py +++ b/tests/unit/plugins/inventory/test_netbird.py @@ -111,3 +111,29 @@ def test_with_multiple_groups(inventory, netbird_api_multigroup): assert inventory.inventory.groups is not None assert 'All' in inventory.inventory.groups assert 'Development' in inventory.inventory.groups + + +def test_with_multiple_groups(inventory, netbird_api_multigroup): + loader = DataLoader() + path = 'tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml' + inventory._build_client = MagicMock() + inventory.client = netbird_api_multigroup + inventory.parse(InventoryData(), loader, path, False) + assert inventory.inventory is not None + assert inventory.inventory.hosts is not None + assert inventory.inventory.groups is not None + assert 'All' in inventory.inventory.groups + assert 'Development' in inventory.inventory.groups + + +def test_use_ip_address(inventory, netbird_api_multigroup): + loader = DataLoader() + path = 'tests/unit/module_utils/inventories/fixtures/ip_address.netbird.yml' + inventory._build_client = MagicMock() + inventory.client = netbird_api_multigroup + inventory.parse(InventoryData(), loader, path, False) + assert inventory.inventory is not None + assert inventory.inventory.hosts is not None + assert inventory.inventory.groups is not None + assert 'All' in inventory.inventory.groups + assert 'Development' in inventory.inventory.groups