++cleanup
Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
parent
033b1e846a
commit
028fd6789f
37 changed files with 783 additions and 420 deletions
|
@ -51,6 +51,12 @@ Installing the postgresql service is os dependent, but some hints:
|
|||
* Alpine: `apk add postgresql-server && rc-update add postgresql && rc-service postgresql start`
|
||||
* Debian/Devuan: `apt install postgresql`
|
||||
|
||||
After postresql is started, apply the migrations:
|
||||
|
||||
```
|
||||
python manage.py migrate
|
||||
```
|
||||
|
||||
### Secrets
|
||||
|
||||
cp `uncloud/secrets_sample.py` to `uncloud/secrets.py` and replace the
|
||||
|
@ -70,3 +76,20 @@ sample values with real values.
|
|||
|
||||
|
||||
### Creating a VM Snapshot
|
||||
|
||||
|
||||
## Working Beta APIs
|
||||
|
||||
These APIs can be used for internal testing.
|
||||
|
||||
### URL Overview
|
||||
|
||||
```
|
||||
http -a nicoschottelius:$(pass ungleich.ch/nico.schottelius@ungleich.ch) http://localhost:8000
|
||||
```
|
||||
|
||||
### Snapshotting
|
||||
|
||||
```
|
||||
http -a nicoschottelius:$(pass ungleich.ch/nico.schottelius@ungleich.ch) http://localhost:8000/vm/snapshot/ vm_uuid=$(uuidgen)
|
||||
```
|
||||
|
|
|
@ -60,7 +60,7 @@ INSTALLED_APPS = [
|
|||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'rest_framework',
|
||||
'uncloud_api',
|
||||
'uncloud_pay',
|
||||
'uncloud_auth',
|
||||
'uncloud_vm',
|
||||
'opennebula'
|
||||
|
|
|
@ -18,27 +18,53 @@ from django.urls import path, include
|
|||
|
||||
from rest_framework import routers
|
||||
|
||||
from uncloud_api import views as apiviews
|
||||
from uncloud_vm import views as vmviews
|
||||
from uncloud_pay import views as payviews
|
||||
from opennebula import views as oneviews
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
|
||||
router.register(r'user', apiviews.UserViewSet, basename='user')
|
||||
# user / regular urls
|
||||
router.register(r'vm/snapshot', vmviews.VMSnapshotProductViewSet, basename='vmsnapshotproduct')
|
||||
router.register(r'vm/disk', vmviews.VMDiskProductViewSet, basename='vmdiskproduct')
|
||||
router.register(r'vm/image/mine', vmviews.VMDiskImageProductMineViewSet, basename='vmdiskimagemineproduct')
|
||||
router.register(r'vm/image/public', vmviews.VMDiskImageProductPublicViewSet, basename='vmdiskimagepublicproduct')
|
||||
|
||||
# images the provider provides :-)
|
||||
# router.register(r'vm/image/official', vmviews.VMDiskImageProductPublicViewSet, basename='vmdiskimagepublicproduct')
|
||||
|
||||
|
||||
|
||||
|
||||
router.register(r'vm/snapshot', apiviews.VMSnapshotView, basename='VMSnapshot')
|
||||
router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct')
|
||||
|
||||
# TBD
|
||||
#router.register(r'vm/disk', vmviews.VMDiskProductViewSet, basename='vmdiskproduct')
|
||||
|
||||
# creates VM from os image
|
||||
#router.register(r'vm/ipv6onlyvm', vmviews.VMProductViewSet, basename='vmproduct')
|
||||
# ... AND adds IPv4 mapping
|
||||
#router.register(r'vm/dualstackvm', vmviews.VMProductViewSet, basename='vmproduct')
|
||||
|
||||
# allow vm creation from own images
|
||||
|
||||
|
||||
# Pay
|
||||
router.register(r'user', payviews.UserViewSet, basename='user')
|
||||
router.register(r'bill', payviews.BillViewSet, basename='bill')
|
||||
router.register(r'order', payviews.OrderViewSet, basename='order')
|
||||
router.register(r'payment', payviews.PaymentViewSet, basename='payment')
|
||||
|
||||
# admin/staff urls
|
||||
router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill')
|
||||
router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/payment')
|
||||
router.register(r'admin/order', payviews.AdminOrderViewSet, basename='admin/order')
|
||||
router.register(r'admin/vmhost', vmviews.VMHostViewSet)
|
||||
router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula')
|
||||
router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet)
|
||||
|
||||
|
||||
# Wire up our API using automatic URL routing.
|
||||
# Additionally, we include login URLs for the browsable API.
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
path('admin/', admin.site.urls), # login to django itself
|
||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) # for login to REST API
|
||||
]
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from .models import Product, Feature
|
||||
|
||||
#admin.site.register(Product)
|
||||
#admin.site.register(Feature)
|
|
@ -1,5 +0,0 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
name = 'uncloud_api'
|
|
@ -1,26 +0,0 @@
|
|||
import time
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
import uncloud_api.models
|
||||
|
||||
import inspect
|
||||
import sys
|
||||
import re
|
||||
|
||||
class Command(BaseCommand):
|
||||
args = '<None>'
|
||||
help = 'hacking - only use if you are Nico'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('command', type=str, help='Command')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
getattr(self, options['command'])(**options)
|
||||
|
||||
@classmethod
|
||||
def classtest(cls, **_):
|
||||
clsmembers = inspect.getmembers(sys.modules['uncloud_api.models'], inspect.isclass)
|
||||
for name, c in clsmembers:
|
||||
if re.match(r'.+Product$', name):
|
||||
print("{} -> {}".format(name, c))
|
|
@ -1,29 +0,0 @@
|
|||
import time
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from uncloud_api import models
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
args = '<None>'
|
||||
help = 'VM Snapshot support'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('command', type=str, help='Command')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
print("Snapshotting")
|
||||
#getattr(self, options['command'])(**options)
|
||||
|
||||
@classmethod
|
||||
def monitor(cls, **_):
|
||||
while True:
|
||||
try:
|
||||
tweets = models.Reply.get_target_tweets()
|
||||
responses = models.Reply.objects.values_list('tweet_id', flat=True)
|
||||
new_tweets = [x for x in tweets if x.id not in responses]
|
||||
models.Reply.send(new_tweets)
|
||||
except TweepError as e:
|
||||
print(e)
|
||||
time.sleep(60)
|
|
@ -1,19 +0,0 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-25 18:16
|
||||
|
||||
from django.db import migrations, models
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_api', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='vm_uuid',
|
||||
field=models.UUIDField(default=uuid.uuid4, editable=False),
|
||||
),
|
||||
]
|
|
@ -1,36 +0,0 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-25 19:50
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('uncloud_api', '0002_vmsnapshotproduct_vm_uuid'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='gb_hdd',
|
||||
field=models.FloatField(editable=False),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='gb_ssd',
|
||||
field=models.FloatField(editable=False),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='owner',
|
||||
field=models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='vm_uuid',
|
||||
field=models.UUIDField(),
|
||||
),
|
||||
]
|
|
@ -1,139 +0,0 @@
|
|||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
# 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):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
# override these fields by default
|
||||
|
||||
description = ""
|
||||
recurring_period = "not_recurring"
|
||||
|
||||
status = models.CharField(max_length=256,
|
||||
choices = (
|
||||
('pending', 'Pending'),
|
||||
('being_created', 'Being created'),
|
||||
('active', 'Active'),
|
||||
('deleted', 'Deleted')
|
||||
),
|
||||
default='pending'
|
||||
)
|
||||
|
||||
# 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
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __str__(self):
|
||||
return "{}".format(self.name)
|
||||
|
||||
|
||||
class VMSnapshotProduct(Product):
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
description = "Create snapshot of a VM"
|
||||
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())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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)
|
||||
|
||||
# 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
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __str__(self):
|
||||
return "'{}' - '{}'".format(self.product, self.name)
|
|
@ -1,26 +0,0 @@
|
|||
from django.contrib.auth.models import Group
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import VMSnapshotProduct
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ['url', 'username', 'email']
|
||||
|
||||
class GroupSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ['url', 'name']
|
||||
|
||||
class VMSnapshotSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = VMSnapshotProduct
|
||||
fields = ['uuid', 'status', 'recurring_price', 'one_time_price' ]
|
||||
|
||||
class VMSnapshotCreateSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = VMSnapshotProduct
|
||||
fields = '__all__'
|
|
@ -1,94 +0,0 @@
|
|||
from django.shortcuts import render
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
from rest_framework import viewsets, permissions, generics
|
||||
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
|
||||
from uncloud_vm.models import VMProduct
|
||||
from .models import VMSnapshotProduct
|
||||
from .serializers import UserSerializer, GroupSerializer, VMSnapshotSerializer, VMSnapshotCreateSerializer
|
||||
|
||||
|
||||
import inspect
|
||||
import sys
|
||||
import re
|
||||
|
||||
|
||||
# POST /vm/snapshot/ vmuuid=... => create snapshot, returns snapshot uuid
|
||||
# GET /vm/snapshot => list
|
||||
# DEL /vm/snapshot/<uuid:uuid> => delete
|
||||
# create-list -> get, post => ListCreateAPIView
|
||||
# del on other!
|
||||
class VMSnapshotView(viewsets.ViewSet):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def list(self, request):
|
||||
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
||||
serializer = VMSnapshotSerializer(queryset, many=True, context={'request': request})
|
||||
return Response(serializer.data)
|
||||
|
||||
def retrieve(self, request, pk=None):
|
||||
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
||||
vm = get_object_or_404(queryset, pk=pk)
|
||||
serializer = VMSnapshotSerializer(vm, context={'request': request})
|
||||
return Response(serializer.data)
|
||||
|
||||
def create(self, request):
|
||||
print(request.data)
|
||||
serializer = VMSnapshotCreateSerializer(data=request.data)
|
||||
|
||||
serializer.gb_ssd = 12
|
||||
serializer.gb_hdd = 120
|
||||
print("F")
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
print(serializer)
|
||||
print("A")
|
||||
serializer.save()
|
||||
print("B")
|
||||
|
||||
|
||||
# snapshot = VMSnapshotProduct(owner=request.user,
|
||||
# **serialzer.data)
|
||||
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
|
||||
# maybe drop or not --- we need something to guide the user!
|
||||
# class ProductsViewSet(viewsets.ViewSet):
|
||||
# permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
# def list(self, request):
|
||||
|
||||
# clsmembers = []
|
||||
# for modules in [ 'uncloud_api.models', 'uncloud_vm.models' ]:
|
||||
# clsmembers.extend(inspect.getmembers(sys.modules[modules], inspect.isclass))
|
||||
|
||||
|
||||
# products = []
|
||||
# for name, c in clsmembers:
|
||||
# # Include everything that ends in Product, but not Product itself
|
||||
# m = re.match(r'(?P<pname>.+)Product$', name)
|
||||
# if m:
|
||||
# products.append({
|
||||
# 'name': m.group('pname'),
|
||||
# 'description': c.description,
|
||||
# 'recurring_period': c.recurring_period,
|
||||
# 'pricing_model': c.pricing_model()
|
||||
# }
|
||||
# )
|
||||
|
||||
|
||||
# return Response(products)
|
||||
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = UserSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return self.request.user
|
3
uncloud/uncloud_net/admin.py
Normal file
3
uncloud/uncloud_net/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
uncloud/uncloud_net/apps.py
Normal file
5
uncloud/uncloud_net/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UncloudNetConfig(AppConfig):
|
||||
name = 'uncloud_net'
|
4
uncloud/uncloud_net/models.py
Normal file
4
uncloud/uncloud_net/models.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from django.db import models
|
||||
|
||||
class MACAdress(models.Model):
|
||||
prefix = 0x420000000000
|
3
uncloud/uncloud_net/views.py
Normal file
3
uncloud/uncloud_net/views.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
3
uncloud/uncloud_pay/admin.py
Normal file
3
uncloud/uncloud_pay/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
uncloud/uncloud_pay/apps.py
Normal file
5
uncloud/uncloud_pay/apps.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UncloudPayConfig(AppConfig):
|
||||
name = 'uncloud_pay'
|
56
uncloud/uncloud_pay/migrations/0001_initial.py
Normal file
56
uncloud/uncloud_pay/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-27 10:50
|
||||
|
||||
from django.conf import settings
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Bill',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('creation_date', models.DateTimeField()),
|
||||
('starting_date', models.DateTimeField()),
|
||||
('ending_date', models.DateTimeField()),
|
||||
('due_date', models.DateField()),
|
||||
('paid', models.BooleanField(default=False)),
|
||||
('valid', models.BooleanField(default=True)),
|
||||
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Payment',
|
||||
fields=[
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('amount', models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)])),
|
||||
('source', models.CharField(choices=[('wire', 'Wire Transfer'), ('strip', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown')], default='unknown', max_length=256)),
|
||||
('timestamp', models.DateTimeField(editable=False)),
|
||||
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Order',
|
||||
fields=[
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('creation_date', models.DateTimeField()),
|
||||
('starting_date', models.DateTimeField()),
|
||||
('ending_date', models.DateTimeField(blank=True, null=True)),
|
||||
('recurring_price', models.FloatField(editable=False)),
|
||||
('one_time_price', models.FloatField(editable=False)),
|
||||
('recurring_period', models.CharField(choices=[('onetime', 'Onetime'), ('per_year', 'Per Year'), ('per_month', 'Per Month'), ('per_week', 'Per Week'), ('per_day', 'Per Day'), ('per_hour', 'Per Hour'), ('per_minute', 'Per Minute'), ('per_second', 'Per Second')], default='onetime', max_length=32)),
|
||||
('bill', models.ManyToManyField(blank=True, editable=False, null=True, to='uncloud_pay.Bill')),
|
||||
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
18
uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py
Normal file
18
uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-27 12:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_pay', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='payment',
|
||||
name='source',
|
||||
field=models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown')], default='unknown', max_length=256),
|
||||
),
|
||||
]
|
123
uncloud/uncloud_pay/models.py
Normal file
123
uncloud/uncloud_pay/models.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
from django.db import models
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.validators import MinValueValidator
|
||||
|
||||
import uuid
|
||||
|
||||
AMOUNT_MAX_DIGITS=10
|
||||
AMOUNT_DECIMALS=2
|
||||
|
||||
class Bill(models.Model):
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
creation_date = models.DateTimeField()
|
||||
starting_date = models.DateTimeField()
|
||||
ending_date = models.DateTimeField()
|
||||
due_date = models.DateField()
|
||||
|
||||
paid = models.BooleanField(default=False)
|
||||
valid = models.BooleanField(default=True)
|
||||
|
||||
@property
|
||||
def amount(self):
|
||||
# iterate over all related orders
|
||||
pass
|
||||
|
||||
|
||||
class Order(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
creation_date = models.DateTimeField()
|
||||
starting_date = models.DateTimeField()
|
||||
ending_date = models.DateTimeField(blank=True,
|
||||
null=True)
|
||||
|
||||
bill = models.ManyToManyField(Bill,
|
||||
editable=False,
|
||||
blank=True,
|
||||
null=True)
|
||||
|
||||
|
||||
recurring_price = models.FloatField(editable=False)
|
||||
one_time_price = models.FloatField(editable=False)
|
||||
|
||||
recurring_period = models.CharField(max_length=32,
|
||||
choices = (
|
||||
('onetime', 'Onetime'),
|
||||
('per_year', 'Per Year'),
|
||||
('per_month', 'Per Month'),
|
||||
('per_week', 'Per Week'),
|
||||
('per_day', 'Per Day'),
|
||||
('per_hour', 'Per Hour'),
|
||||
('per_minute', 'Per Minute'),
|
||||
('per_second', 'Per Second'),
|
||||
),
|
||||
default='onetime'
|
||||
|
||||
)
|
||||
|
||||
# def amount(self):
|
||||
# amount = recurring_price
|
||||
# if recurring and first_month:
|
||||
# amount += one_time_price
|
||||
|
||||
# return amount # you get the picture
|
||||
|
||||
|
||||
|
||||
class Payment(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
amount = models.DecimalField(
|
||||
default=0.0,
|
||||
max_digits=AMOUNT_MAX_DIGITS,
|
||||
decimal_places=AMOUNT_DECIMALS,
|
||||
validators=[MinValueValidator(0)])
|
||||
|
||||
source = models.CharField(max_length=256,
|
||||
choices = (
|
||||
('wire', 'Wire Transfer'),
|
||||
('stripe', 'Stripe'),
|
||||
('voucher', 'Voucher'),
|
||||
('referral', 'Referral'),
|
||||
('unknown', 'Unknown')
|
||||
),
|
||||
default='unknown')
|
||||
timestamp = models.DateTimeField(editable=False)
|
||||
|
||||
|
||||
|
||||
|
||||
class Product(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
description = ""
|
||||
|
||||
status = models.CharField(max_length=256,
|
||||
choices = (
|
||||
('pending', 'Pending'),
|
||||
('being_created', 'Being created'),
|
||||
('active', 'Active'),
|
||||
('deleted', 'Deleted')
|
||||
),
|
||||
default='pending'
|
||||
)
|
||||
|
||||
order = models.ForeignKey(Order,
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
27
uncloud/uncloud_pay/serializers.py
Normal file
27
uncloud/uncloud_pay/serializers.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
from rest_framework import serializers
|
||||
from .models import Bill, Payment, Order
|
||||
|
||||
class BillSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Bill
|
||||
fields = ['owner', 'amount', 'due_date', 'creation_date',
|
||||
'starting_date', 'ending_date', 'paid']
|
||||
|
||||
class PaymentSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Payment
|
||||
fields = ['owner', 'amount', 'source', 'timestamp']
|
||||
|
||||
class OrderSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Order
|
||||
fields = '__all__'
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ['username', 'email']
|
||||
|
||||
def get_balance(self, obj):
|
||||
return 666
|
3
uncloud/uncloud_pay/tests.py
Normal file
3
uncloud/uncloud_pay/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
102
uncloud/uncloud_pay/views.py
Normal file
102
uncloud/uncloud_pay/views.py
Normal file
|
@ -0,0 +1,102 @@
|
|||
from django.shortcuts import render
|
||||
from django.contrib.auth import get_user_model
|
||||
from rest_framework import viewsets, permissions, status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from .models import Bill, Payment, Order
|
||||
from .serializers import BillSerializer, PaymentSerializer, UserSerializer, OrderSerializer
|
||||
from datetime import datetime
|
||||
|
||||
###
|
||||
# Standard user views:
|
||||
|
||||
class BalanceViewSet(viewsets.ViewSet):
|
||||
# here we return a number
|
||||
# number = sum(payments) - sum(bills)
|
||||
|
||||
#bills = Bill.objects.filter(owner=self.request.user)
|
||||
#payments = Payment.objects.filter(owner=self.request.user)
|
||||
|
||||
# sum_paid = sum([ amount for amount payments..,. ]) # you get the picture
|
||||
# sum_to_be_paid = sum([ amount for amount bills..,. ]) # you get the picture
|
||||
pass
|
||||
|
||||
|
||||
class BillViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
serializer_class = BillSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Bill.objects.filter(owner=self.request.user)
|
||||
|
||||
def unpaid(self, request):
|
||||
return Bill.objects.filter(owner=self.request.user, paid=False)
|
||||
|
||||
class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
serializer_class = PaymentSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Payment.objects.filter(owner=self.request.user)
|
||||
|
||||
class OrderViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
serializer_class = OrderSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Order.objects.filter(owner=self.request.user)
|
||||
|
||||
class UserViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
serializer_class = UserSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return get_user_model().objects.all()
|
||||
|
||||
@action(detail=True)
|
||||
def balance(self, request):
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
###
|
||||
# Admin views.
|
||||
|
||||
class AdminPaymentViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = PaymentSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Payment.objects.all()
|
||||
|
||||
def create(self, request):
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save(timestamp=datetime.now())
|
||||
|
||||
headers = self.get_success_headers(serializer.data)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
|
||||
class AdminBillViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = BillSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Bill.objects.all()
|
||||
|
||||
def unpaid(self, request):
|
||||
return Bill.objects.filter(owner=self.request.user, paid=False)
|
||||
|
||||
def create(self, request):
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save(created_at=datetime.now())
|
||||
|
||||
headers = self.get_success_headers(serializer.data)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
|
||||
class AdminOrderViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = OrderSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
return Order.objects.all()
|
|
@ -1,4 +1,4 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-23 17:12
|
||||
# Generated by Django 3.0.3 on 2020-02-27 10:50
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
@ -8,10 +8,10 @@ import uuid
|
|||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('uncloud_pay', '0001_initial'),
|
||||
('uncloud_vm', '0003_auto_20200225_2028'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
|
@ -20,9 +20,11 @@ class Migration(migrations.Migration):
|
|||
fields=[
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('status', models.CharField(choices=[('pending', 'Pending'), ('being_created', 'Being created'), ('active', 'Active'), ('deleted', 'Deleted')], default='pending', max_length=256)),
|
||||
('gb_ssd', models.FloatField()),
|
||||
('gb_hdd', models.FloatField()),
|
||||
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
('gb_ssd', models.FloatField(editable=False)),
|
||||
('gb_hdd', models.FloatField(editable=False)),
|
||||
('vm_uuid', models.UUIDField()),
|
||||
('order', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order')),
|
||||
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
36
uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py
Normal file
36
uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-27 12:30
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_pay', '0002_auto_20200227_1230'),
|
||||
('uncloud_vm', '0004_vmsnapshotproduct'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='vm_uuid',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vmproduct',
|
||||
name='order',
|
||||
field=models.ForeignKey(default=0, editable=False, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order'),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vmproduct',
|
||||
name='status',
|
||||
field=models.CharField(choices=[('pending', 'Pending'), ('being_created', 'Being created'), ('active', 'Active'), ('deleted', 'Deleted')], default='pending', max_length=256),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vmsnapshotproduct',
|
||||
name='vm',
|
||||
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct'),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
53
uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py
Normal file
53
uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-29 15:45
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('uncloud_vm', '0005_auto_20200227_1230'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='VMDiskImageProduct',
|
||||
fields=[
|
||||
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('name', models.CharField(max_length=256)),
|
||||
('is_os_image', models.BooleanField(default=False)),
|
||||
('is_public', models.BooleanField(default=False)),
|
||||
('size_in_gb', models.FloatField()),
|
||||
('storage_class', models.CharField(choices=[('hdd', 'HDD'), ('ssd', 'SSD')], default='ssd', max_length=32)),
|
||||
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='vmdiskproduct',
|
||||
name='storage_class',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vmdiskproduct',
|
||||
name='owner',
|
||||
field=models.ForeignKey(default=1, editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vmnetworkcard',
|
||||
name='ip_address',
|
||||
field=models.GenericIPAddressField(blank=True, null=True),
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='OperatingSystemDisk',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vmdiskproduct',
|
||||
name='image',
|
||||
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMDiskImageProduct'),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
23
uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py
Normal file
23
uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-29 15:59
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_vm', '0006_auto_20200229_1545'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='vmdiskimageproduct',
|
||||
name='import_url',
|
||||
field=models.URLField(blank=True, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmdiskimageproduct',
|
||||
name='size_in_gb',
|
||||
field=models.FloatField(blank=True, null=True),
|
||||
),
|
||||
]
|
23
uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py
Normal file
23
uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-29 16:11
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_vm', '0007_auto_20200229_1559'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='vmdiskimageproduct',
|
||||
name='status',
|
||||
field=models.CharField(choices=[('pending', 'Pending'), ('creating', 'Creating'), ('active', 'Active'), ('disabled', 'Disabled'), ('unusable', 'Unusable'), ('deleted', 'Deleted')], default='pending', max_length=32),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='vmhost',
|
||||
name='status',
|
||||
field=models.CharField(choices=[('pending', 'Pending'), ('creating', 'Creating'), ('active', 'Active'), ('disabled', 'Disabled'), ('unusable', 'Unusable'), ('deleted', 'Deleted')], default='pending', max_length=32),
|
||||
),
|
||||
]
|
|
@ -2,6 +2,18 @@ from django.db import models
|
|||
from django.contrib.auth import get_user_model
|
||||
import uuid
|
||||
|
||||
from uncloud_pay.models import Product
|
||||
|
||||
STATUS_CHOICES = (
|
||||
('pending', 'Pending'), # Initial state
|
||||
('creating', 'Creating'), # Creating VM/image/etc.
|
||||
('active', 'Active'), # Is usable / active
|
||||
('disabled', 'Disabled'), # Is usable, but cannot be used for new things
|
||||
('unusable', 'Unusable'), # Has some kind of error
|
||||
('deleted', 'Deleted'), # Does not exist anymore, only DB entry as a log
|
||||
)
|
||||
|
||||
STATUS_DEFAULT='pending'
|
||||
|
||||
class VMHost(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
@ -20,23 +32,12 @@ class VMHost(models.Model):
|
|||
|
||||
|
||||
status = models.CharField(max_length=32,
|
||||
choices = (
|
||||
('pending', 'Pending'),
|
||||
('active', 'Active'),
|
||||
('unusable', 'Unusable'),
|
||||
('deleted', 'Deleted'),
|
||||
),
|
||||
default='pending'
|
||||
choices=STATUS_CHOICES,
|
||||
default=STATUS_DEFAULT
|
||||
)
|
||||
|
||||
|
||||
class VMProduct(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
class VMProduct(Product):
|
||||
vmhost = models.ForeignKey(VMHost,
|
||||
on_delete=models.CASCADE,
|
||||
editable=False,
|
||||
|
@ -50,10 +51,31 @@ class VMProduct(models.Model):
|
|||
class VMWithOSProduct(VMProduct):
|
||||
pass
|
||||
|
||||
class VMDiskProduct(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
size_in_gb = models.FloatField()
|
||||
|
||||
class VMDiskImageProduct(models.Model):
|
||||
"""
|
||||
Images are used for cloning/linking.
|
||||
|
||||
They are the base for images.
|
||||
|
||||
"""
|
||||
|
||||
uuid = models.UUIDField(primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
name = models.CharField(max_length=256)
|
||||
is_os_image = models.BooleanField(default=False)
|
||||
is_public = models.BooleanField(default=False)
|
||||
|
||||
size_in_gb = models.FloatField(null=True,
|
||||
blank=True)
|
||||
import_url = models.URLField(null=True,
|
||||
blank=True)
|
||||
|
||||
|
||||
storage_class = models.CharField(max_length=32,
|
||||
choices = (
|
||||
|
@ -63,11 +85,42 @@ class VMDiskProduct(models.Model):
|
|||
default='ssd'
|
||||
)
|
||||
|
||||
class OperatingSystemDisk(VMDiskProduct):
|
||||
""" Defines an Operating System Disk that can be cloned for a VM """
|
||||
os_name = models.CharField(max_length=128)
|
||||
status = models.CharField(max_length=32,
|
||||
choices=STATUS_CHOICES,
|
||||
default=STATUS_DEFAULT
|
||||
)
|
||||
|
||||
class VMDiskProduct(models.Model):
|
||||
"""
|
||||
The VMDiskProduct is attached to a VM.
|
||||
|
||||
It is based on a VMDiskImageProduct that will be used as a basis.
|
||||
|
||||
It can be enlarged, but not shrinked compared to the VMDiskImageProduct.
|
||||
"""
|
||||
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE)
|
||||
|
||||
size_in_gb = models.FloatField(blank=True)
|
||||
|
||||
|
||||
class VMNetworkCard(models.Model):
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
|
||||
mac_address = models.IntegerField()
|
||||
|
||||
ip_address = models.GenericIPAddressField(blank=True,
|
||||
null=True)
|
||||
|
||||
|
||||
class VMSnapshotProduct(Product):
|
||||
gb_ssd = models.FloatField(editable=False)
|
||||
gb_hdd = models.FloatField(editable=False)
|
||||
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
|
|
|
@ -1,15 +1,57 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
|
||||
from rest_framework import serializers
|
||||
from .models import VMHost, VMProduct
|
||||
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
|
||||
|
||||
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
|
||||
GB_SSD_PER_DAY=0.012
|
||||
GB_HDD_PER_DAY=0.0006
|
||||
|
||||
GB_SSD_PER_DAY=0.012
|
||||
GB_HDD_PER_DAY=0.0006
|
||||
|
||||
|
||||
class VMHostSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = VMHost
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class VMProductSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class VMProductSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = VMProduct
|
||||
fields = '__all__'
|
||||
|
||||
class VMDiskProductSerializer(serializers.ModelSerializer):
|
||||
# vm = VMProductSerializer()
|
||||
|
||||
class Meta:
|
||||
model = VMDiskProduct
|
||||
fields = '__all__'
|
||||
|
||||
class VMDiskImageProductSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = VMDiskImageProduct
|
||||
fields = '__all__'
|
||||
|
||||
class VMSnapshotProductSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = VMSnapshotProduct
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
# verify that vm.owner == user.request
|
||||
def validate_vm(self, value):
|
||||
if not value.owner == self.context['request'].user:
|
||||
raise serializers.ValidationError("VM {} not found for owner {}.".format(value,
|
||||
self.context['request'].user))
|
||||
disks = VMDiskProduct.objects.filter(vm=value)
|
||||
|
||||
if len(disks) == 0:
|
||||
raise serializers.ValidationError("VM {} does not have any disks, cannot snapshot".format(value.uuid))
|
||||
|
||||
return value
|
||||
|
||||
pricing = {}
|
||||
pricing['per_gb_ssd'] = 0.012
|
||||
pricing['per_gb_hdd'] = 0.0006
|
||||
pricing['recurring_period'] = 'per_day'
|
||||
|
|
|
@ -5,15 +5,78 @@ from django.shortcuts import get_object_or_404
|
|||
|
||||
from rest_framework import viewsets, permissions
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from .models import VMHost, VMProduct
|
||||
from .serializers import VMHostSerializer, VMProductSerializer
|
||||
|
||||
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
|
||||
from uncloud_pay.models import Order
|
||||
|
||||
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer, VMDiskImageProductSerializer, VMDiskProductSerializer
|
||||
|
||||
|
||||
import datetime
|
||||
|
||||
class VMHostViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = VMHostSerializer
|
||||
queryset = VMHost.objects.all()
|
||||
permission_classes = [permissions.IsAdminUser]
|
||||
|
||||
class VMDiskImageProductMineViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
serializer_class = VMDiskImageProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return VMDiskImageProduct.objects.filter(owner=self.request.user)
|
||||
|
||||
def create(self, request):
|
||||
serializer = VMDiskImageProductSerializer(data=request.data, context={'request': request})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
# did not specify size NOR import url?
|
||||
if not serializer.validated_data['size_in_gb']:
|
||||
if not serializer.validated_data['import_url']:
|
||||
raise ValidationError(detail={ 'error_mesage': 'Specify either import_url or size_in_gb' })
|
||||
|
||||
serializer.save(owner=request.user)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class VMDiskImageProductPublicViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
serializer_class = VMDiskImageProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return VMDiskImageProduct.objects.filter(is_public=True)
|
||||
|
||||
class VMDiskProductViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
Let a user modify their own VMDisks
|
||||
"""
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
serializer_class = VMDiskProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return VMDiskProduct.objects.filter(owner=self.request.user)
|
||||
|
||||
def create(self, request):
|
||||
serializer = VMDiskProductSerializer(data=request.data, context={'request': request})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
# get disk size from image, if not specified
|
||||
if not 'size_in_gb' in serializer.validated_data:
|
||||
size_in_gb = serializer.validated_data['image'].size_in_gb
|
||||
else:
|
||||
size_in_gb = serializer.validated_data['size_in_gb']
|
||||
|
||||
if size_in_gb < serializer.validated_data['image'].size_in_gb:
|
||||
raise ValidationError(detail={ 'error_mesage': 'Size is smaller than original image' })
|
||||
|
||||
|
||||
serializer.save(owner=request.user, size_in_gb=size_in_gb)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
|
||||
class VMProductViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
serializer_class = VMProductSerializer
|
||||
|
@ -24,6 +87,53 @@ class VMProductViewSet(viewsets.ModelViewSet):
|
|||
def create(self, request):
|
||||
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save(owner=request.user)
|
||||
# Create order
|
||||
now = datetime.datetime.now()
|
||||
order = Order(owner=request.user,
|
||||
creation_date=now,
|
||||
starting_date=now,
|
||||
recurring_price=20,
|
||||
one_time_price=0,
|
||||
recurring_period="per_month")
|
||||
order.save()
|
||||
|
||||
serializer.save(owner=request.user, order=order)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
class VMSnapshotProductViewSet(viewsets.ModelViewSet):
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
serializer_class = VMSnapshotProductSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
return VMSnapshotProduct.objects.filter(owner=self.request.user)
|
||||
|
||||
def create(self, request):
|
||||
serializer = VMSnapshotProductSerializer(data=request.data, context={'request': request})
|
||||
|
||||
# This verifies that the VM belongs to the request user
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
disks = VMDiskProduct.objects.filter(vm=serializer.validated_data['vm'])
|
||||
ssds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'ssd'])
|
||||
hdds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'hdd'])
|
||||
|
||||
recurring_price = serializer.pricing['per_gb_ssd'] * ssds_size + serializer.pricing['per_gb_hdd'] * hdds_size
|
||||
recurring_period = serializer.pricing['recurring_period']
|
||||
|
||||
# Create order
|
||||
now = datetime.datetime.now()
|
||||
order = Order(owner=request.user,
|
||||
creation_date=now,
|
||||
starting_date=now,
|
||||
recurring_price=recurring_price,
|
||||
one_time_price=0,
|
||||
recurring_period=recurring_period)
|
||||
order.save()
|
||||
|
||||
serializer.save(owner=request.user,
|
||||
order=order,
|
||||
gb_ssd=ssds_size,
|
||||
gb_hdd=hdds_size)
|
||||
|
||||
return Response(serializer.data)
|
||||
|
|
Loading…
Reference in a new issue