First pass at the add-opennebula-vm-orders management command

Command executes but still does not create bills
This commit is contained in:
PCoder 2021-04-14 17:02:40 +05:30
parent df4c0c3060
commit f02f15f09b
7 changed files with 175 additions and 43 deletions

View file

@ -1,14 +1,18 @@
from django.core.management.base import BaseCommand import datetime
from django.contrib.auth import get_user_model
from django.utils import timezone
from datetime import datetime, timedelta
from uncloud_pay.models import *
from uncloud_vm.models import *
import sys import sys
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.utils import timezone
from uncloud_pay.models import (
BillingAddress
)
from uncloud_vm.models import (
VMDiskType, VMProduct
)
def vm_price_2020(cpu=1, ram=2, v6only=False): def vm_price_2020(cpu=1, ram=2, v6only=False):
if v6only: if v6only:
discount = 9 discount = 9
@ -17,6 +21,7 @@ def vm_price_2020(cpu=1, ram=2, v6only=False):
return cpu * 3 + ram * 4 - discount return cpu * 3 + ram * 4 - discount
def disk_price_2020(size_in_gb, disk_type): def disk_price_2020(size_in_gb, disk_type):
if disk_type == VMDiskType.CEPH_SSD: if disk_type == VMDiskType.CEPH_SSD:
price = 3.5 / 10 price = 3.5 / 10
@ -27,6 +32,7 @@ def disk_price_2020(size_in_gb, disk_type):
return size_in_gb * price return size_in_gb * price
class Command(BaseCommand): class Command(BaseCommand):
help = 'Adding VMs / creating orders for user' help = 'Adding VMs / creating orders for user'
@ -40,7 +46,7 @@ class Command(BaseCommand):
owner=user, owner=user,
active=True, active=True,
defaults={'organization': 'Undefined organisation', defaults={'organization': 'Undefined organisation',
'name': 'Undefined name', 'full_name': 'Undefined name',
'street': 'Undefined Street', 'street': 'Undefined Street',
'city': 'Undefined city', 'city': 'Undefined city',
'postal_code': '8750', 'postal_code': '8750',
@ -50,8 +56,10 @@ class Command(BaseCommand):
) )
# 25206 + SSD # 25206 + SSD
vm25206 = VMProduct.objects.create(name="one-25206", cores=1, ram_in_gb=4, owner=user) vm25206 = VMProduct.objects.create(name="one-25206", cores=1,
vm25206.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3))) ram_in_gb=4, owner=user)
vm25206.create_order_at(
timezone.make_aware(datetime.datetime(2020, 3, 3)))
# vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206, owner=user, size_in_gb=30) # vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206, owner=user, size_in_gb=30)
# vm25206_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3))) # vm25206_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
@ -60,7 +68,8 @@ class Command(BaseCommand):
vm25206.cores = 2 vm25206.cores = 2
vm25206.ram_in_gb = 8 vm25206.ram_in_gb = 8
vm25206.save() vm25206.save()
vm25206.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17))) vm25206.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 4, 17)))
sys.exit(0) sys.exit(0)
@ -72,48 +81,51 @@ class Command(BaseCommand):
# 25206 done. # 25206 done.
# 25615 # 25615
vm25615 = VMProduct.objects.create(name="one-25615", cores=1, ram_in_gb=4, owner=user) vm25615 = VMProduct.objects.create(name="one-25615", cores=1,
vm25615.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3))) ram_in_gb=4, owner=user)
vm25615.create_order_at(
timezone.make_aware(datetime.datetime(2020, 3, 3)))
# Change 2020-04-17 # Change 2020-04-17
vm25615.cores = 2 vm25615.cores = 2
vm25615.ram_in_gb = 8 vm25615.ram_in_gb = 8
vm25615.save() vm25615.save()
vm25615.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17))) vm25615.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 4, 17)))
# vm25615_ssd = VMDiskProduct(vm=vm25615, owner=user, size_in_gb=30) # vm25615_ssd = VMDiskProduct(vm=vm25615, owner=user, size_in_gb=30)
# vm25615_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3))) # vm25615_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
# vm25615_ssd.save() # vm25615_ssd.save()
vm25208 = VMProduct.objects.create(name="one-25208", cores=1, ram_in_gb=4, owner=user) vm25208 = VMProduct.objects.create(name="one-25208", cores=1,
vm25208.create_order_at(timezone.make_aware(datetime.datetime(2020,3,5))) ram_in_gb=4, owner=user)
vm25208.create_order_at(
timezone.make_aware(datetime.datetime(2020, 3, 5)))
vm25208.cores = 2 vm25208.cores = 2
vm25208.ram_in_gb = 8 vm25208.ram_in_gb = 8
vm25208.save() vm25208.save()
vm25208.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17))) vm25208.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 4, 17)))
Bill.create_next_bills_for_user(user, ending_date=end_of_month(timezone.make_aware(datetime.datetime(2020,7,31)))) Bill.create_next_bills_for_user(user, ending_date=end_of_month(
timezone.make_aware(datetime.datetime(2020, 7, 31))))
sys.exit(0) sys.exit(0)
vm25615_ssd.size_in_gb = 50 vm25615_ssd.size_in_gb = 50
vm25615_ssd.save() vm25615_ssd.save()
vm25615_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5))) vm25615_ssd.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 8, 5)))
vm25208_ssd = VMDiskProduct.objects.create(vm=vm25208, vm25208_ssd = VMDiskProduct.objects.create(vm=vm25208,
owner=user, owner=user,
size_in_gb=30) size_in_gb=30)
vm25208_ssd.size_in_gb = 50 vm25208_ssd.size_in_gb = 50
vm25208_ssd.save() vm25208_ssd.save()
vm25208_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5))) vm25208_ssd.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 8, 5)))
# 25207 # 25207
vm25207 = VMProduct.objects.create(name="OpenNebula 25207", vm25207 = VMProduct.objects.create(name="OpenNebula 25207",
@ -127,14 +139,14 @@ class Command(BaseCommand):
vm25207_ssd.size_in_gb = 50 vm25207_ssd.size_in_gb = 50
vm25207_ssd.save() vm25207_ssd.save()
vm25207_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5))) vm25207_ssd.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 8, 5)))
vm25207.cores = 2 vm25207.cores = 2
vm25207.ram_in_gb = 8 vm25207.ram_in_gb = 8
vm25207.save() vm25207.save()
vm25207.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,6,19))) vm25207.create_or_update_order(
when_to_start=timezone.make_aware(datetime.datetime(2020, 6, 19)))
# FIXES: check starting times (they are slightly different) # FIXES: check starting times (they are slightly different)
# add vm 25236 # add vm 25236

View file

@ -0,0 +1,21 @@
# Generated by Django 3.1.4 on 2021-04-14 10:40
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_vm', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='vmproduct',
name='owner',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View file

@ -0,0 +1,20 @@
# Generated by Django 3.1.4 on 2021-04-14 10:46
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0002_vmproduct_owner'),
]
operations = [
migrations.AddField(
model_name='vmproduct',
name='created_order_at',
field=models.DateTimeField(default=datetime.datetime(2021, 4, 14, 10, 46, 14, 96330, tzinfo=utc)),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 3.1.4 on 2021-04-14 10:48
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0003_vmproduct_created_order_at'),
]
operations = [
migrations.RemoveField(
model_name='vmproduct',
name='created_order_at',
),
migrations.AddField(
model_name='vmproduct',
name='create_order_at',
field=models.DateTimeField(default=datetime.datetime(2021, 4, 14, 10, 48, 6, 641056, tzinfo=utc)),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 3.1.4 on 2021-04-14 11:19
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0004_auto_20210414_1048'),
]
operations = [
migrations.RemoveField(
model_name='vmproduct',
name='create_order_at',
),
migrations.AddField(
model_name='vmproduct',
name='created_order_at',
field=models.DateTimeField(default=datetime.datetime(2021, 4, 14, 11, 19, 39, 447274, tzinfo=utc)),
),
]

View file

@ -0,0 +1,20 @@
# Generated by Django 3.1.4 on 2021-04-14 11:22
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0005_auto_20210414_1119'),
]
operations = [
migrations.AlterField(
model_name='vmproduct',
name='created_order_at',
field=models.DateTimeField(default=datetime.datetime(2021, 4, 14, 11, 22, 11, 352536, tzinfo=utc)),
),
]

View file

@ -1,3 +1,6 @@
import datetime
from django.utils import timezone
from django.db import models from django.db import models
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
@ -50,6 +53,8 @@ class VMHost(UncloudModel):
class VMProduct(models.Model): class VMProduct(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE,
blank=True, null=True)
vmhost = models.ForeignKey( vmhost = models.ForeignKey(
VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True
) )
@ -61,6 +66,7 @@ class VMProduct(models.Model):
name = models.CharField(max_length=32, blank=True, null=True) name = models.CharField(max_length=32, blank=True, null=True)
cores = models.IntegerField() cores = models.IntegerField()
ram_in_gb = models.FloatField() ram_in_gb = models.FloatField()
created_order_at = models.DateTimeField(default=timezone.make_aware(datetime.datetime.now()))
@property @property
def recurring_price(self): def recurring_price(self):
@ -78,6 +84,11 @@ class VMProduct(models.Model):
# RecurringPeriod.PER_30D, RecurringPeriod.PER_HOUR], # RecurringPeriod.PER_30D, RecurringPeriod.PER_HOUR],
# RecurringPeriod.choices)) # RecurringPeriod.choices))
def create_order_at(self, dt):
self.created_order_at = dt
def create_or_update_order(self, when_to_start):
self.created_order_at = when_to_start
def __str__(self): def __str__(self):
return f"VM id={self.id},name={self.name},cores={self.cores},ram_in_gb={self.ram_in_gb}" return f"VM id={self.id},name={self.name},cores={self.cores},ram_in_gb={self.ram_in_gb}"