Revision 19351457

b/lib/confd/querylib.py
33 33
                             constants.CONFD_ERROR_UNKNOWN_ENTRY)
34 34
QUERY_INTERNAL_ERROR = (constants.CONFD_REPL_STATUS_ERROR,
35 35
                        constants.CONFD_ERROR_INTERNAL)
36
QUERY_ARGUMENT_ERROR = (constants.CONFD_REPL_STATUS_ERROR,
37
                        constants.CONFD_ERROR_ARGUMENT)
36 38

  
37 39

  
38 40
class ConfdQuery(object):
......
141 143
class InstanceIpToNodePrimaryIpQuery(ConfdQuery):
142 144
  """A query for the location of one or more instance's ips.
143 145

  
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
  @type query: string or dict
147
  @param query: instance ip or dict containing:
148
                constants.CONFD_REQQ_LINK: nic link (optional)
149
                constants.CONFD_REQQ_IPLIST: list of ips
150
                constants.CONFD_REQQ_IP: single ip
151
                (one IP type request is mandatory)
152
  @rtype: (integer, ...)
153
  @return: ((status, answer) or (success, [(status, answer)...])
151 154

  
152 155
  """
153 156
  def Exec(self, query):
154 157
    """InstanceIpToNodePrimaryIpQuery main execution.
155 158

  
156 159
    """
157
    if isinstance(query, list):
158
      instances_list = query
159
    else:
160
    if isinstance(query, dict):
161
      if constants.CONFD_REQQ_IP in query:
162
        instances_list = [query[constants.CONFD_REQQ_IP]]
163
        mode = constants.CONFD_REQQ_IP
164
      elif constants.CONFD_REQQ_IPLIST in query:
165
        instances_list = query[constants.CONFD_REQQ_IPLIST]
166
        mode = constants.CONFD_REQQ_IPLIST
167
      else:
168
        status = constants.CONFD_REPL_STATUS_ERROR
169
        logging.debug("missing IP or IPLIST in query dict")
170
        return QUERY_ARGUMENT_ERROR
171

  
172
      if constants.CONFD_REQQ_LINK in query:
173
        network_link = query[constants.CONFD_REQQ_LINK]
174
      else:
175
        network_link = None # default will be used
176
    elif isinstance(query, basestring):
177
      # 2.1 beta1 and beta2 mode, to be deprecated for 2.2
160 178
      instances_list = [query]
179
      network_link = None
180
      mode = constants.CONFD_REQQ_IP
181
    else:
182
      logging.debug("Invalid query argument type for: %s" % query)
183
      return QUERY_ARGUMENT_ERROR
184

  
161 185
    pnodes_list = []
162 186

  
163 187
    for instance_ip in instances_list:
164
      instance = self.reader.GetInstanceByLinkIp(instance_ip, None)
188
      if not isinstance(instance_ip, basestring):
189
        logging.debug("Invalid IP type for: %s" % instance_ip)
190
        return QUERY_ARGUMENT_ERROR
191

  
192
      instance = self.reader.GetInstanceByLinkIp(instance_ip, network_link)
165 193
      if not instance:
166
        logging.debug("Invalid instance IP: %s" % instance)
194
        logging.debug("Unknown instance IP: %s" % instance_ip)
167 195
        pnodes_list.append(QUERY_UNKNOWN_ENTRY_ERROR)
168 196
        continue
169 197

  
......
183 211

  
184 212
      pnodes_list.append((constants.CONFD_REPL_STATUS_OK, pnode_primary_ip))
185 213

  
186
    # If input was a string, return a tuple instead of a 1-element list
187
    if isinstance(query, basestring):
214
    # If a single ip was requested, return a single answer, otherwise the whole
215
    # list, with a success status (since each entry has its own success/failure)
216
    if mode == constants.CONFD_REQQ_IP:
188 217
      return pnodes_list[0]
189 218

  
190 219
    return constants.CONFD_REPL_STATUS_OK, pnodes_list
b/lib/constants.py
664 664
CONFD_REQ_MC_PIP_LIST = 5
665 665
CONFD_REQ_INSTANCES_IPS_LIST = 6
666 666

  
667
# Confd request query fields. These are used to narrow down queries.
668
# These must be strings rather than integers, because json-encoding
669
# converts them to strings anyway, as they're used as dict-keys.
670
CONFD_REQQ_LINK = "0"
671
CONFD_REQQ_IP = "1"
672
CONFD_REQQ_IPLIST = "2"
673

  
667 674
CONFD_REQS = frozenset([
668 675
  CONFD_REQ_PING,
669 676
  CONFD_REQ_NODE_ROLE_BYNAME,
......
694 701
# A few common errors for confd
695 702
CONFD_ERROR_UNKNOWN_ENTRY = 1
696 703
CONFD_ERROR_INTERNAL = 2
704
CONFD_ERROR_ARGUMENT = 3
697 705

  
698 706
# Each request is "salted" by the current timestamp.
699 707
# This constants decides how many seconds of skew to accept.

Also available in: Unified diff