From 915bcf1dca43d17a72fb3cf0657ea85e15298e32 Mon Sep 17 00:00:00 2001 From: dave Date: Wed, 8 May 2019 22:33:04 -0700 Subject: [PATCH] class and cli renaming --- nodepupper/cli.py | 4 +++- nodepupper/daemon.py | 10 ++++++++-- nodepupper/nodeops.py | 10 ++++++++++ setup.py | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/nodepupper/cli.py b/nodepupper/cli.py index 7121dfe..0e9866b 100644 --- a/nodepupper/cli.py +++ b/nodepupper/cli.py @@ -72,6 +72,7 @@ def main(): spr_addc = spr_action.add_parser("addclass", help="add a class") spr_addc.add_argument("cls", help="name of class to add") + spr_addc.add_argument("-r", "--rename", help="rename class") spr_delc = spr_action.add_parser("delclass", help="delete a class") spr_delc.add_argument("cls", help="name of class to delete") @@ -130,7 +131,8 @@ def main(): print(r.get(args.host.rstrip("/") + "/api/class").text) elif args.action == "addclass": - r.put(args.host.rstrip("/") + "/api/class/" + args.cls).raise_for_status() + r.put(args.host.rstrip("/") + "/api/class/" + args.cls, + params={"rename": args.rename} if args.rename else None).raise_for_status() elif args.action == "delclass": r.delete(args.host.rstrip("/") + "/api/class/" + args.cls).raise_for_status() diff --git a/nodepupper/daemon.py b/nodepupper/daemon.py index d12359b..3329e7b 100644 --- a/nodepupper/daemon.py +++ b/nodepupper/daemon.py @@ -219,10 +219,16 @@ class ClassesApi(object): clslist.sort() yield yamldump({"classes": clslist}) - def PUT(self, cls): + def PUT(self, cls, rename=None): with self.nodes.db.transaction() as c: - if cls not in c.root.classes: + print(cls, rename) + if rename: + clsobj = c.root.classes[rename] + self.nodes.rename_cls(c, clsobj, cls) + elif cls not in c.root.classes: c.root.classes[cls] = NClass(cls) + else: + raise cherrypy.HTTPError(500, "Nothing to do") def DELETE(self, cls): with self.nodes.db.transaction() as c: diff --git a/nodepupper/nodeops.py b/nodepupper/nodeops.py index 31ea0b7..9b047fd 100644 --- a/nodepupper/nodeops.py +++ b/nodepupper/nodeops.py @@ -66,3 +66,13 @@ class NodeOps(object): del c.root.nodes[node.fqdn] node.fqdn = newname c.root.nodes[node.fqdn] = node + + def rename_cls(self, c, cls, newname): + # check new name isnt taken + if newname in c.root.classes: + raise Exception(f"{newname} already exists") + + # move in root + del c.root.classes[cls.name] + cls.name = newname + c.root.classes[cls.name] = cls diff --git a/setup.py b/setup.py index fcf2f66..fd5f026 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup import os -__version__ = "0.0.2" +__version__ = "0.0.3" with open(os.path.join(os.path.dirname(__file__), "requirements.txt")) as f: __requirements__ = [line.strip() for line in f.readlines() if not line.startswith("-")]