|
14 | 14 | from rest_framework.permissions import IsAuthenticated
|
15 | 15 | from rest_framework.response import Response
|
16 | 16 |
|
17 |
| -from promgen import filters, models, serializers |
| 17 | +from promgen import filters, models, serializers, signals |
18 | 18 |
|
19 | 19 | class RuleMixin:
|
20 | 20 | @extend_schema(
|
@@ -340,3 +340,194 @@ class URLViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.Gene
|
340 | 340 | lookup_value_regex = "[^/]+"
|
341 | 341 | lookup_field = "id"
|
342 | 342 | pagination_class = PromgenPagination
|
| 343 | + |
| 344 | + |
| 345 | +@extend_schema_view( |
| 346 | + list=extend_schema(summary="List Projects", description="Retrieve a list of all projects."), |
| 347 | + retrieve=extend_schema( |
| 348 | + summary="Retrieve Project", |
| 349 | + description="Retrieve detailed information about a specific project.", |
| 350 | + ), |
| 351 | + create=extend_schema(summary="Create Project", description="Create a new project."), |
| 352 | + update=extend_schema(summary="Update Project", description="Update an existing project."), |
| 353 | + partial_update=extend_schema( |
| 354 | + summary="Partially Update Project", description="Partially update an existing project." |
| 355 | + ), |
| 356 | + destroy=extend_schema(summary="Delete Project", description="Delete an existing project."), |
| 357 | +) |
| 358 | +@extend_schema(tags=["Project"]) |
| 359 | +class ProjectViewSet(NotifierMixin, RuleMixin, viewsets.ModelViewSet): |
| 360 | + queryset = models.Project.objects.prefetch_related("service", "shard", "farm") |
| 361 | + filterset_class = filters.ProjectFilterV2 |
| 362 | + lookup_value_regex = "[^/]+" |
| 363 | + lookup_field = "id" |
| 364 | + pagination_class = PromgenPagination |
| 365 | + |
| 366 | + def get_serializer_class(self): |
| 367 | + if self.action == "list": |
| 368 | + return serializers.ProjectRetrieveSerializer |
| 369 | + if self.action == "retrieve": |
| 370 | + return serializers.ProjectRetrieveSerializer |
| 371 | + if self.action == "create": |
| 372 | + return serializers.ProjectCreateSerializer |
| 373 | + if self.action == "update": |
| 374 | + return serializers.ProjectUpdateSerializer |
| 375 | + if self.action == "partial_update": |
| 376 | + return serializers.ProjectUpdateSerializer |
| 377 | + return serializers.ProjectRetrieveSerializer |
| 378 | + |
| 379 | + @extend_schema( |
| 380 | + summary="List Exporters", |
| 381 | + description="Retrieve all exporters associated with the specified project.", |
| 382 | + responses=serializers.ExporterSerializer(many=True), |
| 383 | + ) |
| 384 | + @action(detail=True, methods=["get"], pagination_class=None, filterset_class=None) |
| 385 | + def exporters(self, request, id): |
| 386 | + project = self.get_object() |
| 387 | + return Response(serializers.ExporterSerializer(project.exporter_set.all(), many=True).data) |
| 388 | + |
| 389 | + @extend_schema( |
| 390 | + summary="List URLs", |
| 391 | + description="Retrieve all URLs associated with the specified project.", |
| 392 | + responses=serializers.URLSerializer(many=True), |
| 393 | + ) |
| 394 | + @action(detail=True, methods=["get"], pagination_class=None, filterset_class=None) |
| 395 | + def urls(self, request, id): |
| 396 | + project = self.get_object() |
| 397 | + return Response(serializers.URLSerializer(project.url_set.all(), many=True).data) |
| 398 | + |
| 399 | + @extend_schema( |
| 400 | + summary="Link Farm", |
| 401 | + description="Link a farm to the specified project.", |
| 402 | + request=serializers.LinkFarmSerializer, |
| 403 | + responses=serializers.ProjectRetrieveSerializer, |
| 404 | + ) |
| 405 | + @action(detail=True, methods=["patch"], url_path="farm-link") |
| 406 | + def link_farm(self, request, id): |
| 407 | + serializer = serializers.LinkFarmSerializer(data=request.data) |
| 408 | + serializer.is_valid(raise_exception=True) |
| 409 | + project = self.get_object() |
| 410 | + farm, created = models.Farm.objects.get_or_create( |
| 411 | + name=serializer.validated_data["farm"], |
| 412 | + source=serializer.validated_data["source"], |
| 413 | + ) |
| 414 | + if created: |
| 415 | + farm.refresh() |
| 416 | + project.farm = farm |
| 417 | + project.save() |
| 418 | + return Response(serializers.ProjectRetrieveSerializer(project).data) |
| 419 | + |
| 420 | + @extend_schema( |
| 421 | + summary="Unlink Farm", |
| 422 | + description="Unlink the farm from the specified project.", |
| 423 | + responses=serializers.ProjectRetrieveSerializer, |
| 424 | + ) |
| 425 | + @action(detail=True, methods=["patch"], url_path="farm-unlink") |
| 426 | + def unlink_farm(self, request, id): |
| 427 | + project = self.get_object() |
| 428 | + if project.farm is None: |
| 429 | + return Response(serializers.ProjectRetrieveSerializer(project).data) |
| 430 | + |
| 431 | + old_farm, project.farm = project.farm, None |
| 432 | + project.save() |
| 433 | + signals.trigger_write_config.send(request) |
| 434 | + |
| 435 | + if old_farm.project_set.count() == 0 and old_farm.editable is False: |
| 436 | + old_farm.delete() |
| 437 | + return Response(serializers.ProjectRetrieveSerializer(project).data) |
| 438 | + |
| 439 | + @extend_schema( |
| 440 | + summary="Register URL", |
| 441 | + description="Register a new URL for the specified project.", |
| 442 | + request=serializers.RegisterURLProjectSerializer, |
| 443 | + responses={201: serializers.URLSerializer(many=True)}, |
| 444 | + ) |
| 445 | + @urls.mapping.post |
| 446 | + def register_url(self, request, id): |
| 447 | + serializer = serializers.RegisterURLProjectSerializer(data=request.data) |
| 448 | + serializer.is_valid(raise_exception=True) |
| 449 | + project = self.get_object() |
| 450 | + |
| 451 | + models.URL.objects.get_or_create( |
| 452 | + project=project, |
| 453 | + url=serializer.validated_data["url"], |
| 454 | + probe=serializer.validated_data["probe"], |
| 455 | + ) |
| 456 | + return Response( |
| 457 | + serializers.URLSerializer(project.url_set, many=True).data, status=HTTPStatus.CREATED |
| 458 | + ) |
| 459 | + |
| 460 | + @extend_schema( |
| 461 | + summary="Delete URL", |
| 462 | + description="Delete a URL from the specified project.", |
| 463 | + ) |
| 464 | + @action( |
| 465 | + detail=True, |
| 466 | + methods=["delete"], |
| 467 | + url_path="urls/(?P<url_id>\d+)", |
| 468 | + pagination_class=None, |
| 469 | + filterset_class=None, |
| 470 | + ) |
| 471 | + def delete_url(self, request, id, url_id): |
| 472 | + project = self.get_object() |
| 473 | + models.URL.objects.filter(project=project, pk=url_id).delete() |
| 474 | + return Response(status=HTTPStatus.NO_CONTENT) |
| 475 | + |
| 476 | + @extend_schema( |
| 477 | + summary="Register Exporter", |
| 478 | + description="Register a new exporter for the specified project.", |
| 479 | + request=serializers.RegisterExporterProjectSerializer, |
| 480 | + responses={201: serializers.ExporterSerializer(many=True)}, |
| 481 | + ) |
| 482 | + @exporters.mapping.post |
| 483 | + def register_exporter(self, request, id): |
| 484 | + serializer = serializers.RegisterExporterProjectSerializer(data=request.data) |
| 485 | + serializer.is_valid(raise_exception=True) |
| 486 | + project = self.get_object() |
| 487 | + |
| 488 | + attributes = { |
| 489 | + "project_id": project.id, |
| 490 | + } |
| 491 | + |
| 492 | + for field in serializer.fields: |
| 493 | + value = serializer.validated_data.get(field) |
| 494 | + if value is not None: |
| 495 | + attributes[field] = value |
| 496 | + |
| 497 | + models.Exporter.objects.get_or_create(**attributes) |
| 498 | + return Response( |
| 499 | + serializers.ExporterSerializer(project.exporter_set, many=True).data, |
| 500 | + status=HTTPStatus.CREATED, |
| 501 | + ) |
| 502 | + |
| 503 | + @extend_schema( |
| 504 | + summary="Update Exporter", |
| 505 | + description="Update an existing exporter for the specified project.", |
| 506 | + request=serializers.UpdateExporterProjectSerializer, |
| 507 | + responses=serializers.ExporterSerializer(many=True), |
| 508 | + ) |
| 509 | + @action( |
| 510 | + detail=True, |
| 511 | + methods=["patch"], |
| 512 | + url_path="exporters/(?P<exporter_id>\d+)", |
| 513 | + pagination_class=None, |
| 514 | + filterset_class=None, |
| 515 | + ) |
| 516 | + def update_exporter(self, request, id, exporter_id): |
| 517 | + serializer = serializers.UpdateExporterProjectSerializer(data=request.data) |
| 518 | + serializer.is_valid(raise_exception=True) |
| 519 | + project = self.get_object() |
| 520 | + exporter = models.Exporter.objects.filter(project=project, pk=exporter_id).first() |
| 521 | + exporter.enabled = serializer.validated_data.get("enabled") |
| 522 | + exporter.save() |
| 523 | + return Response(serializers.ExporterSerializer(project.exporter_set, many=True).data) |
| 524 | + |
| 525 | + @extend_schema( |
| 526 | + summary="Delete Exporter", |
| 527 | + description="Delete an exporter from the specified project.", |
| 528 | + ) |
| 529 | + @update_exporter.mapping.delete |
| 530 | + def delete_exporter(self, request, id, exporter_id): |
| 531 | + project = self.get_object() |
| 532 | + models.Exporter.objects.filter(project=project, pk=exporter_id).delete() |
| 533 | + return Response(status=HTTPStatus.NO_CONTENT) |
0 commit comments