=== modified file 'lava_scheduler_app/api.py'
@@ -1,8 +1,15 @@
import xmlrpclib
+from simplejson import JSONDecodeError
+
from linaro_django_xmlrpc.models import ExposedAPI
-from lava_scheduler_app.models import TestJob
+from lava_scheduler_app.models import (
+ Device,
+ DeviceType,
+ JSONDataError,
+ TestJob,
+ )
class SchedulerAPI(ExposedAPI):
@@ -12,7 +19,17 @@
raise xmlrpclib.Fault(401, "Authentication required.")
if not self.user.has_perm('lava_scheduler_app.add_testjob'):
raise xmlrpclib.Fault(403, "Permission denied.")
- return TestJob.from_json_and_user(job_data, self.user).id
+ try:
+ job = TestJob.from_json_and_user(job_data, self.user)
+ except JSONDecodeError as e:
+ raise xmlrpclib.Fault(400, "Decoding JSON failed: %s." % e)
+ except JSONDataError as e:
+ raise xmlrpclib.Fault(400, str(e))
+ except Device.DoesNotExist:
+ raise xmlrpclib.Fault(404, "Specified device not found.")
+ except DeviceType.DoesNotExist:
+ raise xmlrpclib.Fault(404, "Specified device type not found.")
+ return job.id
def cancel_job(self, job_id):
if not self.user:
=== modified file 'lava_scheduler_app/models.py'
@@ -1,10 +1,14 @@
-import json
+import simplejson
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext as _
+class JSONDataError(ValueError):
+ """Error raised when JSON is syntactically valid but ill-formed."""
+
+
class DeviceType(models.Model):
"""
A class of device, for example a pandaboard or a snowball.
@@ -167,13 +171,16 @@
@classmethod
def from_json_and_user(cls, json_data, user):
- job_data = json.loads(json_data)
+ job_data = simplejson.loads(json_data)
if 'target' in job_data:
target = Device.objects.get(hostname=job_data['target'])
device_type = None
- else:
+ elif 'device_type' in job_data:
target = None
device_type = DeviceType.objects.get(name=job_data['device_type'])
+ else:
+ raise JSONDataError(
+ "Neither 'target' nor 'device_type' found in job data.")
job_name = job_data.get('job_name', '')
job = TestJob(
definition=json_data, submitter=user, requested_device=target,