diff options
author | Bradley A. Thornton <bthornto@thethorntons.net> | 2019-08-09 12:05:55 -0700 |
---|---|---|
committer | Bradley A. Thornton <bthornto@thethorntons.net> | 2019-08-09 12:05:55 -0700 |
commit | 29c342fa51c7a9866366cfc20968be7270e02fc5 (patch) | |
tree | 196722c57cca4211e79781edefe1a101d9c4b0c5 /plugins/module_utils | |
parent | 5fb9df4e907a6ab2da7a6c2dafdec9c1971e8d44 (diff) | |
download | vyos-ansible-collection-29c342fa51c7a9866366cfc20968be7270e02fc5.tar.gz vyos-ansible-collection-29c342fa51c7a9866366cfc20968be7270e02fc5.zip |
79
Diffstat (limited to 'plugins/module_utils')
8 files changed, 90 insertions, 28 deletions
diff --git a/plugins/module_utils/network/vyos/argspec/facts/facts.py b/plugins/module_utils/network/vyos/argspec/facts/facts.py index e660ef2..fc9d438 100644 --- a/plugins/module_utils/network/vyos/argspec/facts/facts.py +++ b/plugins/module_utils/network/vyos/argspec/facts/facts.py @@ -18,7 +18,13 @@ class FactsArgs(object): # pylint: disable=R0903 def __init__(self, **kwargs): pass - choices = ["all", "interfaces", "!interfaces", "l3_interfaces", "!l3_interfaces"] + choices = [ + "all", + "interfaces", + "!interfaces", + "l3_interfaces", + "!l3_interfaces", + ] argument_spec = { "gather_subset": dict(default=["!config"], type="list"), diff --git a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py index 1b6584b..adf61d7 100644 --- a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py @@ -16,7 +16,11 @@ __metaclass__ = type from copy import deepcopy from ansible.module_utils.network.common.cfg.base import ConfigBase -from ansible.module_utils.network.common.utils import to_list, dict_diff, remove_empties +from ansible.module_utils.network.common.utils import ( + to_list, + dict_diff, + remove_empties, +) from ansible.module_utils.six import iteritems from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import ( Facts, @@ -117,7 +121,9 @@ class Interfaces(ConfigBase): elif state == "deleted": if not want: for intf in have: - commands.extend(self._state_deleted({"name": intf["name"]}, intf)) + commands.extend( + self._state_deleted({"name": intf["name"]}, intf) + ) else: for item in want: obj_in_have = search_obj_in_list(item["name"], have) @@ -165,7 +171,9 @@ class Interfaces(ConfigBase): for intf in have: intf_in_want = search_obj_in_list(intf["name"], want) if not intf_in_want: - commands.extend(self._state_deleted({"name": intf["name"]}, intf)) + commands.extend( + self._state_deleted({"name": intf["name"]}, intf) + ) for intf in want: intf_in_have = search_obj_in_list(intf["name"], have) @@ -203,7 +211,10 @@ class Interfaces(ConfigBase): want_vif["vlan_id"], have_vifs, key="vlan_id" ) if not have_vif: - have_vif = {"vlan_id": want_vif["vlan_id"], "enabled": True} + have_vif = { + "vlan_id": want_vif["vlan_id"], + "enabled": True, + } vif_updates = dict_diff(have_vif, want_vif) if vif_updates: @@ -255,7 +266,10 @@ class Interfaces(ConfigBase): have_vif["vlan_id"], want_vifs, key="vlan_id" ) if not want_vif: - want_vif = {"vlan_id": have_vif["vlan_id"], "enabled": True} + want_vif = { + "vlan_id": have_vif["vlan_id"], + "enabled": True, + } for key in dict_delete(have_vif, want_vif).keys(): if key == "enabled": @@ -280,7 +294,9 @@ class Interfaces(ConfigBase): return commands - def _compute_commands(self, interface, key, vif=None, value=None, remove=False): + def _compute_commands( + self, interface, key, vif=None, value=None, remove=False + ): intf_context = "interfaces {0} {1}".format( get_interface_type(interface), interface ) diff --git a/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py index 4260f35..a69db05 100644 --- a/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/vyos/config/l3_interfaces/l3_interfaces.py @@ -52,7 +52,9 @@ class L3_interfaces(ConfigBase): facts, _warnings = Facts(self._module).get_facts( self.gather_subset, self.gather_network_resources ) - l3_interfaces_facts = facts["ansible_network_resources"].get("l3_interfaces") + l3_interfaces_facts = facts["ansible_network_resources"].get( + "l3_interfaces" + ) if not l3_interfaces_facts: return [] return l3_interfaces_facts @@ -120,7 +122,9 @@ class L3_interfaces(ConfigBase): elif state == "deleted": if not want: for intf in have: - commands.extend(self._state_deleted({"name": intf["name"]}, intf)) + commands.extend( + self._state_deleted({"name": intf["name"]}, intf) + ) else: for item in want: obj_in_have = search_obj_in_list(item["name"], have) @@ -168,7 +172,9 @@ class L3_interfaces(ConfigBase): for intf in have: intf_in_want = search_obj_in_list(intf["name"], want) if not intf_in_want: - commands.extend(self._state_deleted({"name": intf["name"]}, intf)) + commands.extend( + self._state_deleted({"name": intf["name"]}, intf) + ) for intf in want: intf_in_have = search_obj_in_list(intf["name"], have) @@ -237,7 +243,10 @@ class L3_interfaces(ConfigBase): for key, value in iteritems(update): commands.append( self._compute_commands( - key=key, value=value, interface=want_copy["name"], remove=True + key=key, + value=value, + interface=want_copy["name"], + remove=True, ) ) @@ -263,7 +272,9 @@ class L3_interfaces(ConfigBase): return commands - def _compute_commands(self, interface, key, vif=None, value=None, remove=False): + def _compute_commands( + self, interface, key, vif=None, value=None, remove=False + ): intf_context = "interfaces {0} {1}".format( get_interface_type(interface), interface ) @@ -284,7 +295,11 @@ class L3_interfaces(ConfigBase): def _get_updates(self, want, have): updates = [] - updates = diff_list_of_dicts(want.get("ipv4", []), have.get("ipv4", [])) - updates.extend(diff_list_of_dicts(want.get("ipv6", []), have.get("ipv6", []))) + updates = diff_list_of_dicts( + want.get("ipv4", []), have.get("ipv4", []) + ) + updates.extend( + diff_list_of_dicts(want.get("ipv6", []), have.get("ipv6", [])) + ) return updates diff --git a/plugins/module_utils/network/vyos/facts/facts.py b/plugins/module_utils/network/vyos/facts/facts.py index f92c761..fcb6bf0 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -31,7 +31,10 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legac Config, ) -from ansible.module_utils.network.vyos.vyos import run_commands, get_capabilities +from ansible.module_utils.network.vyos.vyos import ( + run_commands, + get_capabilities, +) FACT_LEGACY_SUBSETS = dict(default=Default, neighbors=Neighbors, config=Config) @@ -50,7 +53,9 @@ class Facts(FactsBase): def __init__(self, module): super(Facts, self).__init__(module) - def get_facts(self, legacy_facts_type=None, resource_facts_type=None, data=None): + def get_facts( + self, legacy_facts_type=None, resource_facts_type=None, data=None + ): """ Collect the facts for vyos :param legacy_facts_type: List of legacy facts types @@ -59,15 +64,20 @@ class Facts(FactsBase): :rtype: dict :return: the facts gathered """ - netres_choices = FactsArgs.argument_spec["gather_network_resources"].get( - "choices", [] - ) + netres_choices = FactsArgs.argument_spec[ + "gather_network_resources" + ].get("choices", []) if self.VALID_RESOURCE_SUBSETS: self.get_network_resources_facts( - netres_choices, FACT_RESOURCE_SUBSETS, resource_facts_type, data + netres_choices, + FACT_RESOURCE_SUBSETS, + resource_facts_type, + data, ) if self.VALID_LEGACY_GATHER_SUBSETS: - self.get_network_legacy_facts(FACT_LEGACY_SUBSETS, legacy_facts_type) + self.get_network_legacy_facts( + FACT_LEGACY_SUBSETS, legacy_facts_type + ) return self.ansible_facts, self._warnings diff --git a/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py b/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py index 9d8d008..7b73b9b 100644 --- a/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/facts/interfaces/interfaces.py @@ -69,7 +69,9 @@ class InterfacesFacts(object): facts = {} if objs: facts["interfaces"] = [] - params = utils.validate_config(self.argument_spec, {"config": objs}) + params = utils.validate_config( + self.argument_spec, {"config": objs} + ) for cfg in params["config"]: facts["interfaces"].append(utils.remove_empties(cfg)) @@ -88,7 +90,9 @@ class InterfacesFacts(object): """ vif_conf = "\n".join(filter(lambda x: ("vif" in x), conf)) eth_conf = "\n".join(filter(lambda x: ("vif" not in x), conf)) - config = self.parse_attribs(["description", "speed", "mtu", "duplex"], eth_conf) + config = self.parse_attribs( + ["description", "speed", "mtu", "duplex"], eth_conf + ) config["vifs"] = self.parse_vifs(vif_conf) return utils.remove_empties(config) diff --git a/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py b/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py index 306a73b..05973ba 100644 --- a/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py +++ b/plugins/module_utils/network/vyos/facts/l3_interfaces/l3_interfaces.py @@ -74,7 +74,9 @@ class L3_interfacesFacts(object): facts = {} if objs: facts["l3_interfaces"] = [] - params = utils.validate_config(self.argument_spec, {"config": objs}) + params = utils.validate_config( + self.argument_spec, {"config": objs} + ) for cfg in params["config"]: facts["l3_interfaces"].append(utils.remove_empties(cfg)) diff --git a/plugins/module_utils/network/vyos/facts/legacy/base.py b/plugins/module_utils/network/vyos/facts/legacy/base.py index a05516d..34992b1 100644 --- a/plugins/module_utils/network/vyos/facts/legacy/base.py +++ b/plugins/module_utils/network/vyos/facts/legacy/base.py @@ -16,7 +16,10 @@ __metaclass__ = type import platform import re -from ansible.module_utils.network.vyos.vyos import run_commands, get_capabilities +from ansible.module_utils.network.vyos.vyos import ( + run_commands, + get_capabilities, +) class LegacyFactsBase(object): diff --git a/plugins/module_utils/network/vyos/vyos.py b/plugins/module_utils/network/vyos/vyos.py index 2d6a1a8..c578dd5 100644 --- a/plugins/module_utils/network/vyos/vyos.py +++ b/plugins/module_utils/network/vyos/vyos.py @@ -37,13 +37,17 @@ vyos_provider_spec = { "host": dict(), "port": dict(type="int"), "username": dict(fallback=(env_fallback, ["ANSIBLE_NET_USERNAME"])), - "password": dict(fallback=(env_fallback, ["ANSIBLE_NET_PASSWORD"]), no_log=True), + "password": dict( + fallback=(env_fallback, ["ANSIBLE_NET_PASSWORD"]), no_log=True + ), "ssh_keyfile": dict( fallback=(env_fallback, ["ANSIBLE_NET_SSH_KEYFILE"]), type="path" ), "timeout": dict(type="int"), } -vyos_argument_spec = {"provider": dict(type="dict", options=vyos_provider_spec)} +vyos_argument_spec = { + "provider": dict(type="dict", options=vyos_provider_spec) +} vyos_top_spec = { "host": dict(removed_in_version=2.9), "port": dict(removed_in_version=2.9, type="int"), @@ -106,7 +110,9 @@ def get_config(module, flags=None, format=None): def run_commands(module, commands, check_rc=True): connection = get_connection(module) try: - response = connection.run_commands(commands=commands, check_rc=check_rc) + response = connection.run_commands( + commands=commands, check_rc=check_rc + ) except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors="surrogate_then_replace")) return response |