diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 7d9069e4f0..5121f081ed 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -1158,13 +1158,6 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions, CustomVirtualE raise ValidationError(_("Cannot set source_path if not SCM type.")) return self.source_path - def clean_source_vars(self): - injector = self.injectors.get(self.source) - source_vars = dict(self.source_vars_dict) # make a copy - if injector and self.source_vars_dict.get('plugin', '') != injector.get_proper_name(): - source_vars['plugin'] = injector.get_proper_name() - return json.dumps(source_vars) - ''' RelatedJobsMixin ''' @@ -1369,13 +1362,21 @@ class PluginFileInjector(object): """Returns a string that is the content for the inventory file for the inventory plugin """ return yaml.safe_dump( - inventory_update.source_vars_dict, + self.inventory_as_dict(inventory_update, private_data_dir), default_flow_style=False, width=1000 ) def inventory_as_dict(self, inventory_update, private_data_dir): - return inventory_update.source_vars_dict + source_vars = dict(inventory_update.source_vars_dict) # make a copy + proper_name = self.get_proper_name() + ''' + None conveys that we should use the user-provided plugin. + Note that a plugin value of '' should still be overridden. + ''' + if proper_name is not None: + source_vars['plugin'] = proper_name + return source_vars def build_env(self, inventory_update, env, private_data_dir, private_data_files): injector_env = self.get_plugin_env(inventory_update, private_data_dir, private_data_files) @@ -1463,6 +1464,7 @@ class gce(PluginFileInjector): def inventory_as_dict(self, inventory_update, private_data_dir): ret = super().inventory_as_dict(inventory_update, private_data_dir) credential = inventory_update.get_cloud_credential() + # TODO: Align precedence of ENV vs. inventory config w/ Ansible behavior ret['projects'] = [credential.get_input('project', default='')] return ret diff --git a/awx/main/tests/functional/api/test_inventory.py b/awx/main/tests/functional/api/test_inventory.py index a04a68c9bb..5bad1b6f30 100644 --- a/awx/main/tests/functional/api/test_inventory.py +++ b/awx/main/tests/functional/api/test_inventory.py @@ -456,38 +456,6 @@ def test_inventory_source_vars_prohibition(post, inventory, admin_user): assert 'FOOBAR' in r.data['source_vars'][0] -@pytest.mark.django_db -@pytest.mark.parametrize('source,source_var_actual,source_var_expected,description', [ - ('ec2', {'plugin': 'blah'}, {'plugin': 'amazon.aws.aws_ec2'}, 'source plugin mismatch'), - ('ec2', {'plugin': 'amazon.aws.aws_ec2'}, {'plugin': 'amazon.aws.aws_ec2'}, 'valid plugin'), -]) -def test_inventory_source_vars_source_plugin_ok(post, inventory, admin_user, source, source_var_actual, source_var_expected, description): - r = post(reverse('api:inventory_source_list'), - {'name': 'new inv src', 'source_vars': json.dumps(source_var_actual), 'inventory': inventory.pk, 'source': source}, - admin_user, expect=201) - - assert r.data['source_vars'] == json.dumps(source_var_expected) - - -@pytest.mark.django_db -@pytest.mark.parametrize('source_var_actual,description', [ - ({'plugin': 'namespace.collection.script'}, 'scm source type source_vars are ignored valid'), - ({'plugin': 'namespace.collection.script'}, 'scm source type source_vars are ignored invalid'), - ({'plugin': ''}, 'scm source type source_vars are ignored blank'), - ({}, 'scm source type source_vars are ignored non-existent'), -]) -def test_inventory_source_vars_source_plugin_scm_ok(post, inventory, admin_user, project, source_var_actual, description): - r = post(reverse('api:inventory_source_list'), - {'name': 'new inv src', - 'source_vars': json.dumps(source_var_actual), - 'inventory': inventory.pk, - 'source': 'scm', - 'source_project': project.id,}, - admin_user, expect=201) - - assert r.data['source_vars'] == json.dumps(source_var_actual) - - @pytest.mark.django_db @pytest.mark.parametrize('role,expect', [ ('admin_role', 200),