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

Refactor(plugins): Move jinja filter code for arista.avd.generate_lacp_id to PyAVD #4139

Merged
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
Expand Up @@ -5,6 +5,22 @@

__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.generate_lacp_id 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
Expand Down Expand Up @@ -34,12 +50,8 @@
"""


def generate_lacp_id(esi_short):
return esi_short.replace(":", ".")


class FilterModule(object):
def filters(self):
return {
"generate_lacp_id": generate_lacp_id,
"generate_lacp_id": wrap_filter(PLUGIN_NAME)(generate_lacp_id),
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RaiseOnUse:
def __init__(self, exception: Exception):
self.exception = exception

def __call__(self, *args):
def __call__(self, *args, **kwargs):
raise self.exception


Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions python-avd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fix-libs: ## Fix/remove various Ansible specifics things from python files
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.is_in_filter/$(PYAVD_FILTER_IMPORT)\.is_in_filter/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.natural_sort/$(PYAVD_FILTER_IMPORT)\.natural_sort/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.generate_esi/$(PYAVD_FILTER_IMPORT)\.generate_esi/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.generate_lacp_id/$(PYAVD_FILTER_IMPORT)\.generate_lacp_id/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.generate_route_target/$(PYAVD_FILTER_IMPORT)\.generate_route_target/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.hide_passwords/$(PYAVD_FILTER_IMPORT)\.hide_passwords/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.list_compress/$(PYAVD_FILTER_IMPORT)\.list_compress/g' {} +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from typing import TYPE_CHECKING

from ....j2filters.generate_esi import generate_esi
from ....j2filters.generate_lacp_id import generate_lacp_id
from ....j2filters.generate_route_target import generate_route_target
from ....vendor.j2.filter.generate_lacp_id import generate_lacp_id
from ....vendor.j2.filter.range_expand import range_expand
from ....vendor.strip_empties import strip_null_from_data
from ....vendor.utils import append_if_not_duplicate, get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from typing import TYPE_CHECKING

from ....j2filters.generate_esi import generate_esi
from ....j2filters.generate_lacp_id import generate_lacp_id
from ....j2filters.generate_route_target import generate_route_target
from ....j2filters.natural_sort import natural_sort
from ....vendor.j2.filter.generate_lacp_id import generate_lacp_id
from ....vendor.utils import append_if_not_duplicate, get
from .utils import UtilsMixin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typing import TYPE_CHECKING

from ....j2filters.generate_esi import generate_esi
from ....j2filters.generate_lacp_id import generate_lacp_id
from ....j2filters.generate_route_target import generate_route_target
from ....vendor.j2.filter.generate_lacp_id import generate_lacp_id
from ....vendor.utils import get
from ...interface_descriptions.models import InterfaceDescriptionData
from .utils import UtilsMixin
Expand Down
17 changes: 17 additions & 0 deletions python-avd/pyavd/j2filters/generate_lacp_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 annotations


def generate_lacp_id(esi_short: str) -> str:
"""
Transforms short_esi `0303:0202:0101` to LACP ID format `0303.0202.0101`
Args:
esi_short (str): Short ESI value as per AVD definition in eos_designs
Returns:
str: LACP ID
"""
return esi_short.replace(":", ".")
2 changes: 1 addition & 1 deletion python-avd/pyavd/j2filters/is_in_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import annotations


def is_in_filter(hostname: str, hostname_filter: list | None):
def is_in_filter(hostname: str, hostname_filter: list | None) -> bool:
"""
Check if device is part of the filter or not.
Expand Down
2 changes: 2 additions & 0 deletions python-avd/pyavd/templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def import_filters_and_tests(self) -> None:
from .j2filters.convert_dicts import convert_dicts
from .j2filters.default import default
from .j2filters.generate_esi import generate_esi
from .j2filters.generate_lacp_id import generate_lacp_id
from .j2filters.generate_route_target import generate_route_target
from .j2filters.hide_passwords import hide_passwords
from .j2filters.is_in_filter import is_in_filter
Expand All @@ -92,6 +93,7 @@ def import_filters_and_tests(self) -> None:
"arista.avd.default": default,
"arista.avd.encrypt": encrypt,
"arista.avd.generate_esi": generate_esi,
"arista.avd.generate_lacp_id": generate_lacp_id,
"arista.avd.generate_route_target": generate_route_target,
"arista.avd.hide_passwords": hide_passwords,
"arista.avd.is_in_filter": is_in_filter,
Expand Down
32 changes: 32 additions & 0 deletions python-avd/tests/pyavd/j2filters/test_generate_lacp_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.
import pytest
from pyavd.j2filters.generate_lacp_id import generate_lacp_id

ESI_SHORT_VALID_INPUT = [
("0404:0202:0101", "0404.0202.0101"),
("000:000:0303:0202:0101", "000.000.0303.0202.0101"),
("", ""),
]

ESI_SHORT_INVALID_INPUT = [
(10, "'int' object has no attribute 'replace'"),
(None, "'NoneType' object has no attribute 'replace'"),
({}, "'dict' object has no attribute 'replace'"),
([], "'list' object has no attribute 'replace'"),
]


class TestGenerateLacpIdFilter:

@pytest.mark.parametrize("esi_short, lacp_id", ESI_SHORT_VALID_INPUT)
def test_generate_lacp_id_valid(self, esi_short, lacp_id):
resp = generate_lacp_id(esi_short)
assert resp == lacp_id

@pytest.mark.parametrize("esi_short, error_msg", ESI_SHORT_INVALID_INPUT)
def test_generate_lacp_id_invalid(self, esi_short, error_msg):
with pytest.raises(AttributeError) as exc_info:
generate_lacp_id(esi_short)
assert str(exc_info.value) == error_msg
Loading