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:
parent
091bebcbf7
commit
8bcf9b5734
3 changed files with 64 additions and 58 deletions
|
@ -232,18 +232,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
|
||||||
|
|
|
@ -193,10 +193,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
|
||||||
|
|
||||||
|
|
||||||
|
@ -262,7 +268,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')
|
||||||
|
@ -312,12 +318,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']:
|
||||||
|
@ -328,7 +338,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
|
||||||
)
|
)
|
||||||
|
@ -339,39 +351,30 @@ def main():
|
||||||
if meraki.status == 201:
|
if meraki.status == 201:
|
||||||
meraki.result['data'] = r
|
meraki.result['data'] = r
|
||||||
meraki.result['changed'] = True
|
meraki.result['changed'] = True
|
||||||
else:
|
else: # Network exists, make changes
|
||||||
net = meraki.get_net(meraki.params['org_name'], meraki.params['net_name'], data=nets)
|
# meraki.fail_json(msg="nets", nets=nets, net_id=net_id)
|
||||||
if meraki.is_update_required(net, payload):
|
# meraki.fail_json(msg="compare", original=net, payload=payload)
|
||||||
path = meraki.construct_path('update',
|
if meraki.params['enable_vlans'] is not None: # Modify VLANs configuration
|
||||||
net_id=meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
|
status_path = meraki.construct_path('status_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
|
|
||||||
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')
|
status = meraki.request(status_path, method='GET')
|
||||||
payload = {'enabled': meraki.params['enable_vlans']}
|
payload = {'enabled': meraki.params['enable_vlans']}
|
||||||
|
# meraki.fail_json(msg="here", payload=payload)
|
||||||
if meraki.is_update_required(status, payload):
|
if meraki.is_update_required(status, payload):
|
||||||
path = meraki.construct_path('enable_vlans',
|
path = meraki.construct_path('enable_vlans', net_id=net_id)
|
||||||
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
|
||||||
|
meraki.exit_json(**meraki.result)
|
||||||
else:
|
else:
|
||||||
meraki.result['data'] = status
|
meraki.result['data'] = status
|
||||||
elif meraki.is_update_required(net, payload):
|
meraki.exit_json(**meraki.result)
|
||||||
path = meraki.construct_path('update',
|
net = meraki.get_net(meraki.params['org_name'], net_id=net_id, data=nets)
|
||||||
net_id=meraki.get_net_id(net_name=meraki.params['net_name'], 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,
|
r = meraki.request(path,
|
||||||
method='PUT',
|
method='PUT',
|
||||||
payload=json.dumps(payload))
|
payload=json.dumps(payload))
|
||||||
|
@ -381,9 +384,7 @@ def main():
|
||||||
else:
|
else:
|
||||||
meraki.result['data'] = net
|
meraki.result['data'] = net
|
||||||
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:
|
||||||
|
|
|
@ -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 }}'
|
||||||
|
@ -255,12 +259,15 @@
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
register: create_net_tags
|
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:
|
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:
|
||||||
|
@ -292,7 +299,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
|
||||||
|
|
Loading…
Reference in a new issue