Begin to introduce a DCL alike view for VMs

This commit is contained in:
Nico Schottelius 2020-03-06 11:10:20 +01:00
parent aa8336b7e4
commit 263125048d
3 changed files with 67 additions and 0 deletions

View file

@ -127,6 +127,12 @@ class VMDiskImageProduct(models.Model):
max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT
) )
def __str__(self):
return "VMDiskImage {} ({}): {} gb".format(self.uuid,
self.name,
self.size_in_gb)
class VMDiskProduct(models.Model): class VMDiskProduct(models.Model):
""" """

View file

@ -42,6 +42,22 @@ class VMProductSerializer(serializers.HyperlinkedModelSerializer):
'cores', 'ram_in_gb', 'recurring_period'] 'cores', 'ram_in_gb', 'recurring_period']
read_only_fields = ['uuid', 'order', 'owner', 'status'] read_only_fields = ['uuid', 'order', 'owner', 'status']
class DCLVMProductSerializer(serializers.HyperlinkedModelSerializer):
"""
Create an interface similar to standard DCL
"""
# Custom field used at creation (= ordering) only.
recurring_period = serializers.ChoiceField(
choices=VMProduct.allowed_recurring_periods())
os_disk_uuid = serializers.UUIDField()
# os_disk_size =
class Meta:
model = VMProduct
class ManagedVMProductSerializer(serializers.ModelSerializer): class ManagedVMProductSerializer(serializers.ModelSerializer):
""" """
Managed VM serializer used in ungleich_service app. Managed VM serializer used in ungleich_service app.

View file

@ -148,3 +148,48 @@ class VMSnapshotProductViewSet(viewsets.ModelViewSet):
gb_hdd=hdds_size) gb_hdd=hdds_size)
return Response(serializer.data) return Response(serializer.data)
# Also create:
# - /dcl/available_os
# Basically a view of public and my disk images
# -
class DCLCreateVMProductViewSet(ProductViewSet):
"""
This view resembles the way how DCL VMs are created by default.
The user chooses an OS, os disk size, ram, cpu and whether or not to have a mapped IPv4 address
"""
permission_classes = [permissions.IsAuthenticated]
serializer_class = DCLVMProductSerializer
def get_queryset(self):
return VMProduct.objects.filter(owner=self.request.user)
# Use a database transaction so that we do not get half-created structure
# if something goes wrong.
@transaction.atomic
def create(self, request):
# Extract serializer data.
serializer = VMProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
order_recurring_period = serializer.validated_data.pop("recurring_period")
# Create base order.
order = Order.objects.create(
recurring_period=order_recurring_period,
owner=request.user
)
order.save()
# Create VM.
vm = serializer.save(owner=request.user, order=order)
# Add Product record to order (VM is mutable, allows to keep history in order).
# XXX: Move this to some kind of on_create hook in parent Product class?
order.add_record(vm.one_time_price,
vm.recurring_price(order.recurring_period), vm.description)
return Response(serializer.data)