Mjh/14/error out on bad credentials (#23)

* Testing

* Added a fixture that I was prevoiusly missing

* Wrapped as an AnsibleException

* Rolled Version Number

* Going for the tri-fecta.  Fixing #20, too
This commit is contained in:
Mark Horninger 2024-03-10 22:02:10 -04:00 committed by GitHub
parent c9ef2888f5
commit b16c650525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 60 additions and 10 deletions

View File

@ -28,23 +28,23 @@ body:
attributes: attributes:
label: Package Version label: Package Version
description: What version of our Package are you running? Please be as specific as possible description: What version of our Package are you running? Please be as specific as possible
placeholder: 2.0.0 placeholder: 0.1.5
validations: validations:
required: true required: true
- type: input - type: input
id: php-version id: python-version
attributes: attributes:
label: PHP Version label: Python Version
description: What version of PHP are you running? Please be as specific as possible description: What version of Python are you running? Please be as specific as possible
placeholder: 8.2.0 placeholder: 3.10.12
validations: validations:
required: true required: true
- type: input - type: input
id: laravel-version id: ansible-version
attributes: attributes:
label: Laravel Version label: Ansible Version
description: What version of Laravel are you running? Please be as specific as possible description: What version of Laravel are you running? Please be as specific as possible
placeholder: 9.0.0 placeholder: 2.16.4
validations: validations:
required: true required: true
- type: dropdown - type: dropdown

View File

@ -8,7 +8,7 @@ namespace: dominion_solutions
name: netbird name: netbird
# The version of the collection. Must be compatible with semantic versioning # 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 # The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md readme: README.md

View File

@ -93,6 +93,7 @@ from ansible.utils.display import Display
# Specific for the NetbirdAPI Class # Specific for the NetbirdAPI Class
import json import json
import re
try: try:
import requests import requests
@ -148,7 +149,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _get_peer_inventory(self): def _get_peer_inventory(self):
"""Get the inventory from the Netbird API""" """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): def _filter_by_config(self):
"""Filter peers by user specified configuration.""" """Filter peers by user specified configuration."""
@ -286,6 +290,12 @@ class NetbirdApi:
} }
peers = [] peers = []
response = requests.request("GET", url, headers=headers) 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) peer_json = json.loads(response.text)
for current_peer_map in peer_json: 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) current_peer = Peer(current_peer_map["hostname"], current_peer_map['dns_label'], current_peer_map["id"], current_peer_map)

View File

@ -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

View File

@ -9,3 +9,4 @@ groups:
strict: No strict: No
keyed_groups: keyed_groups:
compose: compose:
ansible_ssh_host: ip

View File

@ -111,3 +111,29 @@ def test_with_multiple_groups(inventory, netbird_api_multigroup):
assert inventory.inventory.groups is not None assert inventory.inventory.groups is not None
assert 'All' in inventory.inventory.groups assert 'All' in inventory.inventory.groups
assert 'Development' 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