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_esi to PyAVD #4114

Merged
merged 5 commits into from
Jun 14, 2024
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
23 changes: 18 additions & 5 deletions ansible_collections/arista/avd/plugins/filter/generate_esi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@

__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.generate_esi 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
Expand Down Expand Up @@ -38,12 +55,8 @@
"""


def generate_esi(esi_short, esi_prefix="0000:0000:"):
return esi_prefix + esi_short


class FilterModule(object):
def filters(self):
return {
"generate_esi": generate_esi,
"generate_esi": wrap_filter(PLUGIN_NAME)(generate_esi),
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,12 @@

__metaclass__ = type

from ansible_collections.arista.avd.plugins.filter.generate_esi import generate_esi
from ansible_collections.arista.avd.plugins.filter.generate_lacp_id import generate_lacp_id

ESI_SHORT = "0303:0202:0101"
ESI_PREFIX = "1111:1111"
ESI_SHORT_1 = "0404:0202:0101"


class TestEsiManagementFilter:
def test_generate_esi_without_prefix(self):
resp = generate_esi(ESI_SHORT)
assert resp == "0000:0000:" + ESI_SHORT

def test_generate_esi_with_prefix(self):
assert ESI_PREFIX is not None and ESI_PREFIX != ""
resp = generate_esi(ESI_SHORT, ESI_PREFIX)
assert resp == ESI_PREFIX + ESI_SHORT

def test_lacp_id(self):
assert ESI_SHORT_1 is not None and ESI_SHORT_1 != ""
Expand Down
1 change: 1 addition & 0 deletions python-avd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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.convert_dicts/$(PYAVD_FILTER_IMPORT)\.convert_dicts/g' {} +
find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.default/$(PYAVD_FILTER_IMPORT)\.default/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_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
21 changes: 21 additions & 0 deletions python-avd/pyavd/j2filters/generate_esi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 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


def generate_esi(short_esi: str, esi_prefix: str = "0000:0000:") -> str:
"""
Transforms short_esi to EVPN ESI format.
0303:0202:0101 (short_esi) -> 0000:0000:0303:0202:0101 (EVPN ESI).

Args:
esi_short: Short ESI value as per AVD definition in eos_designs
esi_prefix: ESI prefix value, will be concatenated with the `short_esi`
Returns:
Concatenated string of `esi_prefix` and `short_esi` like `0000:0000:0303:0202:0101`

"""
return esi_prefix + short_esi
2 changes: 2 additions & 0 deletions python-avd/pyavd/templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .j2filters.add_md_toc import add_md_toc
from .j2filters.convert_dicts import convert_dicts
from .j2filters.default import default
from .j2filters.generate_esi import generate_esi
from .j2filters.generate_route_target import generate_route_target
from .j2filters.hide_passwords import hide_passwords
from .j2filters.list_compress import list_compress
Expand Down Expand Up @@ -73,6 +74,7 @@ def import_filters_and_tests(self) -> None:
"arista.avd.decrypt": decrypt,
"arista.avd.default": default,
"arista.avd.encrypt": encrypt,
"arista.avd.generate_esi": generate_esi,
"arista.avd.generate_route_target": generate_route_target,
"arista.avd.hide_passwords": hide_passwords,
"arista.avd.list_compress": list_compress,
Expand Down
24 changes: 24 additions & 0 deletions python-avd/tests/pyavd/j2filters/test_generate_esi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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

import pytest
from pyavd.j2filters.generate_esi import generate_esi

DEFAULT_ESI_PREFIX = "0000:0000:"
GENERATE_ESI_TEST_CASES = [
# (<esi_short>, <esi_prefix>, <esi>)
("0303:0202:0101", "", "0000:0000:0303:0202:0101"), # without prefix
("0303:0202:0101", "1111:1111:", "1111:1111:0303:0202:0101"), # with prefix
]


class TestGenerateEsiFilter:
@pytest.mark.parametrize("esi_short, esi_prefix, esi", GENERATE_ESI_TEST_CASES)
def test_generate_esi(self, esi_short, esi_prefix, esi):
esi_prefix = esi_prefix or DEFAULT_ESI_PREFIX
resp = generate_esi(esi_short, esi_prefix)
assert resp == esi
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
]


class TestEsiManagementFilter:
class TestGenerateRouteTargetFilter:
@pytest.mark.parametrize("esi_short, route_target", ESI_TO_RT_TEST_CASES)
def test_generate_route_target(self, esi_short, route_target):
resp = generate_route_target(esi_short)
Expand Down
Loading