From e6232892997464862b6139ccfff32b6cab112d86 Mon Sep 17 00:00:00 2001 From: Kevin Breit Date: Mon, 2 Nov 2020 10:42:19 -0600 Subject: [PATCH] Fix crash with non-MX devices --- changelogs/fragments/management_ms_fix.yml | 3 + .../modules/meraki_management_interface.py | 27 +++++-- .../tasks/main.yml | 79 ++++++++++++++++++- 3 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/management_ms_fix.yml diff --git a/changelogs/fragments/management_ms_fix.yml b/changelogs/fragments/management_ms_fix.yml new file mode 100644 index 00000000..815a8565 --- /dev/null +++ b/changelogs/fragments/management_ms_fix.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - meraki_management_interface - Fix crash when modifying a non-MX management interface. diff --git a/plugins/modules/meraki_management_interface.py b/plugins/modules/meraki_management_interface.py index a9879732..c0297861 100644 --- a/plugins/modules/meraki_management_interface.py +++ b/plugins/modules/meraki_management_interface.py @@ -19,10 +19,10 @@ short_description: Configure Meraki management interfaces version_added: "1.1.0" description: -- Allows for configuration of management interfaces on Meraki devices. +- Allows for configuration of management interfaces on Meraki MX, MS, and MR devices. notes: - C(WAN2) parameter is only valid for MX appliances. -- C(enabled) can only be set to C(not configured) for MS switches. +- C(wan_enabled) should not be provided for non-MX devies. options: state: description: @@ -60,6 +60,7 @@ wan_enabled: description: - States whether the management interface is enabled. + - Only valid for MX devices. type: str choices: [disabled, enabled, not configured] using_static_ip: @@ -100,6 +101,7 @@ wan_enabled: description: - States whether the management interface is enabled. + - Only valid for MX devices. type: str choices: [disabled, enabled, not configured] using_static_ip: @@ -311,9 +313,11 @@ def main(): interfaces = ('wan1', 'wan2') for interface in interfaces: if meraki.params[interface] is not None: - wan_int = {'wanEnabled': meraki.params[interface]['wan_enabled'], - 'usingStaticIp': meraki.params[interface]['using_static_ip'], - } + wan_int = dict() + if meraki.params[interface]['wan_enabled'] is not None: + wan_int['wanEnabled'] = meraki.params[interface]['wan_enabled'] + if meraki.params[interface]['using_static_ip'] is not None: + wan_int['usingStaticIp'] = meraki.params[interface]['using_static_ip'] if meraki.params[interface]['vlan'] is not None: wan_int['vlan'] = meraki.params[interface]['vlan'] if meraki.params[interface]['using_static_ip'] is True: @@ -341,7 +345,18 @@ def main(): elif meraki.params['state'] == 'present': path = meraki.construct_path('get_one', custom={'serial': meraki.params['serial']}) original = meraki.request(path, method='GET') - if meraki.is_update_required(original, payload): + update_required = False + if 'wan1' in original: + if 'wanEnabled' in original['wan1']: + update_required = meraki.is_update_required(original, payload) + else: + update_required = meraki.is_update_required(original, payload, optional_ignore=['wanEnabled']) + if 'wan2' in original and update_required is False: + if 'wanEnabled' in original['wan2']: + update_required = meraki.is_update_required(original, payload) + else: + update_required = meraki.is_update_required(original, payload, optional_ignore=['wanEnabled']) + if update_required is True: if meraki.check_mode is True: diff = recursive_diff(original, payload) original.update(payload) diff --git a/tests/integration/targets/meraki_management_interface/tasks/main.yml b/tests/integration/targets/meraki_management_interface/tasks/main.yml index 4811cf83..28337707 100644 --- a/tests/integration/targets/meraki_management_interface/tasks/main.yml +++ b/tests/integration/targets/meraki_management_interface/tasks/main.yml @@ -12,7 +12,7 @@ - set_fact: net_name: TestNet - Appliance - - name: Create test network + - name: 'Create test network {{net_name}}' meraki_network: auth_key: '{{auth_key}}' state: present @@ -25,6 +25,83 @@ - set_fact: net_id: '{{net.data.id}}' + - name: Test providing wan_enabled to an MS network + meraki_management_interface: + auth_key: '{{auth_key}}' + state: present + org_id: '{{test_org_id}}' + net_id: '{{test_switch_net_name}}' + serial: '{{serial_switch}}' + wan1: + wan_enabled: enabled + using_static_ip: false + delegate_to: localhost + register: ms_not_configured + + - debug: + var: ms_not_configured + + - assert: + that: + - ms_not_configured.data is defined + + - name: Set management interface on switch + meraki_management_interface: + auth_key: '{{auth_key}}' + state: present + org_id: '{{test_org_id}}' + net_id: '{{test_switch_net_name}}' + serial: '{{serial_switch}}' + wan1: + using_static_ip: no + vlan: 3 + delegate_to: localhost + register: set_switch_mgmt + + - debug: + var: set_switch_mgmt + + - assert: + that: + - set_switch_mgmt.data is defined + + - name: Query non-MX network + meraki_management_interface: + auth_key: '{{auth_key}}' + state: query + org_id: '{{test_org_id}}' + net_id: '{{test_switch_net_name}}' + serial: '{{serial_switch}}' + delegate_to: localhost + register: non_mx_network + + - debug: + var: non_mx_network + + - assert: + that: + - non_mx_network.data is defined + + - name: Reset management interface on switch + meraki_management_interface: + auth_key: '{{auth_key}}' + state: present + org_id: '{{test_org_id}}' + net_id: '{{test_switch_net_name}}' + serial: '{{serial_switch}}' + wan1: + using_static_ip: no + vlan: 1 + delegate_to: localhost + register: reset_switch_mgmt + + - debug: + var: reset_switch_mgmt + + - assert: + that: + - reset_switch_mgmt.data is defined + - name: Set WAN1 as DHCP in check mode meraki_management_interface: auth_key: '{{auth_key}}'