From 6773fd19d77f0ebc8859fdb651a0ea33defdf605 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Fri, 6 Jun 2014 15:21:09 -0400 Subject: [PATCH] Implement passive copying as a new host to a group requires that the variables isn't special on the new host implements AC-1265 --- awx/api/views.py | 11 +++++++++++ awx/main/tests/inventory.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/awx/api/views.py b/awx/api/views.py index 8689471152..8efbde63b7 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -821,6 +821,17 @@ class GroupHostsList(SubListCreateAPIView): parent_model = Group relationship = 'hosts' + def create(self, request, *args, **kwargs): + parent_group = Group.objects.get(id=self.kwargs['pk']) + existing_hosts = Host.objects.filter(inventory=parent_group.inventory, name=request.DATA['name']) + if existing_hosts.count() > 0 and ('variables' not in request.DATA or \ + request.DATA['variables'] == '' or \ + request.DATA['variables'] == '{}' or \ + request.DATA['variables'] == '---'): + request.DATA['id'] = existing_hosts[0].id + return self.attach(request, *args, **kwargs) + return super(GroupHostsList, self).create(request, *args, **kwargs) + class GroupAllHostsList(SubListAPIView): ''' the list of all hosts below a group, even including subgroups ''' diff --git a/awx/main/tests/inventory.py b/awx/main/tests/inventory.py index f2c8fe4334..c4f78174e3 100644 --- a/awx/main/tests/inventory.py +++ b/awx/main/tests/inventory.py @@ -572,6 +572,9 @@ class InventoryTest(BaseTest): # access url1 = reverse('api:group_hosts_list', args=(groups[0].pk,)) + alt_group_hosts = reverse('api:group_hosts_list', args=(groups[1].pk,)) + other_alt_group_hosts = reverse('api:group_hosts_list', args(groups[2].pk,)) + data = self.get(url1, expect=200, auth=self.get_normal_credentials()) self.assertEquals(data['count'], 2) self.assertTrue(host1.pk in [x['id'] for x in data['results']]) @@ -593,6 +596,14 @@ class InventoryTest(BaseTest): data = self.get(url1, expect=200, auth=self.get_normal_credentials()) self.assertEquals(data['count'], 4) + # You should be able to add an existing host to a group as a new host and have it be copied + existing_host = new_host + self.post(alt_group_hosts, data=existing_host, expect=204, auth=self.get_normal_credentials()) + + # Not if the variables are different though + existing_host['variables'] = '{"booh": "bah"}' + self.post(other_alt_group_hosts, data=existing_host, expect=400, auth=self.get_normal_credentials()) + # removal got['disassociate'] = 1 posted = self.post(url1, data=got, expect=204, auth=self.get_normal_credentials())