mirror of
https://github.com/ansible/awx.git
synced 2026-03-23 20:05:03 -02:30
prompt-for acceptance code in JT model
This commit is contained in:
@@ -2121,6 +2121,11 @@ class JobLaunchSerializer(BaseSerializer):
|
|||||||
'credential': {
|
'credential': {
|
||||||
'write_only': True,
|
'write_only': True,
|
||||||
},
|
},
|
||||||
|
'limit': {'write_only': True},
|
||||||
|
'job_tags': {'write_only': True},
|
||||||
|
'skip_tags': {'write_only': True},
|
||||||
|
'job_type': {'write_only': True},
|
||||||
|
'inventory': {'write_only': True},
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_credential_needed_to_start(self, obj):
|
def get_credential_needed_to_start(self, obj):
|
||||||
|
|||||||
@@ -2116,59 +2116,9 @@ class JobTemplateLaunch(RetrieveAPIView, GenericAPIView):
|
|||||||
'credential': serializer.instance.credential.pk,
|
'credential': serializer.instance.credential.pk,
|
||||||
}
|
}
|
||||||
|
|
||||||
# -- following code will be moved to JobTemplate model --
|
prompted_fields, ignored_fields = obj._accept_or_ignore_job_kwargs(user=self.request.user, **request.data)
|
||||||
# Sort the runtime fields allowed and disallowed by job template
|
|
||||||
ignored_fields = {}
|
|
||||||
if 'extra_vars' in request.data:
|
|
||||||
kv['extra_vars'] = {}
|
|
||||||
ignored_fields['extra_vars'] = {}
|
|
||||||
if obj.ask_variables_on_launch:
|
|
||||||
# Accept all extra_vars if the flag is set
|
|
||||||
kv['extra_vars'] = request.data['extra_vars']
|
|
||||||
else:
|
|
||||||
if obj.survey_enabled:
|
|
||||||
# Accept vars defined in the survey and no others
|
|
||||||
survey_vars = [question['variable'] for question in obj.survey_spec['spec']]
|
|
||||||
for key in request.data['extra_vars']:
|
|
||||||
if key in survey_vars:
|
|
||||||
kv['extra_vars'][key] = request.data['extra_vars'][key]
|
|
||||||
else:
|
|
||||||
ignored_fields['extra_vars'][key] = request.data['extra_vars'][key]
|
|
||||||
else:
|
|
||||||
# No survey & prompt flag is false - ignore all
|
|
||||||
ignored_fields['extra_vars'] = request.data['extra_vars']
|
|
||||||
|
|
||||||
if 'limit' in request.data:
|
|
||||||
if obj.ask_limit_on_launch:
|
|
||||||
kv['limit'] = request.data['limit']
|
|
||||||
else:
|
|
||||||
ignored_fields['limit'] = request.data['limit']
|
|
||||||
|
|
||||||
if 'job_tags' or 'skip_tags' in request.data:
|
|
||||||
if obj.ask_tags_on_launch:
|
|
||||||
if 'job_tags' in request.data:
|
|
||||||
kv['job_tags'] = request.data['job_tags']
|
|
||||||
if 'skip_tags' in request.data:
|
|
||||||
kv['skip_tags'] = request.data['skip_tags']
|
|
||||||
else:
|
|
||||||
if 'job_tags' in request.data:
|
|
||||||
ignored_fields['job_tags'] = request.data['job_tags']
|
|
||||||
if 'skip_tags' in request.data:
|
|
||||||
ignored_fields['skip_tags'] = request.data['skip_tags']
|
|
||||||
|
|
||||||
if 'job_type' in request.data:
|
|
||||||
if obj.ask_job_type_on_launch:
|
|
||||||
kv['job_type'] = request.data['job_type']
|
|
||||||
else:
|
|
||||||
ignored_fields['job_type'] = request.data['job_type']
|
|
||||||
|
|
||||||
if 'inventory' in request.data:
|
|
||||||
inv_id = request.data['inventory']
|
|
||||||
if obj.ask_inventory_on_launch and Inventory.objects.get(pk=inv_id).accessible_by(self.request.user, {'write': True}):
|
|
||||||
kv['inventory'] = inv_id
|
|
||||||
else:
|
|
||||||
ignored_fields['inventory'] = inv_id
|
|
||||||
|
|
||||||
|
kv.update(prompted_fields)
|
||||||
kv.update(passwords)
|
kv.update(passwords)
|
||||||
|
|
||||||
new_job = obj.create_unified_job(**kv)
|
new_job = obj.create_unified_job(**kv)
|
||||||
|
|||||||
@@ -378,6 +378,65 @@ class JobTemplate(UnifiedJobTemplate, JobOptions, ResourceMixin):
|
|||||||
kwargs['extra_vars'] = json.dumps(extra_vars)
|
kwargs['extra_vars'] = json.dumps(extra_vars)
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
def _accept_or_ignore_job_kwargs(self, user, **kwargs):
|
||||||
|
# Sort the runtime fields allowed and disallowed by job template
|
||||||
|
ignored_fields = {}
|
||||||
|
prompted_fields = {}
|
||||||
|
if 'extra_vars' in kwargs:
|
||||||
|
prompted_fields['extra_vars'] = {}
|
||||||
|
ignored_fields['extra_vars'] = {}
|
||||||
|
if self.ask_variables_on_launch:
|
||||||
|
# Accept all extra_vars if the flag is set
|
||||||
|
prompted_fields['extra_vars'] = kwargs['extra_vars']
|
||||||
|
else:
|
||||||
|
if self.survey_enabled:
|
||||||
|
# Accept vars defined in the survey and no others
|
||||||
|
survey_vars = [question['variable'] for question in self.survey_spec['spec']]
|
||||||
|
for key in kwargs['extra_vars']:
|
||||||
|
if key in survey_vars:
|
||||||
|
prompted_fields['extra_vars'][key] = kwargs['extra_vars'][key]
|
||||||
|
else:
|
||||||
|
ignored_fields['extra_vars'][key] = kwargs['extra_vars'][key]
|
||||||
|
else:
|
||||||
|
# No survey & prompt flag is false - ignore all
|
||||||
|
ignored_fields['extra_vars'] = kwargs['extra_vars']
|
||||||
|
|
||||||
|
if 'limit' in kwargs:
|
||||||
|
if self.ask_limit_on_launch:
|
||||||
|
prompted_fields['limit'] = kwargs['limit']
|
||||||
|
else:
|
||||||
|
ignored_fields['limit'] = kwargs['limit']
|
||||||
|
|
||||||
|
if 'job_tags' or 'skip_tags' in kwargs:
|
||||||
|
if self.ask_tags_on_launch:
|
||||||
|
if 'job_tags' in kwargs:
|
||||||
|
prompted_fields['job_tags'] = kwargs['job_tags']
|
||||||
|
if 'skip_tags' in kwargs:
|
||||||
|
prompted_fields['skip_tags'] = kwargs['skip_tags']
|
||||||
|
else:
|
||||||
|
if 'job_tags' in kwargs:
|
||||||
|
ignored_fields['job_tags'] = kwargs['job_tags']
|
||||||
|
if 'skip_tags' in kwargs:
|
||||||
|
ignored_fields['skip_tags'] = kwargs['skip_tags']
|
||||||
|
|
||||||
|
if 'job_type' in kwargs:
|
||||||
|
if self.ask_job_type_on_launch:
|
||||||
|
prompted_fields['job_type'] = kwargs['job_type']
|
||||||
|
else:
|
||||||
|
ignored_fields['job_type'] = kwargs['job_type']
|
||||||
|
|
||||||
|
if 'inventory' in kwargs:
|
||||||
|
inv_id = kwargs['inventory']
|
||||||
|
if self.ask_inventory_on_launch:
|
||||||
|
from awx.main.models.inventory import Inventory
|
||||||
|
if Inventory.objects.get(pk=inv_id).accessible_by(user, {'write': True}):
|
||||||
|
prompted_fields['inventory'] = inv_id
|
||||||
|
else:
|
||||||
|
ignored_fields['inventory'] = inv_id
|
||||||
|
else:
|
||||||
|
ignored_fields['inventory'] = inv_id
|
||||||
|
return prompted_fields, ignored_fields
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cache_timeout_blocked(self):
|
def cache_timeout_blocked(self):
|
||||||
if Job.objects.filter(job_template=self, status__in=['pending', 'waiting', 'running']).count() > getattr(tower_settings, 'SCHEDULE_MAX_JOBS', 10):
|
if Job.objects.filter(job_template=self, status__in=['pending', 'waiting', 'running']).count() > getattr(tower_settings, 'SCHEDULE_MAX_JOBS', 10):
|
||||||
|
|||||||
Reference in New Issue
Block a user