Merge branch 'master' into fnux-hacks

This commit is contained in:
fnux 2020-02-28 13:58:36 +01:00
commit b5a242f176
16 changed files with 106 additions and 90 deletions

View file

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UncloudNetConfig(AppConfig):
name = 'uncloud_net'

View file

@ -0,0 +1,4 @@
from django.db import models
class MACAdress(models.Model):
prefix = 0x420000000000

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.3 on 2020-02-27 12:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='payment',
name='source',
field=models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown')], default='unknown', max_length=256),
),
]

View file

@ -0,0 +1,14 @@
# Generated by Django 3.0.3 on 2020-02-28 13:03
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_pay', '0002_auto_20200227_1230'),
('uncloud_pay', '0009_auto_20200228_0825'),
]
operations = [
]

View file

@ -0,0 +1,25 @@
# 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='vmsnapshotproduct',
name='vm',
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.VMProduct'),
preserve_default=False,
),
]

View file

@ -17,11 +17,6 @@ class Migration(migrations.Migration):
name='order',
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_pay.Order'),
),
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.AlterField(
model_name='vmsnapshotproduct',
name='order',

View file

@ -0,0 +1,14 @@
# Generated by Django 3.0.3 on 2020-02-28 13:03
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0005_auto_20200227_1532'),
('uncloud_vm', '0005_auto_20200227_1230'),
]
operations = [
]

View file

@ -75,8 +75,12 @@ class OperatingSystemDisk(VMDiskProduct):
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):
price_per_gb_ssd = 0.35
@ -86,7 +90,8 @@ class VMSnapshotProduct(Product):
gb_ssd = models.FloatField(editable=False)
gb_hdd = models.FloatField(editable=False)
vm_uuid = models.UUIDField()
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
#vm_uuid = models.UUIDField()
# Need to setup recurring_price and one_time_price and recurring period

View file

@ -14,7 +14,17 @@ class VMProductSerializer(serializers.HyperlinkedModelSerializer):
model = VMProduct
fields = '__all__'
# def create(self, validated_data):
# return VMSnapshotProduct()
class VMSnapshotProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMSnapshotProduct
fields = '__all__'
# verify that vm.owner == user.request
def validate_vm(self, value):
print(value)
return True

View file

@ -10,6 +10,7 @@ from .models import VMHost, VMProduct, VMSnapshotProduct
from uncloud_pay.models import Order, RecurringPeriod
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
import datetime
class VMHostViewSet(viewsets.ModelViewSet):

View file

@ -1,84 +0,0 @@
from django.shortcuts import render
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, permissions
from rest_framework.response import Response
<<<<<<< HEAD
from .models import VMHost, VMProduct, VMSnapshotProduct
from uncloud_pay.models import Order
=======
from uncloud_pay.models import Order, RecurringPeriod
from .models import VMHost, VMProduct
>>>>>>> Quickly wire vm creation to orders
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
import datetime
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):
# Create base order.
order = Order.objects.create(
recurring_period=RecurringPeriod.PER_MONTH,
recurring_price=0,
one_time_price=0,
owner=request.user
)
# Create VM.
serializer = VMProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
vm = serializer.save(owner=request.user, order=order)
# FIXME: commit everything (VM + order) at once.
order.recurring_price = vm.recurring_price(order.recurring_period)
order.one_time_price = 0
order.save()
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})
serializer.is_valid(raise_exception=True)
# 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()
# FIXME: calculate the gb_* values
serializer.save(owner=request.user,
order=order,
gb_ssd=12,
gb_hdd=20)
return Response(serializer.data)