Skip to content

Commit

Permalink
Accept black changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kdhlab committed Jan 26, 2025
1 parent dd4ad83 commit 6589dd2
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 426 deletions.
8 changes: 1 addition & 7 deletions plugins/module_utils/firewall_alias_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ansible_collections.puzzle.opnsense.plugins.module_utils.system_access_users_utils import (
Group,
)
from ansible_collections.puzzle.opnsense.plugins.module_utils.interfaces_configuration_utils import ( # pylint: disable=C0301
from ansible_collections.puzzle.opnsense.plugins.module_utils.interfaces_configuration_utils import ( # pylint: disable=C0301
InterfacesSet,
)
from ansible_collections.puzzle.opnsense.plugins.module_utils.enum_utils import ListEnum
Expand Down Expand Up @@ -109,7 +109,6 @@ class FirewallAlias:

# pylint: disable=too-many-instance-attributes
def __init__(self, **kwargs):

# set default attributes
self.uuid: Optional[str] = kwargs.get("uuid", str(uuid.uuid4()))
self.enabled: bool = True
Expand All @@ -135,7 +134,6 @@ def __post_init__(self):

# Check if the value is a string and the field_type is a subclass of ListEnum
if isinstance(value, str) and issubclass(field_type, ListEnum):

# Convert string to ListEnum
setattr(self, field_name, field_type.from_string(value))

Expand Down Expand Up @@ -270,7 +268,6 @@ def to_etree(self) -> Element:
del firewall_alias_dict["uuid"]

for alias_key, alias_val in firewall_alias_dict.copy().items():

if alias_key in ["enabled", "counters"]:
firewall_alias_dict[alias_key] = "0" if alias_val is False else "1"
continue
Expand Down Expand Up @@ -504,7 +501,6 @@ def set_authgroup(self, type_opnvpngroup_alias: FirewallAlias) -> None:

gid_content = []
for group in type_opnvpngroup_alias.content:

gid_content.append(
next((g for g in self.group_list if g.name == group), None).gid
)
Expand Down Expand Up @@ -608,7 +604,6 @@ def validate_content(
}

for content_value in content_values:

# since not all types need validation, unhandled types are ingnored
if not content_type_map.get(content_type.value):
return True
Expand All @@ -618,7 +613,6 @@ def validate_content(
)

if not validation_function(content_value):

raise OPNsenseContentValidationError(
content_type_map[content_type.value]["error_message"].format(
entry=content_value
Expand Down
39 changes: 26 additions & 13 deletions plugins/module_utils/interfaces_configuration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@
from ansible_collections.puzzle.opnsense.plugins.module_utils.config_utils import (
OPNsenseModuleConfig,
)


class OPNSenseDeviceNotFoundError(Exception):
"""
Exception raised when a Device is not found.
"""


class OPNSenseDeviceAlreadyAssignedError(Exception):
"""
Exception raised when a Device is already assigned to an Interface
"""


class OPNSenseInterfaceNotFoundError(Exception):
"""
Exception raised when an Interface is not found.
"""


class OPNSenseGetInterfacesError(Exception):
"""
Exception raised if the function can't query the local device
Expand Down Expand Up @@ -100,12 +105,11 @@ def from_xml(element: Element) -> "InterfaceConfiguration":
# Create the InterfaceConfiguration instance
interface_configuration = InterfaceConfiguration(
identifier=identifier,
extra_attrs=extra_attrs # Include all other attributes
extra_attrs=extra_attrs, # Include all other attributes
)

return interface_configuration


def to_etree(self) -> Element:
"""
Serializes the instance to an XML Element, including extra attributes.
Expand All @@ -124,8 +128,6 @@ def to_etree(self) -> Element:

# Create the main element
main_element = Element(interface_configuration_dict["identifier"])
# Serialize the device to ensure it is always present
#SubElement(main_element, "if").text = interface_configuration_dict["extra_attrs"].get("if")

# Serialize extra attributes
for key, value in interface_configuration_dict["extra_attrs"].items():
Expand Down Expand Up @@ -160,7 +162,7 @@ def from_ansible_module_params(cls, params: dict) -> "InterfaceConfiguration":
elif value is not None:
extra_attrs[key] = str(value)

return cls(identifier,extra_attrs)
return cls(identifier, extra_attrs)


class InterfacesSet(OPNsenseModuleConfig):
Expand Down Expand Up @@ -197,7 +199,6 @@ def __init__(self, path: str = "/conf/config.xml"):
self._interfaces_configuration = self._load_interfaces()

def _load_interfaces(self) -> List["InterfaceConfiguration"]:

element_tree_interfaces: Element = self.get("interfaces")

return [
Expand Down Expand Up @@ -225,8 +226,10 @@ def changed(self) -> bool:
return True

for current, saved in zip(current_interfaces, saved_interfaces):
if current.identifier != saved.identifier or \
current.extra_attrs != saved.extra_attrs:
if (
current.identifier != saved.identifier
or current.extra_attrs != saved.extra_attrs
):
return True

return False
Expand Down Expand Up @@ -321,14 +324,20 @@ def add_or_update(self, interface_configuration: InterfaceConfiguration) -> None
# Merge extra_attrs
for attr, value in interface_configuration.extra_attrs.items():
if attr == "if" and value not in device_interfaces_set:
raise OPNSenseInterfaceNotFoundError("Interface was not found on OPNsense Instance!") # pylint: disable=C0301
raise OPNSenseInterfaceNotFoundError(
"Interface was not found on OPNsense Instance!"
) # pylint: disable=C0301
interface_to_update.extra_attrs[attr] = value
else:
raise OPNSenseDeviceAlreadyAssignedError("This device is already assigned, please unassign this device first") # pylint: disable=C0301
raise OPNSenseDeviceAlreadyAssignedError(
"This device is already assigned, please unassign this device first"
) # pylint: disable=C0301

# Update the internal list with the complete updated configuration
self._interfaces_configuration = [
interface_to_update if iface.identifier == interface_to_update.identifier else iface
interface_to_update
if iface.identifier == interface_to_update.identifier
else iface
for iface in self._interfaces_configuration
]
else:
Expand All @@ -341,7 +350,9 @@ def add_or_update(self, interface_configuration: InterfaceConfiguration) -> None
if value:
interface_to_add.extra_attrs.update({attr: value})
if interface_to_add.extra_attrs["if"] not in device_interfaces_set:
raise OPNSenseInterfaceNotFoundError("Interface was not found on OPNsense Instance!") # pylint: disable=C0301
raise OPNSenseInterfaceNotFoundError(
"Interface was not found on OPNsense Instance!"
) # pylint: disable=C0301
self._interfaces_configuration.append(interface_to_add)

def remove(self, interface_configuration: InterfaceConfiguration) -> None:
Expand All @@ -357,7 +368,9 @@ def remove(self, interface_configuration: InterfaceConfiguration) -> None:
if interface_configuration in self._interfaces_configuration:
self._interfaces_configuration.remove(interface_configuration)
else:
raise OPNSenseInterfaceNotFoundError(f"Interface {interface_configuration.identifier} not found.") # pylint: disable=C0301
raise OPNSenseInterfaceNotFoundError(
f"Interface {interface_configuration.identifier} not found."
) # pylint: disable=C0301

def find(self, **kwargs) -> Optional[InterfaceConfiguration]:
"""
Expand Down
1 change: 0 additions & 1 deletion plugins/modules/firewall_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ def main():
ansible_alias_state: str = module.params.get("state")

with FirewallAliasSet() as alias_set:

if ansible_alias_state == "present":
alias_set.add_or_update(ansible_alias)
else:
Expand Down
Loading

0 comments on commit 6589dd2

Please sign in to comment.