Skip to content

Commit 4b829fa

Browse files
authored
Fixes #28 - Tested with a separate inventory. (#29)
1 parent 8959042 commit 4b829fa

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

plugins/inventory/netbird.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@
5959
description: Whether or not to fail if a group or variable is not found.
6060
compose:
6161
description: compose variables for Ansible based on jinja2 expression and inventory vars
62-
default: False
6362
required: False
64-
type: boolean
63+
type: dict
6564
keyed_groups:
6665
description: create groups for plugins based on variable values and add the corresponding hosts to it
6766
type: list
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[
2+
{
3+
"accessible_peers_count": 1,
4+
"approval_required": false,
5+
"city_name": "",
6+
"connected": false,
7+
"connection_ip": "",
8+
"country_code": "",
9+
"dns_label": "apple.netbird.cloud",
10+
"geoname_id": 0,
11+
"groups": [
12+
{
13+
"id": "3aBcD4eF5gHiJ6kLmNoP",
14+
"name": "All",
15+
"peers_count": 2
16+
},
17+
{
18+
"id": "2j3k4l5m6n7o8p9q0r1",
19+
"name": "Test Group With Spaces",
20+
"peers_count": 1
21+
}
22+
],
23+
"hostname": "apple",
24+
"id": "2j3k4l5m6n7o8p9q0r1",
25+
"ip": "10.10.10.123",
26+
"kernel_version": "",
27+
"last_login": "2024-02-10T22:01:27.744131502Z",
28+
"last_seen": "2024-02-11T03:21:42.202104672Z",
29+
"login_expiration_enabled": true,
30+
"login_expired": false,
31+
"name": "apple",
32+
"os": "Linux Mint 21.3",
33+
"ssh_enabled": false,
34+
"ui_version": "netbird-desktop-ui/0.25.7",
35+
"user_id": "auth0|abc123xyz4567890defg",
36+
"version": "0.25.7"
37+
},
38+
{
39+
"accessible_peers_count": 1,
40+
"approval_required": false,
41+
"city_name": "New York",
42+
"connected": true,
43+
"connection_ip": "146.123.45.67",
44+
"country_code": "US",
45+
"dns_label": "banana.netbird.cloud",
46+
"geoname_id": 1234567,
47+
"groups": [
48+
{
49+
"id": "2j3k4l5m6n7o8p9q0r1",
50+
"name": "Development",
51+
"peers_count": 1
52+
},
53+
{
54+
"id": "3aBcD4eF5gHiJ6kLmNoP",
55+
"name": "All",
56+
"peers_count": 2
57+
}
58+
],
59+
"hostname": "banana",
60+
"id": "hkwJPXNUmGywCLo5S8Wg",
61+
"ip": "10.10.10.124",
62+
"kernel_version": "",
63+
"last_login": "2024-02-02T11:20:05.934889112Z",
64+
"last_seen": "2024-02-24T02:59:35.324496386Z",
65+
"login_expiration_enabled": false,
66+
"login_expired": false,
67+
"name": "docker-manager",
68+
"os": "Alpine Linux 3.19.1",
69+
"ssh_enabled": false,
70+
"ui_version": "",
71+
"user_id": "",
72+
"version": "0.25.5"
73+
}
74+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
netbird_connected: False
7+
leading_separator: No
8+
netbird_groups:
9+
- "Test Group With Spaces"
10+
groups:
11+
connected: connected
12+
ssh_hosts: ssh_enabled
13+
strict: No
14+
keyed_groups:
15+
16+
compose:
17+
ansible_ssh_host: label
18+
ansible_ssh_port: 22

tests/unit/plugins/inventory/test_netbird.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ def netbird_api_multigroup():
5858
return mock_netbird_api
5959

6060

61+
@pytest.fixture(scope="module")
62+
def netbird_api_spaces_in_group():
63+
mock_netbird_api = NetbirdApi(None, None)
64+
response_data = []
65+
with open('tests/unit/module_utils/inventories/fixtures/peers_spaces_in_group.json') as peers_file:
66+
peers_map = json.load(peers_file)
67+
for data in peers_map:
68+
response_data.append(Peer(data['hostname'], data['dns_label'], data['id'], data))
69+
70+
mock_netbird_api.ListPeers = MagicMock(return_value=response_data)
71+
72+
return mock_netbird_api
73+
74+
6175
def test_missing_access_token_lookup(inventory):
6276
loader = DataLoader()
6377
inventory._options = {'api_key': None, 'api_url': None}
@@ -137,3 +151,16 @@ def test_use_ip_address(inventory, netbird_api_multigroup):
137151
assert inventory.inventory.groups is not None
138152
assert 'All' in inventory.inventory.groups
139153
assert 'Development' in inventory.inventory.groups
154+
155+
156+
def test_use_group_with_spaces(inventory, netbird_api_spaces_in_group):
157+
loader = DataLoader()
158+
path = 'tests/unit/module_utils/inventories/fixtures/spaces_in_group.netbird.yml'
159+
inventory._build_client = MagicMock()
160+
inventory.client = netbird_api_spaces_in_group
161+
inventory.parse(InventoryData(), loader, path, False)
162+
assert inventory.inventory is not None
163+
assert inventory.inventory.hosts is not None
164+
assert inventory.inventory.groups is not None
165+
assert 'All' in inventory.inventory.groups
166+
assert 'Test Group With Spaces' in inventory.inventory.groups

0 commit comments

Comments
 (0)