From 5b4b171636797efe398cbe55f0c28d756a372ce3 Mon Sep 17 00:00:00 2001 From: Guillaume Mulocher Date: Tue, 16 Jul 2024 15:27:46 +0200 Subject: [PATCH] Fix(plugins): Prevent deprecation warnings when deprecated filters are not used (#4199) Co-authored-by: Carl Buchmann --- .pre-commit-config.yaml | 3 +- .../documentation/devices/host1.md | 9 +++ .../intended/configs/host1.cfg | 8 +++ .../host1/port-channel-interfaces.yml | 8 +++ .../avd/plugins/action/eos_cli_config_gen.py | 2 +- .../avd/plugins/filter/deprecated_filters.py | 47 +++++++++++++ .../arista/avd/plugins/filter/generate_esi.py | 66 ------------------- .../avd/plugins/filter/generate_esi.yml | 31 +++++++++ .../avd/plugins/filter/generate_lacp_id.py | 61 ----------------- .../avd/plugins/filter/generate_lacp_id.yml | 27 ++++++++ .../plugins/filter/generate_route_target.py | 63 ------------------ .../plugins/filter/generate_route_target.yml | 29 ++++++++ 12 files changed, 162 insertions(+), 192 deletions(-) create mode 100644 ansible_collections/arista/avd/plugins/filter/deprecated_filters.py delete mode 100644 ansible_collections/arista/avd/plugins/filter/generate_esi.py create mode 100644 ansible_collections/arista/avd/plugins/filter/generate_esi.yml delete mode 100644 ansible_collections/arista/avd/plugins/filter/generate_lacp_id.py create mode 100644 ansible_collections/arista/avd/plugins/filter/generate_lacp_id.yml delete mode 100644 ansible_collections/arista/avd/plugins/filter/generate_route_target.py create mode 100644 ansible_collections/arista/avd/plugins/filter/generate_route_target.yml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bd6ab1e3eb2..18f012aaf48 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -180,9 +180,10 @@ repos: name: Build documentation for collection filter plugins. entry: ansible-doc-extractor --template ansible_collections/arista/avd/docs/templates/plugin-docs.j2 --markdown "ansible_collections/arista/avd/docs/plugins/Filter_plugins/" language: python - types: [python] + types_or: [python, yaml] additional_dependencies: ['ansible-doc-extractor>=0.1.10', 'ansible-core>=2.15.0,<2.18.0'] files: ansible_collections/arista/avd/plugins/filter/ + exclude: ^(ansible_collections/arista/avd/plugins/filter/deprecated_filters.py)$ - id: docs-plugin-lookup name: Build documentation for collection lookup plugins. diff --git a/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/documentation/devices/host1.md b/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/documentation/devices/host1.md index 3cc891fb02b..0e8d7be9dee 100644 --- a/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/documentation/devices/host1.md +++ b/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/documentation/devices/host1.md @@ -721,6 +721,7 @@ interface Ethernet47 | Interface | Description | Type | Mode | VLANs | Native VLAN | Trunk Group | LACP Fallback Timeout | LACP Fallback Mode | MLAG ID | EVPN ESI | | --------- | ----------- | ---- | ---- | ----- | ----------- | ------------| --------------------- | ------------------ | ------- | -------- | | Port-Channel1 | SRV01_bond0 | switched | trunk | 2-3000 | - | - | - | - | - | 0000:0000:0404:0404:0303 | +| Port-Channel30 | deprecate_filters_testing | switched | access | - | - | - | - | - | - | deaf:beed:0303:0202:0101 | | Port-Channel51 | ipv6_prefix | switched | trunk | 1-500 | - | - | - | - | - | - | ##### Flexible Encapsulation Interfaces @@ -757,6 +758,14 @@ interface Port-Channel2.1000 route-target import 03:03:02:02:01:01 lacp system-id 0303.0202.0101 ! +interface Port-Channel30 + description deprecate_filters_testing + switchport + evpn ethernet-segment + identifier deaf:beed:0303:0202:0101 + route-target import 03:03:02:02:01:01 + lacp system-id 0303.0202.0101 +! interface Port-Channel51 description ipv6_prefix switchport diff --git a/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/intended/configs/host1.cfg b/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/intended/configs/host1.cfg index 5405be908d7..244ea675c32 100644 --- a/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/intended/configs/host1.cfg +++ b/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/intended/configs/host1.cfg @@ -170,6 +170,14 @@ interface Port-Channel2.1000 route-target import 03:03:02:02:01:01 lacp system-id 0303.0202.0101 ! +interface Port-Channel30 + description deprecate_filters_testing + switchport + evpn ethernet-segment + identifier deaf:beed:0303:0202:0101 + route-target import 03:03:02:02:01:01 + lacp system-id 0303.0202.0101 +! interface Port-Channel51 description ipv6_prefix switchport diff --git a/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/inventory/host_vars/host1/port-channel-interfaces.yml b/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/inventory/host_vars/host1/port-channel-interfaces.yml index 78720cb676a..bcdf9b75f00 100644 --- a/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/inventory/host_vars/host1/port-channel-interfaces.yml +++ b/ansible_collections/arista/avd/molecule/eos_cli_config_gen_deprecated_vars/inventory/host_vars/host1/port-channel-interfaces.yml @@ -30,6 +30,14 @@ port_channel_interfaces: rt: 03:03:02:02:01:01 lacp_id: 0303.0202.0101 + # Testing deprecated filter plugins to be remove in AVD 5.0.0 + Port-Channel30: + description: deprecate_filters_testing + esi: "{{ '0303:0202:0101' | arista.avd.generate_esi('deaf:beed:') }}" + rt: "{{ '0303:0202:0101' | arista.avd.generate_route_target }}" + lacp_id: "{{ '0303:0202:0101' | arista.avd.generate_lacp_id }}" + + Port-Channel51: description: ipv6_prefix vlans: 1-500 diff --git a/ansible_collections/arista/avd/plugins/action/eos_cli_config_gen.py b/ansible_collections/arista/avd/plugins/action/eos_cli_config_gen.py index b36d3c22842..a7cc2732a09 100644 --- a/ansible_collections/arista/avd/plugins/action/eos_cli_config_gen.py +++ b/ansible_collections/arista/avd/plugins/action/eos_cli_config_gen.py @@ -188,7 +188,7 @@ def prepare_task_vars(self, task_vars: dict, structured_config_filename: str, *, try: task_vars[var] = self._templar.template(task_vars[var], fail_on_undefined=False) except Exception as e: - raise AnsibleActionFail(f"Exception during templating of task_var '{var}'") from e + raise AnsibleActionFail(f"Exception during templating of task_var '{var}': '{e}'") from e if not isinstance(task_vars, dict): # Corner case for ansible-test where the passed task_vars is a nested chain-map diff --git a/ansible_collections/arista/avd/plugins/filter/deprecated_filters.py b/ansible_collections/arista/avd/plugins/filter/deprecated_filters.py new file mode 100644 index 00000000000..2d587cf139f --- /dev/null +++ b/ansible_collections/arista/avd/plugins/filter/deprecated_filters.py @@ -0,0 +1,47 @@ +# Copyright (c) 2023-2024 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the LICENSE file. +# +# deprecated filters - grouped together to avoid Ansible to generate warning on loading the module name... +# + +__metaclass__ = type + +from ansible.errors import AnsibleFilterError + +from ansible_collections.arista.avd.plugins.plugin_utils.pyavd_wrappers import RaiseOnUse, wrap_filter + +PLUGIN_NAME_1 = "arista.avd.generate_lacp_id" +PLUGIN_NAME_2 = "arista.avd.generate_esi" +PLUGIN_NAME_3 = "arista.avd.generate_route_target" + +try: + from pyavd.j2filters import generate_esi, generate_lacp_id, generate_route_target +except ImportError as e: + generate_lacp_id = RaiseOnUse( + AnsibleFilterError( + f"The '{PLUGIN_NAME_1}' plugin requires the 'pyavd' Python library. Got import error", + orig_exc=e, + ) + ) + generate_esi = RaiseOnUse( + AnsibleFilterError( + f"The '{PLUGIN_NAME_2}' plugin requires the 'pyavd' Python library. Got import error", + orig_exc=e, + ) + ) + generate_route_target = RaiseOnUse( + AnsibleFilterError( + f"The '{PLUGIN_NAME_3}' plugin requires the 'pyavd' Python library. Got import error", + orig_exc=e, + ) + ) + + +class FilterModule(object): + def filters(self): + return { + "generate_lacp_id": wrap_filter(PLUGIN_NAME_1)(generate_lacp_id), + "generate_esi": wrap_filter(PLUGIN_NAME_2)(generate_esi), + "generate_route_target": wrap_filter(PLUGIN_NAME_3)(generate_route_target), + } diff --git a/ansible_collections/arista/avd/plugins/filter/generate_esi.py b/ansible_collections/arista/avd/plugins/filter/generate_esi.py deleted file mode 100644 index 4af0b52bfaa..00000000000 --- a/ansible_collections/arista/avd/plugins/filter/generate_esi.py +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright (c) 2023-2024 Arista Networks, Inc. -# Use of this source code is governed by the Apache License 2.0 -# that can be found in the LICENSE file. -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -from ansible.errors import AnsibleFilterError - -from ansible_collections.arista.avd.plugins.plugin_utils.pyavd_wrappers import RaiseOnUse, wrap_filter - -PLUGIN_NAME = "arista.avd.generate_esi" - -try: - from pyavd.j2filters import generate_esi -except ImportError as e: - generate_esi = RaiseOnUse( - AnsibleFilterError( - f"The '{PLUGIN_NAME}' plugin requires the 'pyavd' Python library. Got import error", - orig_exc=e, - ) - ) - - -DOCUMENTATION = r""" ---- -name: generate_esi -collection: arista.avd -author: Arista Ansible Team (@aristanetworks) -version_added: "1.1" -short_description: Transforms short_esi `0303:0202:0101` to EVPN ESI format `0000:0000:0303:0202:0101` -description: Concatenates the given `esi_prefix` and `short_esi`. -positional: _input -options: - _input: - description: Short ESI value as per AVD definition in eos_designs. - type: string - required: true - esi_prefix: - description: ESI prefix value. Will be concatenated with the `short_esi`. - type: string - default: "0000:0000:" -deprecated: - removed_in: "5.0.0" - why: This filter is no longer used by AVD and is very simple to replace with generic Jinja syntax. - alternative: Use Jinja string concatenation instead like `{{ ~ }}` -""" - -EXAMPLES = r""" ---- -esi: "{{ short_esi | arista.avd.generate_esi('deaf:beed:') }}" -""" - -RETURN = r""" ---- -_value: - description: Concatenated string of `esi_prefix` and `short_esi` like `0000:0000:0303:0202:0101` - type: string -""" - - -class FilterModule(object): - def filters(self): - return { - "generate_esi": wrap_filter(PLUGIN_NAME)(generate_esi), - } diff --git a/ansible_collections/arista/avd/plugins/filter/generate_esi.yml b/ansible_collections/arista/avd/plugins/filter/generate_esi.yml new file mode 100644 index 00000000000..f4acbd5508b --- /dev/null +++ b/ansible_collections/arista/avd/plugins/filter/generate_esi.yml @@ -0,0 +1,31 @@ +--- +DOCUMENTATION: + name: generate_esi + collection: arista.avd + author: Arista Ansible Team (@aristanetworks) + version_added: "1.1" + short_description: Transforms short_esi `0303:0202:0101` to EVPN ESI format `0000:0000:0303:0202:0101` + description: Concatenates the given `esi_prefix` and `short_esi`. + positional: _input + options: + _input: + description: Short ESI value as per AVD definition in eos_designs. + type: string + required: true + esi_prefix: + description: ESI prefix value. Will be concatenated with the `short_esi`. + type: string + default: "0000:0000:" + deprecated: + removed_in: "5.0.0" + why: This filter is no longer used by AVD and is very simple to replace with generic Jinja syntax. + alternative: Use Jinja string concatenation instead like `{{ ~ }}` + +EXAMPLES: | + --- + esi: "{{ short_esi | arista.avd.generate_esi('deaf:beed:') }}" + +RETURN: + _value: + description: Concatenated string of `esi_prefix` and `short_esi` like `0000:0000:0303:0202:0101` + type: string diff --git a/ansible_collections/arista/avd/plugins/filter/generate_lacp_id.py b/ansible_collections/arista/avd/plugins/filter/generate_lacp_id.py deleted file mode 100644 index f9e9b507686..00000000000 --- a/ansible_collections/arista/avd/plugins/filter/generate_lacp_id.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2023-2024 Arista Networks, Inc. -# Use of this source code is governed by the Apache License 2.0 -# that can be found in the LICENSE file. -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -from ansible.errors import AnsibleFilterError - -from ansible_collections.arista.avd.plugins.plugin_utils.pyavd_wrappers import RaiseOnUse, wrap_filter - -PLUGIN_NAME = "arista.avd.generate_lacp_id" - -try: - from pyavd.j2filters import generate_lacp_id -except ImportError as e: - generate_lacp_id = RaiseOnUse( - AnsibleFilterError( - f"The '{PLUGIN_NAME}' plugin requires the 'pyavd' Python library. Got import error", - orig_exc=e, - ) - ) - -DOCUMENTATION = r""" ---- -name: generate_lacp_id -collection: arista.avd -author: Arista Ansible Team (@aristanetworks) -version_added: "1.1" -short_description: Transforms short_esi `0303:0202:0101` to LACP ID format `0303.0202.0101` -description: Replaces `:` with `.` -positional: _input -options: - _input: - description: Short ESI value as per AVD definition in eos_designs. - type: string - required: true -deprecated: - removed_in: "5.0.0" - why: This filter is no longer used by AVD and is very simple to replace with a generic Jinja filter. - alternative: Use the builtin `replace` filter instead like `{{ | replace(':', '.') }}` -""" - -EXAMPLES = r""" ---- -lacp_id: "{{ short_esi | arista.avd.generate_lacp_id }}" -""" - -RETURN = r""" ---- -_value: - description: String based on LACP ID format like 0303.0202.0101 - type: string -""" - - -class FilterModule(object): - def filters(self): - return { - "generate_lacp_id": wrap_filter(PLUGIN_NAME)(generate_lacp_id), - } diff --git a/ansible_collections/arista/avd/plugins/filter/generate_lacp_id.yml b/ansible_collections/arista/avd/plugins/filter/generate_lacp_id.yml new file mode 100644 index 00000000000..2d19f51297f --- /dev/null +++ b/ansible_collections/arista/avd/plugins/filter/generate_lacp_id.yml @@ -0,0 +1,27 @@ +--- +DOCUMENTATION: + name: generate_lacp_id + collection: arista.avd + author: Arista Ansible Team (@aristanetworks) + version_added: "1.1" + short_description: Transforms short_esi `0303:0202:0101` to LACP ID format `0303.0202.0101` + description: Replaces `:` with `.` + positional: _input + options: + _input: + description: Short ESI value as per AVD definition in eos_designs. + type: string + required: true + deprecated: + removed_in: "5.0.0" + why: This filter is no longer used by AVD and is very simple to replace with a generic Jinja filter. + alternative: Use the builtin `replace` filter instead like `{{ | replace(':', '.') }}` + +EXAMPLES: |- + --- + lacp_id: "{{ short_esi | arista.avd.generate_lacp_id }}" + +RETURN: + _value: + description: String based on LACP ID format like 0303.0202.0101 + type: string diff --git a/ansible_collections/arista/avd/plugins/filter/generate_route_target.py b/ansible_collections/arista/avd/plugins/filter/generate_route_target.py deleted file mode 100644 index 5efa91b9b0d..00000000000 --- a/ansible_collections/arista/avd/plugins/filter/generate_route_target.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright (c) 2023-2024 Arista Networks, Inc. -# Use of this source code is governed by the Apache License 2.0 -# that can be found in the LICENSE file. -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -from ansible.errors import AnsibleFilterError - -from ansible_collections.arista.avd.plugins.plugin_utils.pyavd_wrappers import RaiseOnUse, wrap_filter - -PLUGIN_NAME = "arista.avd.generate_route_target" - -try: - from pyavd.j2filters import generate_route_target -except ImportError as e: - generate_route_target = RaiseOnUse( - AnsibleFilterError( - f"The '{PLUGIN_NAME}' plugin requires the 'pyavd' Python library. Got import error", - orig_exc=e, - ) - ) - -DOCUMENTATION = r""" ---- -name: generate_route_target -collection: arista.avd -author: Arista Ansible Team (@aristanetworks) -version_added: "1.1" -short_description: Transforms short_esi `0303:0202:0101` to route-target format `03:03:02:02:01:01` -description: Removes `:` and inserts new `:` for each two characters. -positional: _input -options: - _input: - description: Short ESI value as per AVD definition in eos_designs. - type: string - required: true -deprecated: - removed_in: "5.0.0" - why: This filter is no longer used by AVD and is very simple to replace with a generic Jinja filter. - alternative: |- - Use the builtin `ansible.builtin.regex_replace` filter instead like - `{{ | ansible.builtin.regex_replace('(\\d{2})(\\d{2}):(\\d{2})(\\d{2}):(\\d{2})(\\d{2})', '\\1:\\2:\\3:\\4:\\5:\\6') }}` -""" - -EXAMPLES = r""" ---- -rt: "{{ short_esi | arista.avd.generate_route_target }}" -""" - -RETURN = r""" ---- -_value: - description: String based on route-target format like 03:03:02:02:01:01 - type: string -""" - - -class FilterModule(object): - def filters(self): - return { - "generate_route_target": wrap_filter(PLUGIN_NAME)(generate_route_target), - } diff --git a/ansible_collections/arista/avd/plugins/filter/generate_route_target.yml b/ansible_collections/arista/avd/plugins/filter/generate_route_target.yml new file mode 100644 index 00000000000..0dbe48afda9 --- /dev/null +++ b/ansible_collections/arista/avd/plugins/filter/generate_route_target.yml @@ -0,0 +1,29 @@ +--- +DOCUMENTATION: + name: generate_route_target + collection: arista.avd + author: Arista Ansible Team (@aristanetworks) + version_added: "1.1" + short_description: Transforms short_esi `0303:0202:0101` to route-target format `03:03:02:02:01:01` + description: Removes `:` and inserts new `:` for each two characters. + positional: _input + options: + _input: + description: Short ESI value as per AVD definition in eos_designs. + type: string + required: true + deprecated: + removed_in: "5.0.0" + why: This filter is no longer used by AVD and is very simple to replace with a generic Jinja filter. + alternative: |- + Use the builtin `ansible.builtin.regex_replace` filter instead like + `{{ | ansible.builtin.regex_replace('(\\d{2})(\\d{2}):(\\d{2})(\\d{2}):(\\d{2})(\\d{2})', '\\1:\\2:\\3:\\4:\\5:\\6') }}` + +EXAMPLES: |- + --- + rt: "{{ short_esi | arista.avd.generate_route_target }}" + +RETURN: + _value: + description: String based on route-target format like 03:03:02:02:01:01 + type: string