-
-
Notifications
You must be signed in to change notification settings - Fork 298
[feature] Mass command asynchronous execution pipeline #1395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c9bb4e0
cbe8ea6
fd83497
09c57a8
55ea8d0
35902dd
0d7c392
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| DeviceConnection = load_model("connection", "DeviceConnection") | ||
| Credentials = load_model("connection", "Credentials") | ||
| Device = load_model("config", "Device") | ||
| BatchCommand = load_model("connection", "BatchCommand") | ||
|
|
||
|
|
||
| class ValidatedDeviceFieldSerializer(ValidatedModelSerializer): | ||
|
|
@@ -43,6 +44,10 @@ class CommandSerializer(ValidatedDeviceFieldSerializer): | |
| required=False, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
| batch_command = serializers.PrimaryKeyRelatedField( | ||
| read_only=True, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
|
|
@@ -115,3 +120,70 @@ class Meta: | |
| "is_working": {"read_only": True}, | ||
| } | ||
| read_only_fields = ("created", "modified") | ||
|
|
||
|
|
||
| class BatchCommandExecuteSerializer( | ||
| FilterSerializerByOrgManaged, serializers.ModelSerializer | ||
| ): | ||
| type = serializers.CharField(source="command_type") | ||
| input = serializers.JSONField( | ||
| source="command_input", allow_null=True, required=False | ||
| ) | ||
| devices = serializers.PrimaryKeyRelatedField( | ||
| many=True, | ||
| queryset=Device.objects.all(), | ||
| required=False, | ||
| allow_empty=True, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
| execute_all = serializers.BooleanField(required=False, default=True) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| request = self.context.get("request") | ||
| if request and request.method == "GET": | ||
| self.fields["type"].required = False | ||
|
|
||
| class Meta: | ||
| model = BatchCommand | ||
| fields = ( | ||
| "organization", | ||
| "type", | ||
| "input", | ||
| "devices", | ||
| "group", | ||
| "location", | ||
| "execute_all", | ||
| ) | ||
| extra_kwargs = { | ||
| "organization": {"required": False, "allow_null": True}, | ||
| } | ||
|
|
||
| def validate(self, data): | ||
| org = data.get("organization") | ||
| execute_all = data.get("execute_all", False) | ||
| devices = data.get("devices") | ||
| group = data.get("group") | ||
| location = data.get("location") | ||
| if not org and not self.context["request"].user.is_superuser: | ||
| raise serializers.ValidationError( | ||
| _("Only superusers can execute batch commands without an organization.") | ||
| ) | ||
|
Comment on lines
+168
to
+171
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if we have existing mixins in openwisp-users which can perform this operation? |
||
| if not execute_all and not org and not devices and not group and not location: | ||
| raise serializers.ValidationError( | ||
| _( | ||
| "Specify at least one targeting option " | ||
| "or set execute_all to true." | ||
| ) | ||
| ) | ||
| if devices: | ||
| for device in devices: | ||
| if org and device.organization_id != org.id: | ||
| raise serializers.ValidationError( | ||
| { | ||
| "devices": _( | ||
| "All devices must belong to the same organization." | ||
| ) | ||
| } | ||
| ) | ||
| return data | ||
|
Comment on lines
+179
to
+189
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the Model also performs this validation. And, it does so more elegantly. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| from django.core.exceptions import ValidationError | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from drf_yasg import openapi | ||
| from drf_yasg.utils import swagger_auto_schema | ||
| from rest_framework import status | ||
| from rest_framework.generics import ( | ||
| GenericAPIView, | ||
| ListCreateAPIView, | ||
| RetrieveAPIView, | ||
| RetrieveUpdateDestroyAPIView, | ||
| get_object_or_404, | ||
| ) | ||
| from rest_framework.response import Response | ||
| from swapper import load_model | ||
|
|
||
| from openwisp_utils.api.pagination import OpenWispPagination | ||
|
|
@@ -17,6 +21,7 @@ | |
| RelatedDeviceProtectedAPIMixin, | ||
| ) | ||
| from .serializers import ( | ||
| BatchCommandExecuteSerializer, | ||
| CommandSerializer, | ||
| CredentialSerializer, | ||
| DeviceConnectionSerializer, | ||
|
|
@@ -26,6 +31,7 @@ | |
| Device = load_model("config", "Device") | ||
| Credentials = load_model("connection", "Credentials") | ||
| DeviceConnection = load_model("connection", "DeviceConnection") | ||
| BatchCommand = load_model("connection", "BatchCommand") | ||
|
|
||
|
|
||
| class BaseCommandView(RelatedDeviceProtectedAPIMixin): | ||
|
|
@@ -138,6 +144,37 @@ class DeviceConnectionListCreateView(BaseDeviceConnection, ListCreateAPIView): | |
| DeviceConnenctionListCreateView = DeviceConnectionListCreateView | ||
|
|
||
|
|
||
| class BatchCommandExecuteView(ProtectedAPIMixin, GenericAPIView): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're lacking a List endpoint. |
||
| model = BatchCommand | ||
| queryset = BatchCommand.objects.all() | ||
| serializer_class = BatchCommandExecuteSerializer | ||
|
|
||
| def post(self, request): | ||
| serializer = self.get_serializer(data=request.data) | ||
| serializer.is_valid(raise_exception=True) | ||
| try: | ||
| batch = BatchCommand.execute(**serializer.validated_data) | ||
| except ValidationError as e: | ||
| return Response( | ||
| getattr(e, "message_dict", e.messages), | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return Response({"batch": str(batch.pk)}, status=201) | ||
|
|
||
| def get(self, request): | ||
| serializer = self.get_serializer(data=request.query_params) | ||
| serializer.is_valid(raise_exception=True) | ||
| try: | ||
| data = BatchCommand.dry_run(**serializer.validated_data) | ||
| except ValidationError as e: | ||
| return Response( | ||
| getattr(e, "message_dict", e.messages), | ||
| status=status.HTTP_400_BAD_REQUEST, | ||
| ) | ||
| data["devices"] = [str(d.pk) for d in data["devices"]] | ||
| return Response(data) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class DeviceConnectionDetailView(BaseDeviceConnection, RetrieveUpdateDestroyAPIView): | ||
| def get_object(self): | ||
| queryset = self.filter_queryset(self.get_queryset()) | ||
|
|
@@ -158,3 +195,5 @@ def get_object(self): | |
|
|
||
| # TODO: remove in version 1.4 | ||
| deviceconnection_details_view = deviceconnection_detail_view | ||
|
|
||
| batch_command_execute_view = BatchCommandExecuteView.as_view() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this?