From 99b261ccccc2f8a08ee2d8fca9f54ef9d69899d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Ollivier?= Date: Fri, 3 Aug 2018 09:15:58 +0200 Subject: [PATCH] Reuse the router passed as config if available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It allows forcing the router and the external network because the current process can easily detect a wrong external network or router. It detects if the router name set in config already exits. If yes, it will be selected and the external network will be its gateway. If the router is not connected to an external network, the process will be unchanged. Then a new router will be created (same name). It doesn't change the purpose of reuse_network_name (provider networks). Change-Id: I50069118fa7020900bea3dd579db18d087d3e47f Signed-off-by: Cédric Ollivier --- vmtp/network.py | 58 +++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/vmtp/network.py b/vmtp/network.py index 52b3b14..75d4018 100644 --- a/vmtp/network.py +++ b/vmtp/network.py @@ -57,38 +57,54 @@ class Network(object): raise vmtp.VmtpException("Unable to find the network to be reused.") return else: + ########################################## + # If the user defined router is available, + # then select it and its gateway + ########################################## + routers = neutron_client.list_routers( + name=config.router_name)['routers'] + if routers and routers[0].get("external_gateway_info"): + self.ext_router = routers[0] + ext_net = self.ext_router["external_gateway_info"][ + 'network_id'] + self.ext_net = neutron_client.show_network(ext_net)['network'] + LOG.info('Using external router: %s', self.ext_router['name']) + ############################################## # If a user provided ext_net_name is not available, # then find the first network that is external ############################################## - for network in self.networks: - if network['router:external']: - try: - if network['name'] == config.ext_net_name: + if not self.ext_net: + for network in self.networks: + if network['router:external']: + try: + if network['name'] == config.ext_net_name: + self.ext_net = network + break + if not self.ext_net: + self.ext_net = network + except AttributeError: + ############################################### + # A attribute error indicates, no user defined + # external network defined, so use the first one + ############################################### self.ext_net = network break - if not self.ext_net: - self.ext_net = network - except AttributeError: - ############################################### - # A attribute error indicates, no user defined - # external network defined, so use the first one - ############################################### - self.ext_net = network - break if self.ext_net: LOG.info("Using external network: %s.", self.ext_net['name']) # Find or create the router to the external network ext_net_id = self.ext_net['id'] - routers = neutron_client.list_routers()['routers'] - for router in routers: - external_gw_info = router['external_gateway_info'] - if external_gw_info: - if external_gw_info['network_id'] == ext_net_id: - self.ext_router = router - LOG.info('Found external router: %s', self.ext_router['name']) - break + if not self.ext_router: + routers = neutron_client.list_routers()['routers'] + for router in routers: + external_gw_info = router['external_gateway_info'] + if external_gw_info: + if external_gw_info['network_id'] == ext_net_id: + self.ext_router = router + LOG.info('Found external router: %s', + self.ext_router['name']) + break # create a new external router if none found and a name was given self.ext_router_name = config.router_name