mirror of
https://github.com/ansible/awx.git
synced 2026-05-08 09:57:35 -02:30
created a serializer for job template launch endpoint
This commit is contained in:
@@ -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):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user