Browse Source

adding custom params

Change-Id: If4b78641097a31ecd471255d534a497b5d1665d4
changes/83/786083/5
Dan Radez 4 weeks ago
parent
commit
90bf6eb269
5 changed files with 63 additions and 31 deletions
  1. +16
    -0
      etc/neutron/plugins/ml2/ml2_conf_ansible.ini.sample
  2. +1
    -0
      networking_ansible/constants.py
  3. +15
    -13
      networking_ansible/ml2/mech_driver.py
  4. +4
    -0
      networking_ansible/tests/unit/base.py
  5. +27
    -18
      networking_ansible/tests/unit/ml2/test_mech_driver.py

+ 16
- 0
etc/neutron/plugins/ml2/ml2_conf_ansible.ini.sample View File

@ -23,6 +23,13 @@ coordination_uri = etcd://127.0.0.1:2379
# - Non-ansible variables used only by net-ansible
# * manage_vlans :: Default: True
# Defines whether to create and delete vlans on the switch.
# - Extra Parameters
# These are standardized parameters used by the network_runner ansible roles
# * stp_edge :: Default: False
# Enable spanning tree protocol on ports
# - Custom parameters are collected and passed directly to the ansible role
# for the platform being configured. These parameters are denoted by the
# prefix cp_ which is stripped off before passed to the ansible role.
#
#########
@ -31,6 +38,7 @@ ansible_network_os=vyos
ansible_host=1.2.3.4
ansible_user=ansible
ansible_ssh_pass=password
stp_edge=True
[ansible:openswitch230_rack_23]
ansible_network_os=openswitch
@ -38,3 +46,11 @@ ansible_host=5.6.7.8
ansible_user=ansible
ansible_ssh_pass=password
manage_vlans=False
[ansible:custom_platform]
ansible_network_os=openswitch
ansible_host=5.6.7.8
ansible_user=ansible
ansible_ssh_pass=password
cp_myparam=myvalue
cp_myparam2=myothervalue

+ 1
- 0
networking_ansible/constants.py View File

@ -17,6 +17,7 @@ COORDINATION = 'networking_ansible.ml2.mech_driver.coordination'
DRIVER_TAG = 'ansible:'
LLI = 'local_link_information'
NETWORKING_ENTITY = 'ANSIBLENETWORKING'
CUSTOM_PARAM_PREFIX = 'cp_'
# values that will be cast to Bool in the conf process
BOOLEANS = ['manage_vlans', 'stp_edge']


+ 15
- 13
networking_ansible/ml2/mech_driver.py View File

