Configurable environment's default network config

Network configuration is extracted into a separate file.
By default this is netconfig.yaml residing near murano.conf
But the name and path can be changed in config file.
Example of net-config can be found in etc/murano/netconfig.yaml.sample
If no file present old behavior is kept

Change-Id: I7b74eea69ee2ffe1c721b751e564b54252dcfbe3
Implements: blueprint configure-environment-network-defaults
This commit is contained in:
Stan Lagun 2015-02-04 03:39:49 +03:00
parent 38993f07a6
commit 0d42f01f68
6 changed files with 85 additions and 15 deletions

View File

@ -0,0 +1,8 @@
environment:
?:
type: io.murano.resources.ExistingNeutronNetwork
internalNetworkName: internal
# internalSubnetworkName: subnet1
# externalNetworkName: ext_net
flat: null

View File

@ -176,6 +176,10 @@ networking_opts = [
cfg.BoolOpt('create_router', default=True,
help='This option will create a router when one with '
'"router_name" does not exist'),
cfg.StrOpt('network_config_file', default='netconfig.yaml',
help='If provided networking configuration will be taken '
'from this file')
]
stats_opts = [
cfg.IntOpt('period', default=5,

View File

@ -12,6 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import yaml
from murano.common import config
from murano.common import uuidutils
from murano.db import models
from murano.db.services import sessions
@ -19,10 +22,7 @@ from murano.db import session as db_session
from murano.services import states
DEFAULT_NETWORKS = {
'environment': 'io.murano.resources.NeutronNetwork',
# 'flat': 'io.murano.resources.ExistingNetworkConnector'
}
DEFAULT_NETWORK_TYPE = 'io.murano.resources.NeutronNetwork'
class EnvironmentServices(object):
@ -91,8 +91,9 @@ class EnvironmentServices(object):
'id': uuidutils.generate_uuid(),
}}
objects.update(environment_params)
objects.update(
EnvironmentServices.generate_default_networks(objects['name']))
if not objects.get('defaultNetworks'):
objects['defaultNetworks'] = \
EnvironmentServices.generate_default_networks(objects['name'])
objects['?']['type'] = 'io.murano.Environment'
environment_params['tenant_id'] = tenant_id
@ -196,22 +197,29 @@ class EnvironmentServices(object):
@staticmethod
def generate_default_networks(env_name):
net_config = config.CONF.find_file(
config.CONF.networking.network_config_file)
if net_config:
with open(net_config) as f:
data = yaml.safe_load(f)
return EnvironmentServices._objectify(data, {
'ENV': env_name
})
# TODO(ativelkov):
# This is a temporary workaround. Need to find a better way:
# These objects have to be created in runtime when the environment is
# deployed for the first time. Currently there is no way to persist
# such changes, so we have to create the objects on the API side
return {
'defaultNetworks': {
'environment': {
'?': {
'id': uuidutils.generate_uuid(),
'type': DEFAULT_NETWORKS['environment']
},
'name': env_name + '-network'
'environment': {
'?': {
'id': uuidutils.generate_uuid(),
'type': DEFAULT_NETWORK_TYPE
},
'flat': None
}
'name': env_name + '-network'
},
'flat': None
}
@staticmethod
@ -224,3 +232,21 @@ class EnvironmentServices(object):
EnvironmentServices.remove(session.environment_id)
else:
sessions.SessionServices.deploy(session, environment, unit, token)
@staticmethod
def _objectify(data, replacements):
if isinstance(data, dict):
if isinstance(data.get('?'), dict):
data['?']['id'] = uuidutils.generate_uuid()
result = {}
for key, value in data.iteritems():
result[key] = EnvironmentServices._objectify(
value, replacements)
return result
elif isinstance(data, list):
return [EnvironmentServices._objectify(v, replacements)
for v in data]
elif isinstance(data, (str, unicode)):
for key, value in replacements.iteritems():
data = data.replace('%' + key + '%', value)
return data

View File

@ -15,9 +15,11 @@
import json
from oslo.config import cfg
from oslo.utils import timeutils
from murano.api.v1 import templates
from murano.common import config
from murano.db import models
import murano.tests.unit.api.base as tb
import murano.tests.unit.utils as test_utils
@ -422,6 +424,13 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase):
def test_create_environment(self):
"""Test that environment is created, session configured."""
opts = [
cfg.StrOpt('config_dir'),
cfg.StrOpt('config_file', default='murano.conf'),
cfg.StrOpt('project', default='murano'),
]
config.CONF.register_opts(opts)
self._set_policy_rules(
{'create_env_template': '@',
'create_environment': '@'}
@ -445,6 +454,12 @@ class TestEnvTemplateApi(tb.ControllerTest, tb.MuranoApiTestCase):
"""Test that environment is created and session with template
without services.
"""
opts = [
cfg.StrOpt('config_dir'),
cfg.StrOpt('config_file', default='murano.conf'),
cfg.StrOpt('project', default='murano'),
]
config.CONF.register_opts(opts)
self._set_policy_rules(
{'create_env_template': '@',
'create_environment': '@'}

View File

@ -15,9 +15,11 @@
import json
from oslo.config import cfg
from oslo.utils import timeutils
from murano.api.v1 import environments
from murano.common import config
from murano.db import models
import murano.tests.unit.api.base as tb
import murano.tests.unit.utils as test_utils
@ -41,6 +43,12 @@ class TestEnvironmentApi(tb.ControllerTest, tb.MuranoApiTestCase):
def test_create_environment(self):
"""Create an environment, test environment.show()."""
opts = [
cfg.StrOpt('config_dir'),
cfg.StrOpt('config_file', default='murano.conf'),
cfg.StrOpt('project', default='murano'),
]
config.CONF.register_opts(opts)
self._set_policy_rules(
{'list_environments': '@',
'create_environment': '@',

View File

@ -15,8 +15,11 @@
import json
from oslo.config import cfg
from murano.api.v1 import environments
from murano.api.v1 import sessions
from murano.common import config
import murano.tests.unit.api.base as tb
@ -35,6 +38,12 @@ class TestSessionsApi(tb.ControllerTest, tb.MuranoApiTestCase):
"""
CREDENTIALS_1 = {'tenant': 'test_tenant_1', 'user': 'test_user_1'}
CREDENTIALS_2 = {'tenant': 'test_tenant_2', 'user': 'test_user_2'}
opts = [
cfg.StrOpt('config_dir'),
cfg.StrOpt('config_file', default='murano.conf'),
cfg.StrOpt('project', default='murano'),
]
config.CONF.register_opts(opts)
self._set_policy_rules(
{'create_environment': '@'}