|
|
@ -1,6 +1,7 @@
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2019 Red Hat
|
|
|
|
# Copyright 2019 Red Hat
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# GNU General Public License v3.0+
|
|
|
|
|
|
|
|
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
The eos_interfaces class
|
|
|
|
The eos_interfaces class
|
|
|
|
It is in this file where the current configuration (as dict)
|
|
|
|
It is in this file where the current configuration (as dict)
|
|
|
@ -12,10 +13,10 @@ created
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.network.common.utils import to_list, param_list_to_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.network.common.cfg.base import ConfigBase
|
|
|
|
from ansible.module_utils.network.common.cfg.base import ConfigBase
|
|
|
|
|
|
|
|
from ansible.module_utils.network.common.utils import to_list, dict_diff, param_list_to_dict
|
|
|
|
from ansible.module_utils.network.eos.facts.facts import Facts
|
|
|
|
from ansible.module_utils.network.eos.facts.facts import Facts
|
|
|
|
|
|
|
|
from ansible.module_utils.network.eos.utils.utils import normalize_interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Interfaces(ConfigBase):
|
|
|
|
class Interfaces(ConfigBase):
|
|
|
@ -93,148 +94,143 @@ class Interfaces(ConfigBase):
|
|
|
|
:returns: the commands necessary to migrate the current configuration
|
|
|
|
:returns: the commands necessary to migrate the current configuration
|
|
|
|
to the desired configuration
|
|
|
|
to the desired configuration
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
state = self._module.params['state']
|
|
|
|
want = param_list_to_dict(want)
|
|
|
|
want = param_list_to_dict(want)
|
|
|
|
have = param_list_to_dict(have)
|
|
|
|
have = param_list_to_dict(have)
|
|
|
|
state = self._module.params['state']
|
|
|
|
|
|
|
|
if state == 'overridden':
|
|
|
|
if state == 'overridden':
|
|
|
|
commands = state_overridden(want, have)
|
|
|
|
commands = self._state_overridden(want, have)
|
|
|
|
elif state == 'deleted':
|
|
|
|
elif state == 'deleted':
|
|
|
|
commands = state_deleted(want, have)
|
|
|
|
commands = self._state_deleted(want, have)
|
|
|
|
elif state == 'merged':
|
|
|
|
elif state == 'merged':
|
|
|
|
commands = state_merged(want, have)
|
|
|
|
commands = self._state_merged(want, have)
|
|
|
|
elif state == 'replaced':
|
|
|
|
elif state == 'replaced':
|
|
|
|
commands = state_replaced(want, have)
|
|
|
|
commands = self._state_replaced(want, have)
|
|
|
|
return commands
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def _state_replaced(want, have):
|
|
|
|
|
|
|
|
""" The command generator when state is replaced
|
|
|
|
|
|
|
|
|
|
|
|
def state_replaced(want, have):
|
|
|
|
:rtype: A list
|
|
|
|
""" The command generator when state is replaced
|
|
|
|
:returns: the commands necessary to migrate the current configuration
|
|
|
|
|
|
|
|
to the desired configuration
|
|
|
|
:rtype: A list
|
|
|
|
"""
|
|
|
|
:returns: the commands necessary to migrate the current configuration
|
|
|
|
commands = []
|
|
|
|
to the desired configuration
|
|
|
|
for key, desired in want.items():
|
|
|
|
"""
|
|
|
|
interface_name = normalize_interface(key)
|
|
|
|
commands = _compute_commands(want, have, replace=True, remove=True)
|
|
|
|
if interface_name in have:
|
|
|
|
|
|
|
|
extant = have[interface_name]
|
|
|
|
replace = commands['replace']
|
|
|
|
else:
|
|
|
|
remove = commands['remove']
|
|
|
|
extant = dict()
|
|
|
|
|
|
|
|
|
|
|
|
commands_by_interface = replace
|
|
|
|
|
|
|
|
for interface, commands in remove.items():
|
|
|
|
|
|
|
|
commands_by_interface[interface] = replace.get(interface, []) + commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return _flatten_commands(commands_by_interface)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def state_overridden(want, have):
|
|
|
|
|
|
|
|
""" The command generator when state is overridden
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:rtype: A list
|
|
|
|
|
|
|
|
:returns: the commands necessary to migrate the current configuration
|
|
|
|
|
|
|
|
to the desired configuration
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Add empty desired state for unspecified interfaces
|
|
|
|
|
|
|
|
for key in have:
|
|
|
|
|
|
|
|
if key not in want:
|
|
|
|
|
|
|
|
want[key] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Otherwise it's the same as replaced
|
|
|
|
add_config = dict_diff(extant, desired)
|
|
|
|
return state_replaced(want, have)
|
|
|
|
del_config = dict_diff(desired, extant)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
commands.extend(generate_commands(key, add_config, del_config))
|
|
|
|
|
|
|
|
|
|
|
|
def state_merged(want, have):
|
|
|
|
return commands
|
|
|
|
""" The command generator when state is merged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:rtype: A list
|
|
|
|
@staticmethod
|
|
|
|
:returns: the commands necessary to merge the provided into
|
|
|
|
def _state_overridden(want, have):
|
|
|
|
the current configuration
|
|
|
|
""" The command generator when state is overridden
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = _compute_commands(want, have, replace=True)
|
|
|
|
|
|
|
|
return _flatten_commands(commands['replace'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:rtype: A list
|
|
|
|
|
|
|
|
:returns: the commands necessary to migrate the current configuration
|
|
|
|
|
|
|
|
to the desired configuration
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
for key, extant in have.items():
|
|
|
|
|
|
|
|
if key in want:
|
|
|
|
|
|
|
|
desired = want[key]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
desired = dict()
|
|
|
|
|
|
|
|
|
|
|
|
def state_deleted(want, have):
|
|
|
|
add_config = dict_diff(extant, desired)
|
|
|
|
""" The command generator when state is deleted
|
|
|
|
del_config = dict_diff(desired, extant)
|
|
|
|
|
|
|
|
|
|
|
|
:rtype: A list
|
|
|
|
commands.extend(generate_commands(key, add_config, del_config))
|
|
|
|
:returns: the commands necessary to remove the current configuration
|
|
|
|
|
|
|
|
of the provided objects
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = _compute_commands(want, have, remove=True)
|
|
|
|
|
|
|
|
return _flatten_commands(commands['remove'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
def _compute_commands(want, have, replace=False, remove=False):
|
|
|
|
@staticmethod
|
|
|
|
replace_params = {}
|
|
|
|
def _state_merged(want, have):
|
|
|
|
remove_params = {}
|
|
|
|
""" The command generator when state is merged
|
|
|
|
for name, config in want.items():
|
|
|
|
|
|
|
|
extant = have.get(name, {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if remove:
|
|
|
|
:rtype: A list
|
|
|
|
remove_params[name] = dict(set(extant.items()).difference(config.items()))
|
|
|
|
:returns: the commands necessary to merge the provided into
|
|
|
|
if replace:
|
|
|
|
the current configuration
|
|
|
|
replace_params[name] = dict(set(config.items()).difference(extant.items()))
|
|
|
|
"""
|
|
|
|
if remove:
|
|
|
|
commands = []
|
|
|
|
# We won't need to also clear the configuration if we've
|
|
|
|
for key, desired in want.items():
|
|
|
|
# already set it to something
|
|
|
|
interface_name = normalize_interface(key)
|
|
|
|
for param in replace_params[name]:
|
|
|
|
if interface_name in have:
|
|
|
|
remove_params[name].pop(param, None)
|
|
|
|
extant = have[interface_name]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
extant = dict()
|
|
|
|
|
|
|
|
|
|
|
|
returns = {}
|
|
|
|
add_config = dict_diff(extant, desired)
|
|
|
|
if replace:
|
|
|
|
|
|
|
|
returns['replace'] = _replace_config(replace_params)
|
|
|
|
|
|
|
|
if remove:
|
|
|
|
|
|
|
|
returns['remove'] = _remove_config(remove_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return returns
|
|
|
|
commands.extend(generate_commands(key, add_config, {}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_config(params):
|
|
|
|
@staticmethod
|
|
|
|
"""
|
|
|
|
def _state_deleted(want, have):
|
|
|
|
Generates commands to reset config to defaults based on keys provided.
|
|
|
|
""" The command generator when state is deleted
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = {}
|
|
|
|
|
|
|
|
for interface, config in params.items():
|
|
|
|
|
|
|
|
interface_commands = []
|
|
|
|
|
|
|
|
for param in config:
|
|
|
|
|
|
|
|
if param == 'enabled':
|
|
|
|
|
|
|
|
interface_commands.append('no shutdown')
|
|
|
|
|
|
|
|
elif param in ('description', 'mtu'):
|
|
|
|
|
|
|
|
interface_commands.append('no {0}'.format(param))
|
|
|
|
|
|
|
|
elif param == 'speed':
|
|
|
|
|
|
|
|
interface_commands.append('speed auto')
|
|
|
|
|
|
|
|
if interface_commands:
|
|
|
|
|
|
|
|
commands[interface] = interface_commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
:rtype: A list
|
|
|
|
|
|
|
|
:returns: the commands necessary to remove the current configuration
|
|
|
|
|
|
|
|
of the provided objects
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
for key in want:
|
|
|
|
|
|
|
|
desired = dict()
|
|
|
|
|
|
|
|
if key in have:
|
|
|
|
|
|
|
|
extant = have[key]
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
del_config = dict_diff(desired, extant)
|
|
|
|
|
|
|
|
|
|
|
|
def _replace_config(params):
|
|
|
|
commands.extend(generate_commands(key, {}, del_config))
|
|
|
|
"""
|
|
|
|
|
|
|
|
Generates commands to replace config to new values based on provided dictionary.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = {}
|
|
|
|
|
|
|
|
for interface, config in params.items():
|
|
|
|
|
|
|
|
interface_commands = []
|
|
|
|
|
|
|
|
for param, state in config.items():
|
|
|
|
|
|
|
|
if param == 'description':
|
|
|
|
|
|
|
|
interface_commands.append('description "{0}"'.format(state))
|
|
|
|
|
|
|
|
elif param == 'enabled':
|
|
|
|
|
|
|
|
interface_commands.append('{0}shutdown'.format('no ' if state else ''))
|
|
|
|
|
|
|
|
elif param == 'mtu':
|
|
|
|
|
|
|
|
interface_commands.append('mtu {0}'.format(state))
|
|
|
|
|
|
|
|
if 'speed' in config:
|
|
|
|
|
|
|
|
interface_commands.append('speed {0}{1}'.format(config['speed'], config['duplex']))
|
|
|
|
|
|
|
|
if interface_commands:
|
|
|
|
|
|
|
|
commands[interface] = interface_commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _flatten_commands(command_dict):
|
|
|
|
def generate_commands(interface, to_set, to_remove):
|
|
|
|
commands = []
|
|
|
|
commands = []
|
|
|
|
for interface, interface_commands in command_dict.items():
|
|
|
|
for key, value in to_set.items():
|
|
|
|
commands.append('interface {0}'.format(interface))
|
|
|
|
if value is None:
|
|
|
|
commands.extend(interface_commands)
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if key == "enabled":
|
|
|
|
|
|
|
|
commands.append('{0}shutdown'.format('no ' if value else ''))
|
|
|
|
|
|
|
|
elif key == "speed":
|
|
|
|
|
|
|
|
if value == "auto":
|
|
|
|
|
|
|
|
commands.append("{0} {1}".format(key, value))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
commands.append('speed {0}{1}'.format(value, to_set['duplex']))
|
|
|
|
|
|
|
|
elif key == "duplex":
|
|
|
|
|
|
|
|
# duplex is handled with speed
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
commands.append("{0} {1}".format(key, value))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Don't try to also remove the same key, if present in to_remove
|
|
|
|
|
|
|
|
to_remove.pop(key, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for key in to_remove.keys():
|
|
|
|
|
|
|
|
if key == "enabled":
|
|
|
|
|
|
|
|
commands.append('no shutdown')
|
|
|
|
|
|
|
|
elif key == "speed":
|
|
|
|
|
|
|
|
commands.append("speed auto")
|
|
|
|
|
|
|
|
elif key == "duplex":
|
|
|
|
|
|
|
|
# duplex is handled with speed
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
commands.append("no {0}".format(key))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if commands:
|
|
|
|
|
|
|
|
commands.insert(0, "interface {0}".format(interface))
|
|
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
return commands
|
|
|
|