Revision 95b487bb lib/confd/querylib.py

b/lib/confd/querylib.py
139 139

  
140 140

  
141 141
class InstanceIpToNodePrimaryIpQuery(ConfdQuery):
142
  """A query for the location of an instance's ip.
142
  """A query for the location of one or more instance's ips.
143 143

  
144
  It returns the primary ip of the node hosting the instance having the
145
  requested ip address, or an error if no such address is known.
144
  Given a list of instance IPs, returns an ordered list with the same
145
  number of elements as the input. Each element of the list is a tuple
146
  containing the status (success or failure) and the content of the
147
  query (IP of the primary node if successful, error constant if not).
148

  
149
  If a string (instance's IP) is given instead of a list it will return
150
  a single tuple, as opposed to a 1-element list containing that tuple.
146 151

  
147 152
  """
148 153
  def Exec(self, query):
149 154
    """InstanceIpToNodePrimaryIpQuery main execution.
150 155

  
151 156
    """
152
    instance_ip = query
153
    instance = self.reader.GetInstanceByIp(instance_ip)
154
    if instance is None:
155
      return QUERY_UNKNOWN_ENTRY_ERROR
157
    if isinstance(query, list):
158
      instances_list = query
159
    else:
160
      instances_list = [query]
161
    pnodes_list = []
162

  
163
    for instance_ip in instances_list:
164
      instance = self.reader.GetInstanceByIp(instance_ip)
165
      if not instance:
166
        logging.debug("Invalid instance IP: %s" % instance)
167
        pnodes_list.append(QUERY_UNKNOWN_ENTRY_ERROR)
168
        continue
156 169

  
157
    pnode = self.reader.GetInstancePrimaryNode(instance)
158
    if pnode is None:
159
      # this shouldn't happen
160
      logging.error("Internal configuration inconsistent (instance-to-pnode)")
161
      return QUERY_INTERNAL_ERROR
170
      pnode = self.reader.GetInstancePrimaryNode(instance)
171
      if not pnode:
172
        logging.error("Instance '%s' doesn't have an associated primary"
173
                      " node" % instance)
174
        pnodes_list.append(QUERY_INTERNAL_ERROR)
175
        continue
162 176

  
163
    pnode_primary_ip = self.reader.GetNodePrimaryIp(pnode)
164
    if pnode_primary_ip is None:
165
      # this shouldn't happen
166
      logging.error("Internal configuration inconsistent (node-to-primary-ip)")
167
      return QUERY_INTERNAL_ERROR
177
      pnode_primary_ip = self.reader.GetNodePrimaryIp(pnode)
178
      if not pnode_primary_ip:
179
        logging.error("Primary node '%s' doesn't have an associated"
180
                      " primary IP" % pnode)
181
        pnodes_list.append(QUERY_INTERNAL_ERROR)
182
        continue
168 183

  
169
    return constants.CONFD_REPL_STATUS_OK, pnode_primary_ip
184
      pnodes_list.append((constants.CONFD_REPL_STATUS_OK, pnode_primary_ip))
185

  
186
    # If input was a string, return a tuple instead of a 1-element list
187
    if isinstance(query, basestring):
188
      return pnodes_list[0]
189

  
190
    return constants.CONFD_REPL_STATUS_OK, pnodes_list
170 191

  
171 192

  
172 193
class NodesPipsQuery(ConfdQuery):
......
225 246
    answer = self.reader.GetInstancesIps(link)
226 247

  
227 248
    return status, answer
228

  

Also available in: Unified diff