svn keywords
[plcapi.git] / PLC / Accessors / Factory.py
index 99c782e..02de440 100644 (file)
@@ -1,5 +1,6 @@
 # Thierry Parmentelat - INRIA
 # $Id$
+# $URL$
 
 from types import NoneType
 
@@ -12,9 +13,9 @@ from PLC.Faults import *
 from PLC.Nodes import Nodes, Node
 from PLC.NodeTags import NodeTags, NodeTag
 from PLC.Interfaces import Interfaces, Interface
-from PLC.InterfaceSettings import InterfaceSettings, InterfaceSetting
+from PLC.InterfaceTags import InterfaceTags, InterfaceTag
 from PLC.Slices import Slices, Slice
-from PLC.SliceAttributes import SliceAttributes, SliceAttribute
+from PLC.SliceTags import SliceTags, SliceTag
 
 # this is another story..
 #from PLC.Ilinks import Ilink
@@ -24,39 +25,49 @@ from PLC.TagTypes import TagTypes, TagType
 # known classes : { class -> secondary_key }
 taggable_classes = { Node : {'table_class' : Nodes, 
                              'joins_class' : NodeTags, 'join_class' : NodeTag,
-                             'value_key': 'tagvalue', 'secondary_key': 'hostname'},
+                             'secondary_key': 'hostname'},
                      Interface : {'table_class' : Interfaces, 
-                                  'joins_class': InterfaceSettings, 'join_class': InterfaceSetting,
-                                  'value_key' : 'value' }, 
+                                  'joins_class': InterfaceTags, 'join_class': InterfaceTag,
+                                  'secondary_key' : 'ip'},
                      Slice: {'table_class' : Slices, 
-                             'joins_class': SliceAttributes, 'join_class': SliceAttribute,
-                             'value_key' : 'value', 'secondary_key':'login_base'},
+                             'joins_class': SliceTags, 'join_class': SliceTag,
+                             'secondary_key':'name'},
 #                     Ilink : xxx
                      }
 
 # xxx probably defined someplace else
 all_roles = [ 'admin', 'pi', 'tech', 'user', 'node' ]
+tech_roles = [ 'admin', 'pi', 'tech' ]
 
+#
 # generates 2 method classes:
-# Get<classname><methodsuffix> (auth, id_or_name) -> tagvalue or None
-# Set<classname><methodsuffix> (auth, id_or_name, tagvalue) -> None
-# tagvalue is always a string, no cast nor typecheck for now
+# Get<classname><methodsuffix> (auth, id_or_name) -> value or None
+# Set<classname><methodsuffix> (auth, id_or_name, value) -> None
+# value is always a string, no cast nor typecheck for now
+#
+# The expose_in_api flag tells whether this tag may be handled 
+#   through the Add/Get/Update methods as a native field
 #
 # note: tag_min_role_id gets attached to the tagtype instance, 
 # while get_roles and set_roles get attached to the created methods
+# this might need a cleanup
 # 
-# returns a tuple (get_method, set_method)
-# See Accessors* for examples
 
-def get_set_factory (objclass, methodsuffix, 
-                     tagname, category, description, tag_min_role_id=10,
-                     get_roles=['admin'], set_roles=['admin']):
+def define_accessors (module, objclass, methodsuffix, tagname, 
+                      category, description, 
+                      get_roles=['admin'], set_roles=['admin'], 
+                      tag_min_role_id=10, expose_in_api = False):
     
     if objclass not in taggable_classes:
         try:
             raise PLCInvalidArgument,"PLC.Accessors.Factory: unknown class %s"%objclass.__name__
         except:
             raise PLCInvalidArgument,"PLC.Accessors.Factory: unknown class ??"
+
+    # side-effect on, say, Node.tags, if required
+    if expose_in_api:
+        getattr(objclass,'tags')[tagname]=Parameter(str,"accessor")
+
     classname=objclass.__name__
     get_name = "Get" + classname + methodsuffix
     set_name = "Set" + classname + methodsuffix
