Skip to content

Commit

Permalink
Fix(plugins): Prevent deprecation warnings when deprecated filters ar…
Browse files Browse the repository at this point in the history
…e not used (#4199)

Co-authored-by: Carl Buchmann <[email protected]>
  • Loading branch information
gmuloc and carlbuchmann authored Jul 16, 2024
1 parent 72b2d27 commit 5b4b171
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
}
66 changes: 0 additions & 66 deletions ansible_collections/arista/avd/plugins/filter/generate_esi.py

This file was deleted.

31 changes: 31 additions & 0 deletions ansible_collections/arista/avd/plugins/filter/generate_esi.yml
Original file line number Diff line number Diff line change
@@ -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 `{{ <esi_prefix> ~ <short_esi> }}`

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
61 changes: 0 additions & 61 deletions ansible_collections/arista/avd/plugins/filter/generate_lacp_id.py

This file was deleted.

27 changes: 27 additions & 0 deletions ansible_collections/arista/avd/plugins/filter/generate_lacp_id.yml
Original file line number Diff line number Diff line change
@@ -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 `{{ <short_esi> | 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

This file was deleted.

Loading

0 comments on commit 5b4b171

Please sign in to comment.