From 5da6dbb32e481be479d410136178eb780cf3ef6c Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 9 Feb 2020 11:14:50 +0100 Subject: [PATCH] ++hack / list products Signed-off-by: Nico Schottelius --- uncloud/hack/db.py | 7 +++++++ uncloud/hack/main.py | 14 ++++++++++++++ uncloud/hack/product.py | 19 ++++++++++++++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/uncloud/hack/db.py b/uncloud/hack/db.py index 9086865..3e9a3c6 100644 --- a/uncloud/hack/db.py +++ b/uncloud/hack/db.py @@ -79,12 +79,19 @@ class DB(object): return value + @readable_errors + def get_prefix(self, key, as_json=False, **kwargs): + key_range = self._db_clients[0].get_prefix(self.realkey(key), **kwargs) + + return key_range + @readable_errors def set(self, key, value, as_json=False, **kwargs): if as_json: value = json.dumps(value) + log.debug("Setting {} = {}".format(self.realkey(key), value)) # FIXME: iterate over clients in case of failure ? return self._db_clients[0].put(self.realkey(key), value, **kwargs) diff --git a/uncloud/hack/main.py b/uncloud/hack/main.py index b4717fd..e3e6dc4 100644 --- a/uncloud/hack/main.py +++ b/uncloud/hack/main.py @@ -9,6 +9,7 @@ from uncloud.hack.mac import MAC from uncloud.hack.net import VXLANBridge, DNSRA from uncloud import UncloudException +from uncloud.hack.product import ProductOrder arg_parser = argparse.ArgumentParser('hack', add_help=False) #description="Commands that are unfinished - use at own risk") @@ -41,6 +42,7 @@ arg_parser.add_argument('--hackprefix', help="hackprefix, if you need it you kno # order based commands => later to be shifted below "order" arg_parser.add_argument('--order', action='store_true') +arg_parser.add_argument('--list-orders', help="List all orders", action='store_true') arg_parser.add_argument('--product', choices=["dualstack-vm"]) arg_parser.add_argument('--os-image-name', help="Name of OS image (successor to --image)") arg_parser.add_argument('--os-image-size', help="Size of OS image in GB", type=int, default=10) @@ -48,6 +50,9 @@ arg_parser.add_argument('--os-image-size', help="Size of OS image in GB", type=i arg_parser.add_argument('--username') arg_parser.add_argument('--password') +arg_parser.add_argument('--api', help="Run the API") + + log = logging.getLogger(__name__) @@ -79,6 +84,7 @@ def order(config): # create DB entry for VM vm = VM(config) + vm.product.db_entry["owner"] = config.arguments['username'] vm.product.place_order() @@ -86,9 +92,17 @@ def order(config): def main(arguments): config = Config(arguments) + if arguments['api']: + api = API() + api.run() + if arguments['order']: order(config) + if arguments['list_orders']: + p = ProductOrder(config) + p.list_orders() + if arguments['create_vm']: vm = VM(config) vm.create() diff --git a/uncloud/hack/product.py b/uncloud/hack/product.py index 73f140a..925fcdc 100755 --- a/uncloud/hack/product.py +++ b/uncloud/hack/product.py @@ -24,7 +24,14 @@ import uuid from uncloud import UncloudException from uncloud.hack.db import DB -# states +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): @@ -54,7 +61,7 @@ class Product(object): def valid_status(self): if "status" in self.db_entry: - if self.db_entry["status"] in [ "NEW", "CREATED", "DELETED" ]: + if self.db_entry["status"] in [ "NEW", "SCHEDULED", "CREATED", "DELETED" ]: return False return True @@ -63,6 +70,9 @@ class Product(object): 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 """ @@ -72,9 +82,8 @@ class Product(object): 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 self.features + return json.dumps(self.db_entry)