1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Allow scheduling jobs via. the API

This commit is contained in:
kkthxbye-code
2022-10-20 21:36:43 +02:00
parent 83ff99a130
commit 4f95c5a72c
4 changed files with 30 additions and 11 deletions

View File

@ -282,6 +282,8 @@ http://netbox/api/extras/scripts/example.MyReport/ \
--data '{"data": {"foo": "somevalue", "bar": 123}, "commit": true}'
```
Optionally `schedule_at` can be passed in the form data with a datetime string to schedule a script at the specified date and time.
### Via the CLI
Scripts can be run on the CLI by invoking the management command:

View File

@ -152,6 +152,8 @@ Our example report above would be called as:
POST /api/extras/reports/devices.DeviceConnectionsReport/run/
```
Optionally `schedule_at` can be passed in the form data with a datetime string to schedule a script at the specified date and time.
### Via the CLI
Reports can be run on the CLI by invoking the management command:

View File

@ -38,6 +38,7 @@ __all__ = (
'ObjectChangeSerializer',
'ReportDetailSerializer',
'ReportSerializer',
'ReportInputSerializer',
'ScriptDetailSerializer',
'ScriptInputSerializer',
'ScriptLogMessageSerializer',
@ -388,6 +389,10 @@ class ReportDetailSerializer(ReportSerializer):
result = JobResultSerializer()
class ReportInputSerializer(serializers.Serializer):
schedule_at = serializers.DateTimeField(required=False, allow_null=True)
#
# Scripts
#
@ -419,6 +424,7 @@ class ScriptDetailSerializer(ScriptSerializer):
class ScriptInputSerializer(serializers.Serializer):
data = serializers.JSONField()
commit = serializers.BooleanField()
schedule_at = serializers.DateTimeField(required=False, allow_null=True)
class ScriptLogMessageSerializer(serializers.Serializer):

View File

@ -231,19 +231,26 @@ class ReportViewSet(ViewSet):
# Retrieve and run the Report. This will create a new JobResult.
report = self._retrieve_report(pk)
report_content_type = ContentType.objects.get(app_label='extras', model='report')
job_result = JobResult.enqueue_job(
run_report,
report.full_name,
report_content_type,
request.user,
job_timeout=report.job_timeout
)
report.result = job_result
input_serializer = serializers.ReportInputSerializer(data=request.data)
serializer = serializers.ReportDetailSerializer(report, context={'request': request})
if input_serializer.is_valid():
schedule_at = input_serializer.validated_data.get('schedule_at')
return Response(serializer.data)
report_content_type = ContentType.objects.get(app_label='extras', model='report')
job_result = JobResult.enqueue_job(
run_report,
report.full_name,
report_content_type,
request.user,
job_timeout=report.job_timeout,
schedule_at=schedule_at,
)
report.result = job_result
serializer = serializers.ReportDetailSerializer(report, context={'request': request})
return Response(serializer.data)
return Response(input_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#
@ -312,6 +319,7 @@ class ScriptViewSet(ViewSet):
if input_serializer.is_valid():
data = input_serializer.data['data']
commit = input_serializer.data['commit']
schedule_at = input_serializer.validated_data.get('schedule_at')
script_content_type = ContentType.objects.get(app_label='extras', model='script')
job_result = JobResult.enqueue_job(
@ -323,6 +331,7 @@ class ScriptViewSet(ViewSet):
request=copy_safe_request(request),
commit=commit,
job_timeout=script.job_timeout,
schedule_at=schedule_at,
)
script.result = job_result
serializer = serializers.ScriptDetailSerializer(script, context={'request': request})