Revision 7eb01378 lib/cmdlib.py

b/lib/cmdlib.py
15502 15502

  
15503 15503

  
15504 15504
class _NetworkQuery(_QueryBase):
15505
  FIELDS = query.NETWORK_FIELDS
15506

  
15505 15507
  def ExpandNames(self, lu):
15506
    pass
15508
    lu.needed_locks = {}
15509

  
15510
    self._all_networks = lu.cfg.GetAllNetworksInfo()
15511
    name_to_uuid = dict((n.name, n.uuid) for n in self._all_networks.values())
15512

  
15513
    if not self.names:
15514
      self.wanted = [name_to_uuid[name]
15515
                     for name in utils.NiceSort(name_to_uuid.keys())]
15516
    else:
15517
      # Accept names to be either names or UUIDs.
15518
      missing = []
15519
      self.wanted = []
15520
      all_uuid = frozenset(self._all_networks.keys())
15521

  
15522
      for name in self.names:
15523
        if name in all_uuid:
15524
          self.wanted.append(name)
15525
        elif name in name_to_uuid:
15526
          self.wanted.append(name_to_uuid[name])
15527
        else:
15528
          missing.append(name)
15529

  
15530
      if missing:
15531
        raise errors.OpPrereqError("Some networks do not exist: %s" % missing,
15532
                                   errors.ECODE_NOENT)
15507 15533

  
15508 15534
  def DeclareLocks(self, lu, level):
15509 15535
    pass
15510 15536

  
15511 15537
  def _GetQueryData(self, lu):
15512
    pass
15538
    """Computes the list of networks and their attributes.
15539

  
15540
    """
15541
    do_instances = query.NETQ_INST in self.requested_data
15542
    do_groups = do_instances or (query.NETQ_GROUP in self.requested_data)
15543
    do_stats = query.NETQ_STATS in self.requested_data
15544
    cluster = lu.cfg.GetClusterInfo()
15545

  
15546
    network_to_groups = None
15547
    network_to_instances = None
15548
    stats = None
15549

  
15550
    # For NETQ_GROUP, we need to map network->[groups]
15551
    if do_groups:
15552
      all_groups = lu.cfg.GetAllNodeGroupsInfo()
15553
      network_to_groups = dict((uuid, []) for uuid in self.wanted)
15554
      default_nicpp = cluster.nicparams[constants.PP_DEFAULT]
15555

  
15556
      if do_instances:
15557
        all_instances = lu.cfg.GetAllInstancesInfo()
15558
        all_nodes = lu.cfg.GetAllNodesInfo()
15559
        network_to_instances = dict((uuid, []) for uuid in self.wanted)
15560

  
15561

  
15562
      for group in all_groups.values():
15563
        if do_instances:
15564
          group_nodes = [node.name for node in all_nodes.values() if
15565
                         node.group == group.uuid]
15566
          group_instances = [instance for instance in all_instances.values()
15567
                             if instance.primary_node in group_nodes]
15568

  
15569
        for net_uuid in group.networks.keys():
15570
          if net_uuid in network_to_groups:
15571
            netparams = group.networks[net_uuid]
15572
            mode = netparams[constants.NIC_MODE]
15573
            link = netparams[constants.NIC_LINK]
15574
            info = group.name + '(' + mode + ', ' + link + ')'
15575
            network_to_groups[net_uuid].append(info)
15576

  
15577
            if do_instances:
15578
              for instance in group_instances:
15579
                for nic in instance.nics:
15580
                  if nic.network == self._all_networks[net_uuid].name:
15581
                    network_to_instances[net_uuid].append(instance.name)
15582
                    break
15583

  
15584
    if do_stats:
15585
      stats = {}
15586
      for uuid, net in self._all_networks.items():
15587
        if uuid in self.wanted:
15588
          pool = network.AddressPool(net)
15589
          stats[uuid] = {
15590
            "free_count": pool.GetFreeCount(),
15591
            "reserved_count": pool.GetReservedCount(),
15592
            "map": pool.GetMap(),
15593
            "external_reservations": ", ".join(pool.GetExternalReservations()),
15594
            }
15595

  
15596
    return query.NetworkQueryData([self._all_networks[uuid]
15597
                                   for uuid in self.wanted],
15598
                                   network_to_groups,
15599
                                   network_to_instances,
15600
                                   stats)
15513 15601

  
15514 15602

  
15515 15603
class LUNetworkQuery(NoHooksLU):
15516
  pass
15604
  """Logical unit for querying networks.
15605

  
15606
  """
15607
  REQ_BGL = False
15608

  
15609
  def CheckArguments(self):
15610
    self.nq = _NetworkQuery(qlang.MakeSimpleFilter("name", self.op.names),
15611
                            self.op.output_fields, False)
15612

  
15613
  def ExpandNames(self):
15614
    self.nq.ExpandNames(self)
15615

  
15616
  def Exec(self, feedback_fn):
15617
    return self.nq.OldStyleQuery(self)
15618

  
15517 15619

  
15518 15620

  
15519 15621
class LUNetworkConnect(LogicalUnit):

Also available in: Unified diff