Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes graphql inventory grouping by tags #228

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions plugins/inventory/gql_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ def create_groups(self, device):
self.display.display(f"Could not find value for {parent_attr} on device {device_name}")
continue

if parent_attr == "tags":
if not chain or len(chain) > 1:
self.display.display(f"Tags must be grouped by name or slug. {group_by_path} is not a valid path.")
continue
self.create_tag_groups(device, chain[0])
continue

if not chain:
group_name = device_attr

Expand Down Expand Up @@ -312,6 +319,17 @@ def create_groups(self, device):
f"Groups must be a string. {group_name} is not a string. Please make sure your group_by path specified resolves to a string value."
)

def create_tag_groups(self, device, tag_attr):
"""Create groups based on tags."""
device_name = device["name"]
for tag in device.get("tags", []):
if tag.get(tag_attr):
group_name = f"tags_{tag[tag_attr]}"
group = self.inventory.add_group(group_name)
self.inventory.add_child(group, device_name)
else:
self.display.display(f"Could not find value for tags.{tag_attr} on device {device_name}")

def main(self):
"""Main function."""
if not HAS_NETUTILS:
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/inventory/test_data/graphql_groups/device_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@
"tenant": {
"slug": "mytenant",
"type": "local"
}
},
"tags": [
{
"name": "MyTag",
"slug": "mytag"
},
{
"name": "MyTag2",
"slug": "mytag2"
}
]
}
43 changes: 43 additions & 0 deletions tests/unit/inventory/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,46 @@ def test_ansible_platform(inventory_fixture, device_data):
inventory_fixture.add_ansible_platform(device_data)
mydevice_host = inventory_fixture.inventory.get_host("mydevice")
assert mydevice_host.vars.get("ansible_network_os") == "cisco.asa.asa"


def test_ansible_group_by_tags_slug(inventory_fixture, device_data):
inventory_fixture.group_by = ["tags.slug"]
inventory_fixture.create_groups(device_data)
inventory_groups = list(inventory_fixture.inventory.groups.keys())
mytag_inventory_hosts = inventory_fixture.inventory.get_groups_dict().get("tags_mytag")
mytag2_inventory_hosts = inventory_fixture.inventory.get_groups_dict().get("tags_mytag2")
assert ["all", "ungrouped", "tags_mytag", "tags_mytag2"] == inventory_groups
assert ["mydevice"] == mytag_inventory_hosts
assert ["mydevice"] == mytag2_inventory_hosts


def test_ansible_group_by_tags_name(inventory_fixture, device_data):
inventory_fixture.group_by = ["tags.name"]
inventory_fixture.create_groups(device_data)
inventory_groups = list(inventory_fixture.inventory.groups.keys())
mytag_inventory_hosts = inventory_fixture.inventory.get_groups_dict().get("tags_MyTag")
mytag2_inventory_hosts = inventory_fixture.inventory.get_groups_dict().get("tags_MyTag2")
assert ["all", "ungrouped", "tags_MyTag", "tags_MyTag2"] == inventory_groups
assert ["mydevice"] == mytag_inventory_hosts
assert ["mydevice"] == mytag2_inventory_hosts


@patch.object(Display, "display")
def test_ansible_group_by_tags_invalid(mock_display, inventory_fixture, device_data):
inventory_fixture.group_by = ["tags"]
inventory_fixture.create_groups(device_data)
mock_display.assert_any_call("Tags must be grouped by name or slug. tags is not a valid path.")


@patch.object(Display, "display")
def test_ansible_group_by_tags_invalid_path(mock_display, inventory_fixture, device_data):
inventory_fixture.group_by = ["tags.foo"]
inventory_fixture.create_groups(device_data)
mock_display.assert_any_call("Could not find value for tags.foo on device mydevice")


@patch.object(Display, "display")
def test_ansible_group_by_tags_invalid_nested_path(mock_display, inventory_fixture, device_data):
inventory_fixture.group_by = ["tags.slug.name"]
inventory_fixture.create_groups(device_data)
mock_display.assert_any_call("Tags must be grouped by name or slug. tags.slug.name is not a valid path.")