From ae130c81dbb9b94d7f9fba2ec93f3e934e4e4604 Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Fri, 17 Jul 2009 14:53:37 +0200 Subject: [PATCH] SimpleConfigReader: add IP address lookup helpers Add the following functions to SimpleConfigReader: GetInstanceByIp, nic ip -> instance name GetNodePrimaryIp, node name -> node primary ip GetInstancePrimaryNode, instance name -> primary node For the first one we precalculate the _ip_to_instance, so we don't have to loop over all instances for each query. Signed-off-by: Guido Trotter <ultrotter@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- lib/ssconf.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/ssconf.py b/lib/ssconf.py index bdde8aef3..5f59c4418 100644 --- a/lib/ssconf.py +++ b/lib/ssconf.py @@ -95,6 +95,13 @@ class SimpleConfigReader(object): raise errors.ConfigurationError("Cannot load config file %s: %s" % (self._file_name, err)) + self._ip_to_instance = {} + for iname in self._config_data['instances']: + instance = self._config_data['instances'][iname] + for nic in instance['nics']: + if 'ip' in nic and nic['ip']: + self._ip_to_instance[nic['ip']] = iname + return True # Clients can request a reload of the config file, so we export our internal @@ -148,6 +155,37 @@ class SimpleConfigReader(object): offline = self._config_data["nodes"][node]["offline"] return master_candidate, drained, offline + def GetInstanceByIp(self, ip): + if ip not in self._ip_to_instance: + return None + return self._ip_to_instance[ip] + + def GetNodePrimaryIp(self, node): + """Get a node's primary ip + + @type node: string + @param node: node name + @rtype: string, or None + @return: node's primary ip, or None if no such node + + """ + if node not in self._config_data["nodes"]: + return None + return self._config_data["nodes"][node]["primary_ip"] + + def GetInstancePrimaryNode(self, instance): + """Get an instance's primary node + + @type instance: string + @param instance: instance name + @rtype: string, or None + @return: primary node, or None if no such instance + + """ + if instance not in self._config_data["instances"]: + return None + return self._config_data["instances"][instance]["primary_node"] + class SimpleStore(object): """Interface to static cluster data. -- GitLab