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