Entry point /beta/vm/ works for creating VM + order
This commit is contained in:
parent
9ef5309b91
commit
028f1ebe6e
5 changed files with 28 additions and 38 deletions
|
@ -3,3 +3,4 @@
|
||||||
not yet high prio.
|
not yet high prio.
|
||||||
* Issues
|
* Issues
|
||||||
** TODO Register prefered address in User model
|
** TODO Register prefered address in User model
|
||||||
|
** TODO Allow to specify different recurring periods
|
||||||
|
|
|
@ -855,6 +855,11 @@ class Order(models.Model):
|
||||||
recurring_price=recurring_price,
|
recurring_price=recurring_price,
|
||||||
description=description)
|
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):
|
class OrderRecord(models.Model):
|
||||||
|
@ -925,23 +930,26 @@ class Product(UncloudModel):
|
||||||
# First time saving - create an order
|
# First time saving - create an order
|
||||||
if not self.order:
|
if not self.order:
|
||||||
billing_address = BillingAddress.get_preferred_address_for(self.owner)
|
billing_address = BillingAddress.get_preferred_address_for(self.owner)
|
||||||
|
print(billing_address)
|
||||||
|
|
||||||
if not billing_address:
|
if not billing_address:
|
||||||
raise ValidationError("Cannot order without a 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)
|
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.
|
# # Make sure we only create records on creation.
|
||||||
if being_created:
|
# if being_created:
|
||||||
record = OrderRecord(
|
# record = OrderRecord(
|
||||||
one_time_price=self.one_time_price,
|
# one_time_price=self.one_time_price,
|
||||||
recurring_price=self.recurring_price,
|
# recurring_price=self.recurring_price,
|
||||||
description=self.description)
|
# description=self.description)
|
||||||
self.order.orderrecord_set.add(record, bulk=False)
|
# self.order.orderrecord_set.add(record, bulk=False)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def recurring_price(self):
|
def recurring_price(self):
|
||||||
|
|
|
@ -91,6 +91,12 @@ class VMProduct(Product):
|
||||||
RecurringPeriod.PER_MONTH, RecurringPeriod.PER_HOUR],
|
RecurringPeriod.PER_MONTH, RecurringPeriod.PER_HOUR],
|
||||||
RecurringPeriod.choices))
|
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):
|
class VMWithOSProduct(VMProduct):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -122,6 +122,7 @@ class OrderVMProductSerializer(VMProductSerializer):
|
||||||
|
|
||||||
class NicoVMProductSerializer(serializers.ModelSerializer):
|
class NicoVMProductSerializer(serializers.ModelSerializer):
|
||||||
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
|
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
|
||||||
|
order = serializers.StringRelatedField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = VMProduct
|
model = VMProduct
|
||||||
|
|
|
@ -185,40 +185,14 @@ class NicoVMProductViewSet(ProductViewSet):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
obj = VMProduct.objects.filter(owner=self.request.user)
|
obj = VMProduct.objects.filter(owner=self.request.user)
|
||||||
|
|
||||||
return obj
|
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):
|
def create(self, request):
|
||||||
# Extract serializer data.
|
serializer = self.serializer_class(data=request.data, context={'request': request})
|
||||||
serializer = self.get_serializer(data=request.data)
|
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
|
vm = serializer.save(owner=request.user)
|
||||||
|
|
||||||
order_recurring_period = serializer.validated_data.pop("recurring_period")
|
return Response(serializer.data)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
# Admin stuff.
|
# Admin stuff.
|
||||||
|
|
Loading…
Reference in a new issue