Revision 679cba3a lib/query.py

b/lib/query.py
90 90
 GQ_NODE,
91 91
 GQ_INST) = range(200, 203)
92 92

  
93
(NETQ_CONFIG,
94
 NETQ_GROUP,
95
 NETQ_STATS,
96
 NETQ_INST) = range(300, 304)
93 97

  
94 98
FIELD_NAME_RE = re.compile(r"^[a-z0-9/._]+$")
95 99
TITLE_RE = re.compile(r"^[^\s]+$")
......
1293 1297
  return _PrepareFieldList(fields, [])
1294 1298

  
1295 1299

  
1300
class NetworkQueryData:
1301
  """Data container for network data queries.
1302

  
1303
  """
1304
  def __init__(self, networks, network_to_groups,
1305
               network_to_instances, stats):
1306
    """Initializes this class.
1307

  
1308
    @param networks: List of network objects
1309
    @type network_to_groups: dict; network UUID as key
1310
    @param network_to_groups: Per-network list of groups
1311
    @type network_to_instances: dict; network UUID as key
1312
    @param network_to_instances: Per-network list of instances
1313
    @type stats: dict; network UUID as key
1314
    @param stats: Per-network usage statistics
1315

  
1316
    """
1317
    self.networks = networks
1318
    self.network_to_groups = network_to_groups
1319
    self.network_to_instances = network_to_instances
1320
    self.stats = stats
1321

  
1322
  def __iter__(self):
1323
    """Iterate over all node groups.
1324

  
1325
    """
1326
    for net in self.networks:
1327
      if self.stats:
1328
        self.curstats = self.stats.get(net.uuid, None)
1329
        logging.warn(self.curstats)
1330
      else:
1331
        self.curstats = None
1332
      yield net
1333

  
1334

  
1335
_NETWORK_SIMPLE_FIELDS = {
1336
  "name": ("Network", QFT_TEXT),
1337
  "network": ("Subnet", QFT_TEXT),
1338
  "gateway": ("Gateway", QFT_TEXT),
1339
  }
1340

  
1341

  
1342
_NETWORK_STATS_FIELDS = {
1343
  "free_count": ("FreeCount", QFT_NUMBER),
1344
  "reserved_count": ("ReservedCount", QFT_NUMBER),
1345
  "map": ("Map", QFT_TEXT),
1346
  }
1347

  
1348

  
1349
def _GetNetworkStatsField(field, kind, ctx, net):
1350
  """Gets the value of a "stats" field from L{NetworkQueryData}.
1351

  
1352
  @param field: Field name
1353
  @param kind: Data kind, one of L{constants.QFT_ALL}
1354
  @type ctx: L{NetworkQueryData}
1355

  
1356
  """
1357

  
1358
  try:
1359
    value = ctx.curstats[field]
1360
  except KeyError:
1361
    return _FS_UNAVAIL
1362

  
1363
  if kind == QFT_TEXT:
1364
    return value
1365

  
1366
  assert kind in (QFT_NUMBER, QFT_UNIT)
1367

  
1368
  # Try to convert into number
1369
  try:
1370
    return int(value)
1371
  except (ValueError, TypeError):
1372
    logging.exception("Failed to convert network field '%s' (value %r) to int",
1373
                      value, field)
1374
    return _FS_UNAVAIL
1375

  
1376

  
1377
def _BuildNetworkFields():
1378
  """Builds list of fields for network queries.
1379

  
1380
  """
1381
  # Add simple fields
1382
  fields = [(_MakeField(name, title, kind), NETQ_CONFIG, _GetItemAttr(name))
1383
            for (name, (title, kind)) in _NETWORK_SIMPLE_FIELDS.items()]
1384

  
1385
  def _GetLength(getter):
1386
    return lambda ctx, network: len(getter(ctx)[network.uuid])
1387

  
1388
  def _GetSortedList(getter):
1389
    return lambda ctx, network: utils.NiceSort(getter(ctx)[network.uuid])
1390

  
1391
  network_to_groups = operator.attrgetter("network_to_groups")
1392
  network_to_instances = operator.attrgetter("network_to_instances")
1393

  
1394
  # Add fields for node groups
1395
  fields.extend([
1396
    (_MakeField("group_cnt", "Node_Groups", QFT_NUMBER),
1397
     NETQ_GROUP, _GetLength(network_to_groups)),
1398
    (_MakeField("group_links", "Group_Links", QFT_OTHER),
1399
     NETQ_GROUP, _GetSortedList(network_to_groups)),
1400
    ])
1401

  
1402
  # Add fields for instances
1403
  fields.extend([
1404
    (_MakeField("inst_cnt", "Instances", QFT_NUMBER),
1405
     NETQ_INST, _GetLength(network_to_instances)),
1406
    (_MakeField("inst_list", "InstanceList", QFT_OTHER),
1407
     NETQ_INST, _GetSortedList(network_to_instances)),
1408
    ])
1409

  
1410
  # Add fields for usage statistics
1411
  fields.extend([
1412
    (_MakeField(name, title, kind), NETQ_STATS,
1413
    compat.partial(_GetNetworkStatsField, name, kind))
1414
    for (name, (title, kind)) in _NETWORK_STATS_FIELDS.items()
1415
    ])
1416

  
1417
  return _PrepareFieldList(fields, [])
1418

  
1419

  
1296 1420
#: Fields available for node queries
1297 1421
NODE_FIELDS = _BuildNodeFields()
1298 1422

  
......
1305 1429
#: Fields available for node group queries
1306 1430
GROUP_FIELDS = _BuildGroupFields()
1307 1431

  
1432
#: Fields available for network queries
1433
NETWORK_FIELDS = _BuildNetworkFields()
1434

  
1308 1435
#: All available field lists
1309
ALL_FIELD_LISTS = [NODE_FIELDS, INSTANCE_FIELDS, LOCK_FIELDS, GROUP_FIELDS]
1436
ALL_FIELD_LISTS = [NODE_FIELDS, INSTANCE_FIELDS, LOCK_FIELDS,
1437
                   GROUP_FIELDS, NETWORK_FIELDS]

Also available in: Unified diff