Revision 8ff462ac lib/config.py

b/lib/config.py
51 51
from ganeti import netutils
52 52
from ganeti import runtime
53 53
from ganeti import pathutils
54
from ganeti import network
54 55

  
55 56

  
56 57
_config_lock = locking.SharedLock("ConfigWriter")
......
2094 2095
    nodegroups = ["%s %s" % (nodegroup.uuid, nodegroup.name) for nodegroup in
2095 2096
                  self._config_data.nodegroups.values()]
2096 2097
    nodegroups_data = fn(utils.NiceSort(nodegroups))
2098
    networks = ["%s %s" % (net.uuid, net.name) for net in
2099
                self._config_data.networks.values()]
2100
    networks_data = fn(utils.NiceSort(networks))
2097 2101

  
2098 2102
    ssconf_values = {
2099 2103
      constants.SS_CLUSTER_NAME: cluster.cluster_name,
......
2118 2122
      constants.SS_MAINTAIN_NODE_HEALTH: str(cluster.maintain_node_health),
2119 2123
      constants.SS_UID_POOL: uid_pool,
2120 2124
      constants.SS_NODEGROUPS: nodegroups_data,
2125
      constants.SS_NETWORKS: networks_data,
2121 2126
      }
2122 2127
    bad_values = [(k, v) for k, v in ssconf_values.items()
2123 2128
                  if not isinstance(v, (str, basestring))]
......
2245 2250
    """
2246 2251
    for rm in self._all_rms:
2247 2252
      rm.DropECReservations(ec_id)
2253

  
2254
  @locking.ssynchronized(_config_lock, shared=1)
2255
  def GetAllNetworksInfo(self):
2256
    """Get the configuration of all networks
2257

  
2258
    """
2259
    return dict(self._config_data.networks)
2260

  
2261
  def _UnlockedGetNetworkList(self):
2262
    """Get the list of networks.
2263

  
2264
    This function is for internal use, when the config lock is already held.
2265

  
2266
    """
2267
    return self._config_data.networks.keys()
2268

  
2269
  @locking.ssynchronized(_config_lock, shared=1)
2270
  def GetNetworkList(self):
2271
    """Get the list of networks.
2272

  
2273
    @return: array of networks, ex. ["main", "vlan100", "200]
2274

  
2275
    """
2276
    return self._UnlockedGetNetworkList()
2277

  
2278
  @locking.ssynchronized(_config_lock, shared=1)
2279
  def GetNetworkNames(self):
2280
    """Get a list of network names
2281

  
2282
    """
2283
    names = [network.name
2284
             for network in self._config_data.networks.values()]
2285
    return names
2286

  
2287
  def _UnlockedGetNetwork(self, uuid):
2288
    """Returns information about a network.
2289

  
2290
    This function is for internal use, when the config lock is already held.
2291

  
2292
    """
2293
    if uuid not in self._config_data.networks:
2294
      return None
2295

  
2296
    return self._config_data.networks[uuid]
2297

  
2298
  @locking.ssynchronized(_config_lock, shared=1)
2299
  def GetNetwork(self, uuid):
2300
    """Returns information about a network.
2301

  
2302
    It takes the information from the configuration file.
2303

  
2304
    @param uuid: UUID of the network
2305

  
2306
    @rtype: L{objects.Network}
2307
    @return: the network object
2308

  
2309
    """
2310
    return self._UnlockedGetNetwork(uuid)
2311

  
2312
  @locking.ssynchronized(_config_lock)
2313
  def AddNetwork(self, net, ec_id, check_uuid=True):
2314
    """Add a network to the configuration.
2315

  
2316
    @type net: L{objects.Network}
2317
    @param net: the Network object to add
2318
    @type ec_id: string
2319
    @param ec_id: unique id for the job to use when creating a missing UUID
2320

  
2321
    """
2322
    self._UnlockedAddNetwork(net, ec_id, check_uuid)
2323
    self._WriteConfig()
2324

  
2325
  def _UnlockedAddNetwork(self, net, ec_id, check_uuid):
2326
    """Add a network to the configuration.
2327

  
2328
    """
2329
    logging.info("Adding network %s to configuration", net.name)
2330

  
2331
    if check_uuid:
2332
      self._EnsureUUID(net, ec_id)
2333

  
2334
    existing_uuid = self._UnlockedLookupNetwork(net.name)
2335
    if existing_uuid:
2336
      raise errors.OpPrereqError("Desired network name '%s' already"
2337
                                 " exists as a network (UUID: %s)" %
2338
                                 (net.name, existing_uuid),
2339
                                 errors.ECODE_EXISTS)
2340
    net.serial_no = 1
2341
    self._config_data.networks[net.uuid] = net
2342
    self._config_data.cluster.serial_no += 1
2343

  
2344
  def _UnlockedLookupNetwork(self, target):
2345
    """Lookup a network's UUID.
2346

  
2347
    @type target: string
2348
    @param target: network name or UUID
2349
    @rtype: string
2350
    @return: network UUID
2351
    @raises errors.OpPrereqError: when the target network cannot be found
2352

  
2353
    """
2354
    if target in self._config_data.networks:
2355
      return target
2356
    for net in self._config_data.networks.values():
2357
      if net.name == target:
2358
        return net.uuid
2359
    return None
2360

  
2361
  @locking.ssynchronized(_config_lock, shared=1)
2362
  def LookupNetwork(self, target):
2363
    """Lookup a network's UUID.
2364

  
2365
    This function is just a wrapper over L{_UnlockedLookupNetwork}.
2366

  
2367
    @type target: string
2368
    @param target: network name or UUID
2369
    @rtype: string
2370
    @return: network UUID
2371

  
2372
    """
2373
    return self._UnlockedLookupNetwork(target)
2374

  
2375
  @locking.ssynchronized(_config_lock)
2376
  def RemoveNetwork(self, network_uuid):
2377
    """Remove a network from the configuration.
2378

  
2379
    @type network_uuid: string
2380
    @param network_uuid: the UUID of the network to remove
2381

  
2382
    """
2383
    logging.info("Removing network %s from configuration", network_uuid)
2384

  
2385
    if network_uuid not in self._config_data.networks:
2386
      raise errors.ConfigurationError("Unknown network '%s'" % network_uuid)
2387

  
2388
    del self._config_data.networks[network_uuid]
2389
    self._config_data.cluster.serial_no += 1
2390
    self._WriteConfig()

Also available in: Unified diff