meraki_admin - Enable check mode (#54499)

* Add support for check mode

* Add changelog fragment

* Add diff support
- Fix a few changed status
- Removed auth_key check since that's done in module_utils now
This commit is contained in:
Kevin Breit 2019-06-05 09:23:54 -05:00 committed by Nathaniel Case
parent 30aeab4709
commit 6da2d40500
3 changed files with 148 additions and 8 deletions

View file

@ -0,0 +1,3 @@
---
minor_changes:
- "meraki_admin - Add support for check mode."

View file

@ -255,6 +255,7 @@ import os
from ansible.module_utils.basic import AnsibleModule, json, env_fallback
from ansible.module_utils.urls import fetch_url
from ansible.module_utils._text import to_native
from ansible.module_utils.common.dict_transformations import recursive_diff
from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
@ -337,9 +338,12 @@ def create_admin(meraki, org_id, name, email):
if meraki.params['networks'] is not None:
nets = meraki.get_nets(org_id=org_id)
networks = network_factory(meraki, meraki.params['networks'], nets)
# meraki.fail_json(msg=str(type(networks)), data=networks)
payload['networks'] = networks
if is_admin_existing is None: # Create new admin
if meraki.module.check_mode is True:
meraki.result['data'] = payload
meraki.result['changed'] = True
meraki.exit_json(**meraki.result)
path = meraki.construct_path('create', function='admin', org_id=org_id)
r = meraki.request(path,
method='POST',
@ -354,6 +358,15 @@ def create_admin(meraki, org_id, name, email):
if not meraki.params['networks']:
payload['networks'] = []
if meraki.is_update_required(is_admin_existing, payload) is True:
if meraki.module.check_mode is True:
diff = recursive_diff(is_admin_existing, payload)
is_admin_existing.update(payload)
meraki.result['diff'] = {'before': diff[0],
'after': diff[1],
}
meraki.result['changed'] = True
meraki.result['data'] = payload
meraki.exit_json(**meraki.result)
path = meraki.construct_path('update', function='admin', org_id=org_id) + is_admin_existing['id']
r = meraki.request(path,
method='PUT',
@ -364,6 +377,9 @@ def create_admin(meraki, org_id, name, email):
return r
else:
meraki.result['data'] = is_admin_existing
if meraki.module.check_mode is True:
meraki.result['data'] = payload
meraki.exit_json(**meraki.result)
return -1
@ -395,7 +411,7 @@ def main():
# args/params passed to the execution, as well as if the module
# supports check mode
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=False,
supports_check_mode=True,
)
meraki = MerakiModule(module, function='admin')
@ -421,16 +437,11 @@ def main():
except KeyError:
pass
if meraki.params['auth_key'] is None:
module.fail_json(msg='Meraki Dashboard API key not set')
payload = None
# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
if module.check_mode:
return result
# execute checks for argument completeness
if meraki.params['state'] == 'query':
@ -468,6 +479,10 @@ def main():
if r != -1:
meraki.result['data'] = r
elif meraki.params['state'] == 'absent':
if meraki.module.check_mode is True:
meraki.result['data'] = {}
meraki.result['changed'] = True
meraki.exit_json(**meraki.result)
admin_id = get_admin_id(meraki,
get_admins(meraki, org_id),
email=meraki.params['email']

View file

@ -4,6 +4,24 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
- block:
- name: Create new administrator in check mode
meraki_admin:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
name: Jane Doe
email: '{{email_prefix}}+janedoe@{{email_domain}}'
org_access: read-only
delegate_to: localhost
check_mode: yes
register: create_org_check
- name: Create new admin check mode assertion
assert:
that:
- create_org_check is changed
- 'create_org_check.data.name == "Jane Doe"'
- name: Create new administrator
meraki_admin:
auth_key: '{{auth_key}}'
@ -21,6 +39,20 @@
- create_orgaccess.changed == true
- 'create_orgaccess.data.name == "Jane Doe"'
- name: Delete recently created administrator with check mode
meraki_admin:
auth_key: '{{auth_key}}'
state: absent
org_name: '{{test_org_name}}'
email: '{{email_prefix}}+janedoe@{{email_domain}}'
delegate_to: localhost
register: delete_one_check
check_mode: yes
- assert:
that:
- delete_one_check is changed
- name: Delete recently created administrator
meraki_admin:
auth_key: '{{auth_key}}'
@ -47,6 +79,31 @@
- create_orgaccess_id.changed == true
- 'create_orgaccess_id.data.name == "Jane Doe"'
- name: Create administrator with tags with check mode
meraki_admin:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
name: John Doe
email: '{{email_prefix}}+johndoe@{{email_domain}}'
orgAccess: none
tags:
- { "tag": "production", "access": "read-only" }
- tag: beta
access: full
delegate_to: localhost
register: create_tags_check
check_mode: yes
- debug:
var: create_tags_check
- assert:
that:
- create_tags_check is changed
- create_tags_check.data.name == "John Doe"
- create_tags_check.data.tags | length == 2
- name: Create administrator with tags
meraki_admin:
auth_key: '{{auth_key}}'
@ -119,6 +176,27 @@
- TestNet
- TestNet2
- name: Create administrator with networks with check mode
meraki_admin:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
name: Jim Doe
email: '{{email_prefix}}+jimdoe@{{email_domain}}'
orgAccess: none
networks:
- { "network": "TestNet", "access": "read-only" }
- { "network": "TestNet2", "access": "full" }
delegate_to: localhost
register: create_network_check
check_mode: yes
- assert:
that:
- create_network_check is changed
- create_network_check.data.name == "Jim Doe"
- create_network_check.data.networks | length == 2
- name: Create administrator with networks
meraki_admin:
auth_key: '{{auth_key}}'
@ -139,6 +217,29 @@
- create_network.data.name == "Jim Doe"
- create_network.data.networks | length == 2
- name: Update administrator with check mode
meraki_admin:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
name: Jim Doe
email: '{{email_prefix}}+jimdoe@{{email_domain}}'
orgAccess: none
networks:
- { "network": "TestNet", "access": "full" }
delegate_to: localhost
register: update_network_check
check_mode: yes
- debug:
var: update_network_check
- assert:
that:
- update_network_check is changed
- update_network_check.data.networks.0.access == "full"
- update_network_check.data.networks | length == 1
- name: Update administrator
meraki_admin:
auth_key: '{{auth_key}}'
@ -158,7 +259,28 @@
- update_network.data.networks.0.access == "full"
- update_network.data.networks | length == 1
- name: Update administrator for idempotency check
- name: Update administrator for idempotency check with check mode
meraki_admin:
auth_key: '{{auth_key}}'
state: present
org_name: '{{test_org_name}}'
name: Jim Doe
email: '{{email_prefix}}+jimdoe@{{email_domain}}'
orgAccess: none
networks:
- { "network": "TestNet", "access": "full" }
delegate_to: localhost
register: update_network_idempotent_check
check_mode: yes
- debug:
var: update_network_idempotent_check
- assert:
that:
- update_network_idempotent_check is not changed
- name: Update administrator for idempotency
meraki_admin:
auth_key: '{{auth_key}}'
state: present