From 0c7ca1147a4cc813b18574a04e09ee7ae7cc2adf Mon Sep 17 00:00:00 2001 From: Ahmed Bilal <49-ahmedbilal@users.noreply.code.ungleich.ch> Date: Wed, 26 Feb 2020 11:31:17 +0100 Subject: [PATCH 01/13] fix migrations the ugly way Signed-off-by: Nico Schottelius --- nicohack202002/uncloud/opennebula/models.py | 11 -- nicohack202002/uncloud/opennebula/views.py | 59 -------- nicohack202002/uncloud/uncloud_api/models.py | 125 ---------------- nicohack202002/uncloud/uncloud_api/views.py | 83 ----------- notes-nico.org | 22 +-- uncloud/README.md | 18 +++ .../opennebula/management/commands/syncvm.py | 37 ++--- .../migrations/0004_auto_20200225_1816.py | 23 +++ uncloud/opennebula/models.py | 3 +- uncloud/requirements.txt | 2 +- uncloud/uncloud/settings.py | 7 +- uncloud/uncloud/urls.py | 23 +-- .../uncloud_api/migrations/0001_initial.py | 2 +- .../0002_vmsnapshotproduct_vm_uuid.py | 19 +++ .../migrations/0003_auto_20200225_1950.py | 36 +++++ uncloud/uncloud_api/models.py | 35 +++-- uncloud/uncloud_api/serializers.py | 17 ++- uncloud/uncloud_api/views.py | 133 ++++++++++-------- .../management/commands/schedulevms.py | 21 +++ .../management/commands/vmhealth.py | 26 ++++ uncloud/uncloud_vm/migrations/0001_initial.py | 75 ++++++++++ .../migrations/0002_auto_20200225_1952.py | 38 +++++ .../migrations/0003_auto_20200225_2028.py | 19 +++ uncloud/uncloud_vm/migrations/__init__.py | 0 uncloud/uncloud_vm/models.py | 69 ++++++++- uncloud/uncloud_vm/serializers.py | 15 ++ uncloud/uncloud_vm/tests.py | 3 - uncloud/uncloud_vm/views.py | 35 +++-- 28 files changed, 524 insertions(+), 432 deletions(-) delete mode 100644 nicohack202002/uncloud/opennebula/models.py delete mode 100644 nicohack202002/uncloud/opennebula/views.py delete mode 100644 nicohack202002/uncloud/uncloud_api/models.py delete mode 100644 nicohack202002/uncloud/uncloud_api/views.py create mode 100644 uncloud/opennebula/migrations/0004_auto_20200225_1816.py create mode 100644 uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py create mode 100644 uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py create mode 100644 uncloud/uncloud_vm/management/commands/schedulevms.py create mode 100644 uncloud/uncloud_vm/management/commands/vmhealth.py create mode 100644 uncloud/uncloud_vm/migrations/0001_initial.py create mode 100644 uncloud/uncloud_vm/migrations/0002_auto_20200225_1952.py create mode 100644 uncloud/uncloud_vm/migrations/0003_auto_20200225_2028.py create mode 100644 uncloud/uncloud_vm/migrations/__init__.py create mode 100644 uncloud/uncloud_vm/serializers.py delete mode 100644 uncloud/uncloud_vm/tests.py diff --git a/nicohack202002/uncloud/opennebula/models.py b/nicohack202002/uncloud/opennebula/models.py deleted file mode 100644 index 915862a..0000000 --- a/nicohack202002/uncloud/opennebula/models.py +++ /dev/null @@ -1,11 +0,0 @@ -import uuid - -from django.db import models -from django.contrib.auth import get_user_model - - -class VM(models.Model): - uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) - vmid = models.IntegerField() - owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) - data = models.CharField(max_length=65536, null=True) diff --git a/nicohack202002/uncloud/opennebula/views.py b/nicohack202002/uncloud/opennebula/views.py deleted file mode 100644 index 1030101..0000000 --- a/nicohack202002/uncloud/opennebula/views.py +++ /dev/null @@ -1,59 +0,0 @@ -import json - -from rest_framework import generics -from rest_framework.authentication import SessionAuthentication, BasicAuthentication -from rest_framework.permissions import IsAuthenticated, IsAdminUser - -from .models import VM -from .serializers import VMSerializer - -class VMList(generics.ListAPIView): - authentication_classes = [SessionAuthentication, BasicAuthentication] - permission_classes = [IsAuthenticated, IsAdminUser] - queryset = VM.objects.all() - serializer_class = VMSerializer - - -class VMDetail(generics.RetrieveAPIView): - authentication_classes = [SessionAuthentication, BasicAuthentication] - permission_classes = [IsAuthenticated, IsAdminUser] - lookup_field = 'uuid' - queryset = VM.objects.all() - serializer_class = VMSerializer - - -class UserVMList(generics.ListAPIView): - authentication_classes = [SessionAuthentication, BasicAuthentication] - permission_classes = [IsAuthenticated] - serializer_class = VMSerializer - - def get_queryset(self): - user_email = self.request.user.ldap_user.attrs.data['mail'] - vms = [] - for mail in user_email: - vms += VM.objects.filter(owner__username=mail) - - for vm in vms: - data = json.loads(vm.data) - vm_template = data['TEMPLATE'] - vm.data = { - 'cpu': vm_template['VCPU'], - 'ram': vm_template['MEMORY'], - 'nic': vm_template['NIC'], - 'disks': vm_template['DISK'] - } - - return vms - -####################################### -# Following for quick experimentation # -####################################### - -# from django.http import HttpResponse -# -# def test(request): -# user_email = request.user.ldap_user.attrs.data['mail'] -# vms = [] -# for mail in user_email: -# vms += VM.objects.filter(owner__username=mail) -# return HttpResponse("Hello World") diff --git a/nicohack202002/uncloud/uncloud_api/models.py b/nicohack202002/uncloud/uncloud_api/models.py deleted file mode 100644 index 7eaec7b..0000000 --- a/nicohack202002/uncloud/uncloud_api/models.py +++ /dev/null @@ -1,125 +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): - # override these fields by default - description = "" - recurring_period = "not_recurring" - uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) - status = models.CharField( - max_length=256, choices=( - ('pending', 'Pending'), - ('being_created', 'Being created'), - ('created_active', 'Created'), - ('deleted', 'Deleted') - ), - default='pending' - ) - - def __str__(self): - return "{}".format(self.name) - - -class VMSnapshotProduct(Product): - price_per_gb_ssd = 0.35 - price_per_gb_hdd = 1.5/100 - - 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()) - - gb_ssd = models.FloatField() - gb_hdd = models.FloatField() - - -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 - - def __str__(self): - return "'{}' - '{}'".format(self.product, self.name) - - -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) - - product = models.ForeignKey(Product, - on_delete=models.CASCADE) - - -class VMSnapshotOrder(Order): - pass diff --git a/nicohack202002/uncloud/uncloud_api/views.py b/nicohack202002/uncloud/uncloud_api/views.py deleted file mode 100644 index 68963ff..0000000 --- a/nicohack202002/uncloud/uncloud_api/views.py +++ /dev/null @@ -1,83 +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 .serializers import UserSerializer, GroupSerializer -from rest_framework.views import APIView -from rest_framework.response import Response - - -class CreditCardViewSet(viewsets.ModelViewSet): - - """ - API endpoint that allows credit cards to be listed - """ - queryset = get_user_model().objects.all().order_by('-date_joined') - serializer_class = UserSerializer - - permission_classes = [permissions.IsAuthenticated] - - -class UserViewSet(viewsets.ModelViewSet): - - """ - API endpoint that allows users to be viewed or edited. - """ - queryset = get_user_model().objects.all().order_by('-date_joined') - serializer_class = UserSerializer - - permission_classes = [permissions.IsAuthenticated] - -class GroupViewSet(viewsets.ModelViewSet): - """ - API endpoint that allows groups to be viewed or edited. - """ - queryset = Group.objects.all() - serializer_class = GroupSerializer - - permission_classes = [permissions.IsAuthenticated] - -class GroupViewSet(viewsets.ModelViewSet): - """ - API endpoint that allows groups to be viewed or edited. - """ - queryset = Group.objects.all() - serializer_class = GroupSerializer - - permission_classes = [permissions.IsAuthenticated] - - -# POST /vm/snapshot/ vmuuid=... => create snapshot, returns snapshot uuid -# GET /vm/snapshot => list -# DEL /vm/snapshot/ => delete -# create-list -> get, post => ListCreateAPIView -# del on other! -class VMSnapshotView(generics.ListCreateAPIView): - #lookup_field = 'uuid' - permission_classes = [permissions.IsAuthenticated] - -import inspect -import sys -import re - -# Next: create /order/ urls -# Next: strip off "Product" at the end -class ProductsView(APIView): - def get(self, request, format=None): - clsmembers = inspect.getmembers(sys.modules['uncloud_api.models'], inspect.isclass) - products = [] - for name, c in clsmembers: - # Include everything that ends in Product, but not Product itself - m = re.match(r'(?P.+)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) diff --git a/notes-nico.org b/notes-nico.org index 03c1b97..811fbff 100644 --- a/notes-nico.org +++ b/notes-nico.org @@ -9,6 +9,16 @@ vmuuid=$(http nicocustomer http -a nicocustomer:xxx http://uncloud.ch/vm/create_snapshot uuid= password=... ``` +** backend realisation +*** list snapshots + - have them in the DB + - create an entry on create +*** creating snapshots + - vm sync / fsync? + - rbd snapshot + - host/cluster mapping? + - need image(s) + * steps ** DONE authenticate via ldap CLOSED: [2020-02-20 Thu 19:05] @@ -50,16 +60,8 @@ password=... ** viewset: .list and .create ** view: .get .post * TODO register CC -* TODO list products -* ahmed -** schemas -*** field: is_valid? - used by schemas -*** definition of a "schema" -* penguin pay -## How to place a order with penguin pay - -### Requirements - +* DONE list products + CLOSED: [2020-02-24 Mon 20:15] * An ungleich account - can be registered for free on https://account.ungleich.ch * httpie installed (provides the http command) diff --git a/uncloud/README.md b/uncloud/README.md index 9db1c5c..e0c0d10 100644 --- a/uncloud/README.md +++ b/uncloud/README.md @@ -39,9 +39,27 @@ Then create the database owner by the new role: postgres=# create database uncloud owner nico; ``` +Installing the postgresql service is os dependent, but some hints: + +* Alpine: `apk add postgresql-server && rc-update add postgresql && rc-service postgresql start` ### Secrets cp `uncloud/secrets_sample.py` to `uncloud/secrets.py` and replace the sample values with real values. + + +## Flows / Orders + +### Creating a VMHost + + + +### Creating a VM + +* Create a VMHost +* Create a VM on a VMHost + + +### Creating a VM Snapshot diff --git a/uncloud/opennebula/management/commands/syncvm.py b/uncloud/opennebula/management/commands/syncvm.py index 55844e3..779db61 100644 --- a/uncloud/opennebula/management/commands/syncvm.py +++ b/uncloud/opennebula/management/commands/syncvm.py @@ -8,15 +8,10 @@ from xmlrpc.client import ServerProxy as RPCClient from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model from xmltodict import parse -from ungleich_common.ldap.ldap_manager import LdapManager from opennebula.models import VM as VMModel - -def find_user_based_on_email(users, email): - for user in users: - if email in user.mail.values: - return user +from django_auth_ldap.backend import LDAPBackend class Command(BaseCommand): @@ -26,39 +21,29 @@ class Command(BaseCommand): pass def handle(self, *args, **options): - ldap_server_uri = secrets.LDAP_SERVER_URI.split(',')[0] - ldap_manager = LdapManager( - server=ldap_server_uri, - admin_dn=secrets.LDAP_ADMIN_DN, - admin_password=secrets.LDAP_ADMIN_PASSWORD, - ) - users = ldap_manager.get('') # Get all users - with RPCClient(secrets.OPENNEBULA_URL) as rpc_client: success, response, *_ = rpc_client.one.vmpool.infoextended( secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1 ) if success: vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM'] - unknown_user_with_email = set() + unknown_user = set() + + backend = LDAPBackend() for vm in vms: vm_id = vm['ID'] - vm_owner_email = vm['UNAME'] + vm_owner = vm['UNAME'] + + user = backend.populate_user(username=vm_owner) - user = find_user_based_on_email(users, vm_owner_email) if not user: - unknown_user_with_email.add(vm_owner_email) + unknown_user.add(vm_owner) else: - try: - user_in_db = get_user_model().objects.get(email=vm_owner_email) - except get_user_model().DoesNotExist: - user_in_db = get_user_model().objects.create_user(username=user.uid, email=vm_owner_email) VMModel.objects.update_or_create( - id=f'opennebula{vm_id}', - defaults={'data': vm, 'owner': user_in_db} + vmid=vm_id, + defaults={'data': vm, 'owner': user} ) - print('User with email but not found in ldap:', unknown_user_with_email) + print('User not found in ldap:', unknown_user) else: print(response) - print(secrets.OPENNEBULA_USER_PASS) diff --git a/uncloud/opennebula/migrations/0004_auto_20200225_1816.py b/uncloud/opennebula/migrations/0004_auto_20200225_1816.py new file mode 100644 index 0000000..5b39f26 --- /dev/null +++ b/uncloud/opennebula/migrations/0004_auto_20200225_1816.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.3 on 2020-02-25 18:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('opennebula', '0003_auto_20200225_1428'), + ] + + operations = [ + migrations.RemoveField( + model_name='vm', + name='id', + ), + migrations.AddField( + model_name='vm', + name='vmid', + field=models.IntegerField(default=42, primary_key=True, serialize=False), + preserve_default=False, + ), + ] diff --git a/uncloud/opennebula/models.py b/uncloud/opennebula/models.py index 904699d..fff811b 100644 --- a/uncloud/opennebula/models.py +++ b/uncloud/opennebula/models.py @@ -5,7 +5,7 @@ from django.contrib.postgres.fields import JSONField class VM(models.Model): - id = models.CharField(primary_key=True, editable=True, default=uuid.uuid4, unique=True, max_length=64) + vmid = models.IntegerField(primary_key=True) owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) data = JSONField() @@ -34,7 +34,6 @@ class VM(models.Model): disks = [] if 'DISK' in self.data['TEMPLATE']: - if type(self.data['TEMPLATE']['DISK']) is dict: disks = [ self.data['TEMPLATE']['DISK'] ] else: diff --git a/uncloud/requirements.txt b/uncloud/requirements.txt index e79f479..1b4e05b 100644 --- a/uncloud/requirements.txt +++ b/uncloud/requirements.txt @@ -3,4 +3,4 @@ djangorestframework django-auth-ldap stripe xmltodict -git+https://code.ungleich.ch/ahmedbilal/ungleich-common/#egg=ungleich-common-ldap&subdirectory=ldap +psycopg2 diff --git a/uncloud/uncloud/settings.py b/uncloud/uncloud/settings.py index 91d2f73..614cd25 100644 --- a/uncloud/uncloud/settings.py +++ b/uncloud/uncloud/settings.py @@ -18,7 +18,7 @@ import ldap # Uncommitted file with secrets import uncloud.secrets -from django_auth_ldap.config import LDAPSearch +from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion # Uncommitted file with local settings i.e logging try: @@ -62,6 +62,7 @@ INSTALLED_APPS = [ 'rest_framework', 'uncloud_api', 'uncloud_auth', + 'uncloud_vm', 'opennebula' ] @@ -129,9 +130,7 @@ AUTH_LDAP_USER_ATTR_MAP = { AUTH_LDAP_BIND_DN = uncloud.secrets.LDAP_ADMIN_DN AUTH_LDAP_BIND_PASSWORD = uncloud.secrets.LDAP_ADMIN_PASSWORD -AUTH_LDAP_USER_SEARCH = LDAPSearch( - "dc=ungleich,dc=ch", ldap.SCOPE_SUBTREE, "(uid=%(user)s)" -) +AUTH_LDAP_USER_SEARCH = LDAPSearch("dc=ungleich,dc=ch", ldap.SCOPE_SUBTREE, "(uid=%(user)s)") ################################################################################ diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index a01ef66..23392c5 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -17,21 +17,28 @@ from django.contrib import admin from django.urls import path, include from rest_framework import routers -from uncloud_api import views +from uncloud_api import views as apiviews +from uncloud_vm import views as vmviews from opennebula import views as oneviews router = routers.DefaultRouter() -router.register(r'users', views.UserViewSet) -router.register(r'groups', views.GroupViewSet) -router.register(r'opennebula', oneviews.VMViewSet, basename='opennebula') -router.register(r'opennebula_raw', oneviews.RawVMViewSet) + +router.register(r'user', apiviews.UserViewSet, basename='user') + +router.register(r'vm/snapshot', apiviews.VMSnapshotView, basename='VMSnapshot') +router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') + +# admin/staff urls +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), - path('products/', views.ProductsView.as_view(), name='products'), - path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) + path('admin/', admin.site.urls), # login to django itself + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) # for login to REST API ] diff --git a/uncloud/uncloud_api/migrations/0001_initial.py b/uncloud/uncloud_api/migrations/0001_initial.py index c549a9d..67bdd2e 100644 --- a/uncloud/uncloud_api/migrations/0001_initial.py +++ b/uncloud/uncloud_api/migrations/0001_initial.py @@ -19,7 +19,7 @@ class Migration(migrations.Migration): name='VMSnapshotProduct', fields=[ ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), - ('status', models.CharField(choices=[('pending', 'Pending'), ('being_created', 'Being created'), ('created_active', 'Created'), ('deleted', 'Deleted')], default='pending', max_length=256)), + ('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)), diff --git a/uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py b/uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py new file mode 100644 index 0000000..b35317e --- /dev/null +++ b/uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py @@ -0,0 +1,19 @@ +# 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), + ), + ] diff --git a/uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py b/uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py new file mode 100644 index 0000000..be7624c --- /dev/null +++ b/uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py @@ -0,0 +1,36 @@ +# 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(), + ), + ] diff --git a/uncloud/uncloud_api/models.py b/uncloud/uncloud_api/models.py index 11a7560..6a6f9c8 100644 --- a/uncloud/uncloud_api/models.py +++ b/uncloud/uncloud_api/models.py @@ -34,7 +34,8 @@ from django.contrib.auth import get_user_model 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) + on_delete=models.CASCADE, + editable=False) # override these fields by default @@ -45,12 +46,18 @@ class Product(models.Model): choices = ( ('pending', 'Pending'), ('being_created', 'Being created'), - ('created_active', '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 @@ -62,6 +69,14 @@ 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 @@ -92,8 +107,10 @@ 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()) - gb_ssd = models.FloatField() - gb_hdd = models.FloatField() + + + + @@ -120,13 +137,3 @@ class Feature(models.Model): def __str__(self): return "'{}' - '{}'".format(self.product, self.name) - - -# 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) - -# product = models.ForeignKey(Product, -# on_delete=models.CASCADE) diff --git a/uncloud/uncloud_api/serializers.py b/uncloud/uncloud_api/serializers.py index 1573bf0..7dc3686 100644 --- a/uncloud/uncloud_api/serializers.py +++ b/uncloud/uncloud_api/serializers.py @@ -3,17 +3,24 @@ from django.contrib.auth import get_user_model from rest_framework import serializers +from .models import VMSnapshotProduct -class UserSerializer(serializers.HyperlinkedModelSerializer): +class UserSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() - fields = ['url', 'username', 'email', 'groups'] - + fields = ['url', 'username', 'email'] class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group fields = ['url', 'name'] -class VMSnapshotSerializer(serializers.Serializer): - pass +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__' diff --git a/uncloud/uncloud_api/views.py b/uncloud/uncloud_api/views.py index 68963ff..eb4cc77 100644 --- a/uncloud/uncloud_api/views.py +++ b/uncloud/uncloud_api/views.py @@ -3,49 +3,18 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from rest_framework import viewsets, permissions, generics -from .serializers import UserSerializer, GroupSerializer + from rest_framework.views import APIView from rest_framework.response import Response - -class CreditCardViewSet(viewsets.ModelViewSet): - - """ - API endpoint that allows credit cards to be listed - """ - queryset = get_user_model().objects.all().order_by('-date_joined') - serializer_class = UserSerializer - - permission_classes = [permissions.IsAuthenticated] +from uncloud_vm.models import VMProduct +from .models import VMSnapshotProduct +from .serializers import UserSerializer, GroupSerializer, VMSnapshotSerializer, VMSnapshotCreateSerializer -class UserViewSet(viewsets.ModelViewSet): - - """ - API endpoint that allows users to be viewed or edited. - """ - queryset = get_user_model().objects.all().order_by('-date_joined') - serializer_class = UserSerializer - - permission_classes = [permissions.IsAuthenticated] - -class GroupViewSet(viewsets.ModelViewSet): - """ - API endpoint that allows groups to be viewed or edited. - """ - queryset = Group.objects.all() - serializer_class = GroupSerializer - - permission_classes = [permissions.IsAuthenticated] - -class GroupViewSet(viewsets.ModelViewSet): - """ - API endpoint that allows groups to be viewed or edited. - """ - queryset = Group.objects.all() - serializer_class = GroupSerializer - - permission_classes = [permissions.IsAuthenticated] +import inspect +import sys +import re # POST /vm/snapshot/ vmuuid=... => create snapshot, returns snapshot uuid @@ -53,31 +22,73 @@ class GroupViewSet(viewsets.ModelViewSet): # DEL /vm/snapshot/ => delete # create-list -> get, post => ListCreateAPIView # del on other! -class VMSnapshotView(generics.ListCreateAPIView): - #lookup_field = 'uuid' +class VMSnapshotView(viewsets.ViewSet): permission_classes = [permissions.IsAuthenticated] -import inspect -import sys -import re + def list(self, request): + queryset = VMSnapshotProduct.objects.filter(owner=request.user) + serializer = VMSnapshotSerializer(queryset, many=True, context={'request': request}) + return Response(serializer.data) -# Next: create /order/ urls -# Next: strip off "Product" at the end -class ProductsView(APIView): - def get(self, request, format=None): - clsmembers = inspect.getmembers(sys.modules['uncloud_api.models'], inspect.isclass) - products = [] - for name, c in clsmembers: - # Include everything that ends in Product, but not Product itself - m = re.match(r'(?P.+)Product$', name) - if m: - products.append({ - 'name': m.group('pname'), - 'description': c.description, - 'recurring_period': c.recurring_period, - 'pricing_model': c.pricing_model() - } - ) + 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") - return Response(products) + # 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.+)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 diff --git a/uncloud/uncloud_vm/management/commands/schedulevms.py b/uncloud/uncloud_vm/management/commands/schedulevms.py new file mode 100644 index 0000000..836e100 --- /dev/null +++ b/uncloud/uncloud_vm/management/commands/schedulevms.py @@ -0,0 +1,21 @@ +import json + +import uncloud.secrets as secrets + +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model + +from uncloud_vm.models import VMProduct, VMHost + +class Command(BaseCommand): + help = 'Select VM Host for VMs' + + def add_arguments(self, parser): + pass + + def handle(self, *args, **options): + pending_vms = VMProduct.objects.filter(vmhost__isnull=True) + vmhosts = VMHost.objects.filter(status='active') + for vm in pending_vms: + print(vm) + # FIXME: implement smart placement diff --git a/uncloud/uncloud_vm/management/commands/vmhealth.py b/uncloud/uncloud_vm/management/commands/vmhealth.py new file mode 100644 index 0000000..9397b16 --- /dev/null +++ b/uncloud/uncloud_vm/management/commands/vmhealth.py @@ -0,0 +1,26 @@ +import json + +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model + +from uncloud_vm.models import VMProduct, VMHost + +class Command(BaseCommand): + help = 'Check health of VMs and VMHosts' + + def add_arguments(self, parser): + pass + + def handle(self, *args, **options): + pending_vms = VMProduct.objects.filter(vmhost__isnull=True) + vmhosts = VMHost.objects.filter(status='active') + + # 1. Check that all active hosts reported back N seconds ago + # 2. Check that no VM is running on a dead host + # 3. Migrate VMs if necessary + # 4. Check that no VMs have been pending for longer than Y seconds + + # If VM snapshots exist without a VM -> notify user (?) + + + print("Nothing is good, you should implement me") diff --git a/uncloud/uncloud_vm/migrations/0001_initial.py b/uncloud/uncloud_vm/migrations/0001_initial.py new file mode 100644 index 0000000..dc4d657 --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0001_initial.py @@ -0,0 +1,75 @@ +# 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 +import uuid + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='VMDiskProduct', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('size_in_gb', models.FloatField()), + ('storage_class', models.CharField(choices=[('hdd', 'HDD'), ('ssd', 'SSD')], default='ssd', max_length=32)), + ], + ), + migrations.CreateModel( + name='VMHost', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('hostname', models.CharField(max_length=253)), + ('physical_cores', models.IntegerField()), + ('usable_cores', models.IntegerField()), + ('usable_ram_in_gb', models.FloatField()), + ('status', models.CharField(choices=[('pending', 'Pending'), ('active', 'Active'), ('unusable', 'Unusable')], default='pending', max_length=32)), + ], + ), + migrations.CreateModel( + name='VMProduct', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('cores', models.IntegerField()), + ('ram_in_gb', models.FloatField()), + ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('vmhost', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMHost')), + ], + ), + migrations.CreateModel( + name='OperatingSystemDisk', + fields=[ + ('vmdiskproduct_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_vm.VMDiskProduct')), + ('os_name', models.CharField(max_length=128)), + ], + bases=('uncloud_vm.vmdiskproduct',), + ), + migrations.CreateModel( + name='VMWithOSProduct', + fields=[ + ('vmproduct_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_vm.VMProduct')), + ], + bases=('uncloud_vm.vmproduct',), + ), + migrations.CreateModel( + name='VMNetworkCard', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('mac_address', models.IntegerField()), + ('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct')), + ], + ), + migrations.AddField( + model_name='vmdiskproduct', + name='vm', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct'), + ), + ] diff --git a/uncloud/uncloud_vm/migrations/0002_auto_20200225_1952.py b/uncloud/uncloud_vm/migrations/0002_auto_20200225_1952.py new file mode 100644 index 0000000..46a207b --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0002_auto_20200225_1952.py @@ -0,0 +1,38 @@ +# Generated by Django 3.0.3 on 2020-02-25 19:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_vm', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='vmhost', + name='hostname', + field=models.CharField(max_length=253, unique=True), + ), + migrations.AlterField( + model_name='vmhost', + name='physical_cores', + field=models.IntegerField(default=0), + ), + migrations.AlterField( + model_name='vmhost', + name='status', + field=models.CharField(choices=[('pending', 'Pending'), ('active', 'Active'), ('unusable', 'Unusable'), ('deleted', 'Deleted')], default='pending', max_length=32), + ), + migrations.AlterField( + model_name='vmhost', + name='usable_cores', + field=models.IntegerField(default=0), + ), + migrations.AlterField( + model_name='vmhost', + name='usable_ram_in_gb', + field=models.FloatField(default=0), + ), + ] diff --git a/uncloud/uncloud_vm/migrations/0003_auto_20200225_2028.py b/uncloud/uncloud_vm/migrations/0003_auto_20200225_2028.py new file mode 100644 index 0000000..a4e5976 --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0003_auto_20200225_2028.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-02-25 20:28 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('uncloud_vm', '0002_auto_20200225_1952'), + ] + + operations = [ + migrations.AlterField( + model_name='vmproduct', + name='vmhost', + field=models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMHost'), + ), + ] diff --git a/uncloud/uncloud_vm/migrations/__init__.py b/uncloud/uncloud_vm/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index b1aab40..f4b68dd 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -1,12 +1,73 @@ from django.db import models +from django.contrib.auth import get_user_model +import uuid -class VM(models.Model): +class VMHost(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) - owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) + + # 253 is the maximum DNS name length + hostname = models.CharField(max_length=253, unique=True) + + # indirectly gives a maximum number of cores / VM - f.i. 32 + physical_cores = models.IntegerField(default=0) + + # determines the maximum usable cores - f.i. 320 if you overbook by a factor of 10 + usable_cores = models.IntegerField(default=0) + + # ram that can be used of the server + usable_ram_in_gb = models.FloatField(default=0) + + + status = models.CharField(max_length=32, + choices = ( + ('pending', 'Pending'), + ('active', 'Active'), + ('unusable', 'Unusable'), + ('deleted', 'Deleted'), + ), + default='pending' + ) + + +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) + vmhost = models.ForeignKey(VMHost, + on_delete=models.CASCADE, + editable=False, + blank=True, + null=True) cores = models.IntegerField() - ram = models.FloatField() + ram_in_gb = models.FloatField() -class VMDisk(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() + + storage_class = models.CharField(max_length=32, + choices = ( + ('hdd', 'HDD'), + ('ssd', 'SSD'), + ), + default='ssd' + ) + +class OperatingSystemDisk(VMDiskProduct): + """ Defines an Operating System Disk that can be cloned for a VM """ + os_name = models.CharField(max_length=128) + + +class VMNetworkCard(models.Model): + vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) + mac_address = models.IntegerField() diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py new file mode 100644 index 0000000..4154aee --- /dev/null +++ b/uncloud/uncloud_vm/serializers.py @@ -0,0 +1,15 @@ +from django.contrib.auth import get_user_model + +from rest_framework import serializers +from .models import VMHost, VMProduct + +class VMHostSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = VMHost + fields = '__all__' + + +class VMProductSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = VMProduct + fields = '__all__' diff --git a/uncloud/uncloud_vm/tests.py b/uncloud/uncloud_vm/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/uncloud/uncloud_vm/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index aa5855c..91e81e1 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -1,24 +1,29 @@ from django.shortcuts import render - from django.contrib.auth.models import User from django.shortcuts import get_object_or_404 -from myapps.serializers import UserSerializer -from rest_framework import viewsets + +from rest_framework import viewsets, permissions from rest_framework.response import Response -from opennebula.models import VM as OpenNebulaVM +from .models import VMHost, VMProduct +from .serializers import VMHostSerializer, VMProductSerializer -class VMViewSet(viewsets.ViewSet): - def list(self, request): - queryset = User.objects.all() - serializer = UserSerializer(queryset, many=True) - return Response(serializer.data) - - def retrieve(self, request, pk=None): - queryset = User.objects.all() - user = get_object_or_404(queryset, pk=pk) - serializer = UserSerializer(user) - return Response(serializer.data) +class VMHostViewSet(viewsets.ModelViewSet): + serializer_class = VMHostSerializer + queryset = VMHost.objects.all() + permission_classes = [permissions.IsAdminUser] +class VMProductViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] + serializer_class = VMProductSerializer + + def get_queryset(self): + return VMProduct.objects.filter(owner=self.request.user) + + def create(self, request): + serializer = VMProductSerializer(data=request.data, context={'request': request}) + serializer.is_valid(raise_exception=True) + serializer.save(owner=request.user) + + return Response(serializer.data) From bcbd6f6f8339e7489be0b7e126df6f208dd8465a Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 29 Feb 2020 16:45:52 +0100 Subject: [PATCH 02/13] Introduce disk->image relationship Signed-off-by: Nico Schottelius --- uncloud/uncloud/urls.py | 8 ++ .../migrations/0006_auto_20200229_1545.py | 53 ++++++++++++ uncloud/uncloud_vm/models.py | 86 ++++++++++--------- uncloud/uncloud_vm/serializers.py | 34 ++++++-- uncloud/uncloud_vm/views.py | 36 ++++++-- 5 files changed, 161 insertions(+), 56 deletions(-) create mode 100644 uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 5ee9f07..40b3b20 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -26,8 +26,16 @@ router = routers.DefaultRouter() # user / regular urls router.register(r'vm/snapshot', vmviews.VMSnapshotProductViewSet, basename='vmsnapshotproduct') +router.register(r'vm/image/mine', vmviews.VMDiskImageProductMineViewSet, basename='vmdiskimagemineproduct') +router.register(r'vm/image/public', vmviews.VMDiskImageProductPublicViewSet, basename='vmdiskimagepublicproduct') + + +#router.register(r'vm/disk', vmviews.VMDiskProductViewSet, basename='vmdiskproduct') + router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') + + # Pay router.register(r'user', payviews.UserViewSet, basename='user') router.register(r'bill', payviews.BillViewSet, basename='bill') diff --git a/uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py b/uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py new file mode 100644 index 0000000..208aeaa --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py @@ -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, + ), + ] diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 4ebae25..b585cb9 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -46,11 +46,27 @@ class VMProduct(Product): class VMWithOSProduct(VMProduct): pass -class VMDiskProduct(models.Model): + +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) - vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) + 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() + storage_class = models.CharField(max_length=32, choices = ( ('hdd', 'HDD'), @@ -59,9 +75,32 @@ 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) + # source = models.CharField(max_length=32, + # choices = ( + # ('url', 'HDD'), + # ('ssd', 'SSD'), + # ), + # default='ssd' + # ) + +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() class VMNetworkCard(models.Model): @@ -74,44 +113,7 @@ class VMNetworkCard(models.Model): 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 = models.ForeignKey(VMProduct, on_delete=models.CASCADE) - #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()) diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index b247709..a64fdd0 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -1,22 +1,28 @@ from django.contrib.auth import get_user_model from rest_framework import serializers -from .models import VMHost, VMProduct, VMSnapshotProduct +from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct -class VMHostSerializer(serializers.HyperlinkedModelSerializer): +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): + class Meta: + model = VMDiskProduct + fields = '__all__' -# def create(self, validated_data): -# return VMSnapshotProduct() +class VMDiskImageProductSerializer(serializers.ModelSerializer): + class Meta: + model = VMDiskImageProduct + fields = '__all__' class VMSnapshotProductSerializer(serializers.ModelSerializer): class Meta: @@ -26,5 +32,19 @@ class VMSnapshotProductSerializer(serializers.ModelSerializer): # verify that vm.owner == user.request def validate_vm(self, value): - print(value) - return True + + 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' diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 7e517f5..b9d80f9 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -6,10 +6,10 @@ from django.shortcuts import get_object_or_404 from rest_framework import viewsets, permissions from rest_framework.response import Response -from .models import VMHost, VMProduct, VMSnapshotProduct +from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct from uncloud_pay.models import Order -from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer +from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer, VMDiskImageProductSerializer import datetime @@ -19,6 +19,20 @@ class VMHostViewSet(viewsets.ModelViewSet): 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) + +class VMDiskImageProductPublicViewSet(viewsets.ModelViewSet): + permission_classes = [permissions.IsAuthenticated] + serializer_class = VMDiskImageProductSerializer + + def get_queryset(self): + return VMDiskImageProduct.objects.filter(is_public=True) + class VMProductViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] @@ -54,22 +68,30 @@ class VMSnapshotProductViewSet(viewsets.ModelViewSet): 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=20, + recurring_price=recurring_price, one_time_price=0, - recurring_period="per_month") + recurring_period=recurring_period) order.save() - # FIXME: calculate the gb_* values serializer.save(owner=request.user, order=order, - gb_ssd=12, - gb_hdd=20) + gb_ssd=ssds_size, + gb_hdd=hdds_size) return Response(serializer.data) From 6a38e4e0a44576c829685ed503cbca61cbc5b1f5 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 29 Feb 2020 17:00:13 +0100 Subject: [PATCH 03/13] add url for importing disk image Signed-off-by: Nico Schottelius --- .../migrations/0007_auto_20200229_1559.py | 23 +++++++++++++++++++ uncloud/uncloud_vm/models.py | 9 ++++++-- uncloud/uncloud_vm/serializers.py | 9 ++++++-- uncloud/uncloud_vm/views.py | 10 ++++++-- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py diff --git a/uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py b/uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py new file mode 100644 index 0000000..6e08c0c --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py @@ -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), + ), + ] diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index b585cb9..f2cbf13 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -55,7 +55,9 @@ class VMDiskImageProduct(models.Model): """ - uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + uuid = models.UUIDField(primary_key=True, + default=uuid.uuid4, + editable=False) owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, editable=False) @@ -64,7 +66,10 @@ class VMDiskImageProduct(models.Model): is_os_image = models.BooleanField(default=False) is_public = models.BooleanField(default=False) - size_in_gb = models.FloatField() + size_in_gb = models.FloatField(null=True, + blank=True) + import_url = models.URLField(null=True, + blank=True) storage_class = models.CharField(max_length=32, diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index a64fdd0..f8618ee 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -3,6 +3,13 @@ from django.contrib.auth import get_user_model from rest_framework import serializers from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct +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 @@ -32,11 +39,9 @@ class VMSnapshotProductSerializer(serializers.ModelSerializer): # 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: diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index b9d80f9..851041e 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -26,7 +26,14 @@ class VMDiskImageProductMineViewSet(viewsets.ModelViewSet): def get_queryset(self): return VMDiskImageProduct.objects.filter(owner=self.request.user) -class VMDiskImageProductPublicViewSet(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) + return Response(serializer.data) + + +class VMDiskImageProductPublicViewSet(viewsets.ReadOnlyModelViewSet): permission_classes = [permissions.IsAuthenticated] serializer_class = VMDiskImageProductSerializer @@ -55,7 +62,6 @@ class VMProductViewSet(viewsets.ModelViewSet): order.save() serializer.save(owner=request.user, order=order) - return Response(serializer.data) From 5c33bc5c02411778e354b322cdcd258b8b33ffc8 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 29 Feb 2020 17:57:57 +0100 Subject: [PATCH 04/13] support creating disks Signed-off-by: Nico Schottelius --- uncloud/uncloud/urls.py | 15 +++++++++++- uncloud/uncloud_vm/models.py | 32 +++++++++++++------------ uncloud/uncloud_vm/serializers.py | 2 ++ uncloud/uncloud_vm/views.py | 40 +++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 40b3b20..02862a1 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -26,14 +26,27 @@ router = routers.DefaultRouter() # 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/disk', vmviews.VMDiskProductViewSet, basename='vmdiskproduct') 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 diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index f2cbf13..7aac05b 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -4,6 +4,16 @@ 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) @@ -22,13 +32,8 @@ 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 ) @@ -80,13 +85,10 @@ class VMDiskImageProduct(models.Model): default='ssd' ) - # source = models.CharField(max_length=32, - # choices = ( - # ('url', 'HDD'), - # ('ssd', 'SSD'), - # ), - # default='ssd' - # ) + status = models.CharField(max_length=32, + choices=STATUS_CHOICES, + default=STATUS_DEFAULT + ) class VMDiskProduct(models.Model): """ @@ -105,7 +107,7 @@ class VMDiskProduct(models.Model): vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE) - size_in_gb = models.FloatField() + size_in_gb = models.FloatField(blank=True) class VMNetworkCard(models.Model): diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index f8618ee..07d6c51 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -22,6 +22,8 @@ class VMProductSerializer(serializers.ModelSerializer): fields = '__all__' class VMDiskProductSerializer(serializers.ModelSerializer): +# vm = VMProductSerializer() + class Meta: model = VMDiskProduct fields = '__all__' diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 851041e..62edaa0 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -5,11 +5,13 @@ 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, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct from uncloud_pay.models import Order -from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer, VMDiskImageProductSerializer +from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer, VMDiskImageProductSerializer, VMDiskProductSerializer import datetime @@ -27,8 +29,14 @@ class VMDiskImageProductMineViewSet(viewsets.ModelViewSet): return VMDiskImageProduct.objects.filter(owner=self.request.user) def create(self, request): - serializer = VMProductSerializer(data=request.data, context={'request': 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) @@ -40,6 +48,34 @@ class VMDiskImageProductPublicViewSet(viewsets.ReadOnlyModelViewSet): 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] From 4115eed2a8398144ef1f1faba628e9a9d2c1edfd Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 29 Feb 2020 17:58:10 +0100 Subject: [PATCH 05/13] +migration Signed-off-by: Nico Schottelius --- .../migrations/0008_auto_20200229_1611.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py diff --git a/uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py b/uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py new file mode 100644 index 0000000..8a9be67 --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py @@ -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), + ), + ] From 028fd6789f979115481028d12f1cf575eb7bfe87 Mon Sep 17 00:00:00 2001 From: Ahmed Bilal <49-ahmedbilal@users.noreply.code.ungleich.ch> Date: Mon, 2 Mar 2020 07:17:04 +0100 Subject: [PATCH 06/13] ++cleanup Signed-off-by: Nico Schottelius --- uncloud/README.md | 23 +++ uncloud/uncloud/settings.py | 2 +- uncloud/uncloud/urls.py | 38 ++++- uncloud/uncloud_api/admin.py | 6 - uncloud/uncloud_api/apps.py | 5 - .../uncloud_api/management/commands/hack.py | 26 ---- .../management/commands/snapshot.py | 29 ---- .../0002_vmsnapshotproduct_vm_uuid.py | 19 --- .../migrations/0003_auto_20200225_1950.py | 36 ----- uncloud/uncloud_api/models.py | 139 ------------------ uncloud/uncloud_api/serializers.py | 26 ---- uncloud/uncloud_api/views.py | 94 ------------ .../{uncloud_api => uncloud_net}/__init__.py | 0 uncloud/uncloud_net/admin.py | 3 + uncloud/uncloud_net/apps.py | 5 + .../migrations}/__init__.py | 0 uncloud/uncloud_net/models.py | 4 + uncloud/{uncloud_api => uncloud_net}/tests.py | 0 uncloud/uncloud_net/views.py | 3 + .../commands => uncloud_pay}/__init__.py | 0 uncloud/uncloud_pay/admin.py | 3 + uncloud/uncloud_pay/apps.py | 5 + .../uncloud_pay/migrations/0001_initial.py | 56 +++++++ .../migrations/0002_auto_20200227_1230.py | 18 +++ .../migrations/__init__.py | 0 uncloud/uncloud_pay/models.py | 123 ++++++++++++++++ uncloud/uncloud_pay/serializers.py | 27 ++++ uncloud/uncloud_pay/tests.py | 3 + uncloud/uncloud_pay/views.py | 102 +++++++++++++ .../migrations/0004_vmsnapshotproduct.py} | 14 +- .../migrations/0005_auto_20200227_1230.py | 36 +++++ .../migrations/0006_auto_20200229_1545.py | 53 +++++++ .../migrations/0007_auto_20200229_1559.py | 23 +++ .../migrations/0008_auto_20200229_1611.py | 23 +++ uncloud/uncloud_vm/models.py | 95 +++++++++--- uncloud/uncloud_vm/serializers.py | 48 +++++- uncloud/uncloud_vm/views.py | 116 ++++++++++++++- 37 files changed, 783 insertions(+), 420 deletions(-) delete mode 100644 uncloud/uncloud_api/admin.py delete mode 100644 uncloud/uncloud_api/apps.py delete mode 100644 uncloud/uncloud_api/management/commands/hack.py delete mode 100644 uncloud/uncloud_api/management/commands/snapshot.py delete mode 100644 uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py delete mode 100644 uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py delete mode 100644 uncloud/uncloud_api/models.py delete mode 100644 uncloud/uncloud_api/serializers.py delete mode 100644 uncloud/uncloud_api/views.py rename uncloud/{uncloud_api => uncloud_net}/__init__.py (100%) create mode 100644 uncloud/uncloud_net/admin.py create mode 100644 uncloud/uncloud_net/apps.py rename uncloud/{uncloud_api/management => uncloud_net/migrations}/__init__.py (100%) create mode 100644 uncloud/uncloud_net/models.py rename uncloud/{uncloud_api => uncloud_net}/tests.py (100%) create mode 100644 uncloud/uncloud_net/views.py rename uncloud/{uncloud_api/management/commands => uncloud_pay}/__init__.py (100%) create mode 100644 uncloud/uncloud_pay/admin.py create mode 100644 uncloud/uncloud_pay/apps.py create mode 100644 uncloud/uncloud_pay/migrations/0001_initial.py create mode 100644 uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py rename uncloud/{uncloud_api => uncloud_pay}/migrations/__init__.py (100%) create mode 100644 uncloud/uncloud_pay/models.py create mode 100644 uncloud/uncloud_pay/serializers.py create mode 100644 uncloud/uncloud_pay/tests.py create mode 100644 uncloud/uncloud_pay/views.py rename uncloud/{uncloud_api/migrations/0001_initial.py => uncloud_vm/migrations/0004_vmsnapshotproduct.py} (57%) create mode 100644 uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py create mode 100644 uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py create mode 100644 uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py create mode 100644 uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py diff --git a/uncloud/README.md b/uncloud/README.md index 67f960f..390a3af 100644 --- a/uncloud/README.md +++ b/uncloud/README.md @@ -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) +``` diff --git a/uncloud/uncloud/settings.py b/uncloud/uncloud/settings.py index 614cd25..179ff0b 100644 --- a/uncloud/uncloud/settings.py +++ b/uncloud/uncloud/settings.py @@ -60,7 +60,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', - 'uncloud_api', + 'uncloud_pay', 'uncloud_auth', 'uncloud_vm', 'opennebula' diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 23392c5..02862a1 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -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 ] diff --git a/uncloud/uncloud_api/admin.py b/uncloud/uncloud_api/admin.py deleted file mode 100644 index d242668..0000000 --- a/uncloud/uncloud_api/admin.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.contrib import admin - -from .models import Product, Feature - -#admin.site.register(Product) -#admin.site.register(Feature) diff --git a/uncloud/uncloud_api/apps.py b/uncloud/uncloud_api/apps.py deleted file mode 100644 index 6830fa2..0000000 --- a/uncloud/uncloud_api/apps.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.apps import AppConfig - - -class ApiConfig(AppConfig): - name = 'uncloud_api' diff --git a/uncloud/uncloud_api/management/commands/hack.py b/uncloud/uncloud_api/management/commands/hack.py deleted file mode 100644 index e129952..0000000 --- a/uncloud/uncloud_api/management/commands/hack.py +++ /dev/null @@ -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 = '' - 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)) diff --git a/uncloud/uncloud_api/management/commands/snapshot.py b/uncloud/uncloud_api/management/commands/snapshot.py deleted file mode 100644 index 41d0e38..0000000 --- a/uncloud/uncloud_api/management/commands/snapshot.py +++ /dev/null @@ -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 = '' - 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) diff --git a/uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py b/uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py deleted file mode 100644 index b35317e..0000000 --- a/uncloud/uncloud_api/migrations/0002_vmsnapshotproduct_vm_uuid.py +++ /dev/null @@ -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), - ), - ] diff --git a/uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py b/uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py deleted file mode 100644 index be7624c..0000000 --- a/uncloud/uncloud_api/migrations/0003_auto_20200225_1950.py +++ /dev/null @@ -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(), - ), - ] diff --git a/uncloud/uncloud_api/models.py b/uncloud/uncloud_api/models.py deleted file mode 100644 index 6a6f9c8..0000000 --- a/uncloud/uncloud_api/models.py +++ /dev/null @@ -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) diff --git a/uncloud/uncloud_api/serializers.py b/uncloud/uncloud_api/serializers.py deleted file mode 100644 index 7dc3686..0000000 --- a/uncloud/uncloud_api/serializers.py +++ /dev/null @@ -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__' diff --git a/uncloud/uncloud_api/views.py b/uncloud/uncloud_api/views.py deleted file mode 100644 index eb4cc77..0000000 --- a/uncloud/uncloud_api/views.py +++ /dev/null @@ -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/ => 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.+)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 diff --git a/uncloud/uncloud_api/__init__.py b/uncloud/uncloud_net/__init__.py similarity index 100% rename from uncloud/uncloud_api/__init__.py rename to uncloud/uncloud_net/__init__.py diff --git a/uncloud/uncloud_net/admin.py b/uncloud/uncloud_net/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/uncloud/uncloud_net/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/uncloud/uncloud_net/apps.py b/uncloud/uncloud_net/apps.py new file mode 100644 index 0000000..489beb1 --- /dev/null +++ b/uncloud/uncloud_net/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class UncloudNetConfig(AppConfig): + name = 'uncloud_net' diff --git a/uncloud/uncloud_api/management/__init__.py b/uncloud/uncloud_net/migrations/__init__.py similarity index 100% rename from uncloud/uncloud_api/management/__init__.py rename to uncloud/uncloud_net/migrations/__init__.py diff --git a/uncloud/uncloud_net/models.py b/uncloud/uncloud_net/models.py new file mode 100644 index 0000000..6d0c742 --- /dev/null +++ b/uncloud/uncloud_net/models.py @@ -0,0 +1,4 @@ +from django.db import models + +class MACAdress(models.Model): + prefix = 0x420000000000 diff --git a/uncloud/uncloud_api/tests.py b/uncloud/uncloud_net/tests.py similarity index 100% rename from uncloud/uncloud_api/tests.py rename to uncloud/uncloud_net/tests.py diff --git a/uncloud/uncloud_net/views.py b/uncloud/uncloud_net/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/uncloud/uncloud_net/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/uncloud/uncloud_api/management/commands/__init__.py b/uncloud/uncloud_pay/__init__.py similarity index 100% rename from uncloud/uncloud_api/management/commands/__init__.py rename to uncloud/uncloud_pay/__init__.py diff --git a/uncloud/uncloud_pay/admin.py b/uncloud/uncloud_pay/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/uncloud/uncloud_pay/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/uncloud/uncloud_pay/apps.py b/uncloud/uncloud_pay/apps.py new file mode 100644 index 0000000..051ffb4 --- /dev/null +++ b/uncloud/uncloud_pay/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class UncloudPayConfig(AppConfig): + name = 'uncloud_pay' diff --git a/uncloud/uncloud_pay/migrations/0001_initial.py b/uncloud/uncloud_pay/migrations/0001_initial.py new file mode 100644 index 0000000..6e57c59 --- /dev/null +++ b/uncloud/uncloud_pay/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py b/uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py new file mode 100644 index 0000000..0643e9a --- /dev/null +++ b/uncloud/uncloud_pay/migrations/0002_auto_20200227_1230.py @@ -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), + ), + ] diff --git a/uncloud/uncloud_api/migrations/__init__.py b/uncloud/uncloud_pay/migrations/__init__.py similarity index 100% rename from uncloud/uncloud_api/migrations/__init__.py rename to uncloud/uncloud_pay/migrations/__init__.py diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py new file mode 100644 index 0000000..6a33fd5 --- /dev/null +++ b/uncloud/uncloud_pay/models.py @@ -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 diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py new file mode 100644 index 0000000..130f683 --- /dev/null +++ b/uncloud/uncloud_pay/serializers.py @@ -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 diff --git a/uncloud/uncloud_pay/tests.py b/uncloud/uncloud_pay/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/uncloud/uncloud_pay/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py new file mode 100644 index 0000000..ae88861 --- /dev/null +++ b/uncloud/uncloud_pay/views.py @@ -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() diff --git a/uncloud/uncloud_api/migrations/0001_initial.py b/uncloud/uncloud_vm/migrations/0004_vmsnapshotproduct.py similarity index 57% rename from uncloud/uncloud_api/migrations/0001_initial.py rename to uncloud/uncloud_vm/migrations/0004_vmsnapshotproduct.py index 67bdd2e..13840b5 100644 --- a/uncloud/uncloud_api/migrations/0001_initial.py +++ b/uncloud/uncloud_vm/migrations/0004_vmsnapshotproduct.py @@ -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, diff --git a/uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py b/uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py new file mode 100644 index 0000000..1bd711b --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0005_auto_20200227_1230.py @@ -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, + ), + ] diff --git a/uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py b/uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py new file mode 100644 index 0000000..208aeaa --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0006_auto_20200229_1545.py @@ -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, + ), + ] diff --git a/uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py b/uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py new file mode 100644 index 0000000..6e08c0c --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0007_auto_20200229_1559.py @@ -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), + ), + ] diff --git a/uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py b/uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py new file mode 100644 index 0000000..8a9be67 --- /dev/null +++ b/uncloud/uncloud_vm/migrations/0008_auto_20200229_1611.py @@ -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), + ), + ] diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index f4b68dd..7aac05b 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -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) diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index 4154aee..07d6c51 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -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' diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 91e81e1..62edaa0 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -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) From 6c9c63e0da2ce69d5199628a76aa2d8137f9daae Mon Sep 17 00:00:00 2001 From: meow Date: Mon, 2 Mar 2020 16:54:36 +0500 Subject: [PATCH 07/13] Add sample clean() for model + Add tests for uncloud_vm --- uncloud/uncloud_vm/models.py | 84 ++++++++++++++------------- uncloud/uncloud_vm/tests.py | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 38 deletions(-) create mode 100644 uncloud/uncloud_vm/tests.py diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 7aac05b..9733841 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -1,19 +1,24 @@ +import uuid + from django.db import models from django.contrib.auth import get_user_model -import uuid + +# Uncomment if you override model's clean method +# from django.core.exceptions import ValidationError 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 + ('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' +STATUS_DEFAULT = 'pending' + class VMHost(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) @@ -30,19 +35,13 @@ class VMHost(models.Model): # ram that can be used of the server usable_ram_in_gb = models.FloatField(default=0) - - status = models.CharField(max_length=32, - choices=STATUS_CHOICES, - default=STATUS_DEFAULT - ) + status = models.CharField(max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT) class VMProduct(Product): - vmhost = models.ForeignKey(VMHost, - on_delete=models.CASCADE, - editable=False, - blank=True, - null=True) + vmhost = models.ForeignKey( + VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True + ) cores = models.IntegerField() ram_in_gb = models.FloatField() @@ -60,36 +59,30 @@ class VMDiskImageProduct(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) + 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) + 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 = ( - ('hdd', 'HDD'), - ('ssd', 'SSD'), - ), - default='ssd' + storage_class = models.CharField( + max_length=32, + choices=( + ('hdd', 'HDD'), + ('ssd', 'SSD'), + ), + default='ssd' ) - status = models.CharField(max_length=32, - choices=STATUS_CHOICES, - default=STATUS_DEFAULT + status = models.CharField( + max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT ) + class VMDiskProduct(models.Model): """ The VMDiskProduct is attached to a VM. @@ -104,14 +97,29 @@ class VMDiskProduct(models.Model): on_delete=models.CASCADE, editable=False) - vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) + vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE) size_in_gb = models.FloatField(blank=True) + # Sample code for clean method + + # Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct + # that is in status 'active' + + # def clean(self): + # if self.image.status != 'active': + # raise ValidationError({ + # 'image': 'VM Disk must be created from an active disk image.' + # }) + + def save(self, *args, **kwargs): + self.full_clean() + super().save(*args, **kwargs) + class VMNetworkCard(models.Model): - vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) + vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) mac_address = models.IntegerField() diff --git a/uncloud/uncloud_vm/tests.py b/uncloud/uncloud_vm/tests.py new file mode 100644 index 0000000..a9ca5ee --- /dev/null +++ b/uncloud/uncloud_vm/tests.py @@ -0,0 +1,107 @@ +import datetime + +from django.test import TestCase +from django.contrib.auth import get_user_model +from django.utils import timezone +from django.core.exceptions import ValidationError + +from uncloud_vm.models import VMDiskImageProduct, VMDiskProduct, VMProduct, VMHost +from uncloud_pay.models import Order + +User = get_user_model() + + +# If you want to check the test database then use the following connecting parameters + +# host: localhost +# database: test_uncloud +# user: root +# password: +# port: 5432 + +class VMTestCase(TestCase): + @classmethod + def setUpClass(cls): + # Setup vm host + cls.vm_host, created = VMHost.objects.get_or_create( + hostname='server1.place11.ungleich.ch', physical_cores=32, usable_cores=320, + usable_ram_in_gb=512.0, status='active' + ) + super().setUpClass() + + def setUp(self) -> None: + # Setup two users as it is common to test with different user + self.user = User.objects.create_user( + username='testuser', email='test@test.com', first_name='Test', last_name='User' + ) + self.user2 = User.objects.create_user( + username='Meow', email='meow123@test.com', first_name='Meow', last_name='Cat' + ) + super().setUp() + + def create_sample_vm(self, owner): + return VMProduct.objects.create( + vmhost=self.vm_host, cores=2, ram_in_gb=4, owner=owner, + order=Order.objects.create( + owner=owner, + creation_date=datetime.datetime.now(tz=timezone.utc), + starting_date=datetime.datetime.now(tz=timezone.utc), + ending_date=datetime.datetime(2020, 4, 2, tzinfo=timezone.utc), + recurring_price=4.0, one_time_price=5.0, recurring_period='per_month' + ) + ) + + def test_disk_product(self): + """Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct + that is in status 'active'""" + + vm = self.create_sample_vm(owner=self.user) + + pending_disk_image = VMDiskImageProduct.objects.create( + owner=self.user, name='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10, + status='pending' + ) + try: + vm_disk_product = VMDiskProduct.objects.create( + owner=self.user, vm=vm, image=pending_disk_image, size_in_gb=10 + ) + except ValidationError: + vm_disk_product = None + + self.assertIsNone( + vm_disk_product, + msg='VMDiskProduct created with disk image whose status is not active.' + ) + + def test_vm_disk_product_creation(self): + """Ensure that a user can only create a VMDiskProduct for an existing VM""" + + disk_image = VMDiskImageProduct.objects.create( + owner=self.user, name='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10, + status='active' + ) + + with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for non-existing VM'): + # Create VMProduct object but don't save it in database + vm = VMProduct() + + vm_disk_product = VMDiskProduct.objects.create( + owner=self.user, vm=vm, image=disk_image, size_in_gb=10 + ) + + def test_vm_disk_product_creation_for_someone_else(self): + """Ensure that a user can only create a VMDiskProduct for his/her own VM""" + + # Create a VM which is ownership of self.user2 + someone_else_vm = self.create_sample_vm(owner=self.user2) + + # 'self.user' would try to create a VMDiskProduct for 'user2's VM + with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for someone else VM.'): + vm_disk_product = VMDiskProduct.objects.create( + owner=self.user, vm=someone_else_vm, + size_in_gb=10, + image=VMDiskImageProduct.objects.create( + owner=self.user, name='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10, + status='active' + ) + ) From afdba3d7d9de49395e008c3e860e8799aab47843 Mon Sep 17 00:00:00 2001 From: meow Date: Mon, 2 Mar 2020 17:17:30 +0500 Subject: [PATCH 08/13] Remove duplicate code --- uncloud/uncloud_vm/models.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index e59d5d2..e54c4ea 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -19,18 +19,6 @@ STATUS_CHOICES = ( STATUS_DEFAULT = 'pending' -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) From 0c3e6d10ae79b63f9450a7924b043ab917e50958 Mon Sep 17 00:00:00 2001 From: meow Date: Mon, 2 Mar 2020 17:20:30 +0500 Subject: [PATCH 09/13] Indentation/Spacing fixes --- uncloud/uncloud_vm/models.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index e54c4ea..4b0d511 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -35,15 +35,15 @@ class VMHost(models.Model): # ram that can be used of the server usable_ram_in_gb = models.FloatField(default=0) - status = models.CharField(max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT) + status = models.CharField( + max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT + ) class VMProduct(Product): - vmhost = models.ForeignKey(VMHost, - on_delete=models.CASCADE, - editable=False, - blank=True, - null=True) + vmhost = models.ForeignKey( + VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True + ) cores = models.IntegerField() ram_in_gb = models.FloatField() From 750d8c8cbf2f6a27202110c09ac72c790c283eeb Mon Sep 17 00:00:00 2001 From: meow Date: Mon, 2 Mar 2020 17:42:54 +0500 Subject: [PATCH 10/13] Use fictional hostname for VMHost --- uncloud/uncloud_vm/tests.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uncloud/uncloud_vm/tests.py b/uncloud/uncloud_vm/tests.py index a9ca5ee..c51f597 100644 --- a/uncloud/uncloud_vm/tests.py +++ b/uncloud/uncloud_vm/tests.py @@ -11,7 +11,8 @@ from uncloud_pay.models import Order User = get_user_model() -# If you want to check the test database then use the following connecting parameters +# If you want to check the test database using some GUI/cli tool +# then use the following connecting parameters # host: localhost # database: test_uncloud @@ -24,7 +25,7 @@ class VMTestCase(TestCase): def setUpClass(cls): # Setup vm host cls.vm_host, created = VMHost.objects.get_or_create( - hostname='server1.place11.ungleich.ch', physical_cores=32, usable_cores=320, + hostname='serverx.placey.ungleich.ch', physical_cores=32, usable_cores=320, usable_ram_in_gb=512.0, status='active' ) super().setUpClass() From 531bfa176837170b53b1f56cc245e5e8b1d884b3 Mon Sep 17 00:00:00 2001 From: meow Date: Mon, 2 Mar 2020 19:20:12 +0500 Subject: [PATCH 11/13] actual thing name is replaced by pseudo names --- uncloud/uncloud_vm/tests.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/uncloud/uncloud_vm/tests.py b/uncloud/uncloud_vm/tests.py index c51f597..8d7994f 100644 --- a/uncloud/uncloud_vm/tests.py +++ b/uncloud/uncloud_vm/tests.py @@ -1,5 +1,7 @@ import datetime +import parsedatetime + from django.test import TestCase from django.contrib.auth import get_user_model from django.utils import timezone @@ -9,6 +11,7 @@ from uncloud_vm.models import VMDiskImageProduct, VMDiskProduct, VMProduct, VMHo from uncloud_pay.models import Order User = get_user_model() +cal = parsedatetime.Calendar() # If you want to check the test database using some GUI/cli tool @@ -41,13 +44,14 @@ class VMTestCase(TestCase): super().setUp() def create_sample_vm(self, owner): + one_month_later, parse_status = cal.parse("1 month later") return VMProduct.objects.create( vmhost=self.vm_host, cores=2, ram_in_gb=4, owner=owner, order=Order.objects.create( owner=owner, creation_date=datetime.datetime.now(tz=timezone.utc), starting_date=datetime.datetime.now(tz=timezone.utc), - ending_date=datetime.datetime(2020, 4, 2, tzinfo=timezone.utc), + ending_date=datetime.datetime(*one_month_later[:6], tzinfo=timezone.utc), recurring_price=4.0, one_time_price=5.0, recurring_period='per_month' ) ) @@ -59,7 +63,7 @@ class VMTestCase(TestCase): vm = self.create_sample_vm(owner=self.user) pending_disk_image = VMDiskImageProduct.objects.create( - owner=self.user, name='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10, + owner=self.user, name='pending_disk_image', is_os_image=True, is_public=True, size_in_gb=10, status='pending' ) try: @@ -78,7 +82,7 @@ class VMTestCase(TestCase): """Ensure that a user can only create a VMDiskProduct for an existing VM""" disk_image = VMDiskImageProduct.objects.create( - owner=self.user, name='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10, + owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10, status='active' ) @@ -102,7 +106,7 @@ class VMTestCase(TestCase): owner=self.user, vm=someone_else_vm, size_in_gb=10, image=VMDiskImageProduct.objects.create( - owner=self.user, name='alpine3.11', is_os_image=True, is_public=True, size_in_gb=10, + owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10, status='active' ) ) From e176ad08176aa25ad0a237a226eafeb9830b8875 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 3 Mar 2020 11:26:16 +0100 Subject: [PATCH 12/13] Remove second stripe key definition --- uncloud/uncloud/settings.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/uncloud/uncloud/settings.py b/uncloud/uncloud/settings.py index f28e0f4..c6c89d5 100644 --- a/uncloud/uncloud/settings.py +++ b/uncloud/uncloud/settings.py @@ -176,8 +176,3 @@ USE_TZ = True STATIC_URL = '/static/' stripe.api_key = uncloud.secrets.STRIPE_KEY - -############ -# Stripe - -STRIPE_API_KEY = uncloud.secrets.STRIPE_API_KEY From ea00e81b1e99d6b7f6b207447b0c2157a0c40ca3 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 3 Mar 2020 11:31:32 +0100 Subject: [PATCH 13/13] Move all stripe stuff to stripe.py --- uncloud/uncloud/settings.py | 4 ---- uncloud/uncloud_pay/stripe.py | 5 ++--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/uncloud/uncloud/settings.py b/uncloud/uncloud/settings.py index c6c89d5..cc0ec3a 100644 --- a/uncloud/uncloud/settings.py +++ b/uncloud/uncloud/settings.py @@ -11,8 +11,6 @@ https://docs.djangoproject.com/en/3.0/ref/settings/ """ import os - -import stripe import ldap # Uncommitted file with secrets @@ -174,5 +172,3 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' - -stripe.api_key = uncloud.secrets.STRIPE_KEY diff --git a/uncloud/uncloud_pay/stripe.py b/uncloud/uncloud_pay/stripe.py index 6399a1a..c50317f 100644 --- a/uncloud/uncloud_pay/stripe.py +++ b/uncloud/uncloud_pay/stripe.py @@ -2,13 +2,12 @@ import stripe import stripe.error import logging -from django.conf import settings +import uncloud.secrets # Static stripe configuration used below. CURRENCY = 'chf' -# Register stripe (secret) API key from config. -stripe.api_key = settings.STRIPE_API_KEY +stripe.api_key = uncloud.secrets.STRIPE_KEY # Helper (decorator) used to catch errors raised by stripe logic. def handle_stripe_error(f):