From a07af2a1f7afdb1a04fcab5164436238b9b5adc1 Mon Sep 17 00:00:00 2001
From: Samer Deeb <samerd@mellanox.com>
Date: Thu, 23 Aug 2018 23:59:10 -0700
Subject: [PATCH] issue:43021 add support for onyx version 3.6.6000 and above
 (#44527)

* issue:43021 add support for onyx version 3.6.6000

Signed-off-by: Samer Deeb <samerd@mellanox.com>

* issue:43021 add support for onyx version 3.6.6000

Signed-off-by: Samer Deeb <samerd@mellanox.com>
---
 .../modules/network/onyx/onyx_interface.py    | 16 +++--
 .../modules/network/onyx/onyx_l2_interface.py |  4 ++
 .../modules/network/onyx/onyx_l3_interface.py | 58 +++++++++++--------
 .../network/onyx/test_onyx_interface.py       |  5 ++
 .../network/onyx/test_onyx_l2_interface.py    |  5 ++
 .../network/onyx/test_onyx_l3_interface.py    |  5 ++
 6 files changed, 65 insertions(+), 28 deletions(-)

diff --git a/lib/ansible/modules/network/onyx/onyx_interface.py b/lib/ansible/modules/network/onyx/onyx_interface.py
index 2b39b16a1ab..207fceef9ce 100644
--- a/lib/ansible/modules/network/onyx/onyx_interface.py
+++ b/lib/ansible/modules/network/onyx/onyx_interface.py
@@ -335,14 +335,22 @@ class OnyxInterfaceModule(BaseOnyxModule):
         return get_interfaces_config(self._module, self._interface_type)
 
     def load_current_config(self):
+        self._os_version = self._get_os_version()
         self._current_config = dict()
         config = self._get_interfaces_config()
         if not config:
             return
-
-        for item in config:
-            name = self.get_if_name(item)
-            self._current_config[name] = self._create_if_data(name, item)
+        if self._os_version < self.ONYX_API_VERSION:
+            for if_data in config:
+                if_name = self.get_if_name(if_data)
+                self._current_config[if_name] = self._create_if_data(
+                    if_name, if_data)
+        else:
+            for if_config in config:
+                for if_name, if_data in iteritems(if_config):
+                    if_data = if_data[0]
+                    self._current_config[if_name] = self._create_if_data(
+                        if_name, if_data)
 
     def _generate_no_if_commands(self, req_if, curr_if):
         if self._interface_type == self.IF_TYPE_ETH:
diff --git a/lib/ansible/modules/network/onyx/onyx_l2_interface.py b/lib/ansible/modules/network/onyx/onyx_l2_interface.py
index ee25c4031d1..368cf782d4b 100644
--- a/lib/ansible/modules/network/onyx/onyx_l2_interface.py
+++ b/lib/ansible/modules/network/onyx/onyx_l2_interface.py
@@ -162,6 +162,9 @@ class OnyxL2InterfaceModule(BaseOnyxModule):
             return int(access_vlan)
 
     def _create_switchport_data(self, if_name, if_data):
+        if self._os_version >= self.ONYX_API_VERSION:
+            if_data = if_data[0]
+
         return {
             'name': if_name,
             'mode': self.get_config_attr(if_data, 'Mode'),
@@ -174,6 +177,7 @@ class OnyxL2InterfaceModule(BaseOnyxModule):
 
     def load_current_config(self):
         # called in base class in run function
+        self._os_version = self._get_os_version()
         self._current_config = dict()
         switchports_config = self._get_switchport_config()
         if not switchports_config:
diff --git a/lib/ansible/modules/network/onyx/onyx_l3_interface.py b/lib/ansible/modules/network/onyx/onyx_l3_interface.py
index 93583e11ee5..77516861ede 100644
--- a/lib/ansible/modules/network/onyx/onyx_l3_interface.py
+++ b/lib/ansible/modules/network/onyx/onyx_l3_interface.py
@@ -197,34 +197,44 @@ class OnyxL3InterfaceModule(BaseOnyxModule):
         return get_interfaces_config(self._module, interface_type)
 
     def _parse_interfaces_config(self, if_type, if_config):
+        if self._os_version < self.ONYX_API_VERSION:
+            for if_data in if_config:
+                if_name = self.get_config_attr(if_data, 'header')
+                self._get_if_attributes(if_type, if_name, if_data)
+        else:
+            for if_config_item in if_config:
+                for if_name, if_data in iteritems(if_config_item):
+                    if_data = if_data[0]
+                    self._get_if_attributes(if_type, if_name, if_data)
+
+    def _get_if_attributes(self, if_type, if_name, if_data):
         ipaddr_attr = self.IP_ADDR_ATTR_MAP[if_type]
-        for if_data in if_config:
-            if_name = self.get_config_attr(if_data, 'header')
-            regex = self.IF_TYPE_MAP[if_type]
-            match = regex.match(if_name)
-            if not match:
-                continue
-            ipv4 = self.get_config_attr(if_data, ipaddr_attr)
-            if ipv4:
-                ipv4 = ipv4.replace(' ', '')
-            ipv6 = self.get_config_attr(if_data, 'IPv6 address(es)')
-            if ipv6:
-                ipv6 = ipv6.replace('[primary]', '')
-                ipv6 = ipv6.strip()
-            if_id = match.group(1)
-            switchport = self.get_config_attr(if_data, 'Switchport mode')
-            if_obj = {
-                'name': if_name,
-                'if_id': if_id,
-                'if_type': if_type,
-                'ipv4': ipv4,
-                'ipv6': ipv6,
-                'switchport': switchport,
-            }
-            self._current_config[if_name] = if_obj
+        regex = self.IF_TYPE_MAP[if_type]
+        match = regex.match(if_name)
+        if not match:
+            return
+        ipv4 = self.get_config_attr(if_data, ipaddr_attr)
+        if ipv4:
+            ipv4 = ipv4.replace(' ', '')
+        ipv6 = self.get_config_attr(if_data, 'IPv6 address(es)')
+        if ipv6:
+            ipv6 = ipv6.replace('[primary]', '')
+            ipv6 = ipv6.strip()
+        if_id = match.group(1)
+        switchport = self.get_config_attr(if_data, 'Switchport mode')
+        if_obj = {
+            'name': if_name,
+            'if_id': if_id,
+            'if_type': if_type,
+            'ipv4': ipv4,
+            'ipv6': ipv6,
+            'switchport': switchport,
+        }
+        self._current_config[if_name] = if_obj
 
     def load_current_config(self):
         # called in base class in run function
+        self._os_version = self._get_os_version()
         self._current_config = dict()
         if_types = set([if_obj['if_type'] for if_obj in self._required_config])
         for if_type in if_types:
diff --git a/test/units/modules/network/onyx/test_onyx_interface.py b/test/units/modules/network/onyx/test_onyx_interface.py
index a8a92070c6d..8583e24f312 100644
--- a/test/units/modules/network/onyx/test_onyx_interface.py
+++ b/test/units/modules/network/onyx/test_onyx_interface.py
@@ -34,6 +34,10 @@ class TestOnyxInterfaceModule(TestOnyxModule):
             'ansible.module_utils.network.onyx.onyx.load_config')
         self.load_config = self.mock_load_config.start()
 
+        self.mock_get_version = patch.object(
+            onyx_interface.OnyxInterfaceModule, "_get_os_version")
+        self.get_version = self.mock_get_version.start()
+
     def tearDown(self):
         super(TestOnyxInterfaceModule, self).tearDown()
         self.mock_get_config.stop()
@@ -43,6 +47,7 @@ class TestOnyxInterfaceModule(TestOnyxModule):
         config_file = 'onyx_interfaces_show.cfg'
         self.get_config.return_value = load_fixture(config_file)
         self.load_config.return_value = None
+        self.get_version.return_value = "3.6.5000"
 
     def test_mtu_no_change(self):
         set_module_args(dict(name='Eth1/1', mtu=1500))
diff --git a/test/units/modules/network/onyx/test_onyx_l2_interface.py b/test/units/modules/network/onyx/test_onyx_l2_interface.py
index 71e331c7772..e05673721ef 100644
--- a/test/units/modules/network/onyx/test_onyx_l2_interface.py
+++ b/test/units/modules/network/onyx/test_onyx_l2_interface.py
@@ -40,6 +40,10 @@ class TestOnyxInterfaceModule(TestOnyxModule):
             'ansible.module_utils.network.onyx.onyx.load_config')
         self.load_config = self.mock_load_config.start()
 
