Revision 1e0d3321

b/lib/cmdlib.py
15471 15471

  
15472 15472

  
15473 15473
class LUNetworkSetParams(LogicalUnit):
15474
  def BuildHooksNodes(self):
15475
    pass
15474
  """Modifies the parameters of a network.
15475

  
15476
  """
15477
  HPATH = "network-modify"
15478
  HTYPE = constants.HTYPE_NETWORK
15479
  REQ_BGL = False
15480

  
15481
  def CheckArguments(self):
15482
    if (self.op.gateway and
15483
        (self.op.add_reserved_ips or self.op.remove_reserved_ips)):
15484
      raise errors.OpPrereqError("Cannot modify gateway and reserved ips"
15485
                                 " at once", errors.ECODE_INVAL)
15486

  
15487

  
15488
  def ExpandNames(self):
15489
    self.network_uuid = self.cfg.LookupNetwork(self.op.network_name)
15490
    self.network = self.cfg.GetNetwork(self.network_uuid)
15491
    self.needed_locks = {
15492
      locking.LEVEL_NETWORK: [self.network_uuid],
15493
      }
15494

  
15495

  
15496
    if self.network is None:
15497
      raise errors.OpPrereqError("Could not retrieve network '%s' (UUID: %s)" %
15498
                                 (self.op.network_name, self.network_uuid),
15499
                                 errors.ECODE_INVAL)
15500

  
15501
  def CheckPrereq(self):
15502
    """Check prerequisites.
15503

  
15504
    """
15505
    self.gateway = self.network.gateway
15506
    self.network_type = self.network.network_type
15507
    self.mac_prefix = self.network.mac_prefix
15508
    self.network6 = self.network.network6
15509
    self.gateway6 = self.network.gateway6
15510

  
15511
    self.pool = network.AddressPool(self.network)
15512

  
15513
    if self.op.gateway:
15514
      if self.op.gateway == constants.VALUE_NONE:
15515
        self.gateway = None
15516
      else:
15517
        self.gateway = self.op.gateway
15518
        if self.pool.IsReserved(self.gateway):
15519
          raise errors.OpPrereqError("%s is already reserved" %
15520
                                     self.gateway, errors.ECODE_INVAL)
15521

  
15522
    if self.op.network_type:
15523
      if self.op.network_type == constants.VALUE_NONE:
15524
        self.network_type = None
15525
      else:
15526
        self.network_type = self.op.network_type
15527

  
15528
    if self.op.mac_prefix:
15529
      if self.op.mac_prefix == constants.VALUE_NONE:
15530
        self.mac_prefix = None
15531
      else:
15532
        self.mac_prefix = self.op.mac_prefix
15533

  
15534
    if self.op.gateway6:
15535
      if self.op.gateway6 == constants.VALUE_NONE:
15536
        self.gateway6 = None
15537
      else:
15538
        self.gateway6 = self.op.gateway6
15539

  
15540
    if self.op.network6:
15541
      if self.op.network6 == constants.VALUE_NONE:
15542
        self.network6 = None
15543
      else:
15544
        self.network6 = self.op.network6
15545

  
15546

  
15476 15547

  
15477 15548
  def BuildHooksEnv(self):
15478
    pass
15549
    """Build hooks env.
15550

  
15551
    """
15552
    env = {
15553
      "NETWORK_NAME": self.op.network_name,
15554
      "NETWORK_SUBNET": self.network.network,
15555
      "NETWORK_GATEWAY": self.gateway,
15556
      "NETWORK_SUBNET6": self.network6,
15557
      "NETWORK_GATEWAY6": self.gateway6,
15558
      "NETWORK_MAC_PREFIX": self.mac_prefix,
15559
      "NETWORK_TYPE": self.network_type,
15560
      }
15561
    return env
15562

  
15563
  def BuildHooksNodes(self):
15564
    """Build hooks nodes.
15565

  
15566
    """
15567
    mn = self.cfg.GetMasterNode()
15568
    return ([mn], [mn])
15569

  
15570
  def Exec(self, feedback_fn):
15571
    """Modifies the network.
15572

  
15573
    """
15574
    #TODO: reserve/release via temporary reservation manager
15575
    #      extend cfg.ReserveIp/ReleaseIp with the external flag
15576
    if self.op.gateway:
15577
      if self.gateway == self.network.gateway:
15578
        self.LogWarning("Gateway is already %s" % self.gateway)
15579
      else:
15580
        if self.gateway:
15581
          self.pool.Reserve(self.gateway, external=True)
15582
        if self.network.gateway:
15583
          self.pool.Release(self.network.gateway, external=True)
15584
        self.network.gateway = self.gateway
15585

  
15586
    if self.op.add_reserved_ips:
15587
      for ip in self.op.add_reserved_ips:
15588
        try:
15589
          if self.pool.IsReserved(ip):
15590
            self.LogWarning("IP %s is already reserved" % ip)
15591
          else:
15592
            self.pool.Reserve(ip, external=True)
15593
        except errors.AddressPoolError, e:
15594
          self.LogWarning("Cannot reserve ip %s. %s" % (ip, e))
15595

  
15596
    if self.op.remove_reserved_ips:
15597
      for ip in self.op.remove_reserved_ips:
15598
        if ip == self.network.gateway:
15599
          self.LogWarning("Cannot unreserve Gateway's IP")
15600
          continue
15601
        try:
15602
          if not self.pool.IsReserved(ip):
15603
            self.LogWarning("IP %s is already unreserved" % ip)
15604
          else:
15605
            self.pool.Release(ip, external=True)
15606
        except errors.AddressPoolError, e:
15607
          self.LogWarning("Cannot release ip %s. %s" % (ip, e))
15608

  
15609
    if self.op.mac_prefix:
15610
      self.network.mac_prefix = self.mac_prefix
15611

  
15612
    if self.op.network6:
15613
      self.network.network6 = self.network6
15614

  
15615
    if self.op.gateway6:
15616
      self.network.gateway6 = self.gateway6
15617

  
15618
    if self.op.network_type:
15619
      self.network.network_type = self.network_type
15620

  
15621
    self.pool.Validate()
15622

  
15623
    self.cfg.Update(self.network, feedback_fn)
15479 15624

  
15480 15625

  
15481 15626
class _NetworkQuery(_QueryBase):
b/lib/config.py
2224 2224
      test = target in self._config_data.instances.values()
2225 2225
    elif isinstance(target, objects.NodeGroup):
2226 2226
      test = target in self._config_data.nodegroups.values()
2227
    elif isinstance(target, objects.Network):
2228
      test = target in self._config_data.networks.values()
2227 2229
    else:
2228 2230
      raise errors.ProgrammerError("Invalid object type (%s) passed to"
2229 2231
                                   " ConfigWriter.Update" % type(target))

Also available in: Unified diff