uncloud/uncloud/hack/product.py

90 lines
2.7 KiB
Python
Executable File

#!/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
from uncloud import UncloudException
from uncloud.hack.db import DB
class ProductOrder(object):
def __init__(self, config):
self.config = config
self.db = DB(self.config, prefix="/orders")
def list_orders(self, filter_key=None, filter_regexp_value=None):
for k,m in self.db.get_prefix(""):
print("{} {}".format(k,m))
class Product(object):
def __init__(self, config, product_name, db_entry=None):
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
# Existing product? Read in db_entry
if db_entry:
self.db_entry = db_entry
@staticmethod
def define_feature(self,
name,
feature,
one_time_price,
recurring_price,
recurring_period,
minimum_period):
feature = {}
feature[name] = {}
def valid_status(self):
if "status" in self.db_entry:
if self.db_entry["status"] in [ "NEW", "SCHEDULED", "CREATED", "DELETED" ]:
return False
return True
def validate_product(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 place_order(self):
""" Schedule creating the product in etcd """
self.validate_product()
# FIXME: very status
if not self.db_entry["status"] == "NEW":
raise UncloudException("Cannot re-order product")
self.db.set(self.db_entry["uuid"], str(self))
def __str__(self):
return json.dumps(self.db_entry)