X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FPeers.py;h=c272a2acc15358f8e39419d9f3b89c13ec9f588b;hb=79d959a7decd4af5b0b15e660bb3bd5bb5edfcaf;hp=e78f568dcd2236cfbdb2cc72c1e97594626969ea;hpb=b4b902d85a7966403dc09cc1c3f0da8a07b7a1e9;p=plcapi.git diff --git a/PLC/Peers.py b/PLC/Peers.py index e78f568..c272a2a 100644 --- a/PLC/Peers.py +++ b/PLC/Peers.py @@ -1,30 +1,31 @@ -# $Id$ -# $URL$ # # Thierry Parmentelat - INRIA -# +# import re from types import StringTypes +import traceback from urlparse import urlparse +import PLC.Auth +from PLC.Logger import logger from PLC.Faults import * +from PLC.Namespace import hostname_to_hrn from PLC.Parameter import Parameter, Mixed from PLC.Filter import Filter from PLC.Table import Row, Table -import PLC.Auth -from PLC.Shell import * from PLC.Sites import Site, Sites from PLC.Persons import Person, Persons from PLC.Keys import Key, Keys from PLC.Nodes import Node, Nodes from PLC.TagTypes import TagType, TagTypes +from PLC.NodeTags import NodeTag, NodeTags from PLC.SliceTags import SliceTag, SliceTags from PLC.Slices import Slice, Slices class Peer(Row): """ - Stores the list of peering PLCs in the peers table. + Stores the list of peering PLCs in the peers table. See the Row class for more details """ @@ -45,7 +46,7 @@ class Peer(Row): 'key_ids': Parameter([int], "List of keys for which this peer is authoritative"), 'node_ids': Parameter([int], "List of nodes for which this peer is authoritative"), 'slice_ids': Parameter([int], "List of slices for which this peer is authoritative"), - } + } def validate_peername(self, peername): if not len(peername): @@ -68,8 +69,8 @@ class Peer(Row): raise PLCInvalidArgument, "Peer URL scheme must be https" if path[-1] != '/': raise PLCInvalidArgument, "Peer URL should end with /" - - return url + + return url def delete(self, commit = True): """ @@ -108,7 +109,7 @@ class Peer(Row): """ Unassociate a site with this peer. """ - + remove = Row.remove_object(Site, 'peer_site') remove(self, site, commit) @@ -123,12 +124,12 @@ class Peer(Row): 'person_id': person['person_id'], 'peer_person_id': peer_person_id}, commit = commit) - + def remove_person(self, person, commit = True): """ Unassociate a site with this peer. """ - + remove = Row.remove_object(Person, 'peer_person') remove(self, person, commit) @@ -148,7 +149,7 @@ class Peer(Row): """ Unassociate a key with this peer. """ - + remove = Row.remove_object(Key, 'peer_key') remove(self, key, commit) @@ -164,22 +165,33 @@ class Peer(Row): 'peer_node_id': peer_node_id}, commit = commit) - # calling UpdateNode with the node's hostname - # will force the 'hrn' tag to be updated with the - # correct root auth - shell = Shell() - UpdateNode = self.api.callable('UpdateNode') - UpdateNode(shell.auth, node['node_id'], {'hostname': node['hostname']}) + sites = Sites(self.api, node['site_id'], ['login_base']) + site = sites[0] + login_base = site['login_base'] + try: + # attempt to manually update the 'hrn' tag with the remote prefix + hrn_root = self['hrn_root'] + hrn = hostname_to_hrn(hrn_root, login_base, node['hostname']) + tags = {'hrn': hrn} + Node(self.api, node).update_tags(tags) + except: + logger.exception("Could not find out hrn on hostname=%s"%node['hostname']) def remove_node(self, node, commit = True): """ Unassociate a node with this peer. """ - + remove = Row.remove_object(Node, 'peer_node') remove(self, node, commit) - UpdateNode = self.api.callable('UpdateNode') - UpdateNode(shell.auth, node['node_id'], {'hostname': node['hostname']}) + # attempt to manually update the 'hrn' tag now that the node is local + root_auth = self.api.config.PLC_HRN_ROOT + sites = Sites(self.api, node['site_id'], ['login_base']) + site = sites[0] + login_base = site['login_base'] + hrn = hostname_to_hrn(root_auth, login_base, node['hostname']) + tags = {'hrn': hrn} + Node(self.api, node).update_tags(tags) def add_slice(self, slice, peer_slice_id, commit = True): """ @@ -265,14 +277,14 @@ class Peer(Row): raise AttributeError, "type object 'Peer' has no attribute '%s'" % attr class Peers (Table): - """ + """ Maps to the peers table in the database """ - + def __init__ (self, api, peer_filter = None, columns = None): Table.__init__(self, api, Peer, columns) - sql = "SELECT %s FROM view_peers WHERE deleted IS False" % \ + sql = "SELECT %s FROM view_peers WHERE deleted IS False" % \ ", ".join(self.columns) if peer_filter is not None: @@ -285,5 +297,13 @@ class Peers (Table): elif isinstance(peer_filter, dict): peer_filter = Filter(Peer.fields, peer_filter) sql += " AND (%s) %s" % peer_filter.sql(api, "AND") + elif isinstance(peer_filter, (int, long)): + peer_filter = Filter(Peer.fields, {'peer_id': peer_filter}) + sql += " AND (%s) %s" % peer_filter.sql(api, "AND") + elif isinstance(peer_filter, StringTypes): + peer_filter = Filter(Peer.fields, {'peername': peer_filter}) + sql += " AND (%s) %s" % peer_filter.sql(api, "AND") + else: + raise PLCInvalidArgument, "Wrong peer filter %r"%peer_filter - self.selectall(sql) + self.selectall(sql)