1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00

created a serializer for job template launch endpoint

This commit is contained in:
Chris Meyers 2015-04-24 08:26:12 -04:00
parent 4fc04a3709
commit b117e14e19
2 changed files with 57 additions and 28 deletions

View File

@ -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,55 @@ 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_id = serializers.IntegerField(write_only=True, required=False)
errors = serializers.IntegerField(read_only=True) # only 'visible' when returning errors
credential = serializers.IntegerField(write_only=True, required=False)
credential_id = serializers.IntegerField(write_only=True, required=False)
extra_vars = serializers.CharField(write_only=True, required=False)
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_id', 'credential')
def to_native(self, obj):
res = super(JobLaunchSerializer, self).to_native(obj)
if obj:
res['credential_needed_to_start'] = obj.credential is None
res['survey_enabled'] = obj.survey_enabled and 'spec' in obj.survey_spec
return res
def validate(self, attrs):
obj = self.context.get('obj')
if obj.survey_enabled and 'spec' in obj.survey_spec:
try:
extra_vars = literal_eval(attrs.get('extra_vars', {}))
except Exception:
if not isinstance(extra_vars, dict):
raise serializers.ValidationError({
'extra_vars': "Invalid format. JSON expected."
})
validation_errors = obj.survey_variable_validation(extra_vars)
if validation_errors:
raise serializers.ValidationError({
'variables_needed_to_start': validation_errors
})
return attrs
def validate_errors(self, attrs):
obj = self.context.get('obj')
if obj.credential is None and 'credential' not in attrs and 'credential_id' not in attrs:
raise serializers.ValidationError("Credential not provided")
if obj.job_type != PERM_INVENTORY_SCAN and (obj.project is None or not obj.project.active):
raise serializers.ValidationError("Job Template Project is missing or undefined")
if obj.inventory is None or not obj.inventory.active:
return serializers.ValidationError("Job Template Inventory is missing or undefined")
return attrs
class ScheduleSerializer(BaseSerializer):

View File

@ -1434,42 +1434,21 @@ 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)
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)
new_job = obj.create_unified_job(**request.DATA)
result = new_job.signal_start(**request.DATA)
if not result: