Created
June 15, 2022 20:04
-
-
Save cidrblock/bf0756a0232af555443bd8adb0a453fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Copyright 2020 Red Hat | |
# GNU General Public License v3.0+ | |
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
""" | |
flatten a complex object to dot bracket notation | |
""" | |
from __future__ import absolute_import, division, print_function | |
from dataclasses import dataclass, asdict, field | |
__metaclass__ = type | |
import random | |
from typing import List | |
from ansible.errors import AnsibleFilterError | |
from enum import Enum | |
class DesignationNativeMapping(Enum): | |
"""Enum for mapping the designation to a native vlan.""" | |
"""The production vlan.""" | |
BM_PROD = 200 | |
"""The non production vlan.""" | |
BM_NON_PROD = 300 | |
"""The ESX server DMZ vlan.""" | |
BM_ESX_DMZ = 600 | |
@dataclass | |
class BaseInterface: | |
"""The base interface, contains common attributes.""" | |
"""The interface name""" | |
name: str | |
"""The interface description""" | |
description: str = "default description" | |
"""The interface number""" | |
number: int = -1 | |
"""The interface type""" | |
type: str = "switched" | |
"""The interface shutdown status""" | |
shutdown: bool = False | |
"""The interface spanning tree portfast status""" | |
spanning_tree_portfast: str = "edge" | |
def __post__init__(self): | |
"""Post initalize the interface. | |
Extract the name from the number | |
""" | |
self.number = int(self.name.replace("Ethernet", "")) | |
def to_keyed_dict(self): | |
"""Convert the interface to a dict keyed with interface name.""" | |
details = asdict(self) | |
return {details.pop("name"): details} | |
@dataclass | |
class TrunkInterface(BaseInterface): | |
native_vlan: int = 100 | |
mode: str = "trunk" | |
trunk_groups: List = field(default_factory=lambda: ["SERVER"]) | |
@dataclass | |
class AccessInterface(BaseInterface): | |
"""The access interface.""" | |
"""The access vlan""" | |
vlan: int = -1 | |
"""The port mode""" | |
mode: str = "access" | |
def _eth_builder(data): | |
result = {} | |
for interface in data["interfaces"]: | |
if interface["type"] != "25GBASE_SFP28": | |
continue | |
if interface["name"] in data["access_interfaces"]: | |
obj_interface = AccessInterface(name=interface["name"]) | |
else: | |
designation = "BM_PROD" # do the cross ref here | |
try: | |
vlan = DesignationNativeMapping[designation].value | |
except KeyError: | |
raise AnsibleFilterError( | |
f"Unknown designation '{designation}' for '{interface['name']}'" | |
) | |
obj_interface = TrunkInterface( | |
name=interface["name"], | |
native_vlan=DesignationNativeMapping[designation].value, | |
) | |
result.update(obj_interface.to_keyed_dict()) | |
return result | |
class FilterModule(object): | |
def filters(self): | |
return {"eth_builder": _eth_builder} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment