Creates a router if one doesn't exist

If it doesn't find a router by the name specified in router_name
in murano.conf, create a router with that name. Uses
external_network_id or external_network_name as the
external_gateway_info ID. Requires:
https://review.openstack.org/#/c/119800

Change-Id: If8b966a7d43eb2af485113de2a0708e554605725
This commit is contained in:
Ryan Peters 2014-09-15 16:40:32 -05:00
parent f1905be336
commit 09fe0567a2
3 changed files with 53 additions and 2 deletions

View File

@ -760,10 +760,18 @@
# (string value)
#default_dns=8.8.8.8
# ID or name of the external network for routers to connect to
# (string value)
#external_network=ext-net
# Name of the router that going to be used in order to join
# all networks created by Murano (string value)
#router_name=murano-default-router
# This option will create a router when one with "router_name"
# does not exist (boolean value)
#create_router=true
[neutron]

View File

@ -160,9 +160,17 @@ networking_opts = [
help='Default DNS nameserver to be assigned to '
'created Networks'),
cfg.StrOpt('external_network', default='ext-net',
help='ID or name of the external network for routers '
'to connect to'),
cfg.StrOpt('router_name', default='murano-default-router',
help='Name of the router that going to be used in order to '
'join all networks created by Murano')
'join all networks created by Murano'),
cfg.BoolOpt('create_router', default=True,
help='This option will create a router when one with '
'"router_name" does not exist'),
]
stats_opt = [
cfg.IntOpt('period', default=5,

View File

@ -24,6 +24,11 @@ import murano.common.config as config
import murano.dsl.helpers as helpers
import murano.dsl.murano_class as murano_class
import murano.dsl.murano_object as murano_object
from murano.openstack.common import log as logging
import muranoclient.openstack.common.uuidutils as uuidutils
LOG = logging.getLogger(__name__)
@murano_class.classname('io.murano.system.NetworkExplorer')
@ -69,7 +74,37 @@ class NetworkExplorer(murano_object.MuranoObject):
list_routers(tenant_id=self._tenant_id, name=router_name).\
get('routers')
if len(routers) == 0:
raise KeyError('Router %s was not found' % router_name)
LOG.debug('Router {0} not found'.format(router_name))
if self._settings.create_router:
LOG.debug('Attempting to create Router {0}'.
format(router_name))
external_network = self._settings.external_network
kwargs = {'id': external_network} \
if uuidutils.is_uuid_like(external_network) \
else {'name': external_network}
networks = self._neutron.list_networks(**kwargs). \
get('networks')
ext_nets = filter(lambda n: n['router:external'], networks)
if len(ext_nets) == 0:
raise KeyError('Router %s could not be created, '
'no external network found' % router_name)
nid = ext_nets[0]['id']
body_data = {
'router': {
'name': router_name,
'external_gateway_info': {
'network_id': nid
},
'admin_state_up': True,
}
}
router = self._neutron.create_router(body=body_data).\
get('router')
LOG.debug('Created router: {0}'.format(router))
return router['id']
else:
raise KeyError('Router %s was not found' % router_name)
else:
if routers[0]['external_gateway_info'] is None:
raise Exception('Please set external gateway '