@ -62,15 +62,17 @@ class AnsibleMechanismDriver(ml2api.MechanismDriver):
_inv.deserialize({'all': {'hosts': self.ml2config.inventory}})
self.net_runr = net_runr_api.NetworkRunner(_inv)
# build the extra_params dict.
# this holds extra config params per host passed to network runner
self.extra_params = {}
# build the custom params and extra params dict.
# this holds kwargs per host to pass to network runner
self.kwargs = {}
for host_name in self.ml2config.inventory:
self.extra_params[host_name] = {}
for i in c.EXTRA_PARAMS:
if i in self.ml2config.inventory[host_name]:
self.extra_params[host_name][i] = \
self.ml2config.inventory[host_name].get(i)
self.kwargs[host_name] = {}
for key, val in self.ml2config.inventory[host_name].items():
if key in c.EXTRA_PARAMS or \
key.startswith(c.CUSTOM_PARAM_PREFIX):
if key.startswith(c.CUSTOM_PARAM_PREFIX):
key = key[len(c.CUSTOM_PARAM_PREFIX):]
self.kwargs[host_name][key] = val
self.coordinator = coordination.get_coordinator(
cfg.CONF.ml2_ansible.coordination_uri,
@ -139,7 +141,7 @@ class AnsibleMechanismDriver(ml2api.MechanismDriver):
self.net_runr.create_vlan(
host_name,
segmentation_id,
**self.extra_params[host_name])
**self.kwargs[host_name])
LOG.info('Network {net_id}, segmentation '
'{seg} has been added on '
'ansible host {host}'.format(
@ -212,7 +214,7 @@ class AnsibleMechanismDriver(ml2api.MechanismDriver):
self.net_runr.delete_vlan(
host_name,
segmentation_id,
**self.extra_params[host_name])
**self.kwargs[host_name])
LOG.info('Network {net_id} has been deleted on '
'ansible host {host}'.format(
net_id=network['id'],
@ -511,14 +513,14 @@ class AnsibleMechanismDriver(ml2api.MechanismDriver):
switch_port,
segmentation_id,
trunked_vlans,
**self.extra_params[switch_name])
**self.kwargs[switch_name])
else:
self.net_runr.conf_access_port(
switch_name,
switch_port,
segmentation_id,
**self.extra_params[switch_name])
**self.kwargs[switch_name])
LOG.info('Port {neutron_port} has been plugged into '
'switch port {sp} on device {switch_name}'.format(
@ -545,7 +547,7 @@ class AnsibleMechanismDriver(ml2api.MechanismDriver):
try:
self.net_runr.delete_port(switch_name,
switch_port,
**self.extra_params[switch_name])
**self.kwargs[switch_name])
LOG.info('Unplugged port {switch_port} '
'on {switch_name}'.format(switch_port=switch_port,
switch_name=switch_name))


+ 4
- 0
networking_ansible/tests/unit/base.py View File

@ -54,6 +54,10 @@ class MockConfig(object):
for i in self.inventory:
self.inventory[i]['stp_edge'] = True
def add_custom_params(self):
for i in self.inventory:
self.inventory[i]['cp_custom'] = 'param'
class BaseTestCase(base.BaseTestCase):
test_config_files = []


+ 27
- 18
networking_ansible/tests/unit/ml2/test_mech_driver.py View File

@ -267,26 +267,35 @@ class TestDeletePortPostCommit(base.NetworkingAnsibleTestCase):
mock_ensure_port.assert_not_called()
@mock.patch(c.COORDINATION)
@mock.patch('networking_ansible.config.Config')
class TestInit(base.NetworkingAnsibleTestCase):
def test_intialize(self, m_config):
with mock.patch(c.COORDINATION) as m_coord:
m_coord.get_coordinator = lambda *args: mock.create_autospec(
coordination.CoordinationDriver).return_value
m_config.return_value = base.MockConfig()
self.mech.initialize()
m_config.assert_called_once_with()
def test_intialize_w_extra_params(self, m_config):
with mock.patch(c.COORDINATION) as m_coord:
m_coord.get_coordinator = lambda *args: mock.create_autospec(
coordination.CoordinationDriver).return_value
m_config.return_value = base.MockConfig(self.testhost,
self.testmac)
m_config.return_value.add_extra_params()
self.mech.initialize()
assert self.mech.extra_params == {self.testhost:
{'stp_edge': True}}
def test_intialize(self, m_config, m_coord):
m_coord.get_coordinator = lambda *args: mock.create_autospec(
coordination.CoordinationDriver).return_value
m_config.return_value = base.MockConfig()
self.mech.initialize()
m_config.assert_called_once_with()
def test_intialize_w_extra_params(self, m_config, m_coord):
m_coord.get_coordinator = lambda *args: mock.create_autospec(
coordination.CoordinationDriver).return_value
m_config.return_value = base.MockConfig(self.testhost,
self.testmac)
m_config.return_value.add_extra_params()
self.mech.initialize()
self.assertEqual(self.mech.kwargs,
{self.testhost: {'stp_edge': True}})
def test_intialize_w_custom_params(self, m_config, m_coord):
m_coord.get_coordinator = lambda *args: mock.create_autospec(
coordination.CoordinationDriver).return_value
m_config.return_value = base.MockConfig(self.testhost,
self.testmac)
m_config.return_value.add_custom_params()
self.mech.initialize()
self.assertEqual(self.mech.kwargs,
{self.testhost: {'custom': 'param'}})
@mock.patch('networking_ansible.ml2.mech_driver.'


Loading…
Cancel
Save