From 109988da13f5e82d700cd2907542df1f57414c27 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Wed, 3 Dec 2014 18:35:33 -0500 Subject: [PATCH] Fix typo, modify can_update to prevent inventory update from even starting when source script is missing. --- awx/main/access.py | 2 +- awx/main/models/inventory.py | 5 ++++- awx/main/tests/inventory.py | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/awx/main/access.py b/awx/main/access.py index f1985cf7e8..28e0e4ecf6 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -1472,7 +1472,7 @@ class CustomInventoryScriptAccess(BaseAccess): if self.user.is_superuser: return True if not self.active: - return Flase + return False return bool(obj.organization in self.user.organizations.all() or obj.organization in self.user.admin_of_organizations.all()) def can_add(self, data): diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 0743579c8c..b2ed06da14 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -1140,7 +1140,10 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions): return reverse('api:inventory_source_detail', args=(self.pk,)) def _can_update(self): - return bool(self.source in CLOUD_INVENTORY_SOURCES) + if self.source == 'custom': + return bool(self.source_script and self.source_script.active) + else: + return bool(self.source in CLOUD_INVENTORY_SOURCES) def create_inventory_update(self, **kwargs): return self.create_unified_job(**kwargs) diff --git a/awx/main/tests/inventory.py b/awx/main/tests/inventory.py index 8f349f96b9..46c743520e 100644 --- a/awx/main/tests/inventory.py +++ b/awx/main/tests/inventory.py @@ -1116,7 +1116,10 @@ class InventoryUpdatesTest(BaseTransactionTest): should_error = kwargs.pop('should_error', False) if not inventory_update: inventory_update = inventory_source.update(**kwargs) - self.assertTrue(inventory_update) + if not should_fail and not should_error: + self.assertTrue(inventory_update) + elif not inventory_update: + return None inventory_update = InventoryUpdate.objects.get(pk=inventory_update.pk) #print inventory_update.result_stdout if should_error: @@ -1746,7 +1749,7 @@ class InventoryUpdatesTest(BaseTransactionTest): # Create the inventory script self.create_test_license_file() inventory_scripts = reverse('api:inventory_script_list') - new_script = dict(name="Test", description="Test Script", script=TEST_SIMPLE_INVENTORY_SCRIPT) + new_script = dict(name="Test", description="Test Script", script=TEST_SIMPLE_INVENTORY_SCRIPT, organization=self.organization.id) script_data = self.post(inventory_scripts, data=new_script, expect=201, auth=self.get_super_credentials()) custom_inv = self.organization.inventories.create(name='Custom Script Inventory') @@ -1762,3 +1765,11 @@ class InventoryUpdatesTest(BaseTransactionTest): with self.current_user(self.super_django_user): response = self.put(custom_inv_src, inv_src_opts, expect=200) self.check_inventory_source(custom_group.inventory_source) + + # Delete script, verify that update fails. + inventory_source = InventorySource.objects.get(pk=custom_group.inventory_source.pk) + self.assertTrue(inventory_source.can_update) + self.delete(script_data['url'], expect=204, auth=self.get_super_credentials()) + inventory_source = InventorySource.objects.get(pk=inventory_source.pk) + self.assertFalse(inventory_source.can_update) + self.check_inventory_update(inventory_source, should_fail=True)