From 72f2d05b6f64fe070111f37b284dd04df283d76b Mon Sep 17 00:00:00 2001 From: Kevin Breit Date: Wed, 5 Jun 2019 09:31:50 -0500 Subject: [PATCH] meraki_content_filtering - Enable check mode (#54638) * Add support for check mode. * Add diff support - Need to analyze diff for accuracy - Updated check mode changed value * Improve test coverage * Remove a duplicate integration test --- ...97-meraki_content_filtering_check_mode.yml | 2 + .../meraki/meraki_content_filtering.py | 20 +++- .../meraki_content_filtering/tasks/main.yml | 103 +++++++++++++----- 3 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 changelogs/fragments/53597-meraki_content_filtering_check_mode.yml diff --git a/changelogs/fragments/53597-meraki_content_filtering_check_mode.yml b/changelogs/fragments/53597-meraki_content_filtering_check_mode.yml new file mode 100644 index 00000000000..7a2a5430fd5 --- /dev/null +++ b/changelogs/fragments/53597-meraki_content_filtering_check_mode.yml @@ -0,0 +1,2 @@ +minor_changes: +- "meraki_content_filtering - Add support for check mode." diff --git a/lib/ansible/modules/network/meraki/meraki_content_filtering.py b/lib/ansible/modules/network/meraki/meraki_content_filtering.py index cdab752bf70..f862e98b045 100644 --- a/lib/ansible/modules/network/meraki/meraki_content_filtering.py +++ b/lib/ansible/modules/network/meraki/meraki_content_filtering.py @@ -120,6 +120,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 @@ -203,15 +204,26 @@ def main(): current = meraki.request(path, method='GET') proposed = current.copy() proposed.update(payload) - if module.check_mode: - meraki.result['data'] = payload - meraki.exit_json(**meraki.result) - if meraki.is_update_required(current, payload): + if meraki.is_update_required(current, payload) is True: + meraki.result['diff'] = dict() + diff = recursive_diff(current, payload) + meraki.result['diff']['before'] = diff[0] + meraki.result['diff']['after'] = diff[1] + if module.check_mode: + current.update(payload) + meraki.result['changed'] = True + meraki.result['data'] = current + meraki.exit_json(**meraki.result) response = meraki.request(path, method='PUT', payload=json.dumps(payload)) meraki.result['data'] = response meraki.result['changed'] = True else: meraki.result['data'] = current + if module.check_mode: + meraki.result['data'] = current + meraki.exit_json(**meraki.result) + meraki.result['data'] = current + meraki.exit_json(**meraki.result) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results diff --git a/test/integration/targets/meraki_content_filtering/tasks/main.yml b/test/integration/targets/meraki_content_filtering/tasks/main.yml index 6ff83fd397c..36ad0014ca0 100644 --- a/test/integration/targets/meraki_content_filtering/tasks/main.yml +++ b/test/integration/targets/meraki_content_filtering/tasks/main.yml @@ -8,24 +8,8 @@ fail: msg: Please define an API key when: auth_key is not defined - - - name: Use an invalid domain - meraki_config_template: - auth_key: '{{ auth_key }}' - host: marrrraki.com - state: query - org_name: DevTestOrg - output_level: debug - delegate_to: localhost - register: invalid_domain - ignore_errors: yes - - name: Connection assertions - assert: - that: - - '"Failed to connect to" in invalid_domain.msg' - - - name: Create network with type switch + - name: Create network meraki_network: auth_key: '{{ auth_key }}' state: present @@ -36,6 +20,38 @@ delegate_to: localhost register: create_net_appliance + - name: Test net_name and id exclusivity + meraki_content_filtering: + auth_key: '{{auth_key}}' + org_name: '{{test_org_name}}' + net_name: '{{test_net_name}}' + net_id: 12345 + state: present + allowed_urls: + - "http://www.ansible.com/*" + register: net_exclusive + ignore_errors: yes + + - assert: + that: + - 'net_exclusive.msg == "net_name and net_id are mutually exclusive"' + + - name: Set single allowed URL pattern with check mode + meraki_content_filtering: + auth_key: '{{auth_key}}' + org_name: '{{test_org_name}}' + net_name: '{{test_net_name}}' + state: present + allowed_urls: + - "http://www.ansible.com/*" + register: single_allowed_check + check_mode: yes + + - assert: + that: + - single_allowed_check.data.allowedUrlPatterns | length == 1 + - single_allowed_check is changed + - name: Set single allowed URL pattern meraki_content_filtering: auth_key: '{{auth_key}}' @@ -46,13 +62,28 @@ - "http://www.ansible.com/*" register: single_allowed - - debug: - var: single_allowed.data.allowedUrlPatterns - - assert: that: - single_allowed.data.allowedUrlPatterns | length == 1 + - name: Set single allowed URL pattern for idempotency with check mode + meraki_content_filtering: + auth_key: '{{auth_key}}' + org_name: '{{test_org_name}}' + net_name: '{{test_net_name}}' + state: present + allowed_urls: + - "http://www.ansible.com/*" + register: single_allowed_idempotent_check + check_mode: yes + + - debug: + var: single_allowed_idempotent_check + + - assert: + that: + - single_allowed_idempotent_check is not changed + - name: Set single allowed URL pattern for idempotency meraki_content_filtering: auth_key: '{{auth_key}}' @@ -116,16 +147,36 @@ category_list_size: full list blocked_categories: - "Adult and Pornography" - register: blocked_cateogry + register: blocked_category - debug: - var: blocked_cateogry + var: blocked_category - assert: that: - - blocked_cateogry.changed == True - - blocked_cateogry.data.blockedUrlCategories | length == 1 - - blocked_cateogry.data.urlCategoryListSize == "fullList" + - blocked_category.changed == True + - blocked_category.data.blockedUrlCategories | length == 1 + - blocked_category.data.urlCategoryListSize == "fullList" + + - name: Set blocked URL category with top sites + meraki_content_filtering: + auth_key: '{{auth_key}}' + org_name: '{{test_org_name}}' + net_name: '{{test_net_name}}' + state: present + category_list_size: top sites + blocked_categories: + - "Adult and Pornography" + register: blocked_category + + - debug: + var: blocked_category + + - assert: + that: + - blocked_category.changed == True + - blocked_category.data.blockedUrlCategories | length == 1 + - blocked_category.data.urlCategoryListSize == "topSites" always: - name: Reset policies @@ -139,5 +190,3 @@ - blocked_urls: - - # blocked_categories: - # -