Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diff check modes support for vrfs logging and ip_neighbor modules #285

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
major_changes:
- vrfs_logging_ip_neighbor - Playbook check and diff modes supports for sonic_vrfs, sonic_logging and sonic_ip_neighbor modules (https://github.com/ansible-collections/dellemc.enterprise_sonic/pull/285).
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
get_diff,
update_states,
)
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.formatted_diff_utils import (
get_new_config,
get_formatted_config_diff
)
from ansible.module_utils.connection import ConnectionError

GET = 'get'
Expand Down Expand Up @@ -59,6 +63,34 @@
}


def __derive_ip_neighbor_config_delete_op(key_set, command, exist_conf):
new_conf = exist_conf

if 'ipv4_arp_timeout' in command:
new_conf['ipv4_arp_timeout'] = IP_NEIGH_CONFIG_DEFAULT['ipv4_arp_timeout']

if 'ipv4_drop_neighbor_aging_time' in command:
new_conf['ipv4_drop_neighbor_aging_time'] = \
IP_NEIGH_CONFIG_DEFAULT['ipv4_drop_neighbor_aging_time']

if 'ipv6_drop_neighbor_aging_time' in command:
new_conf['ipv6_drop_neighbor_aging_time'] = \
IP_NEIGH_CONFIG_DEFAULT['ipv6_drop_neighbor_aging_time']

if 'ipv6_nd_cache_expiry' in command:
new_conf['ipv6_nd_cache_expiry'] = IP_NEIGH_CONFIG_DEFAULT['ipv6_nd_cache_expiry']

if 'num_local_neigh' in command:
new_conf['num_local_neigh'] = IP_NEIGH_CONFIG_DEFAULT['num_local_neigh']

return True, new_conf


TEST_KEYS_formatted_diff = [
{'__delete_op_default': {'__delete_op': __derive_ip_neighbor_config_delete_op}},
]


class Ip_neighbor(ConfigBase):
"""
The sonic_ip_neighbor class
Expand Down Expand Up @@ -130,6 +162,16 @@ def execute_module(self):
if result['changed']:
result['after'] = changed_ip_neighbor_facts

new_config = changed_ip_neighbor_facts
if self._module.check_mode:
result.pop('after', None)
new_config = get_new_config(commands, existing_ip_neighbor_facts,
TEST_KEYS_formatted_diff)
result['after(generated)'] = new_config

if self._module._diff:
result['config_diff'] = get_formatted_config_diff(existing_ip_neighbor_facts,
new_config)
result['warnings'] = warnings
return result

Expand Down Expand Up @@ -255,7 +297,7 @@ def _state_overridden(self, want, have):
requests = self.build_merge_requests(commands)

if len(requests) > 0:
commands = update_states(commands, "overriden")
commands = update_states(commands, "overridden")
else:
commands = []

Expand Down
65 changes: 42 additions & 23 deletions plugins/module_utils/network/sonic/config/logging/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.utils import (
get_diff,
update_states,
send_requests,
get_normalize_interface_name,
)
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.connection import ConnectionError

PATCH = 'PATCH'
Expand All @@ -44,6 +48,11 @@
"remote_servers": {"host": ""}
}
]
TEST_KEYS_formatted_diff = [
{
"remote_servers": {"host": "", '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}
}
]


class Logging(ConfigBase):
Expand Down Expand Up @@ -104,6 +113,16 @@ def execute_module(self):
if result['changed']:
result['after'] = changed_logging_facts

new_config = changed_logging_facts
if self._module.check_mode:
result.pop('after', None)
new_config = get_new_config(commands, existing_logging_facts,
TEST_KEYS_formatted_diff)
result['after(generated)'] = new_config

if self._module._diff:
result['config_diff'] = get_formatted_config_diff(existing_logging_facts,
new_config)
result['warnings'] = warnings
return result

Expand Down Expand Up @@ -222,6 +241,9 @@ def _state_replaced(self, want, have):
:returns: the commands necessary to migrate the current configuration
to the desired configuration
"""
commands = []
requests = []

replaced_config = self.get_replaced_config(have, want)
if 'remote_servers' in replaced_config:
replaced_config['remote_servers'].sort(key=self.get_host)
Expand All @@ -230,21 +252,18 @@ def _state_replaced(self, want, have):

if replaced_config and replaced_config != want:
delete_all = False
requests = self.get_delete_requests(replaced_config, delete_all)
send_requests(self._module, requests)
del_requests = self.get_delete_requests(replaced_config, delete_all)
requests.extend(del_requests)
commands.extend(update_states(replaced_config, "deleted"))
replaced_config = []

commands = []
requests = []

if not replaced_config and want:
commands = want
requests = self.get_merge_requests(commands, replaced_config)
add_commands = want
add_requests = self.get_merge_requests(add_commands, replaced_config)

