Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
kdhlab committed Jan 11, 2025
1 parent b10db2f commit 9fa8234
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 94 deletions.
117 changes: 45 additions & 72 deletions plugins/module_utils/interfaces_assignments_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from dataclasses import dataclass, asdict, field
from typing import List, Optional, Dict, Any
from pprint import pprint


from xml.etree.ElementTree import Element, ElementTree, SubElement

Expand Down Expand Up @@ -134,72 +132,47 @@ def to_etree(self) -> Element:
SubElement(main_element, "if").text = interface_assignment_dict.get("device")
SubElement(main_element, "descr").text = interface_assignment_dict.get("descr")
if getattr(self, "enable", None):
SubElement(main_element, "enable").text = "1"
# Enumerate the basic attributes if the interface is enabled

SubElement(main_element, "enable").text = "1"
# Enumerate the basic attributes if the interface is enabled
if getattr(self, "blockpriv", None):
SubElement(main_element, "blockpriv").text = "1"
if getattr(self, "blockbogons", None):
SubElement(main_element, "blockbogons").text = "1"
if getattr(self, "spoofmac", None):
interface_assignment_dict["spoofmac"] = getattr(self, "spoofmac")
if getattr(self, "promisc", None):
SubElement(main_element, "promisc").text = "1"
if getattr(self, "mtu", None):
interface_assignment_dict["mtu"] = getattr(self, "mtu")
if getattr(self, "mss", None):
interface_assignment_dict["mss"] = getattr(self, "mss")
if getattr(self, "gateway_interface", None):
SubElement(main_element, "gateway_interface").text = "1"
#ipv4 configuration
if getattr(self, "ipv4_configuration_type", None):
if getattr(self, "ipv4_configuration") == "dhcp":
ipv4_element = SubElement(main_element, "ipv4")
SubElement(ipv4_element, "dhcp").text = "1"
elif getattr(self, "ipv4_configuration") == "static":
ipv4_element = SubElement(main_element, "ipv4")
if getattr(self, "ipv4_address", None):
SubElement(ipv4_element, "ipaddr").text = getattr(self, "ipv4_address")
if getattr(self, "ipv4_subnet", None):
SubElement(ipv4_element, "subnet").text = getattr(self, "ipv4_subnet")
if getattr(self, "ipv4_gateway", None):
SubElement(ipv4_element, "gateway").text = getattr(self, "ipv4_gateway")
elif getattr(self, "ipv4_configuration") == "ppp":
#modem config
elif getattr(self, "ipv4_configuration") == "pppoe":
#pppoe config
elif getattr(self, "ipv4_configuration") == "pptp":
#pptp config
elif getattr(self, "ipv4_configuration") == "l2tp":
#l2tp config

if getattr(self, "lock", None):
SubElement(main_element, "lock").text = "1"
# handle special cases
if getattr(self, "alias-subnet", None):
interface_assignment_dict["extra_attrs"]["alias-subnet"] = getattr(
self, "alias-subnet", None
)

interface_assignment_dict["extra_attrs"]["alias-address"] = getattr(
self, "alias-address", None
)

if getattr(self, "dhcp6-ia-pd-len", None):
interface_assignment_dict["extra_attrs"]["dhcp6-ia-pd-len"] = getattr(
self, "dhcp6-ia-pd-len", None
)

if getattr(self, "track6-interface", None):
interface_assignment_dict["extra_attrs"]["track6-interface"] = getattr(
self, "track6-interface", None
)

if getattr(self, "track6-prefix-id", None):
interface_assignment_dict["extra_attrs"]["track6-prefix-id"] = getattr(
self, "track6-prefix-id", None
)
# Serialize extra attributes
for key, value in interface_assignment_dict["extra_attrs"].items():
if (
key
in [
"spoofmac",
"alias-address",
"alias-subnet",
"dhcp6-ia-pd-len",
"adv_dhcp_pt_timeout",
"adv_dhcp_pt_retry",
"adv_dhcp_pt_select_timeout",
"adv_dhcp_pt_reboot",
"adv_dhcp_pt_backoff_cutoff",
"adv_dhcp_pt_initial_interval",
"adv_dhcp_pt_values",
"adv_dhcp_send_options",
"adv_dhcp_request_options",
"adv_dhcp_required_options",
"adv_dhcp_option_modifiers",
"adv_dhcp_config_advanced",
"adv_dhcp_config_file_override",
"adv_dhcp_config_file_override_path",
"dhcprejectfrom",
"track6-interface",
"track6-prefix-id",
]
and value is None
):
sub_element = SubElement(main_element, key)
if value is None and key not in exceptions:
continue
sub_element = SubElement(main_element, key)
if value is True:
sub_element.text = "1"
elif value is not None:
sub_element.text = str(value)

