mirror of
https://github.com/ansible/awx.git
synced 2024-11-01 16:51:11 +03:00
Merge pull request #154 from chrismeyersfsu/fix-launch_500
created a serializer for job template launch endpoint
This commit is contained in:
commit
2093b3db7a
@ -6,6 +6,7 @@ import json
|
||||
import re
|
||||
import logging
|
||||
from dateutil import rrule
|
||||
from ast import literal_eval
|
||||
|
||||
# PyYAML
|
||||
import yaml
|
||||
@ -1690,6 +1691,69 @@ class AdHocCommandEventSerializer(BaseSerializer):
|
||||
res['host'] = reverse('api:host_detail', args=(obj.host.pk,))
|
||||
return res
|
||||
|
||||
class JobLaunchSerializer(BaseSerializer):
|
||||
passwords_needed_to_start = serializers.Field(source='passwords_needed_to_start')
|
||||
can_start_without_user_input = serializers.Field(source='can_start_without_user_input')
|
||||
variables_needed_to_start = serializers.Field(source='variables_needed_to_start')
|
||||
credential_needed_to_start = serializers.SerializerMethodField('get_credential_needed_to_start')
|
||||
survey_enabled = serializers.SerializerMethodField('get_survey_enabled')
|
||||
|
||||
class Meta:
|
||||
model = JobTemplate
|
||||
fields = ('can_start_without_user_input', 'passwords_needed_to_start', 'extra_vars',
|
||||
'ask_variables_on_launch', 'survey_enabled', 'variables_needed_to_start',
|
||||
'credential', 'credential_needed_to_start',)
|
||||
read_only_fields = ('ask_variables_on_launch',)
|
||||
write_only_fields = ('credential','extra_vars',)
|
||||
|
||||
def cred_valid(self, obj):
|
||||
if obj.credential is not None:
|
||||
return obj.credential.active
|
||||
return False
|
||||
|
||||
def get_credential_needed_to_start(self, obj):
|
||||
if obj:
|
||||
return not self.cred_valid(obj)
|
||||
return True
|
||||
|
||||
def get_survey_enabled(self, obj):
|
||||
if obj:
|
||||
return obj.survey_enabled and 'spec' in obj.survey_spec
|
||||
return False
|
||||
|
||||
def validate_extra_vars(self, attrs, source):
|
||||
extra_vars = attrs.get(source, {})
|
||||
if not extra_vars:
|
||||
return attrs
|
||||
|
||||
try:
|
||||
extra_vars = literal_eval(extra_vars)
|
||||
attrs['extra_vars'] = extra_vars
|
||||
except Exception:
|
||||
if not isinstance(extra_vars, dict):
|
||||
raise serializers.ValidationError("Invalid format. JSON expected.")
|
||||
return attrs
|
||||
|
||||
def validate_variables_needed_to_start(self, attrs, source):
|
||||
obj = self.context.get('obj')
|
||||
|
||||
if self.get_survey_enabled(obj):
|
||||
validation_errors = obj.survey_variable_validation(attrs.get('extra_vars', {}))
|
||||
if validation_errors:
|
||||
raise serializers.ValidationError(validation_errors)
|
||||
return attrs
|
||||
|
||||
def validate(self, attrs):
|
||||
obj = self.context.get('obj')
|
||||
|
||||
if not self.cred_valid(obj) and (attrs.get('credential', None) is None and attrs.get('credential_id', None) is None):
|
||||
raise serializers.ValidationError(dict(errors=["Credential not provided"]))
|
||||
if obj.job_type != PERM_INVENTORY_SCAN and (obj.project is None or not obj.project.active):
|
||||
raise serializers.ValidationError(dict(errors=["Job Template Project is missing or undefined"]))
|
||||
if obj.inventory is None or not obj.inventory.active:
|
||||
raise serializers.ValidationError(dict(errors=["Job Template Inventory is missing or undefined"]))
|
||||
|
||||
return attrs
|
||||
|
||||
class ScheduleSerializer(BaseSerializer):
|
||||
|
||||
|
@ -1434,44 +1434,27 @@ class JobTemplateDetail(RetrieveUpdateDestroyAPIView):
|
||||
return super(JobTemplateDetail, self).destroy(request, *args, **kwargs)
|
||||
|
||||
|
||||
class JobTemplateLaunch(GenericAPIView):
|
||||
class JobTemplateLaunch(RetrieveAPIView, GenericAPIView):
|
||||
|
||||
model = JobTemplate
|
||||
# FIXME: Add serializer class to define fields in OPTIONS request!
|
||||
serializer_class = JobLaunchSerializer
|
||||
is_job_start = True
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
obj = self.get_object()
|
||||
data = {}
|
||||
data['can_start_without_user_input'] = obj.can_start_without_user_input()
|
||||
data['passwords_needed_to_start'] = obj.passwords_needed_to_start
|
||||
data['ask_variables_on_launch'] = obj.ask_variables_on_launch
|
||||
data['variables_needed_to_start'] = obj.variables_needed_to_start
|
||||
data['credential_needed_to_start'] = obj.credential is None
|
||||
data['survey_enabled'] = obj.survey_enabled and 'spec' in obj.survey_spec
|
||||
return Response(data)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
obj = self.get_object()
|
||||
if not request.user.can_access(self.model, 'start', obj):
|
||||
raise PermissionDenied()
|
||||
if obj.survey_enabled and 'spec' in obj.survey_spec:
|
||||
if request.DATA == "":
|
||||
request_data = {}
|
||||
else:
|
||||
request_data = request.DATA
|
||||
validation_errors = obj.survey_variable_validation(request_data.get('extra_vars', {}))
|
||||
if validation_errors:
|
||||
return Response(dict(variables_needed_to_start=validation_errors),
|
||||
status=status.HTTP_400_BAD_REQUEST)
|
||||
if obj.credential is None and ('credential' not in request.DATA and 'credential_id' not in request.DATA):
|
||||
return Response(dict(errors="Credential not provided"), status=status.HTTP_400_BAD_REQUEST)
|
||||
if obj.job_type != PERM_INVENTORY_SCAN and (obj.project is None or not obj.project.active):
|
||||
return Response(dict(errors="Job Template Project is missing or undefined"), status=status.HTTP_400_BAD_REQUEST)
|
||||
if obj.inventory is None or not obj.inventory.active:
|
||||
return Response(dict(errors="Job Template Inventory is missing or undefined"), status=status.HTTP_400_BAD_REQUEST)
|
||||
new_job = obj.create_unified_job(**request.DATA)
|
||||
result = new_job.signal_start(**request.DATA)
|
||||
|
||||
serializer = self.serializer_class(data=request.DATA, context={'obj': obj})
|
||||
if not serializer.is_valid():
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
kv = {
|
||||
'credential': serializer.object.credential,
|
||||
'extra_vars': serializer.object.extra_vars
|
||||
}
|
||||
new_job = obj.create_unified_job(**kv)
|
||||
result = new_job.signal_start(**kv)
|
||||
if not result:
|
||||
data = dict(passwords_needed_to_start=new_job.passwords_needed_to_start)
|
||||
new_job.delete()
|
||||
|
Loading…
Reference in New Issue
Block a user