Revision 909064a1 lib/cmdlib.py

b/lib/cmdlib.py
15518 15518

  
15519 15519

  
15520 15520
class _NetworkQuery(_QueryBase):
15521
  FIELDS = query.NETWORK_FIELDS
15522

  
15521 15523
  def ExpandNames(self, lu):
15522
    pass
15524
    lu.needed_locks = {}
15525

  
15526
    self._all_networks = lu.cfg.GetAllNetworksInfo()
15527
    name_to_uuid = dict((n.name, n.uuid) for n in self._all_networks.values())
15528

  
15529
    if not self.names:
15530
      self.wanted = [name_to_uuid[name]
15531
                     for name in utils.NiceSort(name_to_uuid.keys())]
15532
    else:
15533
      # Accept names to be either names or UUIDs.
15534
      missing = []
15535
      self.wanted = []
15536
      all_uuid = frozenset(self._all_networks.keys())
15537

  
15538
      for name in self.names:
15539
        if name in all_uuid:
15540
          self.wanted.append(name)
15541
        elif name in name_to_uuid:
15542
          self.wanted.append(name_to_uuid[name])
15543
        else:
15544
          missing.append(name)
15545

  
15546
      if missing:
15547
        raise errors.OpPrereqError("Some networks do not exist: %s" % missing,
15548
                                   errors.ECODE_NOENT)
15523 15549

  
15524 15550
  def DeclareLocks(self, lu, level):
15525 15551
    pass
15526 15552

  
15527 15553
  def _GetQueryData(self, lu):
15528
    pass
15554
    """Computes the list of networks and their attributes.
15555

  
15556
    """
15557
    do_instances = query.NETQ_INST in self.requested_data
15558
    do_groups = do_instances or (query.NETQ_GROUP in self.requested_data)
15559
    do_stats = query.NETQ_STATS in self.requested_data
15560
    cluster = lu.cfg.GetClusterInfo()
15561

  
15562
    network_to_groups = None
15563
    network_to_instances = None
15564
    stats = None
15565

  
15566
    # For NETQ_GROUP, we need to map network->[groups]
15567
    if do_groups:
15568
      all_groups = lu.cfg.GetAllNodeGroupsInfo()
15569
      network_to_groups = dict((uuid, []) for uuid in self.wanted)
15570
      default_nicpp = cluster.nicparams[constants.PP_DEFAULT]
15571

  
15572
      if do_instances:
15573
        all_instances = lu.cfg.GetAllInstancesInfo()
15574
        all_nodes = lu.cfg.GetAllNodesInfo()
15575
        network_to_instances = dict((uuid, []) for uuid in self.wanted)
15576

  
15577

  
15578
      for group in all_groups.values():
15579
        if do_instances:
15580
          group_nodes = [node.name for node in all_nodes.values() if
15581
                         node.group == group.uuid]
15582
          group_instances = [instance for instance in all_instances.values()
15583
                             if instance.primary_node in group_nodes]
15584

  
15585
        for net_uuid in group.networks.keys():
15586
          if net_uuid in network_to_groups:
15587
            netparams = group.networks[net_uuid]
15588
            mode = netparams[constants.NIC_MODE]
15589
            link = netparams[constants.NIC_LINK]
15590
            info = group.name + '(' + mode + ', ' + link + ')'
15591
            network_to_groups[net_uuid].append(info)
15592

  
15593
            if do_instances:
15594
              for instance in group_instances:
15595
                for nic in instance.nics:
15596
                  if nic.network == self._all_networks[net_uuid].name:
15597
                    network_to_instances[net_uuid].append(instance.name)
15598
                    break
15599

  
15600
    if do_stats:
15601
      stats = {}
15602
      for uuid, net in self._all_networks.items():
15603
        if uuid in self.wanted:
15604
          pool = network.AddressPool(net)
15605
          stats[uuid] = {
15606
            "free_count": pool.GetFreeCount(),
15607
            "reserved_count": pool.GetReservedCount(),
15608
            "map": pool.GetMap(),
15609
            "external_reservations": ", ".join(pool.GetExternalReservations()),
15610
            }
15611

  
15612
    return query.NetworkQueryData([self._all_networks[uuid]
15613
                                   for uuid in self.wanted],
15614
                                   network_to_groups,
15615
                                   network_to_instances,
15616
                                   stats)
15529 15617

  
15530 15618

  
15531 15619
class LUNetworkQuery(NoHooksLU):
15532
  pass
15620
  """Logical unit for querying networks.
15621

  
15622
  """
15623
  REQ_BGL = False
15624

  
15625
  def CheckArguments(self):
15626
    self.nq = _NetworkQuery(qlang.MakeSimpleFilter("name", self.op.names),
15627
                            self.op.output_fields, False)
15628

  
15629
  def ExpandNames(self):
15630
    self.nq.ExpandNames(self)
15631

  
15632
  def Exec(self, feedback_fn):
15633
    return self.nq.OldStyleQuery(self)
15634

  
15533 15635

  
15534 15636

  
15535 15637
class LUNetworkConnect(LogicalUnit):

Also available in: Unified diff