+        self.mock_get_version = patch.object(
+            onyx_l2_interface.OnyxL2InterfaceModule, "_get_os_version")
+        self.get_version = self.mock_get_version.start()
+
     def tearDown(self):
         super(TestOnyxInterfaceModule, self).tearDown()
         self.mock_get_config.stop()
@@ -49,6 +53,7 @@ class TestOnyxInterfaceModule(TestOnyxModule):
         config_file = 'onyx_l2_interface_show.cfg'
         self.get_config.return_value = load_fixture(config_file)
         self.load_config.return_value = None
+        self.get_version.return_value = "3.6.5000"
 
     def test_access_vlan_no_change(self):
         set_module_args(dict(name='Eth1/11', access_vlan=1))
diff --git a/test/units/modules/network/onyx/test_onyx_l3_interface.py b/test/units/modules/network/onyx/test_onyx_l3_interface.py
index 839f7b3c6d0..7926b407f14 100644
--- a/test/units/modules/network/onyx/test_onyx_l3_interface.py
+++ b/test/units/modules/network/onyx/test_onyx_l3_interface.py
@@ -27,6 +27,10 @@ class TestOnyxL3InterfaceModule(TestOnyxModule):
             'ansible.module_utils.network.onyx.onyx.load_config')
         self.load_config = self.mock_load_config.start()
 
+        self.mock_get_version = patch.object(
+            onyx_l3_interface.OnyxL3InterfaceModule, "_get_os_version")
+        self.get_version = self.mock_get_version.start()
+
     def tearDown(self):
         super(TestOnyxL3InterfaceModule, self).tearDown()
         self.mock_get_config.stop()
@@ -52,6 +56,7 @@ class TestOnyxL3InterfaceModule(TestOnyxModule):
     def load_fixture(self, config_file):
         self.get_config.return_value = load_fixture(config_file)
         self.load_config.return_value = None
+        self.get_version.return_value = "3.6.5000"
 
     def load_eth_ifc_fixture(self):
         config_file = 'onyx_l3_interface_show.cfg'