init rintik server
This commit is contained in:
commit
a6c0f1d67a
35 changed files with 1381 additions and 0 deletions
245
.gitignore
vendored
Normal file
245
.gitignore
vendored
Normal file
|
@ -0,0 +1,245 @@
|
|||
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/django,pycharm+all
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=django,pycharm+all
|
||||
|
||||
### Django ###
|
||||
*.log
|
||||
*.pot
|
||||
*.pyc
|
||||
__pycache__/
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
media
|
||||
|
||||
# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
|
||||
# in your Git repository. Update and uncomment the following line accordingly.
|
||||
# <django-project-name>/staticfiles/
|
||||
|
||||
### Django.Python Stack ###
|
||||
# Byte-compiled / optimized / DLL files
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
|
||||
# Django stuff:
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
### PyCharm+all ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
### PyCharm+all Patch ###
|
||||
# Ignores the whole .idea folder and all .iml files
|
||||
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360
|
||||
|
||||
.idea/
|
||||
|
||||
# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
|
||||
|
||||
*.iml
|
||||
modules.xml
|
||||
.idea/misc.xml
|
||||
*.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
.idea/sonarlint
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/django,pycharm+all
|
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
6
api/apps.py
Normal file
6
api/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api'
|
57
api/serializers.py
Normal file
57
api/serializers.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
from rest_framework import serializers
|
||||
|
||||
from core.models import FlavorPrice, VolumePrice, FloatingIpsPrice, Invoice, InvoiceInstance, InvoiceVolume, \
|
||||
InvoiceFloatingIp
|
||||
|
||||
|
||||
class FlavorPriceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = FlavorPrice
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class FloatingIpsPriceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = FloatingIpsPrice
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class VolumePriceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = VolumePrice
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class InvoiceInstanceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = InvoiceInstance
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class InvoiceVolumeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = InvoiceVolume
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class InvoiceFloatingIpSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = InvoiceFloatingIp
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class InvoiceSerializer(serializers.ModelSerializer):
|
||||
instances = InvoiceInstanceSerializer(many=True)
|
||||
floating_ips = InvoiceFloatingIpSerializer(many=True)
|
||||
volumes = InvoiceVolumeSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = Invoice
|
||||
fields = '__all__'
|
||||
|
||||
|
||||
class SimpleInvoiceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Invoice
|
||||
fields = '__all__'
|
||||
|
15
api/urls.py
Normal file
15
api/urls.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
from django.urls import path, include
|
||||
from rest_framework import routers
|
||||
|
||||
from api import views
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'flavor_price', views.FlavorPriceViewSet)
|
||||
router.register(r'floating_ips_price', views.FloatingIpsPriceViewSet)
|
||||
router.register(r'volume_price', views.VolumePriceViewSet)
|
||||
router.register(r'invoice', views.InvoiceViewSet, basename='invoice')
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
path('api-auth/', include('rest_framework.urls')),
|
||||
]
|
41
api/views.py
Normal file
41
api/views.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from api.serializers import FlavorPriceSerializer, FloatingIpsPriceSerializer, VolumePriceSerializer, InvoiceSerializer, \
|
||||
SimpleInvoiceSerializer
|
||||
from core.models import FlavorPrice, FloatingIpsPrice, VolumePrice, Invoice
|
||||
|
||||
|
||||
class FlavorPriceViewSet(viewsets.ModelViewSet):
|
||||
queryset = FlavorPrice.objects
|
||||
serializer_class = FlavorPriceSerializer
|
||||
|
||||
|
||||
class FloatingIpsPriceViewSet(viewsets.ModelViewSet):
|
||||
queryset = FloatingIpsPrice.objects
|
||||
serializer_class = FloatingIpsPriceSerializer
|
||||
|
||||
|
||||
class VolumePriceViewSet(viewsets.ModelViewSet):
|
||||
queryset = VolumePrice.objects
|
||||
serializer_class = VolumePriceSerializer
|
||||
|
||||
|
||||
class InvoiceViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = InvoiceSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
tenant_id = self.request.query_params.get('tenant_id', None)
|
||||
return Invoice.objects.filter(project__tenant_id=tenant_id)
|
||||
|
||||
@action(detail=False)
|
||||
def simple_lists(self, request):
|
||||
serializer = SimpleInvoiceSerializer(self.get_queryset(), many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(detail=False, methods=['POST'])
|
||||
def init_invoice(self, request):
|
||||
# TODO: Init invoice
|
||||
serializer = InvoiceSerializer()
|
||||
return Response(serializer.data)
|
0
core/__init__.py
Normal file
0
core/__init__.py
Normal file
44
core/admin.py
Normal file
44
core/admin.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from core.models import FlavorPrice, VolumePrice, FloatingIpsPrice, BillingProject, Invoice, InvoiceVolume, \
|
||||
InvoiceFloatingIp, InvoiceInstance
|
||||
|
||||
|
||||
@admin.register(FlavorPrice)
|
||||
class FlavorPriceAdmin(admin.ModelAdmin):
|
||||
list_display = ('flavor_id', 'daily_price', 'monthly_price')
|
||||
|
||||
|
||||
@admin.register(FloatingIpsPrice)
|
||||
class FloatingIpsPriceAdmin(admin.ModelAdmin):
|
||||
list_display = ('daily_price', 'monthly_price')
|
||||
|
||||
|
||||
@admin.register(VolumePrice)
|
||||
class VolumePriceAdmin(admin.ModelAdmin):
|
||||
list_display = ('volume_type_id', 'daily_price', 'monthly_price')
|
||||
|
||||
|
||||
@admin.register(BillingProject)
|
||||
class BillingProjectAdmin(admin.ModelAdmin):
|
||||
list_display = ('tenant_id',)
|
||||
|
||||
|
||||
@admin.register(Invoice)
|
||||
class InvoiceAdmin(admin.ModelAdmin):
|
||||
list_display = ('__str__',)
|
||||
|
||||
|
||||
@admin.register(InvoiceInstance)
|
||||
class InvoiceInstanceAdmin(admin.ModelAdmin):
|
||||
list_display = ('instance_id',)
|
||||
|
||||
|
||||
@admin.register(InvoiceFloatingIp)
|
||||
class InvoiceFloatingIpAdmin(admin.ModelAdmin):
|
||||
list_display = ('fip_id',)
|
||||
|
||||
|
||||
@admin.register(InvoiceVolume)
|
||||
class InvoiceVolumeAdmin(admin.ModelAdmin):
|
||||
list_display = ('volume_id',)
|
6
core/apps.py
Normal file
6
core/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core'
|
0
core/management/__init__.py
Normal file
0
core/management/__init__.py
Normal file
0
core/management/commands/__init__.py
Normal file
0
core/management/commands/__init__.py
Normal file
59
core/management/commands/new_invoice.py
Normal file
59
core/management/commands/new_invoice.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
import logging
|
||||
from typing import List
|
||||
|
||||
from django.core.management import BaseCommand
|
||||
from django.utils import timezone
|
||||
|
||||
from core.models import Invoice, InvoiceInstance
|
||||
|
||||
LOG = logging.getLogger("rintik_new_invoice")
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Rintik New Invoice'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.close_date = timezone.now()
|
||||
self.tax_pertentage = 0
|
||||
|
||||
active_invoices = Invoice.objects.filter(state=Invoice.InvoiceState.IN_PROGRESS).all()
|
||||
for active_invoice in active_invoices:
|
||||
self.close_active_invoice(active_invoice)
|
||||
|
||||
def close_active_invoice(self, active_invoice: Invoice):
|
||||
# Close Active instances
|
||||
active_instances: List[InvoiceInstance] = active_invoice.instances.filter(end_date=None).all()
|
||||
for instance in active_instances:
|
||||
instance.end_date = self.close_date
|
||||
instance.save()
|
||||
|
||||
|
||||
# Save current instance
|
||||
active_invoice.state = Invoice.InvoiceState.FINISHED
|
||||
active_invoice.end_date = self.close_date
|
||||
active_invoice.tax = self.tax_pertentage * active_invoice.subtotal
|
||||
active_invoice.total = active_invoice.tax + active_invoice.subtotal
|
||||
active_invoice.save()
|
||||
|
||||
# Creating new Invoice
|
||||
new_invoice = Invoice.objects.create(
|
||||
project=active_invoice.project,
|
||||
start_date=self.close_date,
|
||||
state=Invoice.InvoiceState.IN_PROGRESS
|
||||
)
|
||||
new_invoice.save()
|
||||
|
||||
# Cloning Active Instance to Continue in next invoice
|
||||
# Using the same price
|
||||
for instance in active_instances:
|
||||
InvoiceInstance.objects.create(
|
||||
invoice=new_invoice,
|
||||
instance_id=instance.instance_id,
|
||||
name=instance.name,
|
||||
flavor_id=instance.flavor_id,
|
||||
current_state=instance.current_state,
|
||||
start_date=instance.end_date,
|
||||
daily_price=instance.daily_price,
|
||||
monthly_price=instance.monthly_price,
|
||||
)
|
||||
|
72
core/management/commands/start_rintik_event_monitor.py
Normal file
72
core/management/commands/start_rintik_event_monitor.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
from django.conf import settings
|
||||
import logging
|
||||
import os
|
||||
import signal
|
||||
import time
|
||||
|
||||
from oslo_config import cfg
|
||||
import oslo_messaging as messaging
|
||||
from oslo_messaging import notify # noqa
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from core.notification_endpoint import NotifyEndpoint
|
||||
|
||||
LOG = logging.getLogger("rintik_notification")
|
||||
|
||||
|
||||
class SignalExit(SystemExit):
|
||||
def __init__(self, signo, exccode=1):
|
||||
super(SignalExit, self).__init__(exccode)
|
||||
self.signo = signo
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Start Rintik Event Monitor'
|
||||
|
||||
def signal_handler(self, signum, frame):
|
||||
raise SignalExit(signum)
|
||||
|
||||
def run_server(self, transport, server):
|
||||
try:
|
||||
server.start()
|
||||
server.wait()
|
||||
LOG.info('The server is terminating')
|
||||
time.sleep(1)
|
||||
except SignalExit as e:
|
||||
LOG.info('Signal %s is caught. Interrupting the execution',
|
||||
e.signo)
|
||||
server.stop()
|
||||
server.wait()
|
||||
finally:
|
||||
transport.cleanup()
|
||||
|
||||
def notify_server(self, transport, topic):
|
||||
endpoints = [NotifyEndpoint()]
|
||||
target = messaging.Target(topic='notifications')
|
||||
target_versioned = messaging.Target(topic='versioned_notifications')
|
||||
server = notify.get_notification_listener(
|
||||
transport,
|
||||
[target, target_versioned],
|
||||
endpoints,
|
||||
executor='threading'
|
||||
)
|
||||
self.run_server(transport, server)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
url = settings.RINTIK_NOTIFICATION_URL
|
||||
transport = messaging.get_notification_transport(cfg.CONF,
|
||||
url=url)
|
||||
|
||||
# oslo.config defaults
|
||||
cfg.CONF.heartbeat_interval = 5
|
||||
cfg.CONF.prog = os.path.basename(__file__)
|
||||
cfg.CONF.project = 'rintik'
|
||||
|
||||
signal.signal(signal.SIGTERM, self.signal_handler)
|
||||
signal.signal(signal.SIGINT, self.signal_handler)
|
||||
|
||||
self.notify_server(
|
||||
transport=transport,
|
||||
topic=settings.RINTIK_NOTIFICATION_TOPIC,
|
||||
)
|
128
core/migrations/0001_initial.py
Normal file
128
core/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,128 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-28 07:05
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import djmoney.models.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='BillingProject',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('project_id', models.CharField(max_length=256)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='FlavorPrice',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('flavor_id', models.CharField(max_length=256)),
|
||||
('daily_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('daily_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('monthly_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('monthly_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='FloatingIpsPrice',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('daily_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('daily_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('monthly_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('monthly_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Invoice',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_created=True)),
|
||||
('invoice_number', models.CharField(default=None, max_length=256, null=True)),
|
||||
('start_date', models.DateTimeField()),
|
||||
('end_date', models.DateTimeField()),
|
||||
('due_date', models.DateTimeField(default=None, null=True)),
|
||||
('state', models.IntegerField(choices=[(1, 'In Progress'), (2, 'Waiting Payment'), (100, 'Finished')])),
|
||||
('subtotal', models.BigIntegerField(default=None, null=True)),
|
||||
('tax', models.BigIntegerField(default=None, null=True)),
|
||||
('total', models.BigIntegerField(default=None, null=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.billingproject')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='VolumePrice',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('volume_type_id', models.CharField(max_length=256)),
|
||||
('daily_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('daily_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('monthly_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('monthly_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InvoiceVolume',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_created=True)),
|
||||
('volume_id', models.CharField(max_length=266)),
|
||||
('volume_type_id', models.CharField(max_length=266)),
|
||||
('space_allocation_gb', models.IntegerField()),
|
||||
('current_state', models.CharField(max_length=256)),
|
||||
('start_date', models.DateTimeField()),
|
||||
('end_date', models.DateTimeField()),
|
||||
('daily_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('daily_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('monthly_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('monthly_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('invoice', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.invoice')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InvoiceInstance',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_created=True)),
|
||||
('instance_id', models.CharField(max_length=266)),
|
||||
('name', models.CharField(max_length=256)),
|
||||
('flavor_id', models.CharField(max_length=256)),
|
||||
('current_state', models.CharField(max_length=256)),
|
||||
('start_date', models.DateTimeField()),
|
||||
('end_date', models.DateTimeField()),
|
||||
('daily_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('daily_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('monthly_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('monthly_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('invoice', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.invoice')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InvoiceFloatingIp',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_created=True)),
|
||||
('fip_id', models.CharField(max_length=266)),
|
||||
('ip', models.CharField(max_length=256)),
|
||||
('current_state', models.CharField(max_length=256)),
|
||||
('start_date', models.DateTimeField()),
|
||||
('end_date', models.DateTimeField()),
|
||||
('daily_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('daily_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('monthly_price_currency', djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3)),
|
||||
('monthly_price', djmoney.models.fields.MoneyField(decimal_places=2, max_digits=10)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('invoice', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.invoice')),
|
||||
],
|
||||
),
|
||||
]
|
58
core/migrations/0002_auto_20210828_1112.py
Normal file
58
core/migrations/0002_auto_20210828_1112.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-28 11:12
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_created=True, blank=True, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='due_date',
|
||||
field=models.DateTimeField(blank=True, default=None, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='end_date',
|
||||
field=models.DateTimeField(blank=True, default=None, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='tax',
|
||||
field=models.BigIntegerField(blank=True, default=None, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='total',
|
||||
field=models.BigIntegerField(blank=True, default=None, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='updated_at',
|
||||
field=models.DateTimeField(auto_now=True, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_created=True, blank=True, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='end_date',
|
||||
field=models.DateTimeField(blank=True, default=None, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='updated_at',
|
||||
field=models.DateTimeField(auto_now=True, null=True),
|
||||
),
|
||||
]
|
23
core/migrations/0003_auto_20210828_1115.py
Normal file
23
core/migrations/0003_auto_20210828_1115.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-28 11:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0002_auto_20210828_1112'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='invoice_number',
|
||||
field=models.CharField(blank=True, default=None, max_length=256, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='subtotal',
|
||||
field=models.BigIntegerField(blank=True, default=None, null=True),
|
||||
),
|
||||
]
|
22
core/migrations/0004_auto_20210828_1137.py
Normal file
22
core/migrations/0004_auto_20210828_1137.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-28 11:37
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0003_auto_20210828_1115'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='billingproject',
|
||||
old_name='project_id',
|
||||
new_name='tenant_id',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='invoice',
|
||||
name='invoice_number',
|
||||
),
|
||||
]
|
33
core/migrations/0005_auto_20210830_0931.py
Normal file
33
core/migrations/0005_auto_20210830_0931.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-30 09:31
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0004_auto_20210828_1137'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='invoice',
|
||||
name='due_date',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoicefloatingip',
|
||||
name='invoice',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='floating_ips', to='core.invoice'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='invoice',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='instances', to='core.invoice'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoicevolume',
|
||||
name='invoice',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='volumes', to='core.invoice'),
|
||||
),
|
||||
]
|
103
core/migrations/0006_auto_20210831_0400.py
Normal file
103
core/migrations/0006_auto_20210831_0400.py
Normal file
|
@ -0,0 +1,103 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-31 04:00
|
||||
|
||||
from django.db import migrations, models
|
||||
import djmoney.models.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0005_auto_20210830_0931'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='invoice',
|
||||
name='subtotal',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='invoice',
|
||||
name='tax_currency',
|
||||
field=djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='invoice',
|
||||
name='total_currency',
|
||||
field=djmoney.models.fields.CurrencyField(choices=[('IDR', 'Indonesian Rupiah'), ('USD', 'US Dollar')], default='IDR', editable=False, max_length=3),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='flavorprice',
|
||||
name='daily_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='flavorprice',
|
||||
name='monthly_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='floatingipsprice',
|
||||
name='daily_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='floatingipsprice',
|
||||
name='monthly_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='state',
|
||||
field=models.IntegerField(choices=[(1, 'In Progress'), (100, 'Finished')]),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='tax',
|
||||
field=djmoney.models.fields.MoneyField(blank=True, decimal_places=0, default=None, max_digits=10, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='total',
|
||||
field=djmoney.models.fields.MoneyField(blank=True, decimal_places=0, default=None, max_digits=10, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoicefloatingip',
|
||||
name='daily_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoicefloatingip',
|
||||
name='monthly_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='daily_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='monthly_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoicevolume',
|
||||
name='daily_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoicevolume',
|
||||
name='monthly_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='volumeprice',
|
||||
name='daily_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='volumeprice',
|
||||
name='monthly_price',
|
||||
field=djmoney.models.fields.MoneyField(decimal_places=0, max_digits=10),
|
||||
),
|
||||
]
|
23
core/migrations/0007_auto_20210831_0403.py
Normal file
23
core/migrations/0007_auto_20210831_0403.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.2.6 on 2021-08-31 04:03
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0006_auto_20210831_0400'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='invoice',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_now_add=True, null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='invoiceinstance',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_now_add=True, null=True),
|
||||
),
|
||||
]
|
0
core/migrations/__init__.py
Normal file
0
core/migrations/__init__.py
Normal file
109
core/models.py
Normal file
109
core/models.py
Normal file
|
@ -0,0 +1,109 @@
|
|||
from calendar import monthrange
|
||||
from datetime import timedelta
|
||||
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from djmoney.models.fields import MoneyField
|
||||
|
||||
|
||||
class FlavorPrice(models.Model):
|
||||
flavor_id = models.CharField(max_length=256)
|
||||
daily_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
monthly_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
|
||||
|
||||
class FloatingIpsPrice(models.Model):
|
||||
daily_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
monthly_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
|
||||
|
||||
class VolumePrice(models.Model):
|
||||
volume_type_id = models.CharField(max_length=256)
|
||||
daily_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
monthly_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
|
||||
|
||||
class BillingProject(models.Model):
|
||||
tenant_id = models.CharField(max_length=256)
|
||||
|
||||
|
||||
class Invoice(models.Model):
|
||||
class InvoiceState(models.IntegerChoices):
|
||||
IN_PROGRESS = 1
|
||||
FINISHED = 100
|
||||
|
||||
project = models.ForeignKey('BillingProject', on_delete=models.CASCADE)
|
||||
start_date = models.DateTimeField()
|
||||
end_date = models.DateTimeField(default=None, blank=True, null=True)
|
||||
state = models.IntegerField(choices=InvoiceState.choices)
|
||||
tax = MoneyField(max_digits=10, decimal_places=0, default=None, blank=True, null=True)
|
||||
total = MoneyField(max_digits=10, decimal_places=0, default=None, blank=True, null=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
|
||||
updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)
|
||||
|
||||
@property
|
||||
def subtotal(self):
|
||||
# Need to optimize? currently price is calculated on the fly, maybe need to save to db to make performance faster?
|
||||
# Instance Price
|
||||
instance_price = sum(map(lambda x: x.price_charged, self.instances.all()))
|
||||
|
||||
# TODO: Add other price
|
||||
return instance_price
|
||||
|
||||
|
||||
class InvoiceInstance(models.Model):
|
||||
invoice = models.ForeignKey('Invoice', on_delete=models.CASCADE, related_name='instances')
|
||||
instance_id = models.CharField(max_length=266)
|
||||
name = models.CharField(max_length=256)
|
||||
flavor_id = models.CharField(max_length=256)
|
||||
current_state = models.CharField(max_length=256)
|
||||
start_date = models.DateTimeField()
|
||||
end_date = models.DateTimeField(default=None, blank=True, null=True)
|
||||
daily_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
monthly_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
|
||||
updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)
|
||||
|
||||
@property
|
||||
def price_charged(self):
|
||||
# TODO: Fix price calculation
|
||||
# Currently only calculate daily price and it can return zero if end date not yet 1 day
|
||||
|
||||
current_date = timezone.now()
|
||||
if self.end_date:
|
||||
end_date = self.end_date
|
||||
else:
|
||||
end_date = current_date
|
||||
|
||||
# TODO: For Testing, please delete
|
||||
end_date += timedelta(days=1)
|
||||
|
||||
days = (end_date - self.start_date).days
|
||||
return self.daily_price * days
|
||||
|
||||
|
||||
class InvoiceFloatingIp(models.Model):
|
||||
invoice = models.ForeignKey('Invoice', on_delete=models.CASCADE, related_name='floating_ips')
|
||||
fip_id = models.CharField(max_length=266)
|
||||
ip = models.CharField(max_length=256)
|
||||
current_state = models.CharField(max_length=256)
|
||||
start_date = models.DateTimeField()
|
||||
end_date = models.DateTimeField()
|
||||
daily_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
monthly_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
created_at = models.DateTimeField(auto_created=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
||||
class InvoiceVolume(models.Model):
|
||||
invoice = models.ForeignKey('Invoice', on_delete=models.CASCADE, related_name='volumes')
|
||||
volume_id = models.CharField(max_length=266)
|
||||
volume_type_id = models.CharField(max_length=266)
|
||||
space_allocation_gb = models.IntegerField()
|
||||
current_state = models.CharField(max_length=256)
|
||||
start_date = models.DateTimeField()
|
||||
end_date = models.DateTimeField()
|
||||
daily_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
monthly_price = MoneyField(max_digits=10, decimal_places=0)
|
||||
created_at = models.DateTimeField(auto_created=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
19
core/notification_endpoint.py
Normal file
19
core/notification_endpoint.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
import logging
|
||||
|
||||
from oslo_messaging import NotificationResult
|
||||
|
||||
from core.notification_handler.compute_handler import ComputeHandler
|
||||
|
||||
LOG = logging.getLogger("rintik_notification")
|
||||
|
||||
|
||||
class NotifyEndpoint(object):
|
||||
|
||||
def info(self, ctxt, publisher_id, event_type, payload, metadata):
|
||||
# LOG.info("=== Event Received ===")
|
||||
# LOG.info("Event Type: " + str(event_type))
|
||||
# LOG.info("Payload: " + str(payload))
|
||||
|
||||
ComputeHandler().handle(event_type, payload)
|
||||
|
||||
return NotificationResult.HANDLED
|
0
core/notification_handler/__init__.py
Normal file
0
core/notification_handler/__init__.py
Normal file
71
core/notification_handler/compute_handler.py
Normal file
71
core/notification_handler/compute_handler.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
from django.utils import timezone
|
||||
|
||||
from core.models import Invoice, InvoiceInstance, FlavorPrice
|
||||
|
||||
|
||||
class ComputeHandler:
|
||||
def get_tenant_progress_invoice(self, tenant_id):
|
||||
return Invoice.objects.filter(project_tenant_id=tenant_id, state=Invoice.InvoiceState.IN_PROGRESS).first()
|
||||
|
||||
def handle(self, event_type, payload):
|
||||
if event_type == 'compute.instance.update':
|
||||
tenant_id = payload['tenant_id']
|
||||
|
||||
# Get instance invoice
|
||||
invoice = self.get_tenant_progress_invoice(tenant_id)
|
||||
if not invoice:
|
||||
return
|
||||
|
||||
# print('New Compute State: ' + str(payload['state']))
|
||||
if payload['state'] == 'active':
|
||||
self.handle_active_state(invoice, payload)
|
||||
|
||||
if payload['state'] == 'deleted':
|
||||
self.handle_delete_state(invoice, payload)
|
||||
|
||||
# TODO: Handle flavor change
|
||||
|
||||
def handle_active_state(self, invoice, payload):
|
||||
display_name = payload['display_name']
|
||||
instance_id = payload['instance_id']
|
||||
flavor_id = payload['instance_flavor_id']
|
||||
state = payload['state']
|
||||
|
||||
is_exists = InvoiceInstance.objects.filter(
|
||||
invoice=invoice,
|
||||
instance_id=instance_id,
|
||||
flavor_id=flavor_id
|
||||
).exists()
|
||||
|
||||
if not is_exists:
|
||||
# Get Price
|
||||
flavor_price = FlavorPrice.objects.filter(flavor_id=flavor_id).first()
|
||||
|
||||
# Create new invoice instance
|
||||
InvoiceInstance.objects.create(
|
||||
invoice=invoice,
|
||||
instance_id=instance_id,
|
||||
name=display_name,
|
||||
flavor_id=flavor_id,
|
||||
current_state=state,
|
||||
start_date=timezone.now(),
|
||||
daily_price=flavor_price.daily_price,
|
||||
monthly_price=flavor_price.monthly_price,
|
||||
)
|
||||
|
||||
def handle_delete_state(self, invoice, payload):
|
||||
instance_id = payload['instance_id']
|
||||
flavor_id = payload['instance_flavor_id']
|
||||
state = payload['state']
|
||||
|
||||
invoice_instance = InvoiceInstance.objects.filter(
|
||||
invoice=invoice,
|
||||
instance_id=instance_id,
|
||||
flavor_id=flavor_id,
|
||||
state=state
|
||||
).first()
|
||||
|
||||
if invoice_instance:
|
||||
invoice_instance.end_date = timezone.now()
|
||||
invoice_instance.state = state
|
||||
invoice_instance.save()
|
2
core/notification_handler/network_handler.py
Normal file
2
core/notification_handler/network_handler.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
class NetworkHandler:
|
||||
pass
|
3
core/tests.py
Normal file
3
core/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
3
core/views.py
Normal file
3
core/views.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
22
manage.py
Executable file
22
manage.py
Executable file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rintik.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
10
requirements.txt
Normal file
10
requirements.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
asgiref==3.4.1
|
||||
Django==3.2.6
|
||||
pytz==2021.1
|
||||
sqlparse==0.4.1
|
||||
oslo.messaging>=12.8.0
|
||||
django-money==2.0.1
|
||||
openstacksdk==0.58.0
|
||||
djangorestframework==3.12.4
|
||||
django-filter==2.4.0
|
||||
Markdown==3.3.4
|
0
rintik/__init__.py
Normal file
0
rintik/__init__.py
Normal file
16
rintik/asgi.py
Normal file
16
rintik/asgi.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
ASGI config for rintik project.
|
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rintik.settings')
|
||||
|
||||
application = get_asgi_application()
|
153
rintik/settings.py
Normal file
153
rintik/settings.py
Normal file
|
@ -0,0 +1,153 @@
|
|||
"""
|
||||
Django settings for rintik project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 3.2.6.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/3.2/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/3.2/ref/settings/
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-kt=8eo4p*(5uq4jj^=)l8_4-_&44(#9xp!m0+^+csdok=(@ug3'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'djmoney',
|
||||
'rest_framework',
|
||||
'core',
|
||||
'api',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'rintik.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [BASE_DIR / 'templates']
|
||||
,
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'rintik.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': BASE_DIR / 'db.sqlite3',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/3.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/3.2/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
|
||||
# Logging
|
||||
LOGGING = {
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'handlers': {
|
||||
'console': {
|
||||
'class': 'logging.StreamHandler',
|
||||
},
|
||||
},
|
||||
'root': {
|
||||
'handlers': ['console'],
|
||||
'level': 'INFO',
|
||||
},
|
||||
}
|
||||
|
||||
# Currency Configuration
|
||||
CURRENCIES = ('IDR', 'USD')
|
||||
DEFAULT_CURRENCY = "IDR"
|
||||
|
||||
# Notification
|
||||
RINTIK_NOTIFICATION_URL = "rabbit://openstack:HcmHchx2wbxZYjvbZUFkA5FObioWSkUY74DCpgvB@172.10.10.10:5672/"
|
||||
RINTIK_NOTIFICATION_TOPIC = "notifications"
|
22
rintik/urls.py
Normal file
22
rintik/urls.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
"""rintik URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/3.2/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/', include('api.urls'))
|
||||
]
|
16
rintik/wsgi.py
Normal file
16
rintik/wsgi.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
"""
|
||||
WSGI config for rintik project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rintik.settings')
|
||||
|
||||
application = get_wsgi_application()
|
Loading…
Add table
Reference in a new issue