From b1bdc5dae245c53edea1e920af50f62f439d7b05 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Mon, 26 Feb 2018 13:01:20 +0530 Subject: [PATCH] nxos_vlan, nxos_linkagg fix (#36711) * fix nxos_vlan aggregate (#36710) * fix nxos_vlan aggregate Signed-off-by: Trishna Guha * Add test Signed-off-by: Trishna Guha (cherry picked from commit 44332bda788de397b5d1b31bab2152e5244bbe93) * nxos_linkagg fix (#36706) Signed-off-by: Trishna Guha (cherry picked from commit d6912cf40ee78f78a6eb8921d7ee1c40e6b885dd) --- .../modules/network/nxos/nxos_linkagg.py | 10 +-- lib/ansible/modules/network/nxos/nxos_vlan.py | 24 ++++--- .../targets/nxos_vlan/tests/common/agg.yaml | 70 +++++++++++++++++++ 3 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 test/integration/targets/nxos_vlan/tests/common/agg.yaml diff --git a/lib/ansible/modules/network/nxos/nxos_linkagg.py b/lib/ansible/modules/network/nxos/nxos_linkagg.py index a5814ca9068..af2e21818c1 100644 --- a/lib/ansible/modules/network/nxos/nxos_linkagg.py +++ b/lib/ansible/modules/network/nxos/nxos_linkagg.py @@ -133,19 +133,21 @@ import re from copy import deepcopy from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands -from ansible.module_utils.network.nxos.nxos import nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argument_spec from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.common.utils import remove_default_spec def execute_show_command(command, module): - provider = module.params['provider'] - if provider['transport'] == 'cli': + device_info = get_capabilities(module) + network_api = device_info.get('network_api', 'nxapi') + + if network_api == 'cliconf': if 'show port-channel summary' in command: command += ' | json' cmds = [command] body = run_commands(module, cmds) - elif provider['transport'] == 'nxapi': + elif network_api == 'nxapi': cmds = [command] body = run_commands(module, cmds) diff --git a/lib/ansible/modules/network/nxos/nxos_vlan.py b/lib/ansible/modules/network/nxos/nxos_vlan.py index cb5d339b675..9ce967065bd 100644 --- a/lib/ansible/modules/network/nxos/nxos_vlan.py +++ b/lib/ansible/modules/network/nxos/nxos_vlan.py @@ -171,17 +171,19 @@ def search_obj_in_list(vlan_id, lst): return o -def get_diff(w, have): +def get_diff(w, obj): c = deepcopy(w) - del c['interfaces'] - del c['name'] - del c['associated_interfaces'] - for o in have: - del o['interfaces'] - del o['name'] - if o['vlan_id'] == w['vlan_id']: - diff_dict = dict(set(c.items()) - set(o.items())) - return diff_dict + entries = ('interfaces', 'associated_interfaces', 'name', 'delay', 'vlan_range') + for key in entries: + if key in c: + del c[key] + + o = deepcopy(obj) + del o['interfaces'] + del o['name'] + if o['vlan_id'] == w['vlan_id']: + diff_dict = dict(set(c.items()) - set(o.items())) + return diff_dict def map_obj_to_commands(updates, module, os_platform): @@ -269,7 +271,7 @@ def map_obj_to_commands(updates, module, os_platform): commands.append('no switchport access vlan {0}'.format(vlan_id)) else: - diff = get_diff(w, have) + diff = get_diff(w, obj_in_have) if diff: commands.append('vlan {0}'.format(vlan_id)) for key, value in diff.items(): diff --git a/test/integration/targets/nxos_vlan/tests/common/agg.yaml b/test/integration/targets/nxos_vlan/tests/common/agg.yaml new file mode 100644 index 00000000000..3d4fc4b5155 --- /dev/null +++ b/test/integration/targets/nxos_vlan/tests/common/agg.yaml @@ -0,0 +1,70 @@ +--- +- debug: msg="START connection={{ ansible_connection }}/agg.yaml" +- debug: msg="Using provider={{ connection.transport }}" + when: ansible_connection == "local" + +- name: setup - remove vlan used in test + nxos_config: &rm + lines: + - no vlan 102 + - no vlan 103 + provider: "{{ connection }}" + ignore_errors: yes + + +- name: configure vlan with aggregate + nxos_vlan: &conf1 + aggregate: + - { name: app02, vlan_id: 102 } + - { name: app03, vlan_id: 103 } + vlan_state: active + admin_state: up + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == true' + - '"vlan 102" in result.commands' + - '"vlan 103" in result.commands' + - '"no shutdown" in result.commands' + - '"state active" in result.commands' + +- name: conf1 - Idempotence + nxos_vlan: *conf1 + register: result + +- assert: + that: + - 'result.changed == false' + +- name: change property of existing vlan - admin_state down + nxos_vlan: &conf2 + aggregate: + - { name: app02, vlan_id: 102 } + - { name: app03, vlan_id: 103 } + vlan_state: active + admin_state: down + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == true' + - '"vlan 102" in result.commands' + - '"vlan 103" in result.commands' + - '"shutdown" in result.commands' + +- name: conf2 - Idempotence + nxos_vlan: *conf2 + register: result + +- assert: + that: + - 'result.changed == false' + +- name: teardown + nxos_config: *rm + ignore_errors: yes + +- debug: msg="END connection={{ ansible_connection }}/agg.yaml"