+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def GetAllNetworksInfo(self):
+ """Get configuration info of all the networks.
+
+ """
+ return dict(self._config_data.networks)
+
+ def _UnlockedGetNetworkList(self):
+ """Get the list of networks.
+
+ This function is for internal use, when the config lock is already held.
+
+ """
+ return self._config_data.networks.keys()
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def GetNetworkList(self):
+ """Get the list of networks.
+
+ @return: array of networks, ex. ["main", "vlan100", "200]
+
+ """
+ return self._UnlockedGetNetworkList()
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def GetNetworkNames(self):
+ """Get a list of network names
+
+ """
+ names = [net.name
+ for net in self._config_data.networks.values()]
+ return names
+
+ def _UnlockedGetNetwork(self, uuid):
+ """Returns information about a network.
+
+ This function is for internal use, when the config lock is already held.
+
+ """
+ if uuid not in self._config_data.networks:
+ return None
+
+ return self._config_data.networks[uuid]
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def GetNetwork(self, uuid):
+ """Returns information about a network.
+
+ It takes the information from the configuration file.
+
+ @param uuid: UUID of the network
+
+ @rtype: L{objects.Network}
+ @return: the network object
+
+ """
+ return self._UnlockedGetNetwork(uuid)
+
+ @locking.ssynchronized(_config_lock)
+ def AddNetwork(self, net, ec_id, check_uuid=True):
+ """Add a network to the configuration.
+
+ @type net: L{objects.Network}
+ @param net: the Network object to add
+ @type ec_id: string
+ @param ec_id: unique id for the job to use when creating a missing UUID
+
+ """
+ self._UnlockedAddNetwork(net, ec_id, check_uuid)
+ self._WriteConfig()
+
+ def _UnlockedAddNetwork(self, net, ec_id, check_uuid):
+ """Add a network to the configuration.
+
+ """
+ logging.info("Adding network %s to configuration", net.name)
+
+ if check_uuid:
+ self._EnsureUUID(net, ec_id)
+
+ net.serial_no = 1
+ net.ctime = net.mtime = time.time()
+ self._config_data.networks[net.uuid] = net
+ self._config_data.cluster.serial_no += 1
+
+ def _UnlockedLookupNetwork(self, target):
+ """Lookup a network's UUID.
+
+ @type target: string
+ @param target: network name or UUID
+ @rtype: string
+ @return: network UUID
+ @raises errors.OpPrereqError: when the target network cannot be found
+
+ """
+ if target is None:
+ return None
+ if target in self._config_data.networks:
+ return target
+ for net in self._config_data.networks.values():
+ if net.name == target:
+ return net.uuid
+ raise errors.OpPrereqError("Network '%s' not found" % target,
+ errors.ECODE_NOENT)
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def LookupNetwork(self, target):
+ """Lookup a network's UUID.
+
+ This function is just a wrapper over L{_UnlockedLookupNetwork}.
+
+ @type target: string
+ @param target: network name or UUID
+ @rtype: string
+ @return: network UUID
+
+ """
+ return self._UnlockedLookupNetwork(target)
+
+ @locking.ssynchronized(_config_lock)
+ def RemoveNetwork(self, network_uuid):
+ """Remove a network from the configuration.
+
+ @type network_uuid: string
+ @param network_uuid: the UUID of the network to remove
+
+ """
+ logging.info("Removing network %s from configuration", network_uuid)
+
+ if network_uuid not in self._config_data.networks:
+ raise errors.ConfigurationError("Unknown network '%s'" % network_uuid)
+
+ del self._config_data.networks[network_uuid]
+ self._config_data.cluster.serial_no += 1
+ self._WriteConfig()
+
+ def _UnlockedGetGroupNetParams(self, net_uuid, node):
+ """Get the netparams (mode, link) of a network.
+
+ Get a network's netparams for a given node.
+
+ @type net_uuid: string
+ @param net_uuid: network uuid
+ @type node: string
+ @param node: node name
+ @rtype: dict or None
+ @return: netparams
+
+ """
+ node_info = self._UnlockedGetNodeInfo(node)
+ nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
+ netparams = nodegroup_info.networks.get(net_uuid, None)
+
+ return netparams
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def GetGroupNetParams(self, net_uuid, node):
+ """Locking wrapper of _UnlockedGetGroupNetParams()
+
+ """
+ return self._UnlockedGetGroupNetParams(net_uuid, node)
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def CheckIPInNodeGroup(self, ip, node):
+ """Check IP uniqueness in nodegroup.
+
+ Check networks that are connected in the node's node group
+ if ip is contained in any of them. Used when creating/adding
+ a NIC to ensure uniqueness among nodegroups.
+
+ @type ip: string
+ @param ip: ip address
+ @type node: string
+ @param node: node name
+ @rtype: (string, dict) or (None, None)
+ @return: (network name, netparams)
+
+ """
+ if ip is None:
+ return (None, None)
+ node_info = self._UnlockedGetNodeInfo(node)
+ nodegroup_info = self._UnlockedGetNodeGroup(node_info.group)
+ for net_uuid in nodegroup_info.networks.keys():
+ net_info = self._UnlockedGetNetwork(net_uuid)
+ pool = network.AddressPool(net_info)
+ if pool.Contains(ip):
+ return (net_info.name, nodegroup_info.networks[net_uuid])
+
+ return (None, None)