Revision 82599b3e

b/lib/query.py
1 1
#
2 2
#
3 3

  
4
# Copyright (C) 2010 Google Inc.
4
# Copyright (C) 2010, 2011 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
61 61
from ganeti import objects
62 62
from ganeti import ht
63 63

  
64
from ganeti.constants import (QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER,
65
                              QFT_UNIT, QFT_TIMESTAMP, QFT_OTHER,
66
                              QRFS_NORMAL, QRFS_UNKNOWN, QRFS_NODATA,
67
                              QRFS_UNAVAIL, QRFS_OFFLINE)
68

  
64 69

  
65 70
# Constants for requesting data from the caller/data provider. Each property
66 71
# collected/computed separately by the data provider should have its own to
......
90 95

  
91 96
#: Verification function for each field type
92 97
_VERIFY_FN = {
93
  constants.QFT_UNKNOWN: ht.TNone,
94
  constants.QFT_TEXT: ht.TString,
95
  constants.QFT_BOOL: ht.TBool,
96
  constants.QFT_NUMBER: ht.TInt,
97
  constants.QFT_UNIT: ht.TInt,
98
  constants.QFT_TIMESTAMP: ht.TOr(ht.TInt, ht.TFloat),
99
  constants.QFT_OTHER: lambda _: True,
98
  QFT_UNKNOWN: ht.TNone,
99
  QFT_TEXT: ht.TString,
100
  QFT_BOOL: ht.TBool,
101
  QFT_NUMBER: ht.TInt,
102
  QFT_UNIT: ht.TInt,
103
  QFT_TIMESTAMP: ht.TOr(ht.TInt, ht.TFloat),
104
  QFT_OTHER: lambda _: True,
100 105
  }
101 106

  
102 107

  
......
104 109
  """Gets the contents of an unknown field.
105 110

  
106 111
  """
107
  return (constants.QRFS_UNKNOWN, None)
112
  return (QRFS_UNKNOWN, None)
108 113

  
109 114

  
110 115
def _GetQueryFields(fielddefs, selected):
......
124 129
    try:
125 130
      fdef = fielddefs[name]
126 131
    except KeyError:
127
      fdef = (_MakeField(name, name, constants.QFT_UNKNOWN),
128
              None, _GetUnknownField)
132
      fdef = (_MakeField(name, name, QFT_UNKNOWN), None, _GetUnknownField)
129 133

  
130 134
    assert len(fdef) == 3
131 135

  
......
211 215

  
212 216
    """
213 217
    unknown = set(fdef.name
214
                  for (fdef, _, _) in self._fields
215
                  if fdef.kind == constants.QFT_UNKNOWN)
218
                  for (fdef, _, _) in self._fields if fdef.kind == QFT_UNKNOWN)
216 219
    if unknown:
217 220
      raise errors.OpPrereqError("Unknown output fields selected: %s" %
218 221
                                 (utils.CommaJoin(unknown), ),
......
232 235

  
233 236
  """
