diff --git a/lib/main/tests/inventory.py b/lib/main/tests/inventory.py index 54a7d34c06..428a99c8ad 100644 --- a/lib/main/tests/inventory.py +++ b/lib/main/tests/inventory.py @@ -216,6 +216,8 @@ class InventoryTest(BaseTest): ################################################## # GROUPS->inventories POST via subcollection + root_groups = '/api/v1/inventories/1/root_groups/' + url = '/api/v1/inventories/1/groups/' new_group_a = dict(name='web100') new_group_b = dict(name='web101') @@ -239,7 +241,11 @@ class InventoryTest(BaseTest): self.post(url5, data=new_group_d, expect=400, auth=self.get_other_credentials()) got = self.get(url5, expect=200, auth=self.get_other_credentials()) self.assertEquals(got['count'], 4) - + + # side check: see if root groups URL is operational. These are groups without parents. + root_groups = self.get(root_groups, expect=200, auth=self.get_super_credentials()) + self.assertEquals(root_groups['count'], 2) + remove_me = added_by_collection remove_me['disassociate'] = 1 self.post(url5, data=remove_me, expect=204, auth=self.get_other_credentials()) diff --git a/lib/main/urls.py b/lib/main/urls.py index a4dce23702..93b58a9440 100644 --- a/lib/main/urls.py +++ b/lib/main/urls.py @@ -74,6 +74,7 @@ inventory_urls = patterns('lib.main.views', url(r'^(?P[0-9]+)/$', 'inventory_detail'), url(r'^(?P[0-9]+)/hosts/$', 'inventory_hosts_list'), url(r'^(?P[0-9]+)/groups/$', 'inventory_groups_list'), + url(r'^(?P[0-9]+)/root_groups/$', 'inventory_root_groups_list'), ) hosts_urls = patterns('lib.main.views', diff --git a/lib/main/views.py b/lib/main/views.py index f24520ffcd..e40576179d 100644 --- a/lib/main/views.py +++ b/lib/main/views.py @@ -876,6 +876,25 @@ class InventoryGroupsList(BaseSubList): # FIXME: verify that you can can_read permission on the inventory is required return base +class InventoryRootGroupsList(BaseSubList): + + model = Group + serializer_class = GroupSerializer + permission_classes = (CustomRbac,) + parent_model = Inventory + relationship = 'groups' + postable = True + inject_primary_key_on_post_as = 'inventory' + severable = False + filter_fields = ('name',) + + def _get_queryset(self): + inventory = Inventory.objects.get(pk=self.kwargs['pk']) + base = inventory.groups + all_ids = base.values_list('id', flat=True) + base.exclude(parents__id__in = all_ids) + return base + class GroupsVariableDetail(VariableBaseDetail): model = VariableData