From 7344ffc5dc646bbb362c5cd157a1828c3d1537ec Mon Sep 17 00:00:00 2001 From: Rohit Thakur Date: Wed, 29 Apr 2020 16:45:39 +0530 Subject: new states added Signed-off-by: Rohit Thakur --- .../network/vyos/argspec/interfaces/interfaces.py | 52 +++++++++++---- .../network/vyos/config/interfaces/interfaces.py | 73 ++++++++++++++-------- 2 files changed, 85 insertions(+), 40 deletions(-) (limited to 'plugins/module_utils') diff --git a/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py b/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py index 3542cb19..141fedca 100644 --- a/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py @@ -31,7 +31,6 @@ __metaclass__ = type class InterfacesArgs(object): # pylint: disable=R0903 """The arg spec for the vyos_interfaces module """ - def __init__(self, **kwargs): pass @@ -39,11 +38,23 @@ class InterfacesArgs(object): # pylint: disable=R0903 "config": { "elements": "dict", "options": { - "description": {"type": "str"}, - "duplex": {"choices": ["full", "half", "auto"]}, - "enabled": {"default": True, "type": "bool"}, - "mtu": {"type": "int"}, - "name": {"required": True, "type": "str"}, + "description": { + "type": "str" + }, + "duplex": { + "choices": ["full", "half", "auto"] + }, + "enabled": { + "default": True, + "type": "bool" + }, + "mtu": { + "type": "int" + }, + "name": { + "required": True, + "type": "str" + }, "speed": { "choices": ["auto", "10", "100", "1000", "2500", "10000"], "type": "str", @@ -51,19 +62,34 @@ class InterfacesArgs(object): # pylint: disable=R0903 "vifs": { "elements": "dict", "options": { - "vlan_id": {"type": "int"}, - "description": {"type": "str"}, - "enabled": {"default": True, "type": "bool"}, - "mtu": {"type": "int"}, + "vlan_id": { + "type": "int" + }, + "description": { + "type": "str" + }, + "enabled": { + "default": True, + "type": "bool" + }, + "mtu": { + "type": "int" + }, }, "type": "list", }, }, "type": "list", }, + "running_config": {"type": "str"}, "state": { - "choices": ["merged", "replaced", "overridden", "deleted"], - "default": "merged", - "type": "str", + "choices": [ + "merged", "replaced", "overridden", "deleted", "rendered", + "parsed", "gathered" + ], + "default": + "merged", + "type": + "str", }, } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py index deb504c2..6d6bf775 100644 --- a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py @@ -13,7 +13,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type - from copy import deepcopy from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.cfg.base import ( ConfigBase, @@ -49,16 +48,18 @@ class Interfaces(ConfigBase): def __init__(self, module): super(Interfaces, self).__init__(module) - def get_interfaces_facts(self): + def get_interfaces_facts(self, data=None): """ Get the 'facts' (the current configuration) :rtype: A dictionary :returns: The current configuration as a dictionary """ facts, _warnings = Facts(self._module).get_facts( - self.gather_subset, self.gather_network_resources + self.gather_subset, self.gather_network_resources, data=data + ) + interfaces_facts = facts["ansible_network_resources"].get( + "interfaces" ) - interfaces_facts = facts["ansible_network_resources"].get("interfaces") if not interfaces_facts: return [] return interfaces_facts @@ -72,25 +73,44 @@ class Interfaces(ConfigBase): commands = list() warnings = list() - existing_interfaces_facts = self.get_interfaces_facts() - commands.extend(self.set_config(existing_interfaces_facts)) - if commands: - if self._module.check_mode: - resp = self._connection.edit_config(commands, commit=False) - else: - resp = self._connection.edit_config(commands) - result["changed"] = True + if self.state in self.ACTION_STATES: + existing_interfaces_facts = self.get_interfaces_facts() + else: + existing_interfaces_facts = [] - result["commands"] = commands + if self.state in self.ACTION_STATES or self.state == "rendered": + commands.extend(self.set_config(existing_interfaces_facts)) - if self._module._diff: - result["diff"] = resp["diff"] if result["changed"] else None + if commands and self.state in self.ACTION_STATES: + if not self._module.check_mode: + self._connection.edit_config(commands) + result["changed"] = True - changed_interfaces_facts = self.get_interfaces_facts() + if self.state in self.ACTION_STATES: + result["commands"] = commands + + if self.state in self.ACTION_STATES or self.state == "gathered": + changed_interfaces_facts = self.get_interfaces_facts() + elif self.state == "rendered": + result["rendered"] = commands + elif self.state == "parsed": + running_config = self._module.params["running_config"] + if not running_config: + self._module.fail_json( + msg="value of running_config parameter must not be empty for state parsed" + ) + result["parsed"] = self.get_interfaces_facts( + data=running_config + ) + else: + changed_interfaces_facts = [] - result["before"] = existing_interfaces_facts - if result["changed"]: - result["after"] = changed_interfaces_facts + if self.state in self.ACTION_STATES: + result["before"] = existing_interfaces_facts + if result["changed"]: + result["after"] = changed_interfaces_facts + elif self.state == "gathered": + result["gathered"] = changed_interfaces_facts result["warnings"] = warnings return result @@ -118,19 +138,18 @@ class Interfaces(ConfigBase): to the desired configuration """ commands = [] - state = self._module.params["state"] - if state in ("merged", "replaced", "overridden") and not want: + if self.state in ("merged", "replaced", "overridden", "rendered") and not want: self._module.fail_json( msg="value of config parameter must not be empty for state {0}".format( - state + self.state ) ) - if state == "overridden": + if self.state == "overridden": commands.extend(self._state_overridden(want=want, have=have)) - elif state == "deleted": + elif self.state == "deleted": if not want: for intf in have: commands.extend( @@ -146,12 +165,12 @@ class Interfaces(ConfigBase): obj_in_have = search_obj_in_list(name, have) if not obj_in_have: - obj_in_have = {"name": item["name"]} + obj_in_have = {"name": name} - elif state == "merged": + if self.state in ("merged", "rendered"): commands.extend(self._state_merged(item, obj_in_have)) - elif state == "replaced": + elif self.state == "replaced": commands.extend(self._state_replaced(item, obj_in_have)) return commands -- cgit v1.2.3 From fb6934cf290a3020bed292ae4f7dae7bd2ab28cf Mon Sep 17 00:00:00 2001 From: Rohit Thakur Date: Mon, 4 May 2020 09:49:22 +0530 Subject: tox linters fix Signed-off-by: Rohit Thakur --- .../network/vyos/argspec/interfaces/interfaces.py | 55 ++++++++-------------- .../network/vyos/config/interfaces/interfaces.py | 13 +++-- plugins/modules/vyos_interfaces.py | 4 +- .../vyos_interfaces/tests/cli/_parsed_config.cfg | 2 +- .../vyos_interfaces/tests/cli/gathered.yaml | 2 +- .../targets/vyos_interfaces/tests/cli/parsed.yaml | 2 +- .../vyos_interfaces/tests/cli/rendered.yaml | 2 +- 7 files changed, 32 insertions(+), 48 deletions(-) (limited to 'plugins/module_utils') diff --git a/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py b/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py index 141fedca..fd8b6123 100644 --- a/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/argspec/interfaces/interfaces.py @@ -31,6 +31,7 @@ __metaclass__ = type class InterfacesArgs(object): # pylint: disable=R0903 """The arg spec for the vyos_interfaces module """ + def __init__(self, **kwargs): pass @@ -38,23 +39,11 @@ class InterfacesArgs(object): # pylint: disable=R0903 "config": { "elements": "dict", "options": { - "description": { - "type": "str" - }, - "duplex": { - "choices": ["full", "half", "auto"] - }, - "enabled": { - "default": True, - "type": "bool" - }, - "mtu": { - "type": "int" - }, - "name": { - "required": True, - "type": "str" - }, + "description": {"type": "str"}, + "duplex": {"choices": ["full", "half", "auto"]}, + "enabled": {"default": True, "type": "bool"}, + "mtu": {"type": "int"}, + "name": {"required": True, "type": "str"}, "speed": { "choices": ["auto", "10", "100", "1000", "2500", "10000"], "type": "str", @@ -62,19 +51,10 @@ class InterfacesArgs(object): # pylint: disable=R0903 "vifs": { "elements": "dict", "options": { - "vlan_id": { - "type": "int" - }, - "description": { - "type": "str" - }, - "enabled": { - "default": True, - "type": "bool" - }, - "mtu": { - "type": "int" - }, + "vlan_id": {"type": "int"}, + "description": {"type": "str"}, + "enabled": {"default": True, "type": "bool"}, + "mtu": {"type": "int"}, }, "type": "list", }, @@ -84,12 +64,15 @@ class InterfacesArgs(object): # pylint: disable=R0903 "running_config": {"type": "str"}, "state": { "choices": [ - "merged", "replaced", "overridden", "deleted", "rendered", - "parsed", "gathered" + "merged", + "replaced", + "overridden", + "deleted", + "rendered", + "parsed", + "gathered", ], - "default": - "merged", - "type": - "str", + "default": "merged", + "type": "str", }, } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py index 6d6bf775..51bf98e3 100644 --- a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py @@ -57,9 +57,7 @@ class Interfaces(ConfigBase): facts, _warnings = Facts(self._module).get_facts( self.gather_subset, self.gather_network_resources, data=data ) - interfaces_facts = facts["ansible_network_resources"].get( - "interfaces" - ) + interfaces_facts = facts["ansible_network_resources"].get("interfaces") if not interfaces_facts: return [] return interfaces_facts @@ -99,9 +97,7 @@ class Interfaces(ConfigBase): self._module.fail_json( msg="value of running_config parameter must not be empty for state parsed" ) - result["parsed"] = self.get_interfaces_facts( - data=running_config - ) + result["parsed"] = self.get_interfaces_facts(data=running_config) else: changed_interfaces_facts = [] @@ -139,7 +135,10 @@ class Interfaces(ConfigBase): """ commands = [] - if self.state in ("merged", "replaced", "overridden", "rendered") and not want: + if ( + self.state in ("merged", "replaced", "overridden", "rendered") + and not want + ): self._module.fail_json( msg="value of config parameter must not be empty for state {0}".format( self.state diff --git a/plugins/modules/vyos_interfaces.py b/plugins/modules/vyos_interfaces.py index e2186e87..d15e5d71 100644 --- a/plugins/modules/vyos_interfaces.py +++ b/plugins/modules/vyos_interfaces.py @@ -37,7 +37,7 @@ ANSIBLE_METADATA = { } DOCUMENTATION = """module: vyos_interfaces -short_description: Manages attributes of interfaces for VyOS - interfaces resource module +short_description: Interfaces resource module description: - This module manages the interface attributes on VyOS network devices. - This module supports managing base attributes of Ethernet, Bonding, VXLAN, Loopback @@ -129,6 +129,8 @@ options: - The state I(parsed) reads the configuration from C(running_config) option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the I(parsed) key within the result. + type: str + version_added: "1.0.0" state: description: - The state of the configuration after module completion. diff --git a/tests/integration/targets/vyos_interfaces/tests/cli/_parsed_config.cfg b/tests/integration/targets/vyos_interfaces/tests/cli/_parsed_config.cfg index f3f24c5c..577e2067 100644 --- a/tests/integration/targets/vyos_interfaces/tests/cli/_parsed_config.cfg +++ b/tests/integration/targets/vyos_interfaces/tests/cli/_parsed_config.cfg @@ -12,4 +12,4 @@ set interfaces ethernet eth2 description 'Configured by Ansible' set interfaces ethernet eth2 duplex 'auto' set interfaces ethernet eth2 mtu '1500' set interfaces ethernet eth2 speed 'auto' -set interfaces ethernet eth2 vif 200 description 'VIF - 200' \ No newline at end of file +set interfaces ethernet eth2 vif 200 description 'VIF - 200' diff --git a/tests/integration/targets/vyos_interfaces/tests/cli/gathered.yaml b/tests/integration/targets/vyos_interfaces/tests/cli/gathered.yaml index be630307..62485b97 100644 --- a/tests/integration/targets/vyos_interfaces/tests/cli/gathered.yaml +++ b/tests/integration/targets/vyos_interfaces/tests/cli/gathered.yaml @@ -31,4 +31,4 @@ - result['changed'] == false always: - - include_tasks: _remove_config.yaml \ No newline at end of file + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_interfaces/tests/cli/parsed.yaml b/tests/integration/targets/vyos_interfaces/tests/cli/parsed.yaml index 32af476a..8b4b4c14 100644 --- a/tests/integration/targets/vyos_interfaces/tests/cli/parsed.yaml +++ b/tests/integration/targets/vyos_interfaces/tests/cli/parsed.yaml @@ -39,4 +39,4 @@ - result['changed'] == false always: - - include_tasks: _remove_config.yaml \ No newline at end of file + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_interfaces/tests/cli/rendered.yaml b/tests/integration/targets/vyos_interfaces/tests/cli/rendered.yaml index 1d2e1081..85b5021a 100644 --- a/tests/integration/targets/vyos_interfaces/tests/cli/rendered.yaml +++ b/tests/integration/targets/vyos_interfaces/tests/cli/rendered.yaml @@ -53,4 +53,4 @@ - result['changed'] == false always: - - include_tasks: _remove_config.yaml \ No newline at end of file + - include_tasks: _remove_config.yaml -- cgit v1.2.3