Skip to content

Commit

Permalink
meraki_admin - Enable check mode (ansible#54499)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kbreit authored and Qalthos committed Jun 5, 2019
1 parent 30aeab4 commit 6da2d40
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/53597-meraki_admin_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- "meraki_admin - Add support for check mode."
29 changes: 22 additions & 7 deletions lib/ansible/modules/network/meraki/meraki_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
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


Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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


Expand Down Expand Up @@ -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')

Expand All @@ -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':
Expand Down Expand Up @@ -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']
Expand Down
124 changes: 123 additions & 1 deletion test/integration/targets/meraki_admin/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}'
Expand All @@ -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}}'
Expand All @@ -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}}'
Expand Down Expand Up @@ -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}}'
Expand All @@ -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}}'
Expand All @@ -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
Expand Down

0 comments on commit 6da2d40

Please sign in to comment.