Revision 8e21cfc0

b/lib/query.py
46 46
 LQ_OWNER,
47 47
 LQ_PENDING) = range(10, 13)
48 48

  
49
(GQ_CONFIG,
50
 GQ_NODE,
51
 GQ_INST) = range(200, 203)
52

  
53

  
49 54
FIELD_NAME_RE = re.compile(r"^[a-z0-9/._]+$")
50 55
TITLE_RE = re.compile(r"^[^\s]+$")
51 56

  
......
1064 1069
    ])
1065 1070

  
1066 1071

  
1072
class GroupQueryData:
1073
  """Data container for node group data queries.
1074

  
1075
  """
1076
  def __init__(self, groups, group_to_nodes, group_to_instances):
1077
    """Initializes this class.
1078

  
1079
    @param groups: List of node group objects
1080
    @type group_to_nodes: dict; group UUID as key
1081
    @param group_to_nodes: Per-group list of nodes
1082
    @type group_to_instances: dict; group UUID as key
1083
    @param group_to_instances: Per-group list of (primary) instances
1084

  
1085
    """
1086
    self.groups = groups
1087
    self.group_to_nodes = group_to_nodes
1088
    self.group_to_instances = group_to_instances
1089

  
1090
  def __iter__(self):
1091
    """Iterate over all node groups.
1092

  
1093
    """
1094
    return iter(self.groups)
1095

  
1096

  
1097
_GROUP_SIMPLE_FIELDS = {
1098
  "alloc_policy": ("AllocPolicy", constants.QFT_TEXT),
1099
  "name": ("Group", constants.QFT_TEXT),
1100
  "serial_no": ("SerialNo", constants.QFT_NUMBER),
1101
  "uuid": ("UUID", constants.QFT_TEXT),
1102
  }
1103

  
1104

  
1105
def _BuildGroupFields():
1106
  """Builds list of fields for node group queries.
1107

  
1108
  """
1109
  # Add simple fields
1110
  fields = [(_MakeField(name, title, kind), GQ_CONFIG, _GetItemAttr(name))
1111
            for (name, (title, kind)) in _GROUP_SIMPLE_FIELDS.items()]
1112

  
1113
  def _GetLength(getter):
1114
    return lambda ctx, group: (constants.QRFS_NORMAL,
1115
                               len(getter(ctx)[group.uuid]))
1116

  
1117
  def _GetSortedList(getter):
1118
    return lambda ctx, group: (constants.QRFS_NORMAL,
1119
                               utils.NiceSort(getter(ctx)[group.uuid]))
1120

  
1121
  group_to_nodes = operator.attrgetter("group_to_nodes")
1122
  group_to_instances = operator.attrgetter("group_to_instances")
1123

  
1124
  # Add fields for nodes
1125
  fields.extend([
1126
    (_MakeField("node_cnt", "Nodes", constants.QFT_NUMBER),
1127
     GQ_NODE, _GetLength(group_to_nodes)),
1128
    (_MakeField("node_list", "NodeList", constants.QFT_OTHER),
1129
     GQ_NODE, _GetSortedList(group_to_nodes)),
1130
    ])
1131

  
1132
  # Add fields for instances
1133
  fields.extend([
1134
    (_MakeField("pinst_cnt", "Instances", constants.QFT_NUMBER),
1135
     GQ_INST, _GetLength(group_to_instances)),
1136
    (_MakeField("pinst_list", "InstanceList", constants.QFT_OTHER),
1137
     GQ_INST, _GetSortedList(group_to_instances)),
1138
    ])
1139

  
1140
  fields.extend(_GetItemTimestampFields(GQ_CONFIG))
1141

  
1142
  return _PrepareFieldList(fields)
1143

  
1144

  
1067 1145
#: Fields available for node queries
1068 1146
NODE_FIELDS = _BuildNodeFields()
1069 1147

  
......
1073 1151
#: Fields available for lock queries
1074 1152
LOCK_FIELDS = _BuildLockFields()
1075 1153

  
1154
#: Fields available for node group queries
1155
GROUP_FIELDS = _BuildGroupFields()
1156

  
1076 1157
#: All available field lists
1077
ALL_FIELD_LISTS = [NODE_FIELDS, INSTANCE_FIELDS, LOCK_FIELDS]
1158
ALL_FIELD_LISTS = [NODE_FIELDS, INSTANCE_FIELDS, LOCK_FIELDS, GROUP_FIELDS]

Also available in: Unified diff