overwrite plugin: at runtime

* Before, we were re-writing `plugin:` when users updated the
InventorySource via the API. Now, we just override at run-time. This
makes for a more sane API interaction
This commit is contained in:
Chris Meyers 2020-08-05 10:02:18 -04:00 committed by Ryan Petrello
parent c7794fc3e4
commit 48fb1e973c
No known key found for this signature in database
GPG Key ID: F2AA5F2122351777
2 changed files with 11 additions and 41 deletions

View File

@ -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

View File

@ -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),