+ def add_site(self, site, peer_site_id, commit = True):
+ """
+ Associate a local site entry with this peer.
+ """
+
+ add = Row.add_object(Site, 'peer_site')
+ add(self, site,
+ {'peer_id': self['peer_id'],
+ 'site_id': site['site_id'],
+ 'peer_site_id': peer_site_id},
+ commit = commit)
+
+ def add_person(self, person, peer_person_id, commit = True):
+ """
+ Associate a local user entry with this peer.
+ """
+
+ add = Row.add_object(Person, 'peer_person')
+ add(self, person,
+ {'peer_id': self['peer_id'],
+ 'person_id': person['person_id'],
+ 'peer_person_id': peer_person_id},
+ commit = commit)
+
+ def add_key(self, key, peer_key_id, commit = True):
+ """
+ Associate a local key entry with this peer.
+ """
+
+ add = Row.add_object(Key, 'peer_key')
+ add(self, key,
+ {'peer_id': self['peer_id'],
+ 'key_id': key['key_id'],
+ 'peer_key_id': peer_key_id},
+ commit = commit)
+
+ def add_node(self, node, peer_node_id, commit = True):
+ """
+ Associate a local node entry with this peer.
+ """
+
+ add = Row.add_object(Node, 'peer_node')
+ add(self, node,
+ {'peer_id': self['peer_id'],
+ 'node_id': node['node_id'],
+ 'peer_node_id': peer_node_id},
+ commit = commit)
+
+ def add_slice(self, slice, peer_slice_id, commit = True):
+ """
+ Associate a local slice entry with this peer.
+ """
+
+ add = Row.add_object(Slice, 'peer_slice')
+ add(self, slice,
+ {'peer_id': self['peer_id'],
+ 'slice_id': slice['slice_id'],
+ 'peer_slice_id': peer_slice_id},
+ commit = commit)
+
+ def connect(self, **kwds):
+ """
+ Connect to this peer via XML-RPC.
+ """
+
+ import xmlrpclib
+ from PLC.PyCurl import PyCurlTransport
+ self.server = xmlrpclib.ServerProxy(self['peer_url'],
+ PyCurlTransport(self['peer_url'], self['cacert']),
+ allow_none = 1, **kwds)
+
+ def add_auth(self, function, methodname, **kwds):
+ """
+ Sign the specified XML-RPC call and add an auth struct as the
+ first argument of the call.
+ """
+
+ def wrapper(*args, **kwds):
+ from PLC.GPG import gpg_sign
+ signature = gpg_sign(args,
+ self.api.config.PLC_ROOT_GPG_KEY,
+ self.api.config.PLC_ROOT_GPG_KEY_PUB,
+ methodname)
+
+ auth = {'AuthMethod': "gpg",
+ 'name': self.api.config.PLC_NAME,
+ 'signature': signature}
+
+ # Automagically add auth struct to every call
+ args = (auth,) + args
+
+ return function(*args)
+
+ return wrapper
+
+ def __getattr__(self, attr):
+ """
+ Returns a callable API function if attr is the name of a
+ PLCAPI function; otherwise, returns the specified attribute.
+ """
+
+ try:
+ # Figure out if the specified attribute is the name of a
+ # PLCAPI function. If so and the function requires an
+ # authentication structure as its first argument, return a
+ # callable that automagically adds an auth struct to the
+ # call.
+ methodname = attr
+ api_function = self.api.callable(methodname)
+ if api_function.accepts and \
+ (isinstance(api_function.accepts[0], PLC.Auth.Auth) or \
+ (isinstance(api_function.accepts[0], Mixed) and \
+ filter(lambda param: isinstance(param, Auth), api_function.accepts[0]))):
+ function = getattr(self.server, methodname)
+ return self.add_auth(function, methodname)
+ except Exception, err:
+ pass
+
+ if hasattr(self, attr):
+ return getattr(self, attr)
+ else:
+ raise AttributeError, "type object 'Peer' has no attribute '%s'" % attr
+