forked from uncloud/uncloud
Merge branch 'nico/meow-pay-master' into HEAD
This commit is contained in:
commit
e4f2f446f5
75 changed files with 573 additions and 182 deletions
0
uncloud/uncloud_api/__init__.py
Normal file
0
uncloud/uncloud_api/__init__.py
Normal file
6
uncloud/uncloud_api/admin.py
Normal file
6
uncloud/uncloud_api/admin.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from .models import Product, Feature
|
||||
|
||||
#admin.site.register(Product)
|
||||
#admin.site.register(Feature)
|
||||
5
uncloud/uncloud_api/apps.py
Normal file
5
uncloud/uncloud_api/apps.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
name = 'uncloud_api'
|
||||
0
uncloud/uncloud_api/management/__init__.py
Normal file
0
uncloud/uncloud_api/management/__init__.py
Normal file
0
uncloud/uncloud_api/management/commands/__init__.py
Normal file
0
uncloud/uncloud_api/management/commands/__init__.py
Normal file
26
uncloud/uncloud_api/management/commands/hack.py
Normal file
26
uncloud/uncloud_api/management/commands/hack.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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 = '<None>'
|
||||
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))
|
||||
29
uncloud/uncloud_api/management/commands/snapshot.py
Normal file
29
uncloud/uncloud_api/management/commands/snapshot.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import time
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from uncloud_api import models
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
args = '<None>'
|
||||
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)
|
||||
31
uncloud/uncloud_api/migrations/0001_initial.py
Normal file
31
uncloud/uncloud_api/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-23 10:16
|
||||
|
||||
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='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)),
|
||||
('gb_ssd', models.FloatField()),
|
||||
('gb_hdd', models.FloatField()),
|
||||
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
46
uncloud/uncloud_api/migrations/0002_auto_20200222_0719.py
Normal file
46
uncloud/uncloud_api/migrations/0002_auto_20200222_0719.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# Generated by Django 3.0.3 on 2020-02-22 07:19
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('uncloud_api', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='VMSnapshotOrder',
|
||||
fields=[
|
||||
('order_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_api.Order')),
|
||||
],
|
||||
bases=('uncloud_api.order',),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='VMSnapshotProduct',
|
||||
fields=[
|
||||
('product_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_api.Product')),
|
||||
('gb_ssd', models.FloatField()),
|
||||
('gb_hdd', models.FloatField()),
|
||||
],
|
||||
bases=('uncloud_api.product',),
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='OrderReference',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='product',
|
||||
name='name',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='product',
|
||||
name='recurring_period',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='status',
|
||||
field=models.CharField(choices=[('pending', 'Pending'), ('being_created', 'Being created'), ('created_active', 'Created'), ('deleted', 'Deleted')], default='pending', max_length=256),
|
||||
),
|
||||
]
|
||||
0
uncloud/uncloud_api/migrations/__init__.py
Normal file
0
uncloud/uncloud_api/migrations/__init__.py
Normal file
132
uncloud/uncloud_api/models.py
Normal file
132
uncloud/uncloud_api/models.py
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
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)
|
||||
|
||||
# override these fields by default
|
||||
|
||||
description = ""
|
||||
recurring_period = "not_recurring"
|
||||
|
||||
status = models.CharField(max_length=256,
|
||||
choices = (
|
||||
('pending', 'Pending'),
|
||||
('being_created', 'Being created'),
|
||||
('created_active', 'Created'),
|
||||
('deleted', 'Deleted')
|
||||
),
|
||||
default='pending'
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
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)
|
||||
19
uncloud/uncloud_api/serializers.py
Normal file
19
uncloud/uncloud_api/serializers.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
from django.contrib.auth.models import Group
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ['url', 'username', 'email', 'groups']
|
||||
|
||||
|
||||
class GroupSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Group
|
||||
fields = ['url', 'name']
|
||||
|
||||
class VMSnapshotSerializer(serializers.Serializer):
|
||||
pass
|
||||
3
uncloud/uncloud_api/tests.py
Normal file
3
uncloud/uncloud_api/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
83
uncloud/uncloud_api/views.py
Normal file
83
uncloud/uncloud_api/views.py
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
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/<uuid:uuid> => 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/<productname> 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<pname>.+)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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue