creates tag types on a need-by-need basis
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 19 Aug 2008 14:29:01 +0000 (14:29 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Tue, 19 Aug 2008 14:29:01 +0000 (14:29 +0000)
PLC/Shortcuts/Factory.py
PLC/Shortcuts/Shortcuts_site.py.in [new file with mode: 0644]
PLC/Shortcuts/Shortcuts_standard.py [moved from PLC/Shortcuts/Shortcuts.py with 51% similarity]
PLC/Shortcuts/__init__.py

index c31b0a6..c558ca4 100644 (file)
@@ -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<classname><methodsuffix> (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 (file)
index 0000000..e89a2ad
--- /dev/null
@@ -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' ]
similarity index 51%
rename from PLC/Shortcuts/Shortcuts.py
rename to PLC/Shortcuts/Shortcuts_standard.py
index 74abd07..5180189 100644 (file)
@@ -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' ]
index 63aa059..5c4d0ce 100644 (file)
@@ -1,4 +1,5 @@
 # each module to define in "methods" the set of methods that it defines
 __all__ = """
-Shortcuts
+Shortcuts_standard
+Shortcuts_site
 """.split()