return main_element

Expand All @@ -224,13 +197,13 @@ def from_ansible_module_params(cls, params: dict) -> "InterfaceAssignment":
"descr": params.get("description"),
"enable": params.get("enabled"),
"lock": params.get("locked"),
# "blockpriv": params.get("block_private"),
# "blockbogons": params.get("block_bogons"),
# "spoofmac": params.get("mac_address"),
# "promisc": params.get("promiscuous_mode"),
# "mtu": params.get("mtu"),
# "mss": params.get("mss"),
# "gateway_interface": params.get("dynamic_gateway"),
"blockpriv": params.get("block_private"),
"blockbogons": params.get("block_bogons"),
"spoofmac": params.get("mac_address"),
"promisc": params.get("promiscuous_mode"),
"mtu": params.get("mtu"),
"mss": params.get("mss"),
"gateway_interface": params.get("dynamic_gateway"),
}

interface_assignment_dict = {
Expand Down
88 changes: 66 additions & 22 deletions plugins/modules/interfaces_assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,29 @@
required: false
ipv4_configuration_type:
description:
-
- Must be one of the following values: 'none', 'static', 'dhcp', 'ppp', 'pppoe', 'pptp', 'l2tp'
type: str
required: false
ipv6_configuration_type:
description:
-
type: str
required: false
ipv4_address:
description:
-
type: str
required: false
# ipv4_subnet:
# description:
# -
# type: int
# required: false
ipv4_gateway:
description:
-
type: str
required: false
ipv6_configuration_type:
description:
- Must be one of the following values: 'none', 'static', 'dhcp6', 'pppoe6', 'slaac6', '6rd', '6to4', 'track6'
ipv6_address:
description:
-
type: str
required: false
# ipv6_subnet:
# description:
# -
# type: int
# required: false
ipv6_gateway:
description:
-
Expand Down Expand Up @@ -193,18 +183,72 @@ def main():
"identifier": {"type": "str", "required": True},
"device": {"type": "str", "required": True},
"description": {"type": "str", "required": False},
"enabled": {"type": "bool", "required": False, "default": False},
"enabled": {
"type": "bool",
"required": False,
"default": False,
"required_if": ["ipv4_configuration_type", "ipv6_configuration_type", "mac_address", "promiscuous_mode", "mtu", "mss", "dynamic_gateway", "block_private", "block_bogons"],
},
"locked": {"type": "bool", "required": False, "default": False},
"block_private": {"type": "bool", "required": False, "default": False},
"block_bogons": {"type": "bool", "required": False, "default": False},
"ipv4_address": {"type": "str", "required": False},
"ipv4_subnet": {"type": "int", "required": False},
"ipv4_configuration_type": {
"type": "str",
"choices": [None,"static", "dhcp", "ppp", "pppoe", "pptp", "l2tp"],
"required": False,
"default": None,
},
"ipv4_address": {
"type": "str",
"required_by": ["ipv4_configuration_type", "static"],
""
"required": False,
"default": None,
},
"ipv4_subnet": {
"type": "int",
"required_by": ["ipv4_configuration_type", "static"],
"required": False,
"default": 32,
},
"ipv4_gateway": {"type": "str", "required": False},
"ipv6_address": {"type": "str", "required": False},
"ipv6_subnet": {"type": "int", "required": False},
"ipv6_gateway": {"type": "str", "required": False},
"track6_interface": {"type": "str", "required": False},
"track6_prefix_id": {"type": "int", "required": False},
"ipv6_configuration_type": {
"type": "str",
"choices": [None, "static", "dhcp6", "pppoe6", "slaac6", "6rd", "6to4", "track6"],
"required": False,
"default": None,
},
"ipv6_address": {
"type": "str",
"required_by": ["ipv6_configuration_type", "static"],
"default": None,
"required": False,
},

"ipv6_subnet": {
"type": "int",
"required_by": ["ipv6_configuration_type", "static"],
"required": False,
"default": 128,
},
"ipv6_gateway": {
"type": "str",
"required_by": ["ipv6_configuration_type", "static"],
"default": None,
"required": False,
},
"track6_interface": {
"type": "str",
"required_by": ["ipv6_configuration_type", "track6"],
"default": None,
"required": False,
},
"track6_prefix_id": {
"type": "str",
"required_by": ["ipv6_configuration_type", "track6"],
"default": None,
"required": False,
},
"mac_address": {"type": "str", "required": False},
"promiscuous_mode": {"type": "bool", "required": False, "default": False},
"mtu": {"type": "int", "required": False},
Expand Down

0 comments on commit 9fa8234

Please sign in to comment.