Revision 31bf511f lib/cmdlib.py

b/lib/cmdlib.py
416 416
def _CheckOutputFields(static, dynamic, selected):
417 417
  """Checks whether all selected fields are valid.
418 418

  
419
  Args:
420
    static: Static fields
421
    dynamic: Dynamic fields
419
  @type static: L{_FieldSet}
420
  @param static: static fields set
421
  @type dynamic: L{_FieldSet}
422
  @param dynamic: dynamic fields set
422 423

  
423 424
  """
424
  static_fields = frozenset(static)
425
  dynamic_fields = frozenset(dynamic)
426

  
427
  all_fields = static_fields | dynamic_fields
425
  f = _FieldSet()
426
  f.Extend(static)
427
  f.Extend(dynamic)
428 428

  
429
  if not all_fields.issuperset(selected):
429
  delta = f.NonMatching(selected)
430
  if delta:
430 431
    raise errors.OpPrereqError("Unknown output fields selected: %s"
431
                               % ",".join(frozenset(selected).
432
                                          difference(all_fields)))
432
                               % ",".join(delta))
433 433

  
434 434

  
435 435
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
......
1362 1362
  """
1363 1363
  _OP_REQP = ["output_fields", "names"]
1364 1364
  REQ_BGL = False
1365
  _FIELDS_STATIC = _FieldSet()
1366
  _FIELDS_DYNAMIC = _FieldSet("name", "valid", "node_status")
1365 1367

  
1366 1368
  def ExpandNames(self):
1367 1369
    if self.op.names:
1368 1370
      raise errors.OpPrereqError("Selective OS query not supported")
1369 1371

  
1370
    self.dynamic_fields = frozenset(["name", "valid", "node_status"])
1371
    _CheckOutputFields(static=[],
1372
                       dynamic=self.dynamic_fields,
1372
    _CheckOutputFields(static=self._FIELDS_STATIC,
1373
                       dynamic=self._FIELDS_DYNAMIC,
1373 1374
                       selected=self.op.output_fields)
1374 1375

  
1375 1376
    # Lock all nodes, in shared mode
......
1517 1518
  """
1518 1519
  _OP_REQP = ["output_fields", "names"]
1519 1520
  REQ_BGL = False
1521
  _FIELDS_DYNAMIC = _FieldSet(
1522
    "dtotal", "dfree",
1523
    "mtotal", "mnode", "mfree",
1524
    "bootid",
1525
    "ctotal",
1526
    )
1527

  
1528
  _FIELDS_STATIC = _FieldSet(
1529
    "name", "pinst_cnt", "sinst_cnt",
1530
    "pinst_list", "sinst_list",
1531
    "pip", "sip", "tags",
1532
    "serial_no",
1533
    )
1520 1534

  
1521 1535
  def ExpandNames(self):
1522
    self.dynamic_fields = frozenset([
1523
      "dtotal", "dfree",
1524
      "mtotal", "mnode", "mfree",
1525
      "bootid",
1526
      "ctotal",
1527
      ])
1528

  
1529
    self.static_fields = frozenset([
1530
      "name", "pinst_cnt", "sinst_cnt",
1531
      "pinst_list", "sinst_list",
1532
      "pip", "sip", "tags",
1533
      "serial_no",
1534
      ])
