From 81dc2ac233723d5aa04d9b1e3ea2f4450068e836 Mon Sep 17 00:00:00 2001 From: Kevin Breit Date: Mon, 17 Jun 2019 23:56:40 -0500 Subject: [PATCH] [stable-2.8] - Restructure meraki_network execution (#57663) * Resolve conflicts * Fixes for the merge since 2.8 and devel are quite different * Add changelog --- .../fragments/meraki_network-structure.yml | 2 + .../module_utils/network/meraki/meraki.py | 15 ++-- .../modules/network/meraki/meraki_network.py | 74 +++++++++++-------- .../targets/meraki_network/tasks/main.yml | 16 ++-- 4 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 changelogs/fragments/meraki_network-structure.yml diff --git a/changelogs/fragments/meraki_network-structure.yml b/changelogs/fragments/meraki_network-structure.yml new file mode 100644 index 00000000000..f09c15a2ee7 --- /dev/null +++ b/changelogs/fragments/meraki_network-structure.yml @@ -0,0 +1,2 @@ +bugfixes: + - meraki_network - Restructure code execution so net_id parameter works in all situations. diff --git a/lib/ansible/module_utils/network/meraki/meraki.py b/lib/ansible/module_utils/network/meraki/meraki.py index 129e06e7304..2b3e949110e 100644 --- a/lib/ansible/module_utils/network/meraki/meraki.py +++ b/lib/ansible/module_utils/network/meraki/meraki.py @@ -211,20 +211,19 @@ class MerakiModule(object): self.nets.append(t) return self.nets - # def get_net(self, org_name, net_name, data=None): - # path = self.construct_path('get_all', function='network', org_id=org_id) - # r = self.request(path, method='GET') - # return r - - def get_net(self, org_name, net_name, org_id=None, data=None): + def get_net(self, org_name, net_name=None, org_id=None, data=None, net_id=None): ''' Return network information ''' if not data: if not org_id: org_id = self.get_org_id(org_name) data = self.get_nets(org_id=org_id) for n in data: - if n['name'] == net_name: - return n + if net_id: + if n['id'] == net_id: + return n + elif net_name: + if n['name'] == net_name: + return n return False def get_net_id(self, org_name=None, net_name=None, data=None): diff --git a/lib/ansible/modules/network/meraki/meraki_network.py b/lib/ansible/modules/network/meraki/meraki_network.py index 1cc84ef9a9a..031d88d4fbe 100644 --- a/lib/ansible/modules/network/meraki/meraki_network.py +++ b/lib/ansible/modules/network/meraki/meraki_network.py @@ -162,10 +162,16 @@ from ansible.module_utils._text import to_native from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec -def is_net_valid(meraki, net_name, data): +def is_net_valid(data, net_name=None, net_id=None): + if net_name is None and net_id is None: + return False for n in data: - if n['name'] == net_name: - return True + if net_name: + if n['name'] == net_name: + return True + elif net_id: + if n['id'] == net_id: + return True return False @@ -222,7 +228,7 @@ def main(): if not meraki.params['org_name'] and not meraki.params['org_id']: meraki.fail_json(msg='org_name or org_id parameters are required') if meraki.params['state'] != 'query': - if not meraki.params['net_name'] or meraki.params['net_id']: + if not meraki.params['net_name'] and not meraki.params['net_id']: meraki.fail_json(msg='net_name or net_id is required for present or absent states') if meraki.params['net_name'] and meraki.params['net_id']: meraki.fail_json(msg='net_name and net_id are mutually exclusive') @@ -256,12 +262,16 @@ def main(): nets = meraki.get_nets(org_id=org_id) # check if network is created - net_id = None - if meraki.params['net_name']: - if is_net_valid(meraki, meraki.params['net_name'], nets) is True: + net_id = meraki.params['net_id'] + net_exists = False + if net_id is not None: + if is_net_valid(nets, net_id=net_id) is False: + meraki.fail_json(msg="Network specified by net_id does not exist.") + net_exists = True + elif meraki.params['net_name']: + if is_net_valid(nets, net_name=meraki.params['net_name']) is True: net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) - elif meraki.params['net_id']: - net_id = meraki.params['net_id'] + net_exists = True if meraki.params['state'] == 'query': if not meraki.params['net_name'] and not meraki.params['net_id']: @@ -272,7 +282,9 @@ def main(): data=nets ) elif meraki.params['state'] == 'present': - if net_id is None: + if net_exists is False: # Network needs to be created + if 'type' not in meraki.params or meraki.params['type'] is None: + meraki.fail_json(msg="type parameter is required when creating a network.") path = meraki.construct_path('create', org_id=org_id ) @@ -284,34 +296,36 @@ def main(): meraki.result['data'] = r meraki.result['changed'] = True else: - net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets) + net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets, net_id=net_id) + # meraki.fail_json(msg="compare", net=net, payload=payload) if meraki.is_update_required(net, payload): - path = meraki.construct_path('update', - net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) - ) + path = meraki.construct_path('update', net_id=net_id) + # else: + # path = meraki.construct_path('update', + # net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) + # ) r = meraki.request(path, method='PUT', payload=json.dumps(payload)) if meraki.status == 200: meraki.result['data'] = r meraki.result['changed'] = True - else: - net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets) - # meraki.fail_json(msg="HERE", net=net, payload=payload) - if meraki.is_update_required(net, payload): - path = meraki.construct_path('update', - net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) - ) - r = meraki.request(path, - method='PUT', - payload=json.dumps(payload)) - if meraki.status == 200: - meraki.result['data'] = r - meraki.result['changed'] = True + # else: + # net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets) + # # meraki.fail_json(msg="HERE", net=net, payload=payload) + # if meraki.is_update_required(net, payload): + # path = meraki.construct_path('update', + # net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) + # ) + # r = meraki.request(path, + # method='PUT', + # payload=json.dumps(payload)) + # if meraki.status == 200: + # meraki.result['data'] = r + # meraki.result['changed'] = True + # meraki.exit_json(**meraki.result) elif meraki.params['state'] == 'absent': - if is_net_valid(meraki, meraki.params['net_name'], nets) is True: - net_id = meraki.get_net_id(net_name=meraki.params['net_name'], - data=nets) + if is_net_valid(nets, net_id=net_id) is True: path = meraki.construct_path('delete', net_id=net_id) r = meraki.request(path, method='DELETE') if meraki.status == 204: diff --git a/test/integration/targets/meraki_network/tasks/main.yml b/test/integration/targets/meraki_network/tasks/main.yml index c077091aece..08ba6e9c16e 100644 --- a/test/integration/targets/meraki_network/tasks/main.yml +++ b/test/integration/targets/meraki_network/tasks/main.yml @@ -15,6 +15,10 @@ register: create_net_no_type ignore_errors: yes + - assert: + that: + - create_net_no_type.msg == 'type parameter is required when creating a network.' + - name: Create network without organization meraki_network: auth_key: '{{ auth_key }}' @@ -135,12 +139,15 @@ - debug: msg: '{{create_net_tags}}' - - name: Modify network + - set_fact: + tag_net_id: '{{create_net_tags.data.id}}' + + - name: Modify network by net_id meraki_network: auth_key: '{{ auth_key }}' state: present org_name: '{{test_org_name}}' - net_name: IntTestNetworkTags + net_id: '{{tag_net_id}}' type: switch timezone: America/Chicago tags: @@ -168,7 +175,6 @@ - name: Present assertions assert: that: - - create_net_no_type.status == 500 - create_net_combined.data.type == 'combined' - create_net_combined.data.disableMyMerakiCom == True - enable_meraki_com.data.disableMyMerakiCom == False @@ -218,9 +224,9 @@ - 'query_config_template.data.name == "{{ test_template_name }}"' -############################################################################# +############################################################################ # Tear down starts here -############################################################################# +############################################################################ always: - name: Delete network without org meraki_network: