uncloud/uncloud_pay/management/commands/add-opennebula-vm-orders.py

146 lines
4.5 KiB
Python
Raw Normal View History

from django.core.management.base import BaseCommand
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 *
2020-08-04 16:56:36 +00:00
import sys
def vm_price_2020(cpu=1, ram=2, v6only=False):
if v6only:
discount = 9
else:
discount = 0
return cpu*3 + ram*4 - discount
def disk_price_2020(size_in_gb, disk_type):
if disk_type == VMDiskType.CEPH_SSD:
price = 3.5/10
elif disk_type == VMDiskType.CEPH_HDD:
price = 1.5/100
else:
raise Exception("not yet defined price")
return size_in_gb * price
class Command(BaseCommand):
help = 'Adding VMs / creating orders for user'
def add_arguments(self, parser):
parser.add_argument('--username', type=str, required=True)
def handle(self, *args, **options):
user = get_user_model().objects.get(username=options['username'])
addr, created = BillingAddress.objects.get_or_create(
owner=user,
active=True,
defaults={'organization': 'Undefined organisation',
'name': 'Undefined name',
'street': 'Undefined Street',
'city': 'Undefined city',
'postal_code': '8750',
'country': 'CH',
'active': True
}
)
2020-08-09 08:14:49 +00:00
# 25206 + SSD
vm25206 = VMProduct(name="25206", cores=1, ram_in_gb=4, owner=user)
2020-08-04 16:56:36 +00:00
vm25206.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
vm25206.save()
2020-08-09 08:14:49 +00:00
vm25206_ssd = VMDiskProduct(vm=vm25206, owner=user, size_in_gb=30)
2020-08-04 16:56:36 +00:00
vm25206_ssd.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
vm25206_ssd.save()
2020-08-08 17:30:25 +00:00
# change 1
vm25206.cores = 2
vm25206.ram_in_gb = 8
vm25206.save()
2020-08-01 22:55:07 +00:00
vm25206.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))
2020-08-09 08:14:49 +00:00
# change 2
vm25206_ssd.size_in_gb = 50
vm25206_ssd.save()
vm25206_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
2020-08-09 08:14:49 +00:00
# 25206 done.
2020-08-08 17:30:25 +00:00
2020-08-09 08:14:49 +00:00
# 25615
vm25615 = VMProduct(name="25615", cores=1, ram_in_gb=4, owner=user)
vm25615.create_order_at(timezone.make_aware(datetime.datetime(2020,3,3)))
vm25615.save()
2020-08-08 17:30:25 +00:00
2020-08-09 08:14:49 +00:00
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.save()
Bill.create_next_bill_for_user(user)
sys.exit(0)
2020-08-08 17:30:25 +00:00
2020-08-01 22:55:07 +00:00
vm25615.cores = 2
vm25615.ram_in_gb = 8
vm25615.save()
vm25615.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))
vm25615_ssd.size_in_gb = 50
vm25615_ssd.save()
vm25615_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
vm25208 = VMProduct.objects.create(name="OpenNebula 25208",
cores=1,
ram_in_gb=4,
owner=user)
vm25208_ssd = VMDiskProduct.objects.create(vm=vm25208,
owner=user,
size_in_gb=30)
vm25208.cores = 2
vm25208.ram_in_gb = 8
vm25208.save()
vm25208.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,4,17)))
vm25208_ssd.size_in_gb = 50
vm25208_ssd.save()
vm25208_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
2020-08-04 16:56:36 +00:00
# 25207
2020-08-01 22:55:07 +00:00
vm25207 = VMProduct.objects.create(name="OpenNebula 25207",
cores=1,
ram_in_gb=4,
owner=user)
vm25207_ssd = VMDiskProduct.objects.create(vm=vm25207,
owner=user,
size_in_gb=30)
vm25207_ssd.size_in_gb = 50
vm25207_ssd.save()
vm25207_ssd.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,8,5)))
vm25207.cores = 2
vm25207.ram_in_gb = 8
vm25207.save()
vm25207.create_or_update_order(when_to_start=timezone.make_aware(datetime.datetime(2020,6,19)))
# FIXES: check starting times (they are slightly different)
# add vm 25236