Skip to content

Commit

Permalink
Check and diff modes support for l2- and lag-interfaces modules (#303)
Browse files Browse the repository at this point in the history
* Check and diff modes support for l2- and lag-interfaces modules

* Add fragment file

* Add sanity ignore file
  • Loading branch information
mingjunzhang2019 authored Oct 25, 2023
1 parent 19b30a9 commit 2d7a56f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
major_changes:
- l2_lag_interfaces - Playbook check and diff modes supports for sonic_l2_interfaces and sonic_lag_interfaces modules (https://github.com/ansible-collections/dellemc.enterprise_sonic/pull/303).
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
to_request,
edit_config
)
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.formatted_diff_utils import (
__DELETE_CONFIG,
__DELETE_CONFIG_IF_NO_SUBCONFIG,
get_new_config,
get_formatted_config_diff
)
from ansible.module_utils._text import to_native
from ansible.module_utils.connection import ConnectionError

Expand All @@ -56,6 +62,10 @@
TEST_KEYS = [
{'allowed_vlans': {'vlan': ''}},
]
TEST_KEYS_formatted_diff = [
{'config': {'name': '', '__delete_op': __DELETE_CONFIG}},
{'allowed_vlans': {'vlan': '', '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}},
]


class L2_interfaces(ConfigBase):
Expand Down Expand Up @@ -114,6 +124,18 @@ def execute_module(self):
if result['changed']:
result['after'] = changed_l2_interfaces_facts

new_config = changed_l2_interfaces_facts
old_config = existing_l2_interfaces_facts
if self._module.check_mode:
result.pop('after', None)
new_config = get_new_config(commands, existing_l2_interfaces_facts,
TEST_KEYS_formatted_diff)
result['after(generated)'] = new_config
if self._module._diff:
self.sort_config(new_config)
self.sort_config(old_config)
result['config_diff'] = get_formatted_config_diff(old_config,
new_config)
result['warnings'] = warnings
return result

Expand Down Expand Up @@ -591,3 +613,15 @@ def get_interface_names(configs):
interface_names.add(conf['name'])

return interface_names

def sort_config(self, configs):
# natsort provides better result.
# The use of natsort causes sanity error due to it is not available in
# python version currently used.
# new_config = natsorted(new_config, key=lambda x: x['name'])
# For time-being, use simple "sort"
configs.sort(key=lambda x: x['name'])

for conf in configs:
if conf.get('trunk', {}) and conf['trunk'].get('allowed_vlans', []):
conf['trunk']['allowed_vlans'].sort(key=lambda x: x['vlan'])
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

import json

from copy import (
deepcopy
)
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base import (
ConfigBase,
)
Expand All @@ -39,6 +42,11 @@
to_request,
edit_config
)
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.formatted_diff_utils import (
__DELETE_CONFIG_IF_NO_SUBCONFIG,
get_new_config,
get_formatted_config_diff
)
from ansible.module_utils._text import to_native
from ansible.module_utils.connection import ConnectionError
import traceback
Expand All @@ -60,6 +68,10 @@
TEST_KEYS = [
{'interfaces': {'member': ''}},
]
TEST_KEYS_formatted_diff = [
{'config': {'name': '', '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}},
{'interfaces': {'member': '', '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}},
]


class Lag_interfaces(ConfigBase):
Expand Down Expand Up @@ -119,6 +131,18 @@ def execute_module(self):
if result['changed']:
result['after'] = changed_lag_interfaces_facts

new_config = changed_lag_interfaces_facts
old_config = existing_lag_interfaces_facts
if self._module.check_mode:
result.pop('after', None)
new_config = get_new_config(commands, existing_lag_interfaces_facts,
TEST_KEYS_formatted_diff)
result['after(generated)'] = new_config
if self._module._diff:
self.sort_config(new_config)
self.sort_config(old_config)
result['config_diff'] = get_formatted_config_diff(old_config,
new_config)
result['warnings'] = warnings
return result

Expand Down Expand Up @@ -188,7 +212,7 @@ def _state_replaced(self, want, have, diff_members, diff_portchannels):
replaced_list.append(list_obj)
requests = self.get_delete_lag_interfaces_requests(replaced_list)
if requests:
commands.extend(update_states(replaced_list, "replaced"))
commands.extend(update_states(replaced_list, "deleted"))
replaced_commands, replaced_requests = self.template_for_lag_creation(have, diff_members, diff_portchannels, "replaced")
if replaced_requests:
commands.extend(replaced_commands)
Expand Down Expand Up @@ -226,7 +250,8 @@ def _state_overridden(self, want, have, diff_members, diff_portchannels):

requests_deleted_po = self.get_delete_portchannel_requests(deleted_po_list)
requests.extend(requests_deleted_po)
commands.extend(update_states(deleted_po_list, "deleted"))
commands_del = self.prune_commands(deleted_po_list)
commands.extend(update_states(commands_del, "deleted"))

override_commands, override_requests = self.template_for_lag_creation(have, diff_members, diff_portchannels, "overridden")
commands.extend(override_commands)
Expand Down Expand Up @@ -258,7 +283,8 @@ def _state_deleted(self, want, have, diff):
requests = self.get_delete_all_lag_interfaces_requests()
portchannel_requests = self.get_delete_all_portchannel_requests()
requests.extend(portchannel_requests)
commands.extend(update_states(have, "Deleted"))
commands_del = self.prune_commands(have)
commands.extend(update_states(commands_del, "deleted"))
else: # delete specific lag interfaces and specific portchannels
commands = get_diff(want, diff, TEST_KEYS)
commands = remove_empties_from_list(commands)
Expand Down Expand Up @@ -322,7 +348,8 @@ def template_for_lag_deletion(self, have, delete_members, delete_portchannels, s
commands.extend(update_states(delete_members, state_name))
if delete_portchannels:
portchannel_requests = self.get_delete_portchannel_requests(delete_portchannels)
commands.extend(update_states(delete_portchannels, state_name))
commands_del = self.prune_commands(delete_portchannels)
commands.extend(update_states(commands_del, state_name))
if requests:
requests.extend(portchannel_requests)
else:
Expand Down Expand Up @@ -428,3 +455,21 @@ def get_delete_portchannel_requests(self, commands):
requests.append(request)

return requests

def sort_config(self, configs):
# natsort provides better result.
# The use of natsort causes sanity error due to it is not available in
# python version currently used.
# new_config = natsorted(new_config, key=lambda x: x['name'])
# For time-being, use simple "sort"
configs.sort(key=lambda x: x['name'])

for conf in configs:
if conf.get('members', {}) and conf['members'].get('interfaces', []):
conf['members']['interfaces'].sort(key=lambda x: x['member'])

def prune_commands(self, commands):
cmds = deepcopy(commands)
for cmd in cmds:
cmd.pop('members', None)
return cmds
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.17.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugins/action/sonic.py action-plugin-docs #action plugin for base class

0 comments on commit 2d7a56f

Please sign in to comment.