uncloud/uncloud_pay/management/commands/add-opennebula-vm-orders.py
Nico Schottelius f7b14bf507 cleanup migrations
Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
2020-08-01 18:38:38 +02:00

77 lines
2.8 KiB
Python

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 *
#import opennebula.models as one
from uncloud_vm.models import *
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
}
)
orders = []
# 25206
vm25206 = VMProduct.objects.create(name="OpenNebula 25206",
cores=1,
ram_in_gb=4,
owner=user)
vm25206_ssd = VMDiskProduct.objects.create(vm=vm25206,
owner=user,
size_in_gb=30)
order_vm_25206 = Order.objects.create(owner=user,
billing_address=addr,
starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
recurring_period=RecurringPeriod.PER_30D,
recurring_price = vm_price_2020(cpu=1, ram=4) / 2,
description = "VM %s" % vm25206
)
order_vm_25206_ssd = Order.objects.create(owner=user,
billing_address=addr,
starting_date=timezone.make_aware(datetime.datetime(2020,3,3)),
recurring_period=RecurringPeriod.PER_30D,
recurring_price = disk_price_2020(30, VMDiskType.CEPH_SSD) / 2,
description = vm25206_ssd
)