@@ -71,12 +82,8 @@ def get_set_factory (objclass, methodsuffix,
     # accepts 
     get_accepts = [ Auth () ]
     primary_key=objclass.primary_key
-    try:
-        secondary_key = taggable_classes[objclass]['secondary_key']
-        get_accepts += [ Mixed (objclass.fields[primary_key], objclass.fields[secondary_key]) ]
-    except:
-        secondary_key = None
-        get_accepts += [ objclass.fields[primary_key] ]
+    secondary_key = taggable_classes[objclass]['secondary_key']
+    get_accepts += [ Mixed (objclass.fields[primary_key], objclass.fields[secondary_key]) ]
     # for set, idem set of arguments + one additional arg, the new value
     set_accepts = get_accepts + [ Parameter (str,"New tag value") ]
 
@@ -98,7 +105,6 @@ def get_set_factory (objclass, methodsuffix,
     table_class = taggable_classes[objclass]['table_class']
     joins_class = taggable_classes[objclass]['joins_class']
     join_class = taggable_classes[objclass]['join_class']
-    value_key = taggable_classes[objclass]['value_key']
 
     # body of the get method
     def get_call (self, auth, id_or_name):
@@ -112,24 +118,24 @@ def get_set_factory (objclass, methodsuffix,
             filter[primary_key]=id_or_name
         else:
             filter[secondary_key]=id_or_name
-        joins = joins_class (self.api,filter,[value_key])
+        joins = joins_class (self.api,filter,['value'])
         if not joins:
             # xxx - we return None even if id_or_name is not valid 
             return None
         else:
-            return joins[0][value_key]
+            return joins[0]['value']
 
     # attach it
     setattr (get_class,"call",get_call)
 
     # body of the set method 
-    def set_call (self, auth, id_or_name, tagvalue):
+    def set_call (self, auth, id_or_name, value):
         # locate the object
         if isinstance (id_or_name, int):
             filter={primary_key:id_or_name}
         else:
             filter={secondary_key:id_or_name}
-        objs = table_class(self.api, filter,[primary_key])
+        objs = table_class(self.api, filter,[primary_key,secondary_key])
         if not objs:
             raise PLCInvalidArgument, "Cannot set tag on %s %r"%(objclass.__name__,id_or_name)
         primary_id = objs[0][primary_key]
@@ -146,26 +152,51 @@ def get_set_factory (objclass, methodsuffix,
                                'min_role_id': tag_min_role_id}
             tag_type = TagType (self.api, tag_type_fields)
             tag_type.sync()
-        # proceed
         tag_type_id = tag_type['tag_type_id']
+
+        # locate the join object (e.g. NodeTag, SliceTag or InterfaceTag)
         filter = {'tag_type_id':tag_type_id}
         if isinstance (id_or_name,int):
             filter[primary_key]=id_or_name
         else:
             filter[secondary_key]=id_or_name
         joins = joins_class (self.api,filter)
-        if not joins:
-            join = join_class (self.api)
-            join['tag_type_id']=tag_type_id
-            join[primary_key]=primary_id
-            join[value_key]=tagvalue
-            join.sync()
+        # setting to something non void
+        if value is not None:
+            if not joins:
+                join = join_class (self.api)
+                join['tag_type_id']=tag_type_id
+                join[primary_key]=primary_id
+                join['value']=value
+                join.sync()
+            else:
+                joins[0]['value']=value
+                joins[0].sync()
+        # providing an empty value means clean up
+        else:
+            if joins:
+                join=joins[0]
+                join.delete()
+        # log it
+        self.event_objects= { objclass.__name__ : [primary_id] }
+        self.message=objclass.__name__
+        if secondary_key in objs[0]:
+            self.message += " %s "%objs[0][secondary_key]
         else:
-            joins[0][value_key]=tagvalue
-            joins[0].sync()
+            self.message += " %d "%objs[0][primary_key]
+        self.message += "updated"
 
     # attach it
     setattr (set_class,"call",set_call)
 
-    return ( get_class, set_class )
+    # define in module
+    setattr(module,get_name,get_class)
+    setattr(module,set_name,set_class)
+    # add in <module>.methods
+    try:
+        methods=getattr(module,'methods')
+    except:
+        methods=[]
+    methods += [get_name,set_name]
+    setattr(module,'methods',methods)