uncloud-mravi/uncloud/uncloud_api/models.py

140 lines
3.8 KiB
Python
Raw Normal View History

import uuid
from django.db import models
from django.contrib.auth import get_user_model
2020-02-21 23:22:42 +00:00
# Product in DB vs. product in code
# DB:
# - need to define params (+param types) in db -> messy?
# - get /products/ is easy / automatic
#
# code
# - can have serializer/verification of fields easily in DRF
# - can have per product side effects / extra code running
# - might (??) make features easier??
# - how to setup / query the recurring period (?)
# - could get products list via getattr() + re ...Product() classes
# -> this could include the url for ordering => /order/vm_snapshot (params)
# ---> this would work with urlpatterns
# Combination: create specific product in DB (?)
# - a table per product (?) with 1 entry?
# Orders
# define state in DB
# select a price from a product => product might change, order stays
# params:
# - the product uuid or name (?) => productuuid
# - the product parameters => for each feature
#
# logs
# Should have a log = ... => 1:n field for most models!
class Product(models.Model):
2020-02-23 10:42:15 +00:00
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE,
editable=False)
2020-02-23 10:42:15 +00:00
2020-02-21 23:50:06 +00:00
# override these fields by default
2020-02-23 10:42:15 +00:00
2020-02-21 23:22:42 +00:00
description = ""
2020-02-21 23:50:06 +00:00
recurring_period = "not_recurring"
2020-02-21 23:22:42 +00:00
2020-02-21 23:50:06 +00:00
status = models.CharField(max_length=256,
choices = (
('pending', 'Pending'),
('being_created', 'Being created'),
('active', 'Active'),
2020-02-21 23:50:06 +00:00
('deleted', 'Deleted')
2020-02-23 10:42:15 +00:00
),
default='pending'
2020-02-23 08:44:55 +00:00
)
2020-02-21 23:22:42 +00:00
# This is calculated by each product and saved in the DB
recurring_price = models.FloatField(editable=False)
one_time_price = models.FloatField(editable=False)
# FIXME: need recurring_time_frame
2020-02-23 10:42:15 +00:00
class Meta:
abstract = True
def __str__(self):
return "{}".format(self.name)
2020-02-21 23:22:42 +00:00
class VMSnapshotProduct(Product):
2020-02-21 23:50:06 +00:00
price_per_gb_ssd = 0.35
price_per_gb_hdd = 1.5/100
# This we need to get from the VM
gb_ssd = models.FloatField(editable=False)
gb_hdd = models.FloatField(editable=False)
vm_uuid = models.UUIDField()
# Need to setup recurring_price and one_time_price and recurring period
2020-02-21 23:50:06 +00:00
sample_ssd = 10
sample_hdd = 100
def recurring_price(self):
return 0
def one_time_price(self):
return 0
@classmethod
def sample_price(cls):
return cls.sample_ssd * cls.price_per_gb_ssd + cls.sample_hdd * cls.price_per_gb_hdd
2020-02-21 23:22:42 +00:00
description = "Create snapshot of a VM"
2020-02-21 23:50:06 +00:00
recurring_period = "monthly"
@classmethod
def pricing_model(cls):
return """
Pricing is on monthly basis and storage prices are equivalent to the storage
price in the VM.
Price per GB SSD is: {}
Price per GB HDD is: {}
Sample price for a VM with {} GB SSD and {} GB HDD VM is: {}.
""".format(cls.price_per_gb_ssd, cls.price_per_gb_hdd,
cls.sample_ssd, cls.sample_hdd, cls.sample_price())
2020-02-21 23:50:06 +00:00
2020-02-21 23:22:42 +00:00
class Feature(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=256)
recurring_price = models.FloatField(default=0)
one_time_price = models.FloatField()
product = models.ForeignKey(Product, on_delete=models.CASCADE)
2020-02-21 23:22:42 +00:00
# params for "cpu": cpu_count -> int
# each feature can only have one parameters
# could call this "value" and set whether it is user usable
# has_value = True/False
# value = string -> int (?)
# value_int
# value_str
# value_float
2020-02-23 10:42:15 +00:00
class Meta:
abstract = True
def __str__(self):
return "'{}' - '{}'".format(self.product, self.name)