234 237
  return (len(row) == len(fields) and
235
          compat.all((status == constants.QRFS_NORMAL and
236
                      _VERIFY_FN[fdef.kind](value)) or
238
          compat.all((status == QRFS_NORMAL and _VERIFY_FN[fdef.kind](value)) or
237 239
                     # Value for an abnormal status must be None
238
                     (status != constants.QRFS_NORMAL and value is None)
240
                     (status != QRFS_NORMAL and value is None)
239 241
                     for ((status, value), (fdef, _, _)) in zip(row, fields)))
240 242

  
241 243

  
......
348 350

  
349 351
  """
350 352
  getter = operator.attrgetter(attr)
351
  return lambda _, item: (constants.QRFS_NORMAL, getter(item))
353
  return lambda _, item: (QRFS_NORMAL, getter(item))
352 354

  
353 355

  
354 356
def _GetItemTimestamp(getter):
......
365 367
    timestamp = getter(item)
366 368
    if timestamp is None:
367 369
      # Old configs might not have all timestamps
368
      return (constants.QRFS_UNAVAIL, None)
370
      return (QRFS_UNAVAIL, None)
369 371
    else:
370
      return (constants.QRFS_NORMAL, timestamp)
372
      return (QRFS_NORMAL, timestamp)
371 373

  
372 374
  return fn
373 375

  
......
379 381

  
380 382
  """
381 383
  return [
382
    (_MakeField("ctime", "CTime", constants.QFT_TIMESTAMP), datatype,
384
    (_MakeField("ctime", "CTime", QFT_TIMESTAMP), datatype,
383 385
     _GetItemTimestamp(operator.attrgetter("ctime"))),
384
    (_MakeField("mtime", "MTime", constants.QFT_TIMESTAMP), datatype,
386
    (_MakeField("mtime", "MTime", QFT_TIMESTAMP), datatype,
385 387
     _GetItemTimestamp(operator.attrgetter("mtime"))),
386 388
    ]
387 389

  
......
424 426

  
425 427
#: Fields that are direct attributes of an L{objects.Node} object
426 428
_NODE_SIMPLE_FIELDS = {
427
  "drained": ("Drained", constants.QFT_BOOL),
428
  "master_candidate": ("MasterC", constants.QFT_BOOL),
429
  "master_capable": ("MasterCapable", constants.QFT_BOOL),
430
  "name": ("Node", constants.QFT_TEXT),
431
  "offline": ("Offline", constants.QFT_BOOL),
432
  "serial_no": ("SerialNo", constants.QFT_NUMBER),
433
  "uuid": ("UUID", constants.QFT_TEXT),
434
  "vm_capable": ("VMCapable", constants.QFT_BOOL),
429
  "drained": ("Drained", QFT_BOOL),
430
  "master_candidate": ("MasterC", QFT_BOOL),
431
  "master_capable": ("MasterCapable", QFT_BOOL),
432
  "name": ("Node", QFT_TEXT),
433
  "offline": ("Offline", QFT_BOOL),
434
  "serial_no": ("SerialNo", QFT_NUMBER),
435
  "uuid": ("UUID", QFT_TEXT),
436
  "vm_capable": ("VMCapable", QFT_BOOL),
435 437
  }
436 438

  
437 439

  
438 440
#: Fields requiring talking to the node
439 441
_NODE_LIVE_FIELDS = {
440
  "bootid": ("BootID", constants.QFT_TEXT, "bootid"),
441
  "cnodes": ("CNodes", constants.QFT_NUMBER, "cpu_nodes"),
442
  "csockets": ("CSockets", constants.QFT_NUMBER, "cpu_sockets"),
443
  "ctotal": ("CTotal", constants.QFT_NUMBER, "cpu_total"),
444
  "dfree": ("DFree", constants.QFT_UNIT, "vg_free"),
445
  "dtotal": ("DTotal", constants.QFT_UNIT, "vg_size"),
446
  "mfree": ("MFree", constants.QFT_UNIT, "memory_free"),
447
  "mnode": ("MNode", constants.QFT_UNIT, "memory_dom0"),
448
  "mtotal": ("MTotal", constants.QFT_UNIT, "memory_total"),
442
  "bootid": ("BootID", QFT_TEXT, "bootid"),
443
  "cnodes": ("CNodes", QFT_NUMBER, "cpu_nodes"),
444
  "csockets": ("CSockets", QFT_NUMBER, "cpu_sockets"),
445
  "ctotal": ("CTotal", QFT_NUMBER, "cpu_total"),
446
  "dfree": ("DFree", QFT_UNIT, "vg_free"),
447
  "dtotal": ("DTotal", QFT_UNIT, "vg_size"),
448
  "mfree": ("MFree", QFT_UNIT, "memory_free"),
449
  "mnode": ("MNode", QFT_UNIT, "memory_dom0"),
450
  "mtotal": ("MTotal", QFT_UNIT, "memory_total"),
449 451
  }
450 452

  
451 453

  
......
466 468
    ng = ctx.groups.get(node.group, None)
467 469
    if ng is None:
468 470
      # Nodes always have a group, or the configuration is corrupt
469
      return (constants.QRFS_UNAVAIL, None)
471
      return (QRFS_UNAVAIL, None)
470 472

  
471 473
    return cb(ctx, node, ng)
472 474

  
......
483 485
  @param ng: The node group this node belongs to
484 486

  
485 487
  """
486
  return (constants.QRFS_NORMAL, ng.name)
488
  return (QRFS_NORMAL, ng.name)
487 489

  
488 490

  
489 491
def _GetNodePower(ctx, node):
......
495 497

  
496 498
  """
497 499
  if ctx.oob_support[node.name]:
498
    return (constants.QRFS_NORMAL, node.powered)
500
    return (QRFS_NORMAL, node.powered)
499 501

  
500
  return (constants.QRFS_UNAVAIL, None)
502
  return (QRFS_UNAVAIL, None)
501 503

  
502 504

  
503 505
def _GetNdParams(ctx, node, ng):
......
510 512
  @param ng: The node group this node belongs to
511 513

  
512 514
  """
513
  return (constants.QRFS_NORMAL, ctx.cluster.SimpleFillND(ng.FillND(node)))
515
  return (QRFS_NORMAL, ctx.cluster.SimpleFillND(ng.FillND(node)))
514 516

  
515 517

  
516 518
def _GetLiveNodeField(field, kind, ctx, node):
......
524 526

  
525 527
  """
526 528
  if node.offline:
527
    return (constants.QRFS_OFFLINE, None)
529
    return (QRFS_OFFLINE, None)
528 530

  
529 531
  if not ctx.curlive_data:
530
    return (constants.QRFS_NODATA, None)
532
    return (QRFS_NODATA, None)
531 533

  
532 534
  try:
533 535
    value = ctx.curlive_data[field]
534 536
  except KeyError:
535
    return (constants.QRFS_UNAVAIL, None)
537
    return (QRFS_UNAVAIL, None)
536 538

  
537
  if kind == constants.QFT_TEXT:
538
    return (constants.QRFS_NORMAL, value)
539
  if kind == QFT_TEXT:
540
    return (QRFS_NORMAL, value)
539 541

  
540
  assert kind in (constants.QFT_NUMBER, constants.QFT_UNIT)
542
  assert kind in (QFT_NUMBER, QFT_UNIT)
541 543

  
542 544
  # Try to convert into number
543 545
  try:
544
    return (constants.QRFS_NORMAL, int(value))
546
    return (QRFS_NORMAL, int(value))
545 547
  except (ValueError, TypeError):
546 548
    logging.exception("Failed to convert node field '%s' (value %r) to int",
547 549
                      value, field)
548
    return (constants.QRFS_UNAVAIL, None)
550
    return (QRFS_UNAVAIL, None)
549 551

  
550 552

  
551 553
def _BuildNodeFields():
......
553 555

  
554 556
  """
555 557
  fields = [
556
    (_MakeField("pip", "PrimaryIP", constants.QFT_TEXT), NQ_CONFIG,
557
     lambda ctx, node: (constants.QRFS_NORMAL, node.primary_ip)),
558
    (_MakeField("sip", "SecondaryIP", constants.QFT_TEXT), NQ_CONFIG,
559
     lambda ctx, node: (constants.QRFS_NORMAL, node.secondary_ip)),
560
    (_MakeField("tags", "Tags", constants.QFT_OTHER), NQ_CONFIG,
561
     lambda ctx, node: (constants.QRFS_NORMAL, list(node.GetTags()))),
562
    (_MakeField("master", "IsMaster", constants.QFT_BOOL), NQ_CONFIG,
563
     lambda ctx, node: (constants.QRFS_NORMAL, node.name == ctx.master_name)),
564
    (_MakeField("role", "Role", constants.QFT_TEXT), NQ_CONFIG,
565
     lambda ctx, node: (constants.QRFS_NORMAL,
566
                        _GetNodeRole(node, ctx.master_name))),
567
    (_MakeField("group", "Group", constants.QFT_TEXT), NQ_GROUP,
558
    (_MakeField("pip", "PrimaryIP", QFT_TEXT), NQ_CONFIG,
559
     lambda ctx, node: (QRFS_NORMAL, node.primary_ip)),
560
    (_MakeField("sip", "SecondaryIP", QFT_TEXT), NQ_CONFIG,
561
     lambda ctx, node: (QRFS_NORMAL, node.secondary_ip)),
562
    (_MakeField("tags", "Tags", QFT_OTHER), NQ_CONFIG,
563
     lambda ctx, node: (QRFS_NORMAL, list(node.GetTags()))),
564
    (_MakeField("master", "IsMaster", QFT_BOOL), NQ_CONFIG,
565
     lambda ctx, node: (QRFS_NORMAL, node.name == ctx.master_name)),
566
    (_MakeField("role", "Role", QFT_TEXT), NQ_CONFIG,
567
     lambda ctx, node: (QRFS_NORMAL, _GetNodeRole(node, ctx.master_name))),
568
    (_MakeField("group", "Group", QFT_TEXT), NQ_GROUP,
568 569
     _GetGroup(_GetNodeGroup)),
569
    (_MakeField("group.uuid", "GroupUUID", constants.QFT_TEXT),
570
     NQ_CONFIG, lambda ctx, node: (constants.QRFS_NORMAL, node.group)),
571
    (_MakeField("powered", "Powered", constants.QFT_BOOL), NQ_OOB,
572
      _GetNodePower),
573
    (_MakeField("ndparams", "NodeParameters", constants.QFT_OTHER), NQ_GROUP,
570
    (_MakeField("group.uuid", "GroupUUID", QFT_TEXT),
571
     NQ_CONFIG, lambda ctx, node: (QRFS_NORMAL, node.group)),
572
    (_MakeField("powered", "Powered", QFT_BOOL), NQ_OOB, _GetNodePower),
573
    (_MakeField("ndparams", "NodeParameters", QFT_OTHER), NQ_GROUP,
574 574
      _GetGroup(_GetNdParams)),
575
    (_MakeField("custom_ndparams", "CustomNodeParameters", constants.QFT_OTHER),
576
      NQ_GROUP, lambda ctx, node: (constants.QRFS_NORMAL, node.ndparams)),
575
    (_MakeField("custom_ndparams", "CustomNodeParameters", QFT_OTHER),
576
      NQ_GROUP, lambda ctx, node: (QRFS_NORMAL, node.ndparams)),
577 577
    ]
578 578

  
579 579
  def _GetLength(getter):
580
    return lambda ctx, node: (constants.QRFS_NORMAL,
581
                              len(getter(ctx)[node.name]))
580
    return lambda ctx, node: (QRFS_NORMAL, len(getter(ctx)[node.name]))
582 581

  
583 582
  def _GetList(getter):
584
    return lambda ctx, node: (constants.QRFS_NORMAL,
585
                              list(getter(ctx)[node.name]))
583
    return lambda ctx, node: (QRFS_NORMAL, list(getter(ctx)[node.name]))
586 584

  
587 585
  # Add fields operating on instance lists
588 586
  for prefix, titleprefix, getter in \
589 587
      [("p", "Pri", operator.attrgetter("node_to_primary")),
590 588
       ("s", "Sec", operator.attrgetter("node_to_secondary"))]:
591 589
    fields.extend([
592
      (_MakeField("%sinst_cnt" % prefix, "%sinst" % prefix.upper(),
593
                  constants.QFT_NUMBER),
590
      (_MakeField("%sinst_cnt" % prefix, "%sinst" % prefix.upper(), QFT_NUMBER),
594 591
       NQ_INST, _GetLength(getter)),
595 592
      (_MakeField("%sinst_list" % prefix, "%sInstances" % titleprefix,
596
                  constants.QFT_OTHER),
593
                  QFT_OTHER),
597 594
       NQ_INST, _GetList(getter)),
598 595
      ])
599 596

  
......
678 675
  # Can't use QRFS_OFFLINE here as it would describe the instance to be offline
679 676
  # when we actually don't know due to missing data
680 677
  if inst.primary_node in ctx.bad_nodes:
681
    return (constants.QRFS_NODATA, None)
678
    return (QRFS_NODATA, None)
682 679
  else:
683
    return (constants.QRFS_NORMAL, bool(ctx.live_data.get(inst.name)))
680
    return (QRFS_NORMAL, bool(ctx.live_data.get(inst.name)))
684 681

  
685 682

  
686 683
def _GetInstLiveData(name):
......
702 699
        inst.primary_node in ctx.offline_nodes):
703 700
      # Can't use QRFS_OFFLINE here as it would describe the instance to be
704 701
      # offline when we actually don't know due to missing data
705
      return (constants.QRFS_NODATA, None)
702
      return (QRFS_NODATA, None)
706 703

  
707 704
    if inst.name in ctx.live_data:
708 705
      data = ctx.live_data[inst.name]
709 706
      if name in data:
710
        return (constants.QRFS_NORMAL, data[name])
707
        return (QRFS_NORMAL, data[name])
711 708

  
712
    return (constants.QRFS_UNAVAIL, None)
709
    return (QRFS_UNAVAIL, None)
713 710

  
714 711
  return fn
715 712

  
......
723 720

  
724 721
  """
725 722
  if inst.primary_node in ctx.offline_nodes:
726
    return (constants.QRFS_NORMAL, "ERROR_nodeoffline")
723
    return (QRFS_NORMAL, "ERROR_nodeoffline")
727 724

  
728 725
  if inst.primary_node in ctx.bad_nodes:
729
    return (constants.QRFS_NORMAL, "ERROR_nodedown")
726
    return (QRFS_NORMAL, "ERROR_nodedown")
730 727

  
731 728
  if bool(ctx.live_data.get(inst.name)):
732 729
    if inst.admin_up:
733
      return (constants.QRFS_NORMAL, "running")
730
      return (QRFS_NORMAL, "running")
734 731
    else:
735
      return (constants.QRFS_NORMAL, "ERROR_up")
732
      return (QRFS_NORMAL, "ERROR_up")
736 733

  
737 734
  if inst.admin_up:
738
    return (constants.QRFS_NORMAL, "ERROR_down")
735
    return (QRFS_NORMAL, "ERROR_down")
739 736

  
740
  return (constants.QRFS_NORMAL, "ADMIN_down")
737
  return (QRFS_NORMAL, "ADMIN_down")
741 738

  
742 739

  
743 740
def _GetInstDiskSize(index):
......
755 752

  
756 753
    """
757 754
    try:
758
      return (constants.QRFS_NORMAL, inst.disks[index].size)
755
      return (QRFS_NORMAL, inst.disks[index].size)
759 756
    except IndexError:
760
      return (constants.QRFS_UNAVAIL, None)
757
      return (QRFS_UNAVAIL, None)
761 758

  
762 759
  return fn
763 760

  
......
782 779
    try:
783 780
      nic = inst.nics[index]
784 781
    except IndexError:
785
      return (constants.QRFS_UNAVAIL, None)
782
      return (QRFS_UNAVAIL, None)
786 783

  
787 784
    return cb(ctx, index, nic)
788 785

  
......
798 795

  
799 796
  """
800 797
  if nic.ip is None:
801
    return (constants.QRFS_UNAVAIL, None)
798
    return (QRFS_UNAVAIL, None)
802 799
  else:
803
    return (constants.QRFS_NORMAL, nic.ip)
800
    return (QRFS_NORMAL, nic.ip)
804 801

  
805 802

  
806 803
def _GetInstNicBridge(ctx, index, _):
......
816 813
  nicparams = ctx.inst_nicparams[index]
817 814

  
818 815
  if nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
819
    return (constants.QRFS_NORMAL, nicparams[constants.NIC_LINK])
816
    return (QRFS_NORMAL, nicparams[constants.NIC_LINK])
820 817
  else:
821
    return (constants.QRFS_UNAVAIL, None)
818
    return (QRFS_UNAVAIL, None)
822 819

  
823 820

  
824 821
def _GetInstAllNicBridges(ctx, inst):
......
841 838

  
842 839
  assert len(result) == len(inst.nics)
843 840

  
844
  return (constants.QRFS_NORMAL, result)
841
  return (QRFS_NORMAL, result)
845 842

  
846 843

  
847 844
def _GetInstNicParam(name):
......
862 859

  
863 860
    """
864 861
    assert len(ctx.inst_nicparams) >= index
865
    return (constants.QRFS_NORMAL, ctx.inst_nicparams[index][name])
862
    return (QRFS_NORMAL, ctx.inst_nicparams[index][name])
866 863

  
867 864
  return fn
868 865

  
......
873 870
  @return: List of field definitions used as input for L{_PrepareFieldList}
874 871

  
875 872
  """
876
  nic_mac_fn = lambda ctx, _, nic: (constants.QRFS_NORMAL, nic.mac)
873
  nic_mac_fn = lambda ctx, _, nic: (QRFS_NORMAL, nic.mac)
877 874
  nic_mode_fn = _GetInstNicParam(constants.NIC_MODE)
878 875
  nic_link_fn = _GetInstNicParam(constants.NIC_LINK)
879 876

  
880 877
  fields = [
881 878
    # First NIC (legacy)
882
    (_MakeField("ip", "IP_address", constants.QFT_TEXT), IQ_CONFIG,
879
    (_MakeField("ip", "IP_address", QFT_TEXT), IQ_CONFIG,
883 880
     _GetInstNic(0, _GetInstNicIp)),
884
    (_MakeField("mac", "MAC_address", constants.QFT_TEXT), IQ_CONFIG,
881
    (_MakeField("mac", "MAC_address", QFT_TEXT), IQ_CONFIG,
885 882
     _GetInstNic(0, nic_mac_fn)),
886
    (_MakeField("bridge", "Bridge", constants.QFT_TEXT), IQ_CONFIG,
883
    (_MakeField("bridge", "Bridge", QFT_TEXT), IQ_CONFIG,
887 884
     _GetInstNic(0, _GetInstNicBridge)),
888
    (_MakeField("nic_mode", "NIC_Mode", constants.QFT_TEXT), IQ_CONFIG,
885
    (_MakeField("nic_mode", "NIC_Mode", QFT_TEXT), IQ_CONFIG,
889 886
     _GetInstNic(0, nic_mode_fn)),
890
    (_MakeField("nic_link", "NIC_Link", constants.QFT_TEXT), IQ_CONFIG,
887
    (_MakeField("nic_link", "NIC_Link", QFT_TEXT), IQ_CONFIG,
891 888
     _GetInstNic(0, nic_link_fn)),
892 889

  
893 890
    # All NICs
894
    (_MakeField("nic.count", "NICs", constants.QFT_NUMBER), IQ_CONFIG,
895
     lambda ctx, inst: (constants.QRFS_NORMAL, len(inst.nics))),
896
    (_MakeField("nic.macs", "NIC_MACs", constants.QFT_OTHER), IQ_CONFIG,
897
     lambda ctx, inst: (constants.QRFS_NORMAL, [nic.mac for nic in inst.nics])),
898
    (_MakeField("nic.ips", "NIC_IPs", constants.QFT_OTHER), IQ_CONFIG,
899
     lambda ctx, inst: (constants.QRFS_NORMAL, [nic.ip for nic in inst.nics])),
900
    (_MakeField("nic.modes", "NIC_modes", constants.QFT_OTHER), IQ_CONFIG,
901
     lambda ctx, inst: (constants.QRFS_NORMAL,
902
                        [nicp[constants.NIC_MODE]
903
                         for nicp in ctx.inst_nicparams])),
904
    (_MakeField("nic.links", "NIC_links", constants.QFT_OTHER), IQ_CONFIG,
905
     lambda ctx, inst: (constants.QRFS_NORMAL,
906
                        [nicp[constants.NIC_LINK]
907
                         for nicp in ctx.inst_nicparams])),
908
    (_MakeField("nic.bridges", "NIC_bridges", constants.QFT_OTHER), IQ_CONFIG,
891
    (_MakeField("nic.count", "NICs", QFT_NUMBER), IQ_CONFIG,
892
     lambda ctx, inst: (QRFS_NORMAL, len(inst.nics))),
893
    (_MakeField("nic.macs", "NIC_MACs", QFT_OTHER), IQ_CONFIG,
894
     lambda ctx, inst: (QRFS_NORMAL, [nic.mac for nic in inst.nics])),
895
    (_MakeField("nic.ips", "NIC_IPs", QFT_OTHER), IQ_CONFIG,
896
     lambda ctx, inst: (QRFS_NORMAL, [nic.ip for nic in inst.nics])),
897
    (_MakeField("nic.modes", "NIC_modes", QFT_OTHER), IQ_CONFIG,
898
     lambda ctx, inst: (QRFS_NORMAL, [nicp[constants.NIC_MODE]
899
                                      for nicp in ctx.inst_nicparams])),
900
    (_MakeField("nic.links", "NIC_links", QFT_OTHER), IQ_CONFIG,
901
     lambda ctx, inst: (QRFS_NORMAL, [nicp[constants.NIC_LINK]
902
                                      for nicp in ctx.inst_nicparams])),
903
    (_MakeField("nic.bridges", "NIC_bridges", QFT_OTHER), IQ_CONFIG,
909 904
     _GetInstAllNicBridges),
910 905
    ]
911 906

  
912 907
  # NICs by number
913 908
  for i in range(constants.MAX_NICS):
914 909
    fields.extend([
915
      (_MakeField("nic.ip/%s" % i, "NicIP/%s" % i, constants.QFT_TEXT),
910
      (_MakeField("nic.ip/%s" % i, "NicIP/%s" % i, QFT_TEXT),
916 911
       IQ_CONFIG, _GetInstNic(i, _GetInstNicIp)),
917
      (_MakeField("nic.mac/%s" % i, "NicMAC/%s" % i, constants.QFT_TEXT),
912
      (_MakeField("nic.mac/%s" % i, "NicMAC/%s" % i, QFT_TEXT),
918 913
       IQ_CONFIG, _GetInstNic(i, nic_mac_fn)),
919
      (_MakeField("nic.mode/%s" % i, "NicMode/%s" % i, constants.QFT_TEXT),
914
      (_MakeField("nic.mode/%s" % i, "NicMode/%s" % i, QFT_TEXT),
920 915
       IQ_CONFIG, _GetInstNic(i, nic_mode_fn)),
921
      (_MakeField("nic.link/%s" % i, "NicLink/%s" % i, constants.QFT_TEXT),
916
      (_MakeField("nic.link/%s" % i, "NicLink/%s" % i, QFT_TEXT),
922 917
       IQ_CONFIG, _GetInstNic(i, nic_link_fn)),
923
      (_MakeField("nic.bridge/%s" % i, "NicBridge/%s" % i, constants.QFT_TEXT),
918
      (_MakeField("nic.bridge/%s" % i, "NicBridge/%s" % i, QFT_TEXT),
924 919
       IQ_CONFIG, _GetInstNic(i, _GetInstNicBridge)),
925 920
      ])
926 921

  
......
940 935
  if usage is None:
941 936
    usage = 0
942 937

  
943
  return (constants.QRFS_NORMAL, usage)
938
  return (QRFS_NORMAL, usage)
944 939

  
945 940

  
946 941
def _GetInstanceDiskFields():
......
950 945

  
951 946
  """
952 947
  fields = [
953
    (_MakeField("disk_usage", "DiskUsage", constants.QFT_UNIT), IQ_DISKUSAGE,
948
    (_MakeField("disk_usage", "DiskUsage", QFT_UNIT), IQ_DISKUSAGE,
954 949
     _GetInstDiskUsage),
955
    (_MakeField("sda_size", "LegacyDisk/0", constants.QFT_UNIT), IQ_CONFIG,
950
    (_MakeField("sda_size", "LegacyDisk/0", QFT_UNIT), IQ_CONFIG,
956 951
     _GetInstDiskSize(0)),
957
    (_MakeField("sdb_size", "LegacyDisk/1", constants.QFT_UNIT), IQ_CONFIG,
952
    (_MakeField("sdb_size", "LegacyDisk/1", QFT_UNIT), IQ_CONFIG,
958 953
     _GetInstDiskSize(1)),
959
    (_MakeField("disk.count", "Disks", constants.QFT_NUMBER), IQ_CONFIG,
960
     lambda ctx, inst: (constants.QRFS_NORMAL, len(inst.disks))),
961
    (_MakeField("disk.sizes", "Disk_sizes", constants.QFT_OTHER), IQ_CONFIG,
962
     lambda ctx, inst: (constants.QRFS_NORMAL,
963
                        [disk.size for disk in inst.disks])),
954
    (_MakeField("disk.count", "Disks", QFT_NUMBER), IQ_CONFIG,
955
     lambda ctx, inst: (QRFS_NORMAL, len(inst.disks))),
956
    (_MakeField("disk.sizes", "Disk_sizes", QFT_OTHER), IQ_CONFIG,
957
     lambda ctx, inst: (QRFS_NORMAL, [disk.size for disk in inst.disks])),
964 958
    ]
965 959

  
966 960
  # Disks by number
967 961
  fields.extend([
968
    (_MakeField("disk.size/%s" % i, "Disk/%s" % i, constants.QFT_UNIT),
962
    (_MakeField("disk.size/%s" % i, "Disk/%s" % i, QFT_UNIT),
969 963
     IQ_CONFIG, _GetInstDiskSize(i))
970 964
    for i in range(constants.MAX_DISKS)
971 965
    ])
......
1000 994

  
1001 995
  fields = [
1002 996
    # Filled parameters
1003
    (_MakeField("hvparams", "HypervisorParameters", constants.QFT_OTHER),
1004
     IQ_CONFIG, lambda ctx, _: (constants.QRFS_NORMAL, ctx.inst_hvparams)),
1005
    (_MakeField("beparams", "BackendParameters", constants.QFT_OTHER),
1006
     IQ_CONFIG, lambda ctx, _: (constants.QRFS_NORMAL, ctx.inst_beparams)),
1007
    (_MakeField("vcpus", "LegacyVCPUs", constants.QFT_NUMBER), IQ_CONFIG,
1008
     lambda ctx, _: (constants.QRFS_NORMAL,
1009
                     ctx.inst_beparams[constants.BE_VCPUS])),
997
    (_MakeField("hvparams", "HypervisorParameters", QFT_OTHER),
998
     IQ_CONFIG, lambda ctx, _: (QRFS_NORMAL, ctx.inst_hvparams)),
999
    (_MakeField("beparams", "BackendParameters", QFT_OTHER),
1000
     IQ_CONFIG, lambda ctx, _: (QRFS_NORMAL, ctx.inst_beparams)),
1001
    (_MakeField("vcpus", "LegacyVCPUs", QFT_NUMBER), IQ_CONFIG,
1002
     lambda ctx, _: (QRFS_NORMAL, ctx.inst_beparams[constants.BE_VCPUS])),
1010 1003

  
1011 1004
    # Unfilled parameters
1012
    (_MakeField("custom_hvparams", "CustomHypervisorParameters",
1013
                constants.QFT_OTHER),
1014
     IQ_CONFIG, lambda ctx, inst: (constants.QRFS_NORMAL, inst.hvparams)),
1015
    (_MakeField("custom_beparams", "CustomBackendParameters",
1016
                constants.QFT_OTHER),
1017
     IQ_CONFIG, lambda ctx, inst: (constants.QRFS_NORMAL, inst.beparams)),
1018
    (_MakeField("custom_nicparams", "CustomNicParameters",
1019
                constants.QFT_OTHER),
1020
     IQ_CONFIG, lambda ctx, inst: (constants.QRFS_NORMAL,
1005
    (_MakeField("custom_hvparams", "CustomHypervisorParameters", QFT_OTHER),
1006
     IQ_CONFIG, lambda ctx, inst: (QRFS_NORMAL, inst.hvparams)),
1007
    (_MakeField("custom_beparams", "CustomBackendParameters", QFT_OTHER),
1008
     IQ_CONFIG, lambda ctx, inst: (QRFS_NORMAL, inst.beparams)),
1009
    (_MakeField("custom_nicparams", "CustomNicParameters", QFT_OTHER),
1010
     IQ_CONFIG, lambda ctx, inst: (QRFS_NORMAL,
1021 1011
                                   [nic.nicparams for nic in inst.nics])),
1022 1012
    ]
1023 1013

  
1024 1014
  # HV params
1025 1015
  def _GetInstHvParam(name):
1026
    return lambda ctx, _: (constants.QRFS_NORMAL,
1027
                           ctx.inst_hvparams.get(name, None))
1016
    return lambda ctx, _: (QRFS_NORMAL, ctx.inst_hvparams.get(name, None))
1028 1017

  
1029 1018
  fields.extend([
1030 1019
    # For now all hypervisor parameters are exported as QFT_OTHER
1031
    (_MakeField("hv/%s" % name, hv_title.get(name, "hv/%s" % name),
1032
                constants.QFT_OTHER),
1020
    (_MakeField("hv/%s" % name, hv_title.get(name, "hv/%s" % name), QFT_OTHER),
1033 1021
     IQ_CONFIG, _GetInstHvParam(name))
1034 1022
    for name in constants.HVS_PARAMETERS
1035 1023
    if name not in constants.HVC_GLOBALS
......
1037 1025

  
1038 1026
  # BE params
1039 1027
  def _GetInstBeParam(name):
1040
    return lambda ctx, _: (constants.QRFS_NORMAL,
1041
                           ctx.inst_beparams.get(name, None))
1028
    return lambda ctx, _: (QRFS_NORMAL, ctx.inst_beparams.get(name, None))
1042 1029

  
1043 1030
  fields.extend([
1044 1031
    # For now all backend parameters are exported as QFT_OTHER
1045
    (_MakeField("be/%s" % name, be_title.get(name, "be/%s" % name),
1046
                constants.QFT_OTHER),
1032
    (_MakeField("be/%s" % name, be_title.get(name, "be/%s" % name), QFT_OTHER),
1047 1033
     IQ_CONFIG, _GetInstBeParam(name))
1048 1034
    for name in constants.BES_PARAMETERS
1049 1035
    ])
......
1052 1038

  
1053 1039

  
1054 1040
_INST_SIMPLE_FIELDS = {
1055
  "disk_template": ("Disk_template", constants.QFT_TEXT),
1056
  "hypervisor": ("Hypervisor", constants.QFT_TEXT),
1057
  "name": ("Node", constants.QFT_TEXT),
1041
  "disk_template": ("Disk_template", QFT_TEXT),
1042
  "hypervisor": ("Hypervisor", QFT_TEXT),
1043
  "name": ("Node", QFT_TEXT),
1058 1044
  # Depending on the hypervisor, the port can be None
1059
  "network_port": ("Network_port", constants.QFT_OTHER),
1060
  "os": ("OS", constants.QFT_TEXT),
1061
  "serial_no": ("SerialNo", constants.QFT_NUMBER),
1062
  "uuid": ("UUID", constants.QFT_TEXT),
1045
  "network_port": ("Network_port", QFT_OTHER),
1046
  "os": ("OS", QFT_TEXT),
1047
  "serial_no": ("SerialNo", QFT_NUMBER),
1048
  "uuid": ("UUID", QFT_TEXT),
1063 1049
  }
1064 1050

  
1065 1051

  
......
1068 1054

  
1069 1055
  """
1070 1056
  fields = [
1071
    (_MakeField("pnode", "Primary_node", constants.QFT_TEXT), IQ_CONFIG,
1072
     lambda ctx, inst: (constants.QRFS_NORMAL, inst.primary_node)),
1073
    (_MakeField("snodes", "Secondary_Nodes", constants.QFT_OTHER), IQ_CONFIG,
1074
     lambda ctx, inst: (constants.QRFS_NORMAL, list(inst.secondary_nodes))),
1075
    (_MakeField("admin_state", "Autostart", constants.QFT_BOOL), IQ_CONFIG,
1076
     lambda ctx, inst: (constants.QRFS_NORMAL, inst.admin_up)),
1077
    (_MakeField("tags", "Tags", constants.QFT_OTHER), IQ_CONFIG,
1078
     lambda ctx, inst: (constants.QRFS_NORMAL, list(inst.GetTags()))),
1057
    (_MakeField("pnode", "Primary_node", QFT_TEXT), IQ_CONFIG,
1058
     lambda ctx, inst: (QRFS_NORMAL, inst.primary_node)),
1059
    (_MakeField("snodes", "Secondary_Nodes", QFT_OTHER), IQ_CONFIG,
1060
     lambda ctx, inst: (QRFS_NORMAL, list(inst.secondary_nodes))),
1061
    (_MakeField("admin_state", "Autostart", QFT_BOOL), IQ_CONFIG,
1062
     lambda ctx, inst: (QRFS_NORMAL, inst.admin_up)),
1063
    (_MakeField("tags", "Tags", QFT_OTHER), IQ_CONFIG,
1064
     lambda ctx, inst: (QRFS_NORMAL, list(inst.GetTags()))),
1079 1065
    ]
1080 1066

  
1081 1067
  # Add simple fields
......
1084 1070

  
1085 1071
  # Fields requiring talking to the node
1086 1072
  fields.extend([
1087
    (_MakeField("oper_state", "Running", constants.QFT_BOOL), IQ_LIVE,
1073
    (_MakeField("oper_state", "Running", QFT_BOOL), IQ_LIVE,
1088 1074
     _GetInstOperState),
1089
    (_MakeField("oper_ram", "RuntimeMemory", constants.QFT_UNIT), IQ_LIVE,
1075
    (_MakeField("oper_ram", "RuntimeMemory", QFT_UNIT), IQ_LIVE,
1090 1076
     _GetInstLiveData("memory")),
1091
    (_MakeField("oper_vcpus", "RuntimeVCPUs", constants.QFT_NUMBER), IQ_LIVE,
1077
    (_MakeField("oper_vcpus", "RuntimeVCPUs", QFT_NUMBER), IQ_LIVE,
1092 1078
     _GetInstLiveData("vcpus")),
1093
    (_MakeField("status", "Status", constants.QFT_TEXT), IQ_LIVE,
1094
     _GetInstStatus),
1079
    (_MakeField("status", "Status", QFT_TEXT), IQ_LIVE, _GetInstStatus),
1095 1080
    ])
1096 1081

  
1097 1082
  fields.extend(_GetInstanceParameterFields())
......
1128 1113
  if owners:
1129 1114
    owners = utils.NiceSort(owners)
1130 1115

  
1131
  return (constants.QRFS_NORMAL, owners)
1116
  return (QRFS_NORMAL, owners)
1132 1117

  
1133 1118

  
1134 1119
def _GetLockPending(_, data):
......
1141 1126
    pending = [(mode, utils.NiceSort(names))
1142 1127
               for (mode, names) in pending]
1143 1128

  
1144
  return (constants.QRFS_NORMAL, pending)
1129
  return (QRFS_NORMAL, pending)
1145 1130

  
1146 1131

  
1147 1132
def _BuildLockFields():
......
1149 1134

  
1150 1135
  """
1151 1136
  return _PrepareFieldList([
1152
    (_MakeField("name", "Name", constants.QFT_TEXT), None,
1153
     lambda ctx, (name, mode, owners, pending): (constants.QRFS_NORMAL, name)),
1154
    (_MakeField("mode", "Mode", constants.QFT_OTHER), LQ_MODE,
1155
     lambda ctx, (name, mode, owners, pending): (constants.QRFS_NORMAL, mode)),
1156
    (_MakeField("owner", "Owner", constants.QFT_OTHER), LQ_OWNER,
1157
     _GetLockOwners),
1158
    (_MakeField("pending", "Pending", constants.QFT_OTHER), LQ_PENDING,
1159
     _GetLockPending),
1137
    (_MakeField("name", "Name", QFT_TEXT), None,
1138
     lambda ctx, (name, mode, owners, pending): (QRFS_NORMAL, name)),
1139
    (_MakeField("mode", "Mode", QFT_OTHER), LQ_MODE,
1140
     lambda ctx, (name, mode, owners, pending): (QRFS_NORMAL, mode)),
1141
    (_MakeField("owner", "Owner", QFT_OTHER), LQ_OWNER, _GetLockOwners),
1142
    (_MakeField("pending", "Pending", QFT_OTHER), LQ_PENDING, _GetLockPending),
1160 1143
    ])
1161 1144

  
1162 1145

  
......
1186 1169

  
1187 1170

  
1188 1171
_GROUP_SIMPLE_FIELDS = {
1189
  "alloc_policy": ("AllocPolicy", constants.QFT_TEXT),
1190
  "name": ("Group", constants.QFT_TEXT),
1191
  "serial_no": ("SerialNo", constants.QFT_NUMBER),
1192
  "uuid": ("UUID", constants.QFT_TEXT),
1193
  "ndparams": ("NDParams", constants.QFT_OTHER),
1172
  "alloc_policy": ("AllocPolicy", QFT_TEXT),
1173
  "name": ("Group", QFT_TEXT),
1174
  "serial_no": ("SerialNo", QFT_NUMBER),
1175
  "uuid": ("UUID", QFT_TEXT),
1176
  "ndparams": ("NDParams", QFT_OTHER),
1194 1177
  }
1195 1178

  
1196 1179

  
......
1203 1186
            for (name, (title, kind)) in _GROUP_SIMPLE_FIELDS.items()]
1204 1187

  
1205 1188
  def _GetLength(getter):
1206
    return lambda ctx, group: (constants.QRFS_NORMAL,
1207
                               len(getter(ctx)[group.uuid]))
1189
    return lambda ctx, group: (QRFS_NORMAL, len(getter(ctx)[group.uuid]))
1208 1190

  
1209 1191
  def _GetSortedList(getter):
1210
    return lambda ctx, group: (constants.QRFS_NORMAL,
1192
    return lambda ctx, group: (QRFS_NORMAL,
1211 1193
                               utils.NiceSort(getter(ctx)[group.uuid]))
1212 1194

  
1213 1195
  group_to_nodes = operator.attrgetter("group_to_nodes")
......
1215 1197

  
1216 1198
  # Add fields for nodes
1217 1199
  fields.extend([
1218
    (_MakeField("node_cnt", "Nodes", constants.QFT_NUMBER),
1200
    (_MakeField("node_cnt", "Nodes", QFT_NUMBER),
1219 1201
     GQ_NODE, _GetLength(group_to_nodes)),
1220
    (_MakeField("node_list", "NodeList", constants.QFT_OTHER),
1202
    (_MakeField("node_list", "NodeList", QFT_OTHER),
1221 1203
     GQ_NODE, _GetSortedList(group_to_nodes)),
1222 1204
    ])
1223 1205

  
1224 1206
  # Add fields for instances
1225 1207
  fields.extend([
1226
    (_MakeField("pinst_cnt", "Instances", constants.QFT_NUMBER),
1208
    (_MakeField("pinst_cnt", "Instances", QFT_NUMBER),
1227 1209
     GQ_INST, _GetLength(group_to_instances)),
1228
    (_MakeField("pinst_list", "InstanceList", constants.QFT_OTHER),
1210
    (_MakeField("pinst_list", "InstanceList", QFT_OTHER),
1229 1211
     GQ_INST, _GetSortedList(group_to_instances)),
1230 1212
    ])
1231 1213

  

Also available in: Unified diff