From 2dcc7ec7493883ce9dbc3b0c51761fdec767cee1 Mon Sep 17 00:00:00 2001 From: Sarabraj Singh Date: Wed, 28 Sep 2022 10:53:17 -0400 Subject: [PATCH] implementing Alan's recommendations for ig_fallback --- awx/main/models/ad_hoc_commands.py | 2 +- awx/main/models/inventory.py | 2 +- awx/main/models/jobs.py | 2 +- awx/main/tests/functional/test_copy.py | 5 +++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/awx/main/models/ad_hoc_commands.py b/awx/main/models/ad_hoc_commands.py index dfb5993e3a..3b71119031 100644 --- a/awx/main/models/ad_hoc_commands.py +++ b/awx/main/models/ad_hoc_commands.py @@ -232,7 +232,7 @@ class AdHocCommand(UnifiedJob, JobNotificationMixin): if self.inventory is not None: for instance_group in self.inventory.instance_groups.all(): selected_groups.append(instance_group) - if not getattr(self.inventory, 'prevent_instance_group_fallback', False) and self.inventory.organization is not None: + if not self.inventory.prevent_instance_group_fallback and self.inventory.organization is not None: for instance_group in self.inventory.organization.instance_groups.all(): selected_groups.append(instance_group) diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index f56bd17f4f..54c7d3e2b1 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -63,7 +63,7 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin, RelatedJobsMixin): an inventory source contains lists and hosts. """ - FIELDS_TO_PRESERVE_AT_COPY = ['hosts', 'groups', 'instance_groups'] + FIELDS_TO_PRESERVE_AT_COPY = ['hosts', 'groups', 'instance_groups', 'prevent_instance_group_fallback'] KIND_CHOICES = [ ('', _('Hosts have a direct link to this inventory.')), ('smart', _('Hosts for inventory generated using the host_filter property.')), diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index f5c5941914..a84a5a67eb 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -203,7 +203,7 @@ class JobTemplate(UnifiedJobTemplate, JobOptions, SurveyJobTemplateMixin, Resour playbook) to an inventory source with a given credential. """ - FIELDS_TO_PRESERVE_AT_COPY = ['labels', 'instance_groups', 'credentials', 'survey_spec'] + FIELDS_TO_PRESERVE_AT_COPY = ['labels', 'instance_groups', 'credentials', 'survey_spec', 'prevent_instance_group_fallback'] FIELDS_TO_DISCARD_AT_COPY = ['vault_credential', 'credential'] SOFT_UNIQUE_TOGETHER = [('polymorphic_ctype', 'name', 'organization')] diff --git a/awx/main/tests/functional/test_copy.py b/awx/main/tests/functional/test_copy.py index 9be8d6574c..0574f9ccbd 100644 --- a/awx/main/tests/functional/test_copy.py +++ b/awx/main/tests/functional/test_copy.py @@ -19,6 +19,7 @@ def test_job_template_copy( job_template_with_survey_passwords.inventory = inventory job_template_with_survey_passwords.labels.add(label) job_template_with_survey_passwords.instance_groups.add(ig) + job_template_with_survey_passwords.prevent_instance_group_fallback = True job_template_with_survey_passwords.save() job_template_with_survey_passwords.credentials.add(credential) job_template_with_survey_passwords.credentials.add(machine_credential) @@ -65,6 +66,7 @@ def test_job_template_copy( assert jt_copy.labels.get(pk=label.pk) == label assert jt_copy.instance_groups.count() != 0 assert jt_copy.instance_groups.get(pk=ig.pk) == ig + assert jt_copy.prevent_instance_group_fallback == True @pytest.mark.django_db @@ -95,6 +97,8 @@ def test_inventory_copy(inventory, group_factory, post, get, alice, organization host = group_1_1.hosts.create(name='host', inventory=inventory) group_2_1.hosts.add(host) inventory.admin_role.members.add(alice) + inventory.prevent_instance_group_fallback = True + inventory.save() assert get(reverse('api:inventory_copy', kwargs={'pk': inventory.pk}), alice, expect=200).data['can_copy'] is False inventory.organization.admin_role.members.add(alice) assert get(reverse('api:inventory_copy', kwargs={'pk': inventory.pk}), alice, expect=200).data['can_copy'] is True @@ -110,6 +114,7 @@ def test_inventory_copy(inventory, group_factory, post, get, alice, organization assert inventory_copy.organization == organization assert inventory_copy.created_by == alice assert inventory_copy.name == 'new inv name' + assert inventory_copy.prevent_instance_group_fallback == True assert set(group_1_1_copy.parents.all()) == set() assert set(group_2_1_copy.parents.all()) == set([group_1_1_copy]) assert set(group_2_2_copy.parents.all()) == set([group_1_1_copy, group_2_1_copy])