if len(requests) > 0:
commands = update_states(commands, "replaced")
else:
commands = []
if len(add_requests) > 0:
requests.extend(add_requests)
commands.extend(update_states(add_commands, "replaced"))

return commands, requests

Expand All @@ -263,23 +282,23 @@ def _state_overridden(self, want, have):
if 'remote_servers' in want:
want['remote_servers'].sort(key=self.get_host)

commands = []
requests = []

if have and have != want:
delete_all = True
requests = self.get_delete_requests(have, delete_all)
send_requests(self._module, requests)
del_requests = self.get_delete_requests(have, delete_all)
requests.extend(del_requests)
commands.extend(update_states(have, "deleted"))
have = []

commands = []
requests = []

if not have and want:
commands = want
requests = self.get_merge_requests(commands, have)
add_commands = want
add_requests = self.get_merge_requests(add_commands, have)

if len(requests) > 0:
commands = update_states(commands, "overridden")
else:
commands = []
if len(add_requests) > 0:
requests.extend(add_requests)
commands.extend(update_states(add_commands, "overridden"))

return commands, requests

Expand Down
66 changes: 42 additions & 24 deletions plugins/module_utils/network/sonic/config/vrfs/vrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,24 @@
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.utils.utils import (
get_diff,
update_states,
send_requests,
normalize_interface_name
)
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.connection import ConnectionError

PATCH = 'patch'
DELETE = 'DELETE'
TEST_KEYS = [
{'interfaces': {'name': ''}}
]
TEST_KEYS_formatted_diff = [
{'config': {'name': ''}},
{'interfaces': {'name': '', '__delete_op': __DELETE_CONFIG_IF_NO_SUBCONFIG}}
]


class Vrfs(ConfigBase):
Expand Down Expand Up @@ -98,6 +106,16 @@ def execute_module(self):
if result['changed']:
result['after'] = changed_vrf_interfaces_facts

new_config = changed_vrf_interfaces_facts
if self._module.check_mode:
result.pop('after', None)
new_config = get_new_config(commands, existing_vrf_interfaces_facts,
TEST_KEYS_formatted_diff)
result['after(generated)'] = new_config

if self._module._diff:
result['config_diff'] = get_formatted_config_diff(existing_vrf_interfaces_facts,
new_config)
result['warnings'] = warnings
return result

Expand Down Expand Up @@ -204,27 +222,27 @@ def _state_replaced(self, want, have):
:returns: the commands necessary to migrate the current configuration
to the desired configuration
"""
commands = []
requests = []

replaced_config = self.get_replaced_config(have, want)
self.sort_config(replaced_config)
self.sort_config(want)

if replaced_config and replaced_config != want:
self.delete_all_flag = False
requests = self.get_delete_vrf_interface_requests(replaced_config, have)
send_requests(self._module, requests)
del_requests = self.get_delete_vrf_interface_requests(replaced_config, have)
requests.extend(del_requests)
commands.extend(update_states(replaced_config, "deleted"))
replaced_config = []

commands = []
requests = []

if not replaced_config and want:
commands = want
requests = self.get_create_requests(commands, have)
add_commands = want
add_requests = self.get_create_requests(add_commands, have)

if len(requests) > 0:
commands = update_states(commands, "replaced")
else:
commands = []
if len(add_requests) > 0:
requests.extend(add_requests)
commands.extend(update_states(add_commands, "replaced"))

return commands, requests

Expand All @@ -241,23 +259,23 @@ def _state_overridden(self, want, have):
self.sort_config(have)
self.sort_config(want)

commands = []
requests = []

if have and have != want:
self.delete_all_flag = True
requests = self.get_delete_vrf_interface_requests(have, have)
send_requests(self._module, requests)
del_requests = self.get_delete_vrf_interface_requests(have, have)
requests.extend(del_requests)
commands.extend(update_states(have, "deleted"))
have = []

commands = []
requests = []

if not have and want:
commands = want
requests = self.get_create_requests(commands, have)
add_commands = want
add_requests = self.get_create_requests(add_commands, have)

if len(requests) > 0:
commands = update_states(commands, "overridden")
else:
commands = []
if len(add_requests) > 0:
requests.extend(add_requests)
commands.extend(update_states(add_commands, "overridden"))

return commands, requests

Expand Down Expand Up @@ -387,7 +405,7 @@ def sort_config(self, conf):
if conf:
conf.sort(key=self.get_vrf_name)
for vrf in conf:
if 'members' in vrf and 'interfaces' in vrf['members']:
if vrf.get('members', None) and vrf['members'].get('interfaces', None):
vrf['members']['interfaces'].sort(key=self.get_interface_name)

def get_replaced_config(self, have, want):
Expand Down