1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/interfaces/api/viewsets.py
catborise 3b8869c72f fix(api): typed URLs and accurate response schemas
- Routers use path converters (use_regex_path=False): computes, instances
  and flavors take <int:...> ids, so non-numeric ids get 404 and
  drf-spectacular derives the parameter types itself.
- Each method declares what it really returns: detail serializers,
  a shared StatusSerializer for status-only replies, no request body for
  actions. Instance properties used by the details serializer get return
  type hints.
- The create-instance API drops the firmware field. Its only accepted
  value was "" (BIOS), which produced an empty, invalid enum, and the
  UEFI branch behind it was unreachable. Clients that still send it are
  unaffected; DRF ignores unknown fields.

The schema now passes spectacular --validate --fail-on-warn; a test
keeps it that way.
2026-10-04 01:08:11 +03:00

33 lines
1.1 KiB
Python

from admin.permissions import IsSuperUser
from django.shortcuts import get_object_or_404
from computes.models import Compute
from rest_framework import status, viewsets
from rest_framework.response import Response
from vrtManager.interface import wvmInterfaces, wvmInterface
from .serializers import InterfacesSerializer
class InterfaceViewSet(viewsets.ViewSet):
"""
A viewset for listing retrieving interfaces.
"""
permission_classes = [IsSuperUser]
serializer_class = InterfacesSerializer
def list(self, request, compute_pk=None):
queryset = []
compute = get_object_or_404(Compute, pk=compute_pk)
conn = wvmInterfaces(compute.hostname, compute.login, compute.password, compute.type)
ifaces = conn.get_ifaces()
for iface in ifaces:
interf = wvmInterface(compute.hostname, compute.login, compute.password, compute.type, iface)
queryset.append(interf.get_details())
serializer = InterfacesSerializer(queryset, many=True, context={'request': request})
return Response(serializer.data)