class and cli renaming

This commit is contained in:
dave 2019-05-08 22:33:04 -07:00
parent 019f8a5e32
commit 915bcf1dca
4 changed files with 22 additions and 4 deletions

View File

@ -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()

View File

@ -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:

View File

@ -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

View File

@ -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("-")]