Repository: ansible/ansible

Issue:
onyx_pfc_interface throws exception when using onyx version 3.6.6000
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and devel branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME
<!---
Name of the module, plugin, task or feature
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path
-->
onyx_pfc_interface.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0 (devel c1008d693c) last updated 2018/03/12 15:51:01 (GMT +000)
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
when running command onyx_pfc_interface on onyx network device with version 3.6.6000 or later, there is an exception.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
run any playbook with onyx_pfc_interface

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Disable Lag members PFC
  onyx_pfc_interface:
    aggregate:
      - name: Eth1/15
      - name: Eth1/16
    state: disabled
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
pfc interface will be configured

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Command failed with exception
<!--- Paste verbatim command output between quotes below -->
```
<swi60> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1520431892.25-242550331138460/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_gof89B/ansible_module_onyx_pfc_interface.py", line 205, in <module>
    main()
  File "/tmp/ansible_gof89B/ansible_module_onyx_pfc_interface.py", line 201, in main
    OnyxPfcInterfaceModule.main()
  File "/tmp/ansible_gof89B/ansible_modlib.zip/ansible/module_utils/network/onyx/onyx.py", line 229, in main
  File "/tmp/ansible_gof89B/ansible_modlib.zip/ansible/module_utils/network/onyx/onyx.py", line 207, in run
  File "/tmp/ansible_gof89B/ansible_module_onyx_pfc_interface.py", line 149, in load_current_config
    for if_name, if_pfc_data in iteritems(pfc_config):
  File "/tmp/ansible_gof89B/ansible_modlib.zip/ansible/module_utils/six/__init__.py", line 610, in iteritems
AttributeError: 'list' object has no attribute 'iteritems'
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/network/onyx/onyx_pfc_interface.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/onyx/onyx_pfc_interface.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Resolving patch:
diff --git a/lib/ansible/modules/network/onyx/onyx_pfc_interface.py b/lib/ansible/modules/network/onyx/onyx_pfc_interface.py
--- a/lib/ansible/modules/network/onyx/onyx_pfc_interface.py
+++ b/lib/ansible/modules/network/onyx/onyx_pfc_interface.py
@@ -140,12 +140,20 @@ def _get_pfc_config(self):
 
     def load_current_config(self):
         # called in base class in run function
+        self._os_version = self._get_os_version()
         self._current_config = dict()
         pfc_config = self._get_pfc_config()
         if not pfc_config:
             return
-        if 'Table 2' in pfc_config:
-            pfc_config = pfc_config['Table 2']
+        if self._os_version >= self.ONYX_API_VERSION:
+            if len(pfc_config) >= 3:
+                pfc_config = pfc_config[2]
+            else:
+                pfc_config = dict()
+        else:
+            if 'Table 2' in pfc_config:
+                pfc_config = pfc_config['Table 2']
+
         for if_name, if_pfc_data in iteritems(pfc_config):
             match = self.PFC_IF_REGEX.match(if_name)
             if not match:


Test patch:
diff --git a/test/units/modules/network/onyx/test_onyx_pfc_interface.py b/test/units/modules/network/onyx/test_onyx_pfc_interface.py
--- a/test/units/modules/network/onyx/test_onyx_pfc_interface.py
+++ b/test/units/modules/network/onyx/test_onyx_pfc_interface.py
@@ -27,11 +27,15 @@ def setUp(self):
         self.mock_load_config = patch(
             'ansible.module_utils.network.onyx.onyx.load_config')
         self.load_config = self.mock_load_config.start()
+        self.mock_get_version = patch.object(
+            onyx_pfc_interface.OnyxPfcInterfaceModule, "_get_os_version")
+        self.get_version = self.mock_get_version.start()
 
     def tearDown(self):
         super(TestOnyxPfcInterfaceModule, self).tearDown()
         self.mock_get_config.stop()
         self.mock_load_config.stop()
+        self.mock_get_version.stop()
 
     def load_fixtures(self, commands=None, transport='cli'):
         if self._pfc_enabled:
@@ -42,6 +46,7 @@ def load_fixtures(self, commands=None, transport='cli'):
 
         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_pfc_if(self, if_name, enabled, changed, commands):
         state = 'enabled' if enabled else 'disabled'