mirror of
https://github.com/retspen/webvirtcloud
synced 2025-07-31 12:41:08 +00:00
Rest framework (#24)
* Add rest framework for API: First Commit * modify some shell scripts to make variable references safer; modify some python scripts to reduce the code complexity and cyclomatic complexity of functions. * Add REST API for some webvirtcloud functions. Instance list/delete/create, compute list/delete/create, storages-network list/retrieve. Add swagger and redoc for API interface * update requirements Co-authored-by: herengui <herengui@uniontech.com>
This commit is contained in:
parent
92254401dc
commit
cfce71ec2b
42 changed files with 1170 additions and 348 deletions
27
computes/api/serializers.py
Normal file
27
computes/api/serializers.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
from rest_framework import serializers
|
||||
from computes.models import Compute
|
||||
from vrtManager.connection import (
|
||||
CONN_SOCKET,
|
||||
CONN_SSH,
|
||||
CONN_TCP,
|
||||
CONN_TLS,
|
||||
)
|
||||
|
||||
|
||||
class ComputeSerializer(serializers.ModelSerializer):
|
||||
# Use <input type="password"> for the input.
|
||||
password = serializers.CharField(style={'input_type': 'password'})
|
||||
# Use a radio input instead of a select input.
|
||||
conn_types = (
|
||||
(CONN_SSH, 'SSH'),
|
||||
(CONN_TCP, 'TCP'),
|
||||
(CONN_TLS, 'TLS'),
|
||||
(CONN_SOCKET, 'SOCK'),
|
||||
)
|
||||
type = serializers.ChoiceField(choices=conn_types)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Compute
|
||||
fields = ['id', 'name', 'hostname', 'login', 'password', 'type', 'details']
|
||||
58
computes/api/viewsets.py
Normal file
58
computes/api/viewsets.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
from computes.models import Compute
|
||||
from rest_framework import viewsets
|
||||
from rest_framework import permissions
|
||||
|
||||
from vrtManager.create import wvmCreate
|
||||
from .serializers import ComputeSerializer
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
||||
class ComputeViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
API endpoint that allows computes to be viewed or edited.
|
||||
"""
|
||||
queryset = Compute.objects.all().order_by('name')
|
||||
serializer_class = ComputeSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
|
||||
class ComputeArchitecturesView(viewsets.ViewSet):
|
||||
|
||||
def list(self, request, compute_pk=None):
|
||||
"""
|
||||
Return a list of supported host architectures.
|
||||
"""
|
||||
compute = Compute.objects.get(pk=compute_pk)
|
||||
conn = wvmCreate(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
)
|
||||
return Response(conn.get_hypervisors_machines())
|
||||
|
||||
def retrieve(self, request, compute_pk=None, pk=None):
|
||||
compute = Compute.objects.get(pk=compute_pk)
|
||||
conn = wvmCreate(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
)
|
||||
return Response(conn.get_machine_types(pk))
|
||||
|
||||
|
||||
class ComputeMachinesView(viewsets.ViewSet):
|
||||
|
||||
def list(self, request, compute_pk=None, archs_pk=None):
|
||||
"""
|
||||
Return a list of supported host architectures.
|
||||
"""
|
||||
compute = Compute.objects.get(pk=compute_pk)
|
||||
conn = wvmCreate(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
)
|
||||
return Response(conn.get_machine_types(archs_pk))
|
||||
Loading…
Add table
Add a link
Reference in a new issue