From 028f1ebe6e3e80121e58fe1bf50ffb8535ba0e91 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 2 May 2020 22:03:34 +0200 Subject: [PATCH] Entry point /beta/vm/ works for creating VM + order --- issues.org | 1 + .../uncloud/uncloud_pay/models.py | 26 +++++++++------ .../uncloud/uncloud_vm/models.py | 6 ++++ .../uncloud/uncloud_vm/serializers.py | 1 + .../uncloud/uncloud_vm/views.py | 32 ++----------------- 5 files changed, 28 insertions(+), 38 deletions(-) diff --git a/issues.org b/issues.org index f79c1dc..840ec3c 100644 --- a/issues.org +++ b/issues.org @@ -3,3 +3,4 @@ not yet high prio. * Issues ** TODO Register prefered address in User model +** TODO Allow to specify different recurring periods diff --git a/uncloud_django_based/uncloud/uncloud_pay/models.py b/uncloud_django_based/uncloud/uncloud_pay/models.py index 3b4535c..a326810 100644 --- a/uncloud_django_based/uncloud/uncloud_pay/models.py +++ b/uncloud_django_based/uncloud/uncloud_pay/models.py @@ -855,6 +855,11 @@ class Order(models.Model): recurring_price=recurring_price, description=description) + def __str__(self): + return "Order {} created at {}, {}->{}, recurring period {}".format( + self.uuid, self.creation_date, + self.starting_date, self.ending_date, + self.recurring_period) class OrderRecord(models.Model): @@ -925,23 +930,26 @@ class Product(UncloudModel): # First time saving - create an order if not self.order: billing_address = BillingAddress.get_preferred_address_for(self.owner) + print(billing_address) if not billing_address: raise ValidationError("Cannot order without a billing address") - self.order = Order(owner=self.owner, + self.order = Order.objects.create(owner=self.owner, billing_address=billing_address) - super(Product, self).save(*args, **kwargs) + print("in save op: {}".format(self)) + print(self.order) + super().save(*args, **kwargs) - # Make sure we only create records on creation. - if being_created: - record = OrderRecord( - one_time_price=self.one_time_price, - recurring_price=self.recurring_price, - description=self.description) - self.order.orderrecord_set.add(record, bulk=False) + # # Make sure we only create records on creation. + # if being_created: + # record = OrderRecord( + # one_time_price=self.one_time_price, + # recurring_price=self.recurring_price, + # description=self.description) + # self.order.orderrecord_set.add(record, bulk=False) @property def recurring_price(self): diff --git a/uncloud_django_based/uncloud/uncloud_vm/models.py b/uncloud_django_based/uncloud/uncloud_vm/models.py index 06b5386..91c9291 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/models.py +++ b/uncloud_django_based/uncloud/uncloud_vm/models.py @@ -91,6 +91,12 @@ class VMProduct(Product): RecurringPeriod.PER_MONTH, RecurringPeriod.PER_HOUR], RecurringPeriod.choices)) + def __str__(self): + return "VM {} ({} Cores/{} GB RAM) running on {} in cluster {}".format( + self.uuid, self.cores, self.ram_in_gb, + self.vmhost, self.vmcluster) + + class VMWithOSProduct(VMProduct): pass diff --git a/uncloud_django_based/uncloud/uncloud_vm/serializers.py b/uncloud_django_based/uncloud/uncloud_vm/serializers.py index a04af8f..701b187 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_vm/serializers.py @@ -122,6 +122,7 @@ class OrderVMProductSerializer(VMProductSerializer): class NicoVMProductSerializer(serializers.ModelSerializer): snapshots = VMSnapshotProductSerializer(many=True, read_only=True) + order = serializers.StringRelatedField() class Meta: model = VMProduct diff --git a/uncloud_django_based/uncloud/uncloud_vm/views.py b/uncloud_django_based/uncloud/uncloud_vm/views.py index 2d0a693..cf90891 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/views.py +++ b/uncloud_django_based/uncloud/uncloud_vm/views.py @@ -185,40 +185,14 @@ class NicoVMProductViewSet(ProductViewSet): def get_queryset(self): obj = VMProduct.objects.filter(owner=self.request.user) - return obj - # 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 = self.get_serializer(data=request.data) + serializer = self.serializer_class(data=request.data, context={'request': request}) serializer.is_valid(raise_exception=True) + vm = serializer.save(owner=request.user) - order_recurring_period = serializer.validated_data.pop("recurring_period") - order_billing_address = serializer.validated_data.pop("billing_address") - - # Create base order. - order = Order( - recurring_period=order_recurring_period, - billing_address=order_billing_address, - owner=request.user, - starting_date=timezone.now() - ) - order.save() - - # Create disk image. - disk = VMDiskProduct(owner=request.user, order=order, - **serializer.validated_data.pop("primary_disk")) - - # Create VM. - vm = serializer.save(owner=request.user, order=order, primary_disk=disk) - disk.vm = vm - disk.save() - - return Response(VMProductSerializer(vm, context={'request': request}).data) - + return Response(serializer.data) ### # Admin stuff.