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