From fab2a2b6bf2cd817471d944149eb7f9008df6d33 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 19 Aug 2008 14:29:01 +0000 Subject: [PATCH] creates tag types on a need-by-need basis --- PLC/Shortcuts/Factory.py | 31 ++++++++++++++++++- PLC/Shortcuts/Shortcuts_site.py.in | 24 ++++++++++++++ .../{Shortcuts.py => Shortcuts_standard.py} | 11 +------ PLC/Shortcuts/__init__.py | 3 +- 4 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 PLC/Shortcuts/Shortcuts_site.py.in rename PLC/Shortcuts/{Shortcuts.py => Shortcuts_standard.py} (51%) diff --git a/PLC/Shortcuts/Factory.py b/PLC/Shortcuts/Factory.py index c31b0a6a..c558ca4f 100644 --- a/PLC/Shortcuts/Factory.py +++ b/PLC/Shortcuts/Factory.py @@ -14,8 +14,16 @@ from PLC.Interfaces import Interface from PLC.Slices import Slice from PLC.Ilinks import Ilink +from PLC.TagTypes import TagTypes, TagType + # known classes : { class -> secondary_key } -taggable_classes = { Node : 'hostname', Interface : None, Slice: 'login_base', Ilink : None} +taggable_classes = { Node : 'hostname', + Interface : None, + Slice: 'login_base', + Ilink : None} + +# xxx probably defined someplace else +all_roles = [ 'admin', 'pi', 'tech', 'user', 'node' ] # generates 2 method classes: # Get (auth, id_or_name) -> tagvalue or None @@ -78,6 +86,8 @@ def get_set_factory (objclass, methodsuffix, def get_call (self, auth, id_or_name): print 'Automagical Shortcut get method',classname,get_name,tagname,primary_key,secondary_key print 'Warning: PLC/Shortcuts/Factory is an ongoing work' + tag_type_id = locate_or_create_tag_type_id (self.api, tagname, + category, description, tag_min_role_id) return 'foobar' setattr (get_class,"call",get_call) @@ -90,3 +100,22 @@ def get_set_factory (objclass, methodsuffix, return ( get_class, set_class ) +### might need to use a cache +def locate_or_create_tag_type_id (api, tagname, category, description, min_role_id): + # search tag + tag_types = TagTypes (api, {'tagname':tagname}) + # not found: create it + if tag_types: + print 'FOUND preexisting' + tag_type_id = tag_types[0]['tag_type_id'] + else: + print 'not FOUND : creating' + tag_type_fields = {'tagname':tagname, + 'category' : category, + 'description' : description, + 'min_role_id': min_role_id} + tag_type = TagType (api, tag_type_fields) + tag_type.sync() + tag_type_id = tag_type['tag_type_id'] + + return tag_type_id diff --git a/PLC/Shortcuts/Shortcuts_site.py.in b/PLC/Shortcuts/Shortcuts_site.py.in new file mode 100644 index 00000000..e89a2ad4 --- /dev/null +++ b/PLC/Shortcuts/Shortcuts_site.py.in @@ -0,0 +1,24 @@ +# Thierry Parmentelat - INRIA +# $Id$ +# +# +# Shortcuts_site.py is the place where you can define your own tag accessors +# +# methods denotes the set of methods (names) that get inserted into the API +# + +methods=[] + +from PLC.Nodes import Node +from PLC.Interfaces import Interface +from PLC.Slices import Slice +from PLC.Ilinks import Ilink + +from PLC.Shortcuts.Factory import all_roles, get_set_factory + +#### example : attach vlan ids on interfaces +###(GetInterfaceVlan, SetInterfaceVlan) = \ +### get_set_factory ( Interface, "Vlan", "vlan","interface/general", "tag for setting VLAN id", +### get_roles=all_roles, +### set_roles=['admin', 'pi', 'tech'] ) +###methods += [ 'GetInterfaceVlan', 'SetInterfaceVlan' ] diff --git a/PLC/Shortcuts/Shortcuts.py b/PLC/Shortcuts/Shortcuts_standard.py similarity index 51% rename from PLC/Shortcuts/Shortcuts.py rename to PLC/Shortcuts/Shortcuts_standard.py index 74abd079..51801896 100644 --- a/PLC/Shortcuts/Shortcuts.py +++ b/PLC/Shortcuts/Shortcuts_standard.py @@ -6,10 +6,7 @@ from PLC.Interfaces import Interface from PLC.Slices import Slice from PLC.Ilinks import Ilink -from PLC.Shortcuts.Factory import get_set_factory - -# xxx probably defined someplace else -all_roles = [ 'admin', 'pi', 'tech', 'user', 'node' ] +from PLC.Shortcuts.Factory import all_roles, get_set_factory methods=[] @@ -21,9 +18,3 @@ methods=[] set_roles=['admin', 'pi', 'tech'] ) methods += [ 'GetNodeArch', 'SetNodeArch' ] -# example : vlan ids on interfaces -(GetInterfaceVlan, SetInterfaceVlan) = \ - get_set_factory ( Interface, "Vlan", "vlan","interface/general", "tag for setting VLAN id", - get_roles=all_roles, - set_roles=['admin', 'pi', 'tech'] ) -methods += [ 'GetInterfaceVlan', 'SetInterfaceVlan' ] diff --git a/PLC/Shortcuts/__init__.py b/PLC/Shortcuts/__init__.py index 63aa0595..5c4d0ced 100644 --- a/PLC/Shortcuts/__init__.py +++ b/PLC/Shortcuts/__init__.py @@ -1,4 +1,5 @@ # each module to define in "methods" the set of methods that it defines __all__ = """ -Shortcuts +Shortcuts_standard +Shortcuts_site """.split() -- 2.47.0