forked from uncloud/uncloud
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.
|
||||
* Issues
|
||||
** 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,
|
||||
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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue