-
-
Notifications
You must be signed in to change notification settings - Fork 300
[feature] Mass Command model and REST APIs for async command execution #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: gsoc26-mass-commands
Are you sure you want to change the base?
Changes from all commits
c36342c
a809fc7
bc9b780
2333066
b6df4db
07d9239
1f8c30e
5d391a6
cfe546c
736004e
0b2fd35
134cd18
a2353a5
a58b585
76763f5
e800e88
cce88e6
fababb3
84c2dd3
5f2277b
8efe276
455ff25
4994e34
d62c23a
8747b4a
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,111 @@ class Meta: | |
| "is_working": {"read_only": True}, | ||
| } | ||
| read_only_fields = ("created", "modified") | ||
|
|
||
|
|
||
| class BatchCommandExecuteSerializer( | ||
| FilterSerializerByOrgManaged, serializers.ModelSerializer | ||
| ): | ||
| input = serializers.JSONField(allow_null=True, required=False) | ||
| devices = serializers.PrimaryKeyRelatedField( | ||
| many=True, | ||
| queryset=Device.objects.all(), | ||
| required=False, | ||
| allow_empty=True, | ||
|
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. Question about the API contract: are we sure that accepting
Member
Author
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.
Member
Author
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. Removed |
||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
|
|
||
| def __init__(self, *args, dry_run=False, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.dry_run = dry_run | ||
| if dry_run: | ||
| self.fields["type"].required = False | ||
| self.fields["label"].required = False | ||
|
|
||
| class Meta: | ||
| model = BatchCommand | ||
| fields = ( | ||
| "organization", | ||
| "type", | ||
| "input", | ||
| "label", | ||
| "notes", | ||
| "devices", | ||
| "group", | ||
| "location", | ||
| ) | ||
| extra_kwargs = { | ||
| "organization": {"required": False, "allow_null": True}, | ||
| } | ||
|
|
||
| def validate(self, data): | ||
| org = data.get("organization") | ||
| if not org and not self.context["request"].user.is_superuser: | ||
| raise serializers.ValidationError( | ||
| _("Only superusers can execute batch commands without an organization.") | ||
| ) | ||
|
dee077 marked this conversation as resolved.
|
||
| # DRF's many=True injects [] for QueryDict even when key is | ||
| # absent; remove it so model can distinguish omitted vs explicit []. | ||
| if "devices" not in self.initial_data and "devices" in data: | ||
| data.pop("devices") | ||
| return data | ||
|
|
||
|
|
||
| class BatchCommandSerializer(BaseSerializer): | ||
| device_count = serializers.IntegerField(read_only=True) | ||
| skipped_devices = serializers.JSONField(read_only=True) | ||
| organization = serializers.PrimaryKeyRelatedField( | ||
| read_only=True, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
| group = serializers.PrimaryKeyRelatedField( | ||
| read_only=True, | ||
| allow_null=True, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
| location = serializers.PrimaryKeyRelatedField( | ||
| read_only=True, | ||
| allow_null=True, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
|
|
||
| def to_representation(self, instance): | ||
| data = super().to_representation(instance) | ||
| # The raw password is visible in API responses between | ||
| # when the batch is saved and when the Celery task runs _clean_sensitive_info(). | ||
| if instance.type == "change_password": | ||
| data["input"] = {"password": "********"} | ||
| return data | ||
|
|
||
| class Meta: | ||
| model = BatchCommand | ||
| fields = ( | ||
| "id", | ||
| "organization", | ||
| "status", | ||
| "type", | ||
| "input", | ||
| "label", | ||
| "notes", | ||
| "group", | ||
| "location", | ||
| "device_count", | ||
| "skipped_devices", | ||
|
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. For shared/global batch commands (
Member
Author
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. Updated! |
||
| "created", | ||
| "modified", | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| read_only_fields = ( | ||
| "created", | ||
| "modified", | ||
| ) | ||
|
|
||
|
|
||
| class BatchCommandDetailSerializer(BatchCommandSerializer): | ||
| devices = serializers.PrimaryKeyRelatedField( | ||
| many=True, | ||
| read_only=True, | ||
| pk_field=serializers.UUIDField(format="hex_verbose"), | ||
| ) | ||
|
|
||
| class Meta(BatchCommandSerializer.Meta): | ||
| fields = BatchCommandSerializer.Meta.fields + ("devices",) | ||

Uh oh!
There was an error while loading. Please reload this page.