Revision 70a6a926

b/lib/cmdlib.py
10224 10224
                  " Domain Name.")
10225 10225

  
10226 10226

  
10227
class LUQueryGroups(NoHooksLU):
10228
  """Logical unit for querying node groups.
10229

  
10230
  """
10231
  # pylint: disable-msg=W0142
10232
  _OP_PARAMS = [
10233
    _POutputFields,
10234
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
10235
    ]
10236

  
10237
  REQ_BGL = False
10238

  
10239
  _FIELDS_DYNAMIC = utils.FieldSet()
10240

  
10241
  _SIMPLE_FIELDS = ["name", "uuid"]
10242

  
10243
  _FIELDS_STATIC = utils.FieldSet(
10244
      "node_cnt", "node_list", "pinst_cnt", "pinst_list", *_SIMPLE_FIELDS)
10245

  
10246
  def CheckArguments(self):
10247
    _CheckOutputFields(static=self._FIELDS_STATIC,
10248
                       dynamic=self._FIELDS_DYNAMIC,
10249
                       selected=self.op.output_fields)
10250

  
10251
  def ExpandNames(self):
10252
    self.needed_locks = {}
10253

  
10254
  def Exec(self, feedback_fn):
10255
    """Computes the list of groups and their attributes.
10256

  
10257
    """
10258
    all_groups = self.cfg.GetAllNodeGroupsInfo()
10259

  
10260
    if not self.op.names:
10261
      my_groups = utils.NiceSort(all_groups.keys())
10262
    else:
10263
      # Accept names to be either names or UUIDs.
10264
      all_uuid = frozenset(all_groups.keys())
10265
      name_to_uuid = dict((g.name, g.uuid) for g in all_groups.values())
10266
      my_groups = []
10267
      missing = []
10268

  
10269
      for name in self.op.names:
10270
        if name in all_uuid:
10271
          my_groups.append(name)
10272
        elif name in name_to_uuid:
10273
          my_groups.append(name_to_uuid[name])
10274
        else:
10275
          missing.append(name)
10276

  
10277
      if missing:
10278
        raise errors.OpPrereqError("Some groups do not exist: %s" % missing,
10279
                                   errors.ECODE_NOENT)
10280

  
10281
    do_nodes = bool(frozenset(["node_cnt", "node_list"]).
10282
                    intersection(self.op.output_fields))
10283

  
10284
    do_instances = bool(frozenset(["pinst_cnt", "pinst_list"]).
10285
                        intersection(self.op.output_fields))
10286

  
10287
    # We need to map group->[nodes], and group->[instances]. The former is
10288
    # directly attainable, but the latter we have to do through instance->node,
10289
    # hence we need to process nodes even if we only need instance information.
10290
    if do_nodes or do_instances:
10291
      all_nodes = self.cfg.GetAllNodesInfo()
10292
      group_to_nodes = dict((all_groups[name].uuid, []) for name in my_groups)
10293
      node_to_group = {}
10294

  
10295
      for node in all_nodes.values():
10296
        if node.group in group_to_nodes:
10297
          group_to_nodes[node.group].append(node.name)
10298
          node_to_group[node.name] = node.group
10299

  
10300
      if do_instances:
10301
        all_instances = self.cfg.GetAllInstancesInfo()
10302
        group_to_instances = dict((all_groups[name].uuid, [])
10303
                                  for name in my_groups)
10304
        for instance in all_instances.values():
10305
          node = instance.primary_node
10306
          if node in node_to_group:
10307
            group_to_instances[node_to_group[node]].append(instance.name)
10308

  
10309
    output = []
10310

  
10311
    for name in my_groups:
10312
      group = all_groups[name]
10313
      group_output = []
10314

  
10315
      for field in self.op.output_fields:
10316
        if field in self._SIMPLE_FIELDS:
10317
          val = getattr(group, field)
10318
        elif field == "node_list":
10319
          val = utils.NiceSort(group_to_nodes[group.uuid])
10320
        elif field == "node_cnt":
10321
          val = len(group_to_nodes[group.uuid])
10322
        elif field == "pinst_list":
10323
          val = utils.NiceSort(group_to_instances[group.uuid])
10324
        elif field == "pinst_cnt":
10325
          val = len(group_to_instances[group.uuid])
10326
        else:
10327
          raise errors.ParameterError(field)
10328
        group_output.append(val)
10329
      output.append(group_output)
10330

  
10331
    return output
10332

  
10333

  
10227 10334
class TagsLU(NoHooksLU): # pylint: disable-msg=W0223
10228 10335
  """Generic tags LU.
10229 10336

  
b/lib/mcpu.py
188 188
    opcodes.OpQueryInstanceData: cmdlib.LUQueryInstanceData,
189 189
    opcodes.OpSetInstanceParams: cmdlib.LUSetInstanceParams,
190 190
    opcodes.OpGrowDisk: cmdlib.LUGrowDisk,
191
    # node group lu
192
    opcodes.OpQueryGroups: cmdlib.LUQueryGroups,
191 193
    # os lu
192 194
    opcodes.OpDiagnoseOS: cmdlib.LUDiagnoseOS,
193 195
    # exports lu
b/lib/opcodes.py
700 700
    ]
701 701

  
702 702

  
703
# Node group opcodes
704

  
705
class OpQueryGroups(OpCode):
706
  """Compute the list of node groups."""
707
  OP_ID = "OP_GROUP_QUERY"
708
  __slots__ = ["output_fields", "names"]
709

  
710

  
703 711
# OS opcodes
704 712
class OpDiagnoseOS(OpCode):
705 713
  """Compute the list of guest operating systems."""

Also available in: Unified diff