2022-11-02 13:05:41 +00:00
|
|
|
from appsettings.settings import app_settings
|
2022-08-22 12:12:33 +00:00
|
|
|
from computes.models import Compute
|
2022-11-02 13:05:41 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2022-08-22 12:12:33 +00:00
|
|
|
from rest_framework import status, viewsets
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2022-11-02 13:05:41 +00:00
|
|
|
from vrtManager.storage import wvmStorage, wvmStorages
|
2022-08-22 12:12:33 +00:00
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
from .serializers import StorageSerializer, StoragesSerializer, VolumeSerializer
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StorageViewSet(viewsets.ViewSet):
|
|
|
|
"""
|
|
|
|
A viewset for listing retrieving storages.
|
|
|
|
"""
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2022-08-22 12:12:33 +00:00
|
|
|
def list(self, request, compute_pk=None):
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2022-08-22 12:12:33 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
2022-11-02 13:05:41 +00:00
|
|
|
|
|
|
|
conn = wvmStorages(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
queryset = conn.get_storages_info()
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
serializer = StoragesSerializer(
|
|
|
|
queryset, many=True, context={"request": request}
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
return Response(serializer.data)
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2022-08-22 12:12:33 +00:00
|
|
|
def retrieve(self, request, pk=None, compute_pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
infoset = {
|
|
|
|
"state": conn.is_active(),
|
|
|
|
"size": conn.get_size()[0],
|
|
|
|
"free": conn.get_size()[1],
|
|
|
|
"status": conn.get_status(),
|
|
|
|
"path": conn.get_target_path(),
|
|
|
|
"type": conn.get_type(),
|
|
|
|
"autostart": conn.get_autostart(),
|
2022-11-02 13:05:41 +00:00
|
|
|
"volumes": conn.update_volumes(),
|
2022-08-22 12:12:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
serializer = StorageSerializer(
|
|
|
|
infoset, many=False, context={"request": request}
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
@action(detail=True, methods=["post"])
|
2022-08-22 12:12:33 +00:00
|
|
|
def start(self, request, pk=None, compute_pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
ret = conn.start()
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Pool start command send: " + str(ret)})
|
2022-08-22 12:12:33 +00:00
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
@action(detail=True, methods=["post"])
|
2022-08-22 12:12:33 +00:00
|
|
|
def stop(self, request, pk=None, compute_pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
ret = conn.stop()
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Pool stop command send: " + str(ret)})
|
2022-08-22 12:12:33 +00:00
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
@action(detail=True, methods=["post"])
|
2022-08-22 12:12:33 +00:00
|
|
|
def refresh(self, request, pk=None, compute_pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
ret = conn.refresh()
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Pool refresh command send: " + str(ret)})
|
2022-08-22 12:12:33 +00:00
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
@action(detail=True, methods=["post"])
|
2022-08-22 12:12:33 +00:00
|
|
|
def XML_description(self, request, pk=None, compute_pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
ret = conn._XMLDesc(0)
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"return": str(ret)})
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VolumeViewSet(viewsets.ViewSet):
|
|
|
|
|
|
|
|
"""
|
|
|
|
A simple ViewSet for listing or retrieving Storage Volumes.
|
|
|
|
"""
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2022-08-22 12:12:33 +00:00
|
|
|
serializer_class = VolumeSerializer
|
|
|
|
lookup_value_regex = "[^/]+"
|
|
|
|
|
|
|
|
def list(self, request, storage_pk=None, compute_pk=None):
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2022-08-22 12:12:33 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, storage_pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
state = conn.is_active()
|
|
|
|
|
|
|
|
if state:
|
|
|
|
conn.refresh()
|
|
|
|
volume_queryset = conn.update_volumes()
|
|
|
|
else:
|
|
|
|
volume_queryset = None
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
serializer = VolumeSerializer(
|
|
|
|
volume_queryset, many=True, context={"request": request}
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
def retrieve(self, request, storage_pk=None, compute_pk=None, pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, storage_pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
state = conn.is_active()
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2022-11-15 09:19:03 +00:00
|
|
|
volume_queryset = conn.get_volume_details(pk) if state else None
|
2022-08-22 12:12:33 +00:00
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
serializer = VolumeSerializer(
|
|
|
|
volume_queryset, many=False, context={"request": request}
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
def create(self, request, storage_pk=None, compute_pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, storage_pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
serializer = VolumeSerializer(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
state = conn.is_active()
|
|
|
|
if state:
|
|
|
|
conn.refresh()
|
|
|
|
ret = conn.create_volume(
|
2022-11-02 13:05:41 +00:00
|
|
|
serializer.validated_data["name"],
|
|
|
|
serializer.validated_data["size"],
|
|
|
|
serializer.validated_data["type"],
|
|
|
|
serializer.validated_data["meta_prealloc"],
|
2022-08-22 12:12:33 +00:00
|
|
|
int(app_settings.INSTANCE_VOLUME_DEFAULT_OWNER_UID),
|
|
|
|
int(app_settings.INSTANCE_VOLUME_DEFAULT_OWNER_GID),
|
|
|
|
)
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Volume: " + ret + " is created"})
|
2022-08-22 12:12:33 +00:00
|
|
|
else:
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Pool is not active"})
|
2022-08-22 12:12:33 +00:00
|
|
|
else:
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Data is not right for create volume"})
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
def destroy(self, request, storage_pk=None, compute_pk=None, pk=None):
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_pk)
|
|
|
|
|
2022-11-02 13:05:41 +00:00
|
|
|
conn = wvmStorage(
|
|
|
|
compute.hostname, compute.login, compute.password, compute.type, storage_pk
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
|
|
|
|
if conn.is_active():
|
|
|
|
conn.del_volume(pk)
|
|
|
|
conn.close()
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Volume: " + pk + " is deleted"})
|
2022-08-22 12:12:33 +00:00
|
|
|
else:
|
2022-11-02 13:05:41 +00:00
|
|
|
return Response({"status": "Pool is not active"})
|