[stable-2.8] - Restructure meraki_network execution (#57663)

* Resolve conflicts

* Fixes for the merge since 2.8 and devel are quite different

* Add changelog
This commit is contained in:
Kevin Breit 2019-06-17 23:56:40 -05:00 committed by Toshio Kuratomi
parent ee48ceaf10
commit 81dc2ac233
4 changed files with 64 additions and 43 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- meraki_network - Restructure code execution so net_id parameter works in all situations.

View file

@ -211,18 +211,17 @@ class MerakiModule(object):
self.nets.append(t) self.nets.append(t)
return self.nets return self.nets
# def get_net(self, org_name, net_name, data=None): def get_net(self, org_name, net_name=None, org_id=None, data=None, net_id=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):
''' Return network information ''' ''' Return network information '''
if not data: if not data:
if not org_id: if not org_id:
org_id = self.get_org_id(org_name) org_id = self.get_org_id(org_name)
data = self.get_nets(org_id=org_id) data = self.get_nets(org_id=org_id)
for n in data: for n in data:
if net_id:
if n['id'] == net_id:
return n
elif net_name:
if n['name'] == net_name: if n['name'] == net_name:
return n return n
return False return False

View file

@ -162,10 +162,16 @@ from ansible.module_utils._text import to_native
from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec 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: for n in data:
if net_name:
if n['name'] == net_name: if n['name'] == net_name:
return True return True
elif net_id:
if n['id'] == net_id:
return True
return False return False
@ -222,7 +228,7 @@ def main():
if not meraki.params['org_name'] and not meraki.params['org_id']: if not meraki.params['org_name'] and not meraki.params['org_id']:
meraki.fail_json(msg='org_name or org_id parameters are required') meraki.fail_json(msg='org_name or org_id parameters are required')
if meraki.params['state'] != 'query': 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') 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']: if meraki.params['net_name'] and meraki.params['net_id']:
meraki.fail_json(msg='net_name and net_id are mutually exclusive') 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) nets = meraki.get_nets(org_id=org_id)
# check if network is created # 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.get_net_id(net_name=meraki.params['net_name'], data=nets)
elif meraki.params['net_id']:
net_id = meraki.params['net_id'] 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)
net_exists = True
if meraki.params['state'] == 'query': if meraki.params['state'] == 'query':
if not meraki.params['net_name'] and not meraki.params['net_id']: if not meraki.params['net_name'] and not meraki.params['net_id']:
@ -272,7 +282,9 @@ def main():
data=nets data=nets
) )
elif meraki.params['state'] == 'present': 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', path = meraki.construct_path('create',
org_id=org_id org_id=org_id
) )
@ -284,34 +296,36 @@ def main():
meraki.result['data'] = r meraki.result['data'] = r
meraki.result['changed'] = True meraki.result['changed'] = True
else: 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): if meraki.is_update_required(net, payload):
path = meraki.construct_path('update', path = meraki.construct_path('update', net_id=net_id)
net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets) # else:
) # path = meraki.construct_path('update',
r = meraki.request(path, # net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
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, r = meraki.request(path,
method='PUT', method='PUT',
payload=json.dumps(payload)) payload=json.dumps(payload))
if meraki.status == 200: if meraki.status == 200:
meraki.result['data'] = r meraki.result['data'] = r
meraki.result['changed'] = True 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': elif meraki.params['state'] == 'absent':
if is_net_valid(meraki, meraki.params['net_name'], nets) is True: if is_net_valid(nets, net_id=net_id) is True:
net_id = meraki.get_net_id(net_name=meraki.params['net_name'],
data=nets)
path = meraki.construct_path('delete', net_id=net_id) path = meraki.construct_path('delete', net_id=net_id)
r = meraki.request(path, method='DELETE') r = meraki.request(path, method='DELETE')
if meraki.status == 204: if meraki.status == 204:

View file

@ -15,6 +15,10 @@
register: create_net_no_type register: create_net_no_type
ignore_errors: yes ignore_errors: yes
- assert:
that:
- create_net_no_type.msg == 'type parameter is required when creating a network.'
- name: Create network without organization - name: Create network without organization
meraki_network: meraki_network:
auth_key: '{{ auth_key }}' auth_key: '{{ auth_key }}'
@ -135,12 +139,15 @@
- debug: - debug:
msg: '{{create_net_tags}}' 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: meraki_network:
auth_key: '{{ auth_key }}' auth_key: '{{ auth_key }}'
state: present state: present
org_name: '{{test_org_name}}' org_name: '{{test_org_name}}'
net_name: IntTestNetworkTags net_id: '{{tag_net_id}}'
type: switch type: switch
timezone: America/Chicago timezone: America/Chicago
tags: tags:
@ -168,7 +175,6 @@
- name: Present assertions - name: Present assertions
assert: assert:
that: that:
- create_net_no_type.status == 500
- create_net_combined.data.type == 'combined' - create_net_combined.data.type == 'combined'
- create_net_combined.data.disableMyMerakiCom == True - create_net_combined.data.disableMyMerakiCom == True
- enable_meraki_com.data.disableMyMerakiCom == False - enable_meraki_com.data.disableMyMerakiCom == False
@ -218,9 +224,9 @@
- 'query_config_template.data.name == "{{ test_template_name }}"' - 'query_config_template.data.name == "{{ test_template_name }}"'
############################################################################# ############################################################################
# Tear down starts here # Tear down starts here
############################################################################# ############################################################################
always: always:
- name: Delete network without org - name: Delete network without org
meraki_network: meraki_network: