From 1e999ee3c835ba9e5282e2a95c383aa27916d494 Mon Sep 17 00:00:00 2001 From: Mattias Loverot Date: Wed, 6 Mar 2024 10:46:26 +0100 Subject: [PATCH] Dont extract null values from custom fields (nb_inventory) --- plugins/inventory/nb_inventory.py | 6 +++++- .../test_data/extract_custom_fields/data.json | 21 +++++++++++++++++++ tests/unit/inventory/test_nb_inventory.py | 11 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/unit/inventory/test_data/extract_custom_fields/data.json diff --git a/plugins/inventory/nb_inventory.py b/plugins/inventory/nb_inventory.py index da9f5ed72..66a21ad96 100644 --- a/plugins/inventory/nb_inventory.py +++ b/plugins/inventory/nb_inventory.py @@ -867,7 +867,11 @@ def extract_interfaces(self, host): def extract_custom_fields(self, host): try: - return host["custom_fields"] + return { + key: value + for key, value in host["custom_fields"].items() + if value is not None + } except Exception: return diff --git a/tests/unit/inventory/test_data/extract_custom_fields/data.json b/tests/unit/inventory/test_data/extract_custom_fields/data.json new file mode 100644 index 000000000..cb63c576e --- /dev/null +++ b/tests/unit/inventory/test_data/extract_custom_fields/data.json @@ -0,0 +1,21 @@ +[ + { + "custom_fields": { + "a_text_value": "value1", + "a_bool_value": false + }, + "expected": { + "a_text_value": "value1", + "a_bool_value": false + } + }, + { + "custom_fields": { + "an_unset_value": null, + "a_set_value": false + }, + "expected": { + "a_set_value": false + } + } +] diff --git a/tests/unit/inventory/test_nb_inventory.py b/tests/unit/inventory/test_nb_inventory.py index 9adfc3e3e..998c5a737 100644 --- a/tests/unit/inventory/test_nb_inventory.py +++ b/tests/unit/inventory/test_nb_inventory.py @@ -246,3 +246,14 @@ def test_new_token(inventory_fixture, templar_fixture): assert "Authorization" in inventory_fixture.headers assert inventory_fixture.headers["Authorization"] == "Foo bar" + + +@pytest.mark.parametrize( + "custom_fields, expected", load_relative_test_data("extract_custom_fields") +) +def test_extract_custom_fields(inventory_fixture, custom_fields, expected): + extracted_custom_fields = inventory_fixture.extract_custom_fields( + {"custom_fields": custom_fields} + ) + + assert extracted_custom_fields == expected