Revision 306bed0e lib/cmdlib.py

b/lib/cmdlib.py
15479 15479

  
15480 15480

  
15481 15481
class _NetworkQuery(_QueryBase):
15482
  FIELDS = query.NETWORK_FIELDS
15483

  
15482 15484
  def ExpandNames(self, lu):
15483
    pass
15485
    lu.needed_locks = {}
15486

  
15487
    self._all_networks = lu.cfg.GetAllNetworksInfo()
15488
    name_to_uuid = dict((n.name, n.uuid) for n in self._all_networks.values())
15489

  
15490
    if not self.names:
15491
      self.wanted = [name_to_uuid[name]
15492
                     for name in utils.NiceSort(name_to_uuid.keys())]
15493
    else:
15494
      # Accept names to be either names or UUIDs.
15495
      missing = []
15496
      self.wanted = []
15497
      all_uuid = frozenset(self._all_networks.keys())
15498

  
15499
      for name in self.names:
15500
        if name in all_uuid:
15501
          self.wanted.append(name)
15502
        elif name in name_to_uuid:
15503
          self.wanted.append(name_to_uuid[name])
15504
        else:
15505
          missing.append(name)
15506

  
15507
      if missing:
15508
        raise errors.OpPrereqError("Some networks do not exist: %s" % missing,
15509
                                   errors.ECODE_NOENT)
15484 15510

  
15485 15511
  def DeclareLocks(self, lu, level):
15486 15512
    pass
15487 15513

  
15488 15514
  def _GetQueryData(self, lu):
15489
    pass
15515
    """Computes the list of networks and their attributes.
15516

  
15517
    """
15518
    do_instances = query.NETQ_INST in self.requested_data
15519
    do_groups = do_instances or (query.NETQ_GROUP in self.requested_data)
15520
    do_stats = query.NETQ_STATS in self.requested_data
15521
    cluster = lu.cfg.GetClusterInfo()
15522

  
15523
    network_to_groups = None
15524
    network_to_instances = None
15525
    stats = None
15526

  
15527
    # For NETQ_GROUP, we need to map network->[groups]
15528
    if do_groups:
15529
      all_groups = lu.cfg.GetAllNodeGroupsInfo()
15530
      network_to_groups = dict((uuid, []) for uuid in self.wanted)
15531
      default_nicpp = cluster.nicparams[constants.PP_DEFAULT]
15532

  
15533
      if do_instances:
15534
        all_instances = lu.cfg.GetAllInstancesInfo()
15535
        all_nodes = lu.cfg.GetAllNodesInfo()
15536
        network_to_instances = dict((uuid, []) for uuid in self.wanted)
15537

  
15538

  
15539
      for group in all_groups.values():
15540
        if do_instances:
15541
          group_nodes = [node.name for node in all_nodes.values() if
15542
                         node.group == group.uuid]
15543
          group_instances = [instance for instance in all_instances.values()
15544
                             if instance.primary_node in group_nodes]
15545

  
15546
        for net_uuid in group.networks.keys():
15547
          if net_uuid in network_to_groups:
15548
            netparams = group.networks[net_uuid]
15549
            mode = netparams[constants.NIC_MODE]
15550
            link = netparams[constants.NIC_LINK]
15551
            info = group.name + '(' + mode + ', ' + link + ')'
15552
            network_to_groups[net_uuid].append(info)
15553

  
15554
            if do_instances:
15555
              for instance in group_instances:
15556
                for nic in instance.nics:
15557
                  if nic.network == self._all_networks[net_uuid].name:
15558
                    network_to_instances[net_uuid].append(instance.name)
15559
                    break
15560

  
15561
    if do_stats:
15562
      stats = {}
15563
      for uuid, net in self._all_networks.items():
15564
        if uuid in self.wanted:
15565
          pool = network.AddressPool(net)
15566
          stats[uuid] = {
15567
            "free_count": pool.GetFreeCount(),
15568
            "reserved_count": pool.GetReservedCount(),
15569
            "map": pool.GetMap(),
15570
            "external_reservations": ", ".join(pool.GetExternalReservations()),
15571
            }
15572

  
15573
    return query.NetworkQueryData([self._all_networks[uuid]
15574
                                   for uuid in self.wanted],
15575
                                   network_to_groups,
15576
                                   network_to_instances,
15577
                                   stats)
15490 15578

  
15491 15579

  
15492 15580
class LUNetworkQuery(NoHooksLU):
15493
  pass
15581
  """Logical unit for querying networks.
15582

  
15583
  """
15584
  REQ_BGL = False
15585

  
15586
  def CheckArguments(self):
15587
    self.nq = _NetworkQuery(qlang.MakeSimpleFilter("name", self.op.names),
15588
                            self.op.output_fields, False)
15589

  
15590
  def ExpandNames(self):
15591
    self.nq.ExpandNames(self)
15592

  
15593
  def Exec(self, feedback_fn):
15594
    return self.nq.OldStyleQuery(self)
15595

  
15494 15596

  
15495 15597

  
15496 15598
class LUNetworkConnect(LogicalUnit):

Also available in: Unified diff