meraki_network - Restructure execution logic (#56818)

* Restructure meraki_network to be simpler and work for all int tests

* Enable all integration tests and enhance error reports

* Slight tweaks to integration tests
This commit is contained in:
Kevin Breit 2019-06-05 09:33:55 -05:00 committed by Nathaniel Case
parent 091bebcbf7
commit 8bcf9b5734
3 changed files with 64 additions and 58 deletions

View file

@ -232,20 +232,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):

View file

@ -193,10 +193,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
@ -262,7 +268,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')
@ -312,12 +318,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']:
@ -328,7 +338,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
)
@ -339,51 +351,40 @@ def main():
if meraki.status == 201:
meraki.result['data'] = r
meraki.result['changed'] = True
else:
net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets)
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: # Update existing network
net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets)
if meraki.params['enable_vlans'] is not None:
status_path = meraki.construct_path('status_vlans', net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets))
status = meraki.request(status_path, method='GET')
payload = {'enabled': meraki.params['enable_vlans']}
if meraki.is_update_required(status, payload):
path = meraki.construct_path('enable_vlans',
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:
meraki.result['data'] = status
elif 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)
)
else: # Network exists, make changes
# meraki.fail_json(msg="nets", nets=nets, net_id=net_id)
# meraki.fail_json(msg="compare", original=net, payload=payload)
if meraki.params['enable_vlans'] is not None: # Modify VLANs configuration
status_path = meraki.construct_path('status_vlans', net_id=net_id)
status = meraki.request(status_path, method='GET')
payload = {'enabled': meraki.params['enable_vlans']}
# meraki.fail_json(msg="here", payload=payload)
if meraki.is_update_required(status, payload):
path = meraki.construct_path('enable_vlans', net_id=net_id)
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)
else:
meraki.result['data'] = net
meraki.result['data'] = status
meraki.exit_json(**meraki.result)
net = meraki.get_net(meraki.params['org_name'], net_id=net_id, data=nets)
if meraki.is_update_required(net, payload):
path = meraki.construct_path('update', net_id=net_id)
# meraki.fail_json(msg="Payload", path=path, payload=payload)
r = meraki.request(path,
method='PUT',
payload=json.dumps(payload))
if meraki.status == 200:
meraki.result['data'] = r
meraki.result['changed'] = True
else:
meraki.result['data'] = net
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:

View file

@ -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 }}'
@ -255,12 +259,15 @@
delegate_to: localhost
register: 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:
@ -292,7 +299,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