Skip to content

Commit b16c650

Browse files
authored
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
1 parent c9ef288 commit b16c650

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

.github/ISSUE_TEMPLATE/bug.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ body:
2828
attributes:
2929
label: Package Version
3030
description: What version of our Package are you running? Please be as specific as possible
31-
placeholder: 2.0.0
31+
placeholder: 0.1.5
3232
validations:
3333
required: true
3434
- type: input
35-
id: php-version
35+
id: python-version
3636
attributes:
37-
label: PHP Version
38-
description: What version of PHP are you running? Please be as specific as possible
39-
placeholder: 8.2.0
37+
label: Python Version
38+
description: What version of Python are you running? Please be as specific as possible
39+
placeholder: 3.10.12
4040
validations:
4141
required: true
4242
- type: input
43-
id: laravel-version
43+
id: ansible-version
4444
attributes:
45-
label: Laravel Version
45+
label: Ansible Version
4646
description: What version of Laravel are you running? Please be as specific as possible
47-
placeholder: 9.0.0
47+
placeholder: 2.16.4
4848
validations:
4949
required: true
5050
- type: dropdown

galaxy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace: dominion_solutions
88
name: netbird
99

1010
# The version of the collection. Must be compatible with semantic versioning
11-
version: 0.1.4
11+
version: 0.1.5
1212

1313
# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
1414
readme: README.md

plugins/inventory/netbird.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393

9494
# Specific for the NetbirdAPI Class
9595
import json
96+
import re
9697

9798
try:
9899
import requests
@@ -148,7 +149,10 @@ def _add_peers_to_group(self):
148149

149150
def _get_peer_inventory(self):
150151
"""Get the inventory from the Netbird API"""
151-
self.peers = self.client.ListPeers()
152+
try:
153+
self.peers = self.client.ListPeers()
154+
except Exception as e:
155+
raise AnsibleError(f"Could not retrieve the Netbird inventory: {e}")
152156

153157
def _filter_by_config(self):
154158
"""Filter peers by user specified configuration."""
@@ -286,6 +290,12 @@ def ListPeers(self):
286290
}
287291
peers = []
288292
response = requests.request("GET", url, headers=headers)
293+
if response.status_code in [401]:
294+
raise ConnectionRefusedError(f"{response.status_code}: {response.text}\nPlease check the API Key and URL.")
295+
296+
elif re.match('4\\d\\d', response.status_code):
297+
raise ConnectionError(f"{response.status_code}: {response.text}\nPlease check the API Key and URL.")
298+
289299
peer_json = json.loads(response.text)
290300
for current_peer_map in peer_json:
291301
current_peer = Peer(current_peer_map["hostname"], current_peer_map['dns_label'], current_peer_map["id"], current_peer_map)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
plugin: dominion_solutions.netbird.netbird
3+
api_key: nbp_this_is_a_fake_api_key
4+
api_url: https://netbird.example.com/api/v1
5+
ip_style: plain
6+
strict: No
7+
netbird_connected: No
8+
netbird_groups:
9+
groups:
10+
keyed_groups:
11+
compose:
12+
ansible_ssh_host: ip
13+
ansible_ssh_port: 22

tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ groups:
99
strict: No
1010
keyed_groups:
1111
compose:
12+
ansible_ssh_host: ip

tests/unit/plugins/inventory/test_netbird.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,29 @@ def test_with_multiple_groups(inventory, netbird_api_multigroup):
111111
assert inventory.inventory.groups is not None
112112
assert 'All' in inventory.inventory.groups
113113
assert 'Development' in inventory.inventory.groups
114+
115+
116+
def test_with_multiple_groups(inventory, netbird_api_multigroup):
117+
loader = DataLoader()
118+
path = 'tests/unit/module_utils/inventories/fixtures/only_connected.netbird.yml'
119+
inventory._build_client = MagicMock()
120+
inventory.client = netbird_api_multigroup
121+
inventory.parse(InventoryData(), loader, path, False)
122+
assert inventory.inventory is not None
123+
assert inventory.inventory.hosts is not None
124+
assert inventory.inventory.groups is not None
125+
assert 'All' in inventory.inventory.groups
126+
assert 'Development' in inventory.inventory.groups
127+
128+
129+
def test_use_ip_address(inventory, netbird_api_multigroup):
130+
loader = DataLoader()
131+
path = 'tests/unit/module_utils/inventories/fixtures/ip_address.netbird.yml'
132+
inventory._build_client = MagicMock()
133+
inventory.client = netbird_api_multigroup
134+
inventory.parse(InventoryData(), loader, path, False)
135+
assert inventory.inventory is not None
136+
assert inventory.inventory.hosts is not None
137+
assert inventory.inventory.groups is not None
138+
assert 'All' in inventory.inventory.groups
139+
assert 'Development' in inventory.inventory.groups

0 commit comments

Comments
 (0)