1535

  
1536
    _CheckOutputFields(static=self.static_fields,
1537
                       dynamic=self.dynamic_fields,
1536
    _CheckOutputFields(static=self._FIELDS_STATIC,
1537
                       dynamic=self._FIELDS_DYNAMIC,
1538 1538
                       selected=self.op.output_fields)
1539 1539

  
1540 1540
    self.needed_locks = {}
......
1545 1545
    else:
1546 1546
      self.wanted = locking.ALL_SET
1547 1547

  
1548
    self.do_locking = not self.static_fields.issuperset(self.op.output_fields)
1548
    self.do_locking = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
1549 1549
    if self.do_locking:
1550 1550
      # if we don't request only static fields, we need to lock the nodes
1551 1551
      self.needed_locks[locking.LEVEL_NODE] = self.wanted
......
1580 1580

  
1581 1581
    # begin data gathering
1582 1582

  
1583
    if self.dynamic_fields.intersection(self.op.output_fields):
1583
    if self.do_locking:
1584 1584
      live_data = {}
1585 1585
      node_data = self.rpc.call_node_info(nodenames, self.cfg.GetVGName(),
1586 1586
                                          self.cfg.GetHypervisorType())
......
1641 1641
          val = list(node.GetTags())
1642 1642
        elif field == "serial_no":
1643 1643
          val = node.serial_no
1644
        elif field in self.dynamic_fields:
1644
        elif self._FIELDS_DYNAMIC.Matches(field):
1645 1645
          val = live_data[node.name].get(field, None)
1646 1646
        else:
1647 1647
          raise errors.ParameterError(field)
......
1657 1657
  """
1658 1658
  _OP_REQP = ["nodes", "output_fields"]
1659 1659
  REQ_BGL = False
1660
  _FIELDS_DYNAMIC = _FieldSet("phys", "vg", "name", "size", "instance")
1661
  _FIELDS_STATIC = _FieldSet("node")
1660 1662

  
1661 1663
  def ExpandNames(self):
1662
    _CheckOutputFields(static=["node"],
1663
                       dynamic=["phys", "vg", "name", "size", "instance"],
1664
    _CheckOutputFields(static=self._FIELDS_STATIC,
1665
                       dynamic=self._FIELDS_DYNAMIC,
1664 1666
                       selected=self.op.output_fields)
1665 1667

  
1666 1668
    self.needed_locks = {}
......
1977 1979
  """
1978 1980
  _OP_REQP = []
1979 1981
  REQ_BGL = False
1982
  _FIELDS_DYNAMIC = _FieldSet()
1983
  _FIELDS_STATIC = _FieldSet("cluster_name", "master_node", "drain_flag")
1980 1984

  
1981 1985
  def ExpandNames(self):
1982 1986
    self.needed_locks = {}
1983 1987

  
1984
    static_fields = ["cluster_name", "master_node", "drain_flag"]
1985
    _CheckOutputFields(static=static_fields,
1986
                       dynamic=[],
1988
    _CheckOutputFields(static=self._FIELDS_STATIC,
1989
                       dynamic=self._FIELDS_DYNAMIC,
1987 1990
                       selected=self.op.output_fields)
1988 1991

  
1989 1992
  def CheckPrereq(self):
......
2711 2714
  """
2712 2715
  _OP_REQP = ["output_fields", "names"]
2713 2716
  REQ_BGL = False
2717
  _FIELDS_STATIC = _FieldSet(*["name", "os", "pnode", "snodes",
2718
                               "admin_state", "admin_ram",
2719
                               "disk_template", "ip", "mac", "bridge",
2720
                               "sda_size", "sdb_size", "vcpus", "tags",
2721
                               "network_port", "beparams",
2722
                               "serial_no", "hypervisor", "hvparams",] +
2723
                             ["hv/%s" % name
2724
                              for name in constants.HVS_PARAMETERS] +
2725
                             ["be/%s" % name
2726
                              for name in constants.BES_PARAMETERS])
2727
  _FIELDS_DYNAMIC = _FieldSet("oper_state", "oper_ram", "status")
2728

  
2714 2729

  
2715 2730
  def ExpandNames(self):
2716
    self.dynamic_fields = frozenset(["oper_state", "oper_ram", "status"])
2717
    hvp = ["hv/%s" % name for name in constants.HVS_PARAMETERS]
2718
    bep = ["be/%s" % name for name in constants.BES_PARAMETERS]
2719
    self.static_fields = frozenset([
2720
      "name", "os", "pnode", "snodes",
2721
      "admin_state", "admin_ram",
2722
      "disk_template", "ip", "mac", "bridge",
2723
      "sda_size", "sdb_size", "vcpus", "tags",
2724
      "network_port", "beparams",
2725
      "serial_no", "hypervisor", "hvparams",
2726
      ] + hvp + bep)
2727

  
2728
    _CheckOutputFields(static=self.static_fields,
2729
                       dynamic=self.dynamic_fields,
2731
    _CheckOutputFields(static=self._FIELDS_STATIC,
2732
                       dynamic=self._FIELDS_DYNAMIC,
2730 2733
                       selected=self.op.output_fields)
2731 2734

  
2732 2735
    self.needed_locks = {}
......
2738 2741
    else:
2739 2742
      self.wanted = locking.ALL_SET
2740 2743

  
2741
    self.do_locking = not self.static_fields.issuperset(self.op.output_fields)
2744
    self.do_locking = self._FIELDS_STATIC.NonMatching(self.op.output_fields)
2742 2745
    if self.do_locking:
2743 2746
      self.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
2744 2747
      self.needed_locks[locking.LEVEL_NODE] = []
......
2780 2783
    hv_list = list(set([inst.hypervisor for inst in instance_list]))
2781 2784

  
2782 2785
    bad_nodes = []
2783
    if self.dynamic_fields.intersection(self.op.output_fields):
2786
    if self.do_locking:
2784 2787
      live_data = {}
2785 2788
      node_data = self.rpc.call_all_instances_info(nodes, hv_list)
2786 2789
      for name in nodes:

Also available in: Unified diff