svn keywords
[plcapi.git] / PLC / Methods / BindObjectToPeer.py
1 # $Id$
2 # $URL$
3
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Filter import Filter
7 from PLC.Auth import Auth
8 from PLC.Persons import Persons
9 from PLC.Sites import Sites
10 from PLC.Nodes import Nodes
11 from PLC.Slices import Slices
12 from PLC.Peers import Peers
13 from PLC.Faults import *
14  
15 class BindObjectToPeer(Method):
16     """
17     This method is a hopefully temporary hack to let the sfa correctly
18     attach the objects it creates to a remote peer object. This is
19     needed so that the sfa federation link can work in parallel with
20     RefreshPeer, as RefreshPeer depends on remote objects being
21     correctly marked. 
22
23     BindRemoteObjectToPeer is allowed to admins only.
24     """
25
26     roles = ['admin']
27
28     known_types = ['site','person','slice','node','key']
29     types_doc = ",".join(["'%s'"%type for type in known_types])
30
31     accepts = [
32         Auth(),
33         Parameter(str,"Object type, among "+types_doc),
34         Parameter(int,"object_id"),
35         Parameter(str,"peer shortname"),
36         Parameter(int,"remote object_id, set to 0 if unknown"),
37         ]
38
39     returns = Parameter (int, '1 if successful')
40
41     def locate_object (self, object_type, object_id):
42         # locate e.g. the Nodes symbol
43         class_obj = globals()[object_type.capitalize()+'s']
44         id_name=object_type+'_id'
45         # invoke e.g. Nodes ({'node_id':node_id})
46         objs=class_obj(self.api,{id_name:object_id})
47         if len(objs) != 1:
48             raise PLCInvalidArgument,"Cannot locate object, type=%s id=%d"%\
49                 (type,object_id)
50         return objs[0]
51
52
53     def call(self, auth, object_type, object_id, shortname,remote_object_id):
54
55         object_type = object_type.lower()
56         if object_type not in self.known_types:
57             raise PLCInvalidArgument, 'Unrecognized object type %s'%object_type
58
59         peers=Peers(self.api,{'shortname':shortname.upper()})
60         if len(peers) !=1:
61             raise PLCInvalidArgument, 'No such peer with shortname %s'%shortname
62
63         peer=peers[0]
64         object = self.locate_object (object_type, object_id)
65         adder_name = 'add_'+object_type
66         add_function = getattr(type(peer),adder_name)
67         add_function(peer,object,remote_object_id)
68
69         return 1