diff --git a/lib/ansible/module_utils/network/exos/argspec/l2_interfaces/__init__.py b/lib/ansible/module_utils/network/exos/argspec/l2_interfaces/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/lib/ansible/module_utils/network/exos/argspec/l2_interfaces/l2_interfaces.py b/lib/ansible/module_utils/network/exos/argspec/l2_interfaces/l2_interfaces.py new file mode 100644 index 00000000000..3c6f250811c --- /dev/null +++ b/lib/ansible/module_utils/network/exos/argspec/l2_interfaces/l2_interfaces.py @@ -0,0 +1,48 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2019 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +############################################# +# WARNING # +############################################# +# +# This file is auto generated by the resource +# module builder playbook. +# +# Do not edit this file manually. +# +# Changes to this file will be over written +# by the resource module builder. +# +# Changes should be made in the model used to +# generate this file or in the resource module +# builder template. +# +############################################# +""" +The arg spec for the exos_l2_interfaces module +""" +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +class L2_interfacesArgs(object): # pylint: disable=R0903 + """The arg spec for the exos_l2_interfaces module + """ + def __init__(self, **kwargs): + pass + + argument_spec = { + 'config': { + 'elements': 'dict', + 'options': { + 'access': {'options': {'vlan': {'type': 'int'}}, + 'type': 'dict'}, + 'name': {'required': True, 'type': 'str'}, + 'trunk': {'options': {'native_vlan': {'type': 'int'}, 'trunk_allowed_vlans': {'type': 'list'}}, + 'type': 'dict'}}, + 'type': 'list'}, + 'state': {'choices': ['merged', 'replaced', 'overridden', 'deleted'], 'default': 'merged', 'type': 'str'} + } # pylint: disable=C0301 diff --git a/lib/ansible/module_utils/network/exos/config/l2_interfaces/__init__.py b/lib/ansible/module_utils/network/exos/config/l2_interfaces/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/lib/ansible/module_utils/network/exos/config/l2_interfaces/l2_interfaces.py b/lib/ansible/module_utils/network/exos/config/l2_interfaces/l2_interfaces.py new file mode 100644 index 00000000000..3644ac4501a --- /dev/null +++ b/lib/ansible/module_utils/network/exos/config/l2_interfaces/l2_interfaces.py @@ -0,0 +1,294 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2019 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +""" +The exos_l2_interfaces class +It is in this file where the current configuration (as dict) +is compared to the provided configuration (as dict) and the command set +necessary to bring the current configuration to it's desired end-state is +created +""" +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import json +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 +from ansible.module_utils.network.exos.facts.facts import Facts +from ansible.module_utils.network.exos.exos import send_requests + + +class L2_interfaces(ConfigBase): + """ + The exos_l2_interfaces class + """ + + gather_subset = [ + '!all', + '!min', + ] + + gather_network_resources = [ + 'l2_interfaces', + ] + + L2_INTERFACE_NATIVE = { + "data": { + "openconfig-vlan:config": { + "interface-mode": "TRUNK", + "native-vlan": None, + "trunk-vlans": [] + } + }, + "method": "PATCH", + "path": None + } + + L2_INTERFACE_TRUNK = { + "data": { + "openconfig-vlan:config": { + "interface-mode": "TRUNK", + "trunk-vlans": [] + } + }, + "method": "PATCH", + "path": None + } + + L2_INTERFACE_ACCESS = { + "data": { + "openconfig-vlan:config": { + "interface-mode": "ACCESS", + "access-vlan": None + } + }, + "method": "PATCH", + "path": None + } + + L2_PATH = "/rest/restconf/data/openconfig-interfaces:interfaces/interface=" + + def __init__(self, module): + super(L2_interfaces, self).__init__(module) + + def get_l2_interfaces_facts(self): + """ 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) + l2_interfaces_facts = facts['ansible_network_resources'].get( + 'l2_interfaces') + if not l2_interfaces_facts: + return [] + return l2_interfaces_facts + + def execute_module(self): + """ Execute the module + + :rtype: A dictionary + :returns: The result from module execution + """ + result = {'changed': False} + warnings = list() + requests = list() + + existing_l2_interfaces_facts = self.get_l2_interfaces_facts() + requests.extend(self.set_config(existing_l2_interfaces_facts)) + if requests: + if not self._module.check_mode: + send_requests(self._module, requests=requests) + result['changed'] = True + result['requests'] = requests + + changed_l2_interfaces_facts = self.get_l2_interfaces_facts() + + result['before'] = existing_l2_interfaces_facts + if result['changed']: + result['after'] = changed_l2_interfaces_facts + + result['warnings'] = warnings + return result + + def set_config(self, existing_l2_interfaces_facts): + """ Collect the configuration from the args passed to the module, + collect the current configuration (as a dict from facts) + + :rtype: A list + :returns: the requests necessary to migrate the current configuration + to the desired configuration + """ + want = self._module.params['config'] + have = existing_l2_interfaces_facts + resp = self.set_state(want, have) + return to_list(resp) + + def set_state(self, want, have): + """ Select the appropriate function based on the state provided + + :param want: the desired configuration as a dictionary + :param have: the current configuration as a dictionary + :rtype: A list + :returns: the requests necessary to migrate the current configuration + to the desired configuration + """ + state = self._module.params['state'] + if state == 'overridden': + requests = self._state_overridden(want, have) + elif state == 'deleted': + requests = self._state_deleted(want, have) + elif state == 'merged': + requests = self._state_merged(want, have) + elif state == 'replaced': + requests = self._state_replaced(want, have) + return requests + + def _state_replaced(self, want, have): + """ The request generator when state is replaced + + :rtype: A list + :returns: the requests necessary to migrate the current configuration + to the desired configuration + """ + requests = [] + for w in want: + for h in have: + if w["name"] == h["name"]: + if dict_diff(w, h): + l2_request = self._update_patch_request(w, h) + l2_request["data"] = json.dumps(l2_request["data"]) + requests.append(l2_request) + break + + return requests + + def _state_overridden(self, want, have): + """ The request generator when state is overridden + + :rtype: A list + :returns: the requests necessary to migrate the current configuration + to the desired configuration + """ + requests = [] + have_copy = [] + for w in want: + for h in have: + if w["name"] == h["name"]: + if dict_diff(w, h): + l2_request = self._update_patch_request(w, h) + l2_request["data"] = json.dumps(l2_request["data"]) + requests.append(l2_request) + have_copy.append(h) + break + + for h in have: + if h not in have_copy: + l2_delete = self._update_delete_request(h) + if l2_delete["path"]: + l2_delete["data"] = json.dumps(l2_delete["data"]) + requests.append(l2_delete) + + return requests + + def _state_merged(self, want, have): + """ The request generator when state is merged + + :rtype: A list + :returns: the requests necessary to merge the provided into + the current configuration + """ + requests = [] + for w in want: + for h in have: + if w["name"] == h["name"]: + if dict_diff(h, w): + l2_request = self._update_patch_request(w, h) + l2_request["data"] = json.dumps(l2_request["data"]) + requests.append(l2_request) + break + + return requests + + def _state_deleted(self, want, have): + """ The request generator when state is deleted + + :rtype: A list + :returns: the requests necessary to remove the current configuration + of the provided objects + """ + requests = [] + if want: + for w in want: + for h in have: + if w["name"] == h["name"]: + l2_delete = self._update_delete_request(h) + if l2_delete["path"]: + l2_delete["data"] = json.dumps(l2_delete["data"]) + requests.append(l2_delete) + break + + else: + for h in have: + l2_delete = self._update_delete_request(h) + if l2_delete["path"]: + l2_delete["data"] = json.dumps(l2_delete["data"]) + requests.append(l2_delete) + + return requests + + def _update_patch_request(self, want, have): + + facts, _warnings = Facts(self._module).get_facts( + self.gather_subset, ['vlans', ]) + vlans_facts = facts['ansible_network_resources'].get('vlans') + + vlan_id = [] + + for vlan in vlans_facts: + vlan_id.append(vlan['vlan_id']) + + if want.get("access"): + if want["access"]["vlan"] in vlan_id: + l2_request = deepcopy(self.L2_INTERFACE_ACCESS) + l2_request["data"]["openconfig-vlan:config"]["access-vlan"] = want["access"]["vlan"] + l2_request["path"] = self.L2_PATH + str(want["name"]) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" + else: + self._module.fail_json(msg="VLAN %s does not exist" % (want["access"]["vlan"])) + + elif want.get("trunk"): + if want["trunk"]["native_vlan"]: + if want["trunk"]["native_vlan"] in vlan_id: + l2_request = deepcopy(self.L2_INTERFACE_NATIVE) + l2_request["data"]["openconfig-vlan:config"]["native-vlan"] = want["trunk"]["native_vlan"] + l2_request["path"] = self.L2_PATH + str(want["name"]) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" + for vlan in want["trunk"]["trunk_allowed_vlans"]: + if int(vlan) in vlan_id: + l2_request["data"]["openconfig-vlan:config"]["trunk-vlans"].append(int(vlan)) + else: + self._module.fail_json(msg="VLAN %s does not exist" % (vlan)) + else: + self._module.fail_json(msg="VLAN %s does not exist" % (want["trunk"]["native_vlan"])) + else: + l2_request = deepcopy(self.L2_INTERFACE_TRUNK) + l2_request["path"] = self.L2_PATH + str(want["name"]) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" + for vlan in want["trunk"]["trunk_allowed_vlans"]: + if int(vlan) in vlan_id: + l2_request["data"]["openconfig-vlan:config"]["trunk-vlans"].append(int(vlan)) + else: + self._module.fail_json(msg="VLAN %s does not exist" % (vlan)) + return l2_request + + def _update_delete_request(self, have): + + l2_request = deepcopy(self.L2_INTERFACE_ACCESS) + + if have["access"] and have["access"]["vlan"] != 1 or have["trunk"] or not have["access"]: + l2_request["data"]["openconfig-vlan:config"]["access-vlan"] = 1 + l2_request["path"] = self.L2_PATH + str(have["name"]) + "/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" + + return l2_request diff --git a/lib/ansible/module_utils/network/exos/facts/facts.py b/lib/ansible/module_utils/network/exos/facts/facts.py index e5380800ebd..6c932b7601d 100644 --- a/lib/ansible/module_utils/network/exos/facts/facts.py +++ b/lib/ansible/module_utils/network/exos/facts/facts.py @@ -17,7 +17,7 @@ from ansible.module_utils.network.exos.facts.lldp_global.lldp_global import Lldp from ansible.module_utils.network.exos.facts.vlans.vlans import VlansFacts from ansible.module_utils.network.exos.facts.legacy.base import Default, Hardware, Interfaces, Config from ansible.module_utils.network.exos.facts.lldp_interfaces.lldp_interfaces import Lldp_interfacesFacts - +from ansible.module_utils.network.exos.facts.l2_interfaces.l2_interfaces import L2_interfacesFacts FACT_LEGACY_SUBSETS = dict( default=Default, @@ -29,6 +29,7 @@ FACT_RESOURCE_SUBSETS = dict( lldp_global=Lldp_globalFacts, vlans=VlansFacts, lldp_interfaces=Lldp_interfacesFacts, + l2_interfaces=L2_interfacesFacts, ) diff --git a/lib/ansible/module_utils/network/exos/facts/l2_interfaces/__init__.py b/lib/ansible/module_utils/network/exos/facts/l2_interfaces/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/lib/ansible/module_utils/network/exos/facts/l2_interfaces/l2_interfaces.py b/lib/ansible/module_utils/network/exos/facts/l2_interfaces/l2_interfaces.py new file mode 100644 index 00000000000..84e4b6fadcb --- /dev/null +++ b/lib/ansible/module_utils/network/exos/facts/l2_interfaces/l2_interfaces.py @@ -0,0 +1,92 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2019 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +""" +The exos l2_interfaces fact class +It is in this file the configuration is collected from the device +for a given resource, parsed, and the facts tree is populated +based on the configuration. +""" +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import re +from copy import deepcopy + +from ansible.module_utils.network.common import utils +from ansible.module_utils.network.exos.argspec.l2_interfaces.l2_interfaces import L2_interfacesArgs +from ansible.module_utils.network.exos.exos import send_requests + + +class L2_interfacesFacts(object): + """ The exos l2_interfaces fact class + """ + def __init__(self, module, subspec='config', options='options'): + self._module = module + self.argument_spec = L2_interfacesArgs.argument_spec + spec = deepcopy(self.argument_spec) + if subspec: + if options: + facts_argument_spec = spec[subspec][options] + else: + facts_argument_spec = spec[subspec] + else: + facts_argument_spec = spec + + self.generated_spec = utils.generate_dict(facts_argument_spec) + + def populate_facts(self, connection, ansible_facts, data=None): + """ Populate the facts for l2_interfaces + :param connection: the device connection + :param ansible_facts: Facts dictionary + :param data: previously collected conf + :rtype: dictionary + :returns: facts + """ + + if not data: + request = [{ + "path": "/rest/restconf/data/openconfig-interfaces:interfaces", + "method": "GET" + }] + data = send_requests(self._module, requests=request) + + objs = [] + if data: + for d in data[0]["openconfig-interfaces:interfaces"]["interface"]: + obj = self.render_config(self.generated_spec, d) + if obj: + objs.append(obj) + + ansible_facts['ansible_network_resources'].pop('l2_interfaces', None) + facts = {} + if objs: + params = utils.validate_config(self.argument_spec, {'config': objs}) + facts['l2_interfaces'] = params['config'] + + ansible_facts['ansible_network_resources'].update(facts) + return ansible_facts + + def render_config(self, spec, conf): + """ + Render config as dictionary structure and delete keys + from spec for null values + + :param spec: The facts tree, generated from the argspec + :param conf: The configuration + :rtype: dictionary + :returns: The generated config + """ + config = deepcopy(spec) + if conf["config"]["type"] == "ethernetCsmacd": + conf_dict = conf["openconfig-if-ethernet:ethernet"]["openconfig-vlan:switched-vlan"]["config"] + config["name"] = conf["name"] + if conf_dict["interface-mode"] == "ACCESS": + config["access"]["vlan"] = conf_dict.get("access-vlan") + else: + if 'native-vlan' in conf_dict: + config["trunk"]["native_vlan"] = conf_dict.get("native-vlan") + config["trunk"]["trunk_allowed_vlans"] = conf_dict.get("trunk-vlans") + return utils.remove_empties(config) diff --git a/lib/ansible/modules/network/exos/exos_l2_interfaces.py b/lib/ansible/modules/network/exos/exos_l2_interfaces.py new file mode 100644 index 00000000000..4655dc732df --- /dev/null +++ b/lib/ansible/modules/network/exos/exos_l2_interfaces.py @@ -0,0 +1,1137 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright 2019 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +############################################# +# WARNING # +############################################# +# +# This file is auto generated by the resource +# module builder playbook. +# +# Do not edit this file manually. +# +# Changes to this file will be over written +# by the resource module builder. +# +# Changes should be made in the model used to +# generate this file or in the resource module +# builder template. +# +############################################# +""" +The module file for exos_l2_interfaces +""" + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = """ +--- +module: exos_l2_interfaces +version_added: "2.10" +short_description: Manage L2 interfaces on Extreme Networks EXOS devices. +description: This module provides declarative management of L2 interfaces on Extreme Networks EXOS network devices. +author: Jayalakshmi Viswanathan (@jayalakshmiV) +notes: + - Tested against EXOS 30.2.1.8 + - This module works with connection C(httpapi). + See L(EXOS Platform Options,../network/user_guide/platform_exos.html) +options: + config: + description: A dictionary of L2 interfaces options + type: list + elements: dict + suboptions: + name: + description: + - Name of the interface + type: str + required: True + access: + description: + - Switchport mode access command to configure the interface as a layer 2 access. + type: dict + suboptions: + vlan: + description: + - Configure given VLAN in access port. It's used as the access VLAN ID. + type: int + trunk: + description: + - Switchport mode trunk command to configure the interface as a Layer 2 trunk. + type: dict + suboptions: + native_vlan: + description: + - Native VLAN to be configured in trunk port. It's used as the trunk native VLAN ID. + type: int + trunk_allowed_vlans: + description: + - List of allowed VLANs in a given trunk port. These are the only VLANs that will be configured on the trunk. + type: list + state: + description: + - The state the configuration should be left in + type: str + choices: + - merged + - replaced + - overridden + - deleted + default: merged +""" +EXAMPLES = """ +# Using deleted + +# Before state: +# ------------- +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 1, +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 10, +# "trunk-vlans": [ +# 20, +# 30 +# ] +# } +# } +# } +# } +# ] +# } +# } + +- name: Delete L2 interface configuration for the given arguments + exos_l2_interfaces: + config: + - name: '3' + state: deleted + +# Module Execution Results: +# ------------------------- +# +# "before": [ +# { +# "access": { +# "vlan": 10 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": null, +# "name": "2", +# "trunk": { +# "native_vlan": 1, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# }, +# { +# "access": null, +# "name": "3", +# "trunk": { +# "native_vlan": 10, +# "trunk_allowed_vlans": [ +# 20, +# 30 +# ] +# } +# } +# ], +# +# "requests": [ +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 1, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# } +# ], +# +# "after": [ +# { +# "access": { +# "vlan": 10 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": null, +# "name": "2", +# "trunk": { +# "native_vlan": 1, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# }, +# { +# "access": { +# "vlan": 1 +# }, +# "name": "3", +# "trunk": null +# } +# ] +# +# After state: +# ------------- +# +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 1, +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# } +# } +# } +# } +# ] +# } +# } + + +# Using deleted without any config passed +#"(NOTE: This will delete all of configured resource module attributes from each configured interface)" + +# Before state: +# ------------- +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 1, +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 10, +# "trunk-vlans": [ +# 20, +# 30 +# ] +# } +# } +# } +# } +# ] +# } +# } + +- name: Delete L2 interface configuration for the given arguments + exos_l2_interfaces: + state: deleted + +# Module Execution Results: +# ------------------------- +# +# "before": [ +# { +# "access": { +# "vlan": 10 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": null, +# "name": "2", +# "trunk": { +# "native_vlan": 1, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# }, +# { +# "access": null, +# "name": "3", +# "trunk": { +# "native_vlan": 10, +# "trunk_allowed_vlans": [ +# 20, +# 30 +# ] +# } +# } +# ], +# +# "requests": [ +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 1, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 1, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 1, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# } +# ], +# +# "after": [ +# { +# "access": { +# "vlan": 1 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 1 +# }, +# "name": "2", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 1 +# }, +# "name": "3", +# "trunk": null +# } +# ] +# +# After state: +# ------------- +# +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# } +# } +# } +# } +# ] +# } +# } + + +# Using merged + +# Before state: +# ------------- +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# }, +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# }, +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# }, +# } +# } +# }, +# ] +# } +# } + +- name: Merge provided configuration with device configuration + exos_l2_interfaces: + config: + - access: + vlan: 10 + name: '1' + - name: '2' + trunk: + trunk_allowed_vlans: 10 + - name: '3' + trunk: + native_vlan: 10 + trunk_allowed_vlans: 20 + state: merged + +# Module Execution Results: +# ------------------------- +# +# "before": [ +# { +# "access": { +# "vlan": 1 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 1 +# }, +# "name": "2", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 1 +# }, +# "name": "3", +# "trunk": null +# } +# ], +# +# "requests": [ +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 10, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "trunk-vlans": [10], +# "interface-mode": "TRUNK" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "native-vlan": 10, +# "trunk-vlans": [20], +# "interface-mode": "TRUNK" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# } +# ], +# +# "after": [ +# { +# "access": { +# "vlan": 10 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": null, +# "name": "2", +# "trunk": { +# "native_vlan": 1, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# }, +# { +# "access": null, +# "name": "3", +# "trunk": { +# "native_vlan": 10, +# "trunk_allowed_vlans": [ +# 20 +# ] +# } +# } +# ] +# +# After state: +# ------------- +# +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 1, +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 10, +# "trunk-vlans": [ +# 20 +# ] +# } +# } +# } +# }, +# ] +# } +# } + + +# Using overridden + +# Before state: +# ------------- +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 1, +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 10, +# "trunk-vlans": [ +# 20, +# 30 +# ] +# } +# } +# } +# } +# ] +# } +# } + +- name: Overrride device configuration of all L2 interfaces with provided configuration + exos_l2_interfaces: + config: + - access: + vlan: 10 + name: '2' + state: overridden + +# Module Execution Results: +# ------------------------- +# +# "before": [ +# { +# "access": { +# "vlan": 10 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": null, +# "name": "2", +# "trunk": { +# "native_vlan": 1, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# }, +# { +# "access": null, +# "name": "3", +# "trunk": { +# "native_vlan": 10, +# "trunk_allowed_vlans": [ +# 20, +# 30 +# ] +# } +# } +# ], +# +# "requests": [ +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 1, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 10, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# } +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 1, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# } +# ], +# +# "after": [ +# { +# "access": { +# "vlan": 1 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 10 +# }, +# "name": "2", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 1 +# }, +# "name": "3", +# "trunk": null +# } +# ] +# +# After state: +# ------------- +# +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 1 +# } +# } +# } +# } +# ] +# } +# } + + +# Using replaced + +# Before state: +# ------------- +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 10 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 20 +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 1, +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# } +# ] +# } +# } + +- name: Replace device configuration of listed L2 interfaces with provided configuration + exos_l2_interfaces: + config: + - access: + vlan: 20 + name: '1' + - name: '2' + trunk: + trunk_allowed_vlans: 10 + - name: '3' + trunk: + native_vlan: 10 + trunk_allowed_vlan: 20,30 + state: replaced + +# Module Execution Results: +# ------------------------- +# +# "before": [ +# { +# "access": { +# "vlan": 10 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": { +# "vlan": 20 +# }, +# "name": "2", +# "trunk": null +# }, +# { +# "access": null, +# "name": "3", +# "trunk": { +# "native_vlan": 1, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# } +# ], +# +# "requests": [ +# { +# "data": { +# "openconfig-vlan:config": { +# "access-vlan": 20, +# "interface-mode": "ACCESS" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "trunk-vlans": [10], +# "interface-mode": "TRUNK" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# }, +# { +# "data": { +# "openconfig-vlan:config": { +# "native-vlan": 10, +# "trunk-vlans": [20, 30] +# "interface-mode": "TRUNK" +# } +# } +# "method": "PATCH", +# "path": "rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config" +# } +# ], +# +# "after": [ +# { +# "access": { +# "vlan": 20 +# }, +# "name": "1", +# "trunk": null +# }, +# { +# "access": null, +# "name": "2", +# "trunk": { +# "native_vlan": null, +# "trunk_allowed_vlans": [ +# 10 +# ] +# } +# }, +# { +# "access": null, +# "name": "3", +# "trunk": { +# "native_vlan": 10, +# "trunk_allowed_vlans": [ +# 20, +# 30 +# ] +# } +# } +# ] +# +# After state: +# ------------- +# +# path: /rest/restconf/data/openconfig-interfaces:interfaces/ +# method: GET +# data: +# { +# "openconfig-interfaces:interfaces": { +# "interface": [ +# { +# "name": "1", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "ACCESS", +# "access-vlan": 20 +# } +# } +# } +# }, +# { +# "name": "2", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "trunk-vlans": [ +# 10 +# ] +# } +# } +# } +# }, +# { +# "name": "3", +# "openconfig-if-ethernet:ethernet": { +# "openconfig-vlan:switched-vlan": { +# "config": { +# "interface-mode": "TRUNK", +# "native-vlan": 10, +# "trunk-vlans": [ +# 20, +# 30 +# ] +# } +# } +# } +# } +# ] +# } +# } + + +""" +RETURN = """ +before: + description: The configuration prior to the model invocation. + returned: always + sample: > + The configuration returned will always be in the same format + of the parameters above. + type: list +after: + description: The resulting configuration model invocation. + returned: when changed + sample: > + The configuration returned will always be in the same format + of the parameters above. + type: list +requests: + description: The set of requests pushed to the remote device. + returned: always + type: list + sample: [{"data": "...", "method": "...", "path": "..."}, {"data": "...", "method": "...", "path": "..."}, {"data": "...", "method": "...", "path": "..."}] +""" + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.network.exos.argspec.l2_interfaces.l2_interfaces import L2_interfacesArgs +from ansible.module_utils.network.exos.config.l2_interfaces.l2_interfaces import L2_interfaces + + +def main(): + """ + Main entry point for module execution + + :returns: the result form module invocation + """ + required_if = [('state', 'merged', ('config', )), + ('state', 'replaced', ('config', ))] + module = AnsibleModule(argument_spec=L2_interfacesArgs.argument_spec, required_if=required_if, + supports_check_mode=True) + + result = L2_interfaces(module).execute_module() + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/exos_l2_interfaces/defaults/main.yaml b/test/integration/targets/exos_l2_interfaces/defaults/main.yaml new file mode 100644 index 00000000000..164afead284 --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "[^_].*" +test_items: [] diff --git a/test/integration/targets/exos_l2_interfaces/tasks/httpapi.yaml b/test/integration/targets/exos_l2_interfaces/tasks/httpapi.yaml new file mode 100644 index 00000000000..7c29713da64 --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tasks/httpapi.yaml @@ -0,0 +1,19 @@ +--- +- name: Collect all httpapi test cases + find: + paths: "{{ role_path }}/tests/httpapi" + patterns: "{{ testcase }}.yaml" + use_regex: true + register: test_cases + delegate_to: localhost + +- name: Set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: Run test case (connection=httpapi) + include: "{{ test_case_to_run }}" + vars: + ansible_connection: httpapi + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/exos_l2_interfaces/tasks/main.yaml b/test/integration/targets/exos_l2_interfaces/tasks/main.yaml new file mode 100644 index 00000000000..22eac154642 --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: httpapi.yaml, tags: ['httpapi'] } diff --git a/test/integration/targets/exos_l2_interfaces/tests/httpapi/_populate_config.yaml b/test/integration/targets/exos_l2_interfaces/tests/httpapi/_populate_config.yaml new file mode 100644 index 00000000000..a69a578a64f --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tests/httpapi/_populate_config.yaml @@ -0,0 +1,12 @@ +--- +- name: Populate config + exos_config: + lines: + - create vlan vlan_10 tag 10 + - create vlan vlan_20 tag 20 + - create vlan vlan_30 tag 30 + - create vlan vlan_40 tag 40 + - configure vlan 10 add ports 1 untagged + - configure vlan 20 add ports 2 tagged + - configure vlan 20 add ports 3 untagged + - configure vlan 30 add ports 3 tagged diff --git a/test/integration/targets/exos_l2_interfaces/tests/httpapi/_reset_config.yaml b/test/integration/targets/exos_l2_interfaces/tests/httpapi/_reset_config.yaml new file mode 100644 index 00000000000..c3a3ecf9449 --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tests/httpapi/_reset_config.yaml @@ -0,0 +1,7 @@ +--- +- name: Reset config + exos_config: + lines: + - configure vlan Default add ports all untagged + - configure vlan 2-4094 delete ports all + - delete vlan 2-4094 diff --git a/test/integration/targets/exos_l2_interfaces/tests/httpapi/deleted.yaml b/test/integration/targets/exos_l2_interfaces/tests/httpapi/deleted.yaml new file mode 100644 index 00000000000..23921ee9a23 --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tests/httpapi/deleted.yaml @@ -0,0 +1,96 @@ +--- +- debug: + msg: "Start exos_l2_interfaces deleted integration tests ansible_connection={{ ansible_connection }}" + +- include_tasks: _reset_config.yaml + ignore_errors: yes + +- include_tasks: _populate_config.yaml + +- set_fact: + config: + - access: + vlan: 1 + name: '2' + trunk: null + - access: + vlan: 1 + name: '3' + trunk: null + config_all: + - access: + vlan: 1 + name: '1' + trunk: null + +- exos_facts: + gather_network_resources: l2_interfaces + +- block: + - name: Delete L2 interface configuration + exos_l2_interfaces: &deleted + config: + - name: '2' + - name: '3' + state: deleted + register: result + + - name: Assert that correct set of results were generated + assert: + that: + - "deleted['requests_1']|symmetric_difference(result['requests']) == []" + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that after dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.after) == []" + - "config|difference(ansible_facts.network_resources.l2_interfaces) == []" + + - name: Delete L2 interface configuration + exos_l2_interfaces: + config: + state: deleted + register: result + + - name: Assert that correct set of results were generated + assert: + that: + - "deleted['requests_2']|symmetric_difference(result['requests']) == []" + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that after dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.after) == []" + - "config_all|difference(ansible_facts.network_resources.l2_interfaces) == []" + + - name: Delete attributes of all configured interfaces (IDEMPOTENT) + exos_l2_interfaces: *deleted + register: result + + - name: Assert that the previous task was idempotent + assert: + that: + - "result.changed == false" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that the before dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + + - set_fact: + expected_config: "{{ config }} + {{ config_all }}" + + - assert: + that: + - "expected_config|difference(ansible_facts.network_resources.l2_interfaces) == []" diff --git a/test/integration/targets/exos_l2_interfaces/tests/httpapi/merged.yaml b/test/integration/targets/exos_l2_interfaces/tests/httpapi/merged.yaml new file mode 100644 index 00000000000..ee2ceba3d97 --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tests/httpapi/merged.yaml @@ -0,0 +1,85 @@ +--- +- debug: + msg: "START exos_l2_interfaces merged integration tests on connection={{ ansible_connection }}" + +- include_tasks: _reset_config.yaml + ignore_errors: yes + +- include_tasks: _populate_config.yaml + +- set_fact: + config: + - access: + vlan: 20 + name: '1' + trunk: null + - access: null + name: '2' + trunk: + native_vlan: 10 + trunk_allowed_vlans: + - 20 + - 30 + invalid_config: + - access: null + name: '3' + trunk: + trunk_allowed_vlans: + - 50 + +- exos_facts: + gather_network_resources: l2_interfaces + +- block: + - name: Merge the provided configuration with the existing running configuration + exos_l2_interfaces: &merged + config: "{{ config }}" + state: merged + register: result + + - name: Assert that correct set of results were generated + assert: + that: + - "merged['requests']|symmetric_difference(result['requests']) == []" + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that after dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.after) == []" + - "config|difference(ansible_facts.network_resources.l2_interfaces) == []" + + - name: Merge the provided configuration with the existing running configuration (IDEMPOTENT) + exos_l2_interfaces: *merged + register: result + + - name: Assert that the previous task was idempotent + assert: + that: + - "result['changed'] == false" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that the before dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + - "config|difference(ansible_facts.network_resources.l2_interfaces) == []" + + - name: Merge provided configuration with existing configuration + exos_l2_interfaces: + config: "{{ invalid_config }}" + state: merged + register: result + ignore_errors: yes + + - name: Assert that the previous task failed + assert: + that: + - "result.changed == false" + - "result.failed == true" + - "result.msg == 'VLAN 50 does not exist'" diff --git a/test/integration/targets/exos_l2_interfaces/tests/httpapi/overridden.yaml b/test/integration/targets/exos_l2_interfaces/tests/httpapi/overridden.yaml new file mode 100644 index 00000000000..aa48e3d832a --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tests/httpapi/overridden.yaml @@ -0,0 +1,76 @@ +--- +- debug: + msg: "Start exos_l2_interfaces overridden integration tests ansible_connection={{ ansible_connection }}" + +- include_tasks: _reset_config.yaml + ignore_errors: yes + +- include_tasks: _populate_config.yaml + +- set_fact: + config: + - access: null + name: '1' + trunk: + native_vlan: 10 + trunk_allowed_vlans: + - 20 + - 30 + config_all: + - access: + vlan: 1 + name: '2' + trunk: null + - access: + vlan: 1 + name: '3' + trunk: null + +- exos_facts: + gather_network_resources: l2_interfaces + +- block: + - name: Override device L2 interface configuration with provided configurations + exos_l2_interfaces: &overridden + config: "{{ config }}" + state: overridden + register: result + + - name: Assert that correct set of results were generated + assert: + that: + - "overridden['requests']|symmetric_difference(result['requests']) == []" + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that after dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.after) == []" + + - set_fact: + expected_config: "{{ config }} + {{ config_all }}" + + - assert: + that: + - "expected_config|difference(ansible_facts.network_resources.l2_interfaces) == []" + + - name: Override device LLDP interface configuration with provided configurations (IDEMPOTENT) + exos_l2_interfaces: *overridden + register: result + + - name: Assert that task was idempotent + assert: + that: + - "result['changed'] == false" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that the before dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + - "expected_config|difference(ansible_facts.network_resources.l2_interfaces) == []" diff --git a/test/integration/targets/exos_l2_interfaces/tests/httpapi/replaced.yaml b/test/integration/targets/exos_l2_interfaces/tests/httpapi/replaced.yaml new file mode 100644 index 00000000000..7018e45cfba --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/tests/httpapi/replaced.yaml @@ -0,0 +1,63 @@ +--- +- debug: + msg: "START exos_l2_interfaces replaced integration tests on connection={{ ansible_connection }}" + +- include_tasks: _reset_config.yaml + +- include_tasks: _populate_config.yaml + +- set_fact: + config: + - access: null + name: '2' + trunk: + native_vlan: 30 + trunk_allowed_vlans: + - 40 + - access: + vlan: 10 + name: '3' + trunk: null + +- exos_facts: + gather_network_resources: l2_interfaces + +- block: + - name: Replace L2 interface configuration with provided configuration + exos_l2_interfaces: &replaced + config: "{{ config }}" + state: replaced + register: result + + - name: Assert that correct set of results were generated + assert: + that: + - "replaced['requests']|symmetric_difference(result['requests']) == []" + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that after dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.after) == []" + - "config|difference(ansible_facts.network_resources.l2_interfaces) == []" + + - name: Replace L2 interface configuration with provided configuration (IDEMPOTENT) + exos_l2_interfaces: *replaced + register: result + + - name: Assert that task was idempotent + assert: + that: + - "result['changed'] == false" + + - exos_facts: + gather_network_resources: l2_interfaces + + - name: Assert that the before dicts were correctly generated + assert: + that: + - "ansible_facts.network_resources.l2_interfaces|symmetric_difference(result.before) == []" + - "config|difference(ansible_facts.network_resources.l2_interfaces) == []" diff --git a/test/integration/targets/exos_l2_interfaces/vars/main.yaml b/test/integration/targets/exos_l2_interfaces/vars/main.yaml new file mode 100644 index 00000000000..66b6fee4aff --- /dev/null +++ b/test/integration/targets/exos_l2_interfaces/vars/main.yaml @@ -0,0 +1,44 @@ +--- +merged: + requests: + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 20}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + - data: '{"openconfig-vlan:config": {"trunk-vlans": [20, 30], "interface-mode": "TRUNK", "native-vlan": 10}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + +replaced: + requests: + - data: '{"openconfig-vlan:config": {"trunk-vlans": [40], "interface-mode": "TRUNK", "native-vlan": 30}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 10}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + +overridden: + requests: + - data: '{"openconfig-vlan:config": {"trunk-vlans": [20, 30], "interface-mode": "TRUNK", "native-vlan": 10}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 1}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 1}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + +deleted: + requests_1: + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 1}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=2/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 1}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=3/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config + + requests_2: + - data: '{"openconfig-vlan:config": {"interface-mode": "ACCESS", "access-vlan": 1}}' + method: PATCH + path: /rest/restconf/data/openconfig-interfaces:interfaces/interface=1/openconfig-if-ethernet:ethernet/openconfig-vlan:switched-vlan/config