Skip to content

Commit 4c3065f

Browse files
authored
Dont extract null values from custom fields (nb_inventory) (#1184)
1 parent 8a2f8b4 commit 4c3065f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,11 @@ def extract_interfaces(self, host):
899899

900900
def extract_custom_fields(self, host):
901901
try:
902-
return host["custom_fields"]
902+
return {
903+
key: value
904+
for key, value in host["custom_fields"].items()
905+
if value is not None
906+
}
903907
except Exception:
904908
return
905909

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"custom_fields": {
4+
"a_text_value": "value1",
5+
"a_bool_value": false
6+
},
7+
"expected": {
8+
"a_text_value": "value1",
9+
"a_bool_value": false
10+
}
11+
},
12+
{
13+
"custom_fields": {
14+
"an_unset_value": null,
15+
"a_set_value": false
16+
},
17+
"expected": {
18+
"a_set_value": false
19+
}
20+
}
21+
]

tests/unit/inventory/test_nb_inventory.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,14 @@ def test_new_token(inventory_fixture, templar_fixture):
249249

250250
assert "Authorization" in inventory_fixture.headers
251251
assert inventory_fixture.headers["Authorization"] == "Foo bar"
252+
253+
254+
@pytest.mark.parametrize(
255+
"custom_fields, expected", load_relative_test_data("extract_custom_fields")
256+
)
257+
def test_extract_custom_fields(inventory_fixture, custom_fields, expected):
258+
extracted_custom_fields = inventory_fixture.extract_custom_fields(
259+
{"custom_fields": custom_fields}
260+
)
261+
262+
assert extracted_custom_fields == expected

0 commit comments

Comments
 (0)