add inventory name and id to meta vars

This commit is contained in:
jainnikhil30
2019-07-05 20:37:36 +05:30
parent d4f50896de
commit 88a4362a7a
3 changed files with 21 additions and 5 deletions

View File

@@ -1397,6 +1397,13 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique
r['{}_user_email'.format(name)] = created_by.email
r['{}_user_first_name'.format(name)] = created_by.first_name
r['{}_user_last_name'.format(name)] = created_by.last_name
inventory = getattr_dne(self, 'inventory')
if inventory:
for name in ('awx', 'tower'):
r['{}_inventory_id'.format(name)] = inventory.pk
r['{}_inventory_name'.format(name)] = inventory.name
return r
def get_queue_name(self):

View File

@@ -9,7 +9,8 @@ from awx.main.models import (
Job,
User,
Project,
JobTemplate
JobTemplate,
Inventory
)
@@ -81,11 +82,13 @@ class TestMetaVars:
def test_job_metavars(self):
maker = User(username='joe', pk=47, id=47)
inv = Inventory(name='example-inv', id=45)
assert Job(
name='fake-job',
pk=42, id=42,
launch_type='manual',
created_by=maker
created_by=maker,
inventory=inv
).awx_meta_vars() == {
'tower_job_id': 42,
'awx_job_id': 42,
@@ -100,7 +103,11 @@ class TestMetaVars:
'awx_user_last_name': '',
'tower_user_last_name': '',
'awx_user_id': 47,
'tower_user_id': 47
'tower_user_id': 47,
'tower_inventory_id': 45,
'awx_inventory_id': 45,
'tower_inventory_name': 'example-inv',
'awx_inventory_name': 'example-inv'
}
def test_project_update_metavars(self):

View File

@@ -256,6 +256,7 @@ class TestExtraVarSanitation(TestJobExecution):
def test_vars_unsafe_by_default(self, job, private_data_dir):
job.created_by = User(pk=123, username='angry-spud')
job.inventory = Inventory(pk=123, name='example-inv')
task = tasks.RunJob()
task.build_extra_vars_file(job, private_data_dir)
@@ -268,13 +269,14 @@ class TestExtraVarSanitation(TestJobExecution):
'awx_user_name', 'tower_job_launch_type',
'awx_project_revision',
'tower_project_revision', 'tower_user_name',
'awx_job_launch_type']:
'awx_job_launch_type',
'awx_inventory_name', 'tower_inventory_name']:
assert hasattr(extra_vars[unsafe], '__UNSAFE__')
# ensure that non-strings are marked as safe
for safe in ['awx_job_template_id', 'awx_job_id', 'awx_user_id',
'tower_user_id', 'tower_job_template_id',
'tower_job_id']:
'tower_job_id', 'awx_inventory_id', 'tower_inventory_id']:
assert not hasattr(extra_vars[safe], '__UNSAFE__')