uncloud-mravi/uncloud/hack/product.py

139 lines
4.7 KiB
Python
Raw Normal View History

2020-02-09 08:36:50 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 2020 Nico Schottelius (nico.schottelius at ungleich.ch)
#
# This file is part of uncloud.
#
# uncloud is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# uncloud is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with uncloud. If not, see <http://www.gnu.org/licenses/>.
import json
import uuid
import logging
2020-02-09 08:36:50 +00:00
from uncloud import UncloudException
from uncloud.hack.db import DB
log = logging.getLogger(__name__)
class ProductOrder(object):
def __init__(self, config, product_entry=None, db_entry=None):
self.config = config
self.db = DB(self.config, prefix="/orders")
self.db_entry = {}
self.db_entry["db_version"] = 1
self.db_entry["product"] = product_entry
# Overwrite if we are loading an existing product order
if db_entry:
self.db_entry = db_entry
# FIXME: this should return a list of our class!
def list_orders(self, filter_key=None, filter_regexp_value=None):
"""List all orders with - filtering not yet implemented """
for k,v in self.db.get_prefix("", as_json=True):
log.debug("{} {}".format(k,v))
yield self.__class__(self.config, db_entry=v)
def process_orders(self):
for orders in self.list_orders():
pass
def set_required_values(self):
if not "uuid" in self.db_entry:
self.db_entry["uuid"] = str(uuid.uuid4())
if not "status" in self.db_entry:
self.db_entry["status"] = "NEW"
if not "owner" in self.db_entry:
self.db_entry["owner"] = "UNKNOWN"
def validate_status(self):
if "status" in self.db_entry:
if self.db_entry["status"] in [ "NEW", "SCHEDULED", "CREATED", "DELETED", "REJECTED" ]:
return False
return True
def order(self):
if not self.db_entry["status"] == "NEW":
raise UncloudException("Cannot re-order same order. Status: {}".format(self.db_entry["status"]))
2020-02-09 08:36:50 +00:00
class Product(object):
def __init__(self,
config,
product_name,
db_entry=None):
2020-02-09 08:36:50 +00:00
self.config = config
self.db = DB(self.config, prefix="/orders")
self.db_entry = {}
self.db_entry["product_name"] = product_name
self.db_entry["db_version"] = 1
self.db_entry["features"] = {}
2020-02-09 08:36:50 +00:00
# Existing product? Read in db_entry
if db_entry:
self.db_entry = db_entry
self.valid_periods = [ "per_year", "per_month", "per_week",
"per_day", "per_hour",
"per_minute", "per_second" ]
2020-02-09 08:36:50 +00:00
def define_feature(self,
name,
one_time_price,
recurring_price,
recurring_period,
minimum_period):
self.db_entry['features'][name] = {}
self.db_entry['features'][name]['one_time_price'] = one_time_price
self.db_entry['features'][name]['recurring_price'] = recurring_price
2020-02-09 08:36:50 +00:00
if not recurring_period in self.valid_periods:
raise UncloudException("Invalid recurring period: {}".format(recurring_period))
self.db_entry['features'][name]['recurring_period'] = recurring_period
if not minimum_period in self.valid_periods:
raise UncloudException("Invalid recurring period: {}".format(recurring_period))
recurring_index = self.valid_periods.index(recurring_period)
minimum_index = self.valid_periods.index(minimum_period)
if minimum_index < recurring_index:
raise UncloudException("Minimum period for product '{}' feature '{}' must be shorter or equal than/as recurring period: {} > {}".format(self.db_entry['product_name'], name, minimum_period, recurring_period))
self.db_entry['features'][name]['minimum_period'] = minimum_period
def validate_product(self):
for feature in self.db_entry['features']:
pass
2020-02-09 08:36:50 +00:00
def place_order(self):
""" Schedule creating the product in etcd """
order = ProductOrder(self.config, self.db_entry)
order.set_required_values()
order.order()
2020-02-09 08:36:50 +00:00
self.db.set(self.db_entry["uuid"], str(self))
2020-02-09 08:36:50 +00:00
def __str__(self):
return json.dumps(self.db_entry)