This commit is contained in:
Mark J. Horninger 2024-03-10 21:42:38 -04:00
parent c9ef2888f5
commit 904141b107
3 changed files with 34 additions and 0 deletions

View File

@ -93,6 +93,7 @@ from ansible.utils.display import Display
# Specific for the NetbirdAPI Class
import json
import re
try:
import requests
@ -286,6 +287,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)

View File

@ -9,3 +9,4 @@ groups:
strict: No
keyed_groups:
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 '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