From 10233ef3b50c8212b2c28d9972a9cde2b136adfb Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Thu, 6 Jul 2017 21:57:12 +0530 Subject: [PATCH] junos_user declarative module changes (#26475) * junos_user declarative module changes * Active/Deactivate support * junos_user integration test * net_user intergration test for junos * Add version_added for active param --- .../modules/network/junos/junos_user.py | 17 +- test/integration/junos.yaml | 7 + .../targets/junos_user/defaults/main.yaml | 3 + .../targets/junos_user/tasks/main.yaml | 2 + .../targets/junos_user/tasks/netconf.yaml | 15 ++ .../junos_user/tests/netconf/basic.yaml | 170 ++++++++++++++++++ .../targets/net_user/tasks/main.yaml | 1 + .../targets/net_user/tasks/netconf.yaml | 16 ++ .../targets/net_user/tests/junos/basic.yaml | 120 +++++++++++++ .../targets/net_user/tests/netconf/basic.yaml | 3 + 10 files changed, 352 insertions(+), 2 deletions(-) create mode 100644 test/integration/targets/junos_user/defaults/main.yaml create mode 100644 test/integration/targets/junos_user/tasks/main.yaml create mode 100644 test/integration/targets/junos_user/tasks/netconf.yaml create mode 100644 test/integration/targets/junos_user/tests/netconf/basic.yaml create mode 100644 test/integration/targets/net_user/tasks/netconf.yaml create mode 100644 test/integration/targets/net_user/tests/junos/basic.yaml create mode 100644 test/integration/targets/net_user/tests/netconf/basic.yaml diff --git a/lib/ansible/modules/network/junos/junos_user.py b/lib/ansible/modules/network/junos/junos_user.py index d144888cae5..0bd3ce099a1 100644 --- a/lib/ansible/modules/network/junos/junos_user.py +++ b/lib/ansible/modules/network/junos/junos_user.py @@ -91,6 +91,12 @@ options: required: false default: present choices: ['present', 'absent'] + active: + description: + - Specifies whether or not the configuration is active or deactivated + default: True + choices: [True, False] + version_added: "2.4" requirements: - ncclient (>=v0.5.2) notes: @@ -160,6 +166,11 @@ def map_obj_to_ele(want): SubElement(user, 'name').text = item['name'] if operation == 'replace': + if item['active']: + user.set('active', 'active') + else: + user.set('inactive', 'inactive') + SubElement(user, 'class').text = item['role'] if item.get('full_name'): @@ -220,7 +231,8 @@ def map_params_to_obj(module): 'full_name': get_value('full_name'), 'role': get_value('role'), 'sshkey': get_value('sshkey'), - 'state': get_value('state') + 'state': get_value('state'), + 'active': get_value('active') }) for key, value in iteritems(item): @@ -247,7 +259,8 @@ def main(): purge=dict(type='bool'), - state=dict(choices=['present', 'absent'], default='present') + state=dict(choices=['present', 'absent'], default='present'), + active=dict(default=True, type='bool') ) mutually_exclusive = [('users', 'name')] diff --git a/test/integration/junos.yaml b/test/integration/junos.yaml index cc825def435..3ae5d4d9bfc 100644 --- a/test/integration/junos.yaml +++ b/test/integration/junos.yaml @@ -92,6 +92,13 @@ rescue: - set_fact: test_failed=true + - block: + - include_role: + name: junos_user + when: "limit_to in ['*', 'junos_user']" + rescue: + - set_fact: test_failed=true + ########### - name: Has any previous test failed? fail: diff --git a/test/integration/targets/junos_user/defaults/main.yaml b/test/integration/targets/junos_user/defaults/main.yaml new file mode 100644 index 00000000000..822f2213a44 --- /dev/null +++ b/test/integration/targets/junos_user/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_cases: [] diff --git a/test/integration/targets/junos_user/tasks/main.yaml b/test/integration/targets/junos_user/tasks/main.yaml new file mode 100644 index 00000000000..cc27f174fd8 --- /dev/null +++ b/test/integration/targets/junos_user/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: netconf.yaml, tags: ['netconf'] } diff --git a/test/integration/targets/junos_user/tasks/netconf.yaml b/test/integration/targets/junos_user/tasks/netconf.yaml new file mode 100644 index 00000000000..bd91bd88cea --- /dev/null +++ b/test/integration/targets/junos_user/tasks/netconf.yaml @@ -0,0 +1,15 @@ +--- +- name: collect netconf test cases + find: + paths: "{{ role_path }}/tests/netconf" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/junos_user/tests/netconf/basic.yaml b/test/integration/targets/junos_user/tests/netconf/basic.yaml new file mode 100644 index 00000000000..03275df8ee0 --- /dev/null +++ b/test/integration/targets/junos_user/tests/netconf/basic.yaml @@ -0,0 +1,170 @@ +--- +- debug: msg="START junos_user netconf/basic.yaml" + +- name: setup - remove user + junos_user: + name: test_user + state: absent + provider: "{{ netconf }}" + +- name: Create user + junos_user: + name: test_user + state: present + full_name: test_user + role: operator + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user' in config.xml" + - "'test_user' in config.xml" + - "'read-only' in config.xml" + +- name: Create user again (idempotent) + junos_user: + name: test_user + state: present + full_name: test_user + role: operator + provider: "{{ netconf }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: Deactivate user + junos_user: + name: test_user + state: present + full_name: test_user + role: operator + active: False + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'' in config.xml" + - "'test_user' in config.xml" + +- name: Activate user + junos_user: + name: test_user + state: present + full_name: test_user + role: operator + active: True + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user' in config.xml" + - "'test_user' in config.xml" + - "'read-only' in config.xml" + +- name: Delete user + junos_user: + name: test_user + state: absent + full_name: test_user + role: operator + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user' not in config.xml" + - "'test_user' not in config.xml" + +- name: Delete user again (idempotent check) + junos_user: + name: test_user + state: absent + full_name: test_user + role: operator + provider: "{{ netconf }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: Teardown list of users + junos_user: + collection: + - {name: test_user1, state: absent} + - {name: test_user2, state: absent} + provider: "{{ netconf }}" + register: result + +- name: Create list of users + junos_user: + collection: + - {name: test_user1, full_name: test_user2, role: operator, state: present} + - {name: test_user2, full_name: test_user2, role: read-only, state: present} + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user1' in config.xml" + - "'test_user2' in config.xml" + +- name: Delete list of users + junos_user: + collection: + - {name: test_user1, full_name: test_user2, role: operator, state: absent} + - {name: test_user2, full_name: test_user2, role: read-only, state: absent} + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user1' not in config.xml" + - "'test_user2' not in config.xml" diff --git a/test/integration/targets/net_user/tasks/main.yaml b/test/integration/targets/net_user/tasks/main.yaml index 415c99d8b12..af08869c922 100644 --- a/test/integration/targets/net_user/tasks/main.yaml +++ b/test/integration/targets/net_user/tasks/main.yaml @@ -1,2 +1,3 @@ --- - { include: cli.yaml, tags: ['cli'] } +- { include: netconf.yaml, tags: ['netconf'] } diff --git a/test/integration/targets/net_user/tasks/netconf.yaml b/test/integration/targets/net_user/tasks/netconf.yaml new file mode 100644 index 00000000000..1286b354228 --- /dev/null +++ b/test/integration/targets/net_user/tasks/netconf.yaml @@ -0,0 +1,16 @@ +--- +- name: collect all netconf test cases + find: + paths: "{{ role_path }}/tests/netconf" + patterns: "{{ testcase }}.yaml" + register: test_cases + delegate_to: localhost + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/net_user/tests/junos/basic.yaml b/test/integration/targets/net_user/tests/junos/basic.yaml new file mode 100644 index 00000000000..2d1fdbb99f8 --- /dev/null +++ b/test/integration/targets/net_user/tests/junos/basic.yaml @@ -0,0 +1,120 @@ +--- +- debug: msg="START net_user junos/basic.yaml" + +- name: setup - remove user + net_user: + name: test_user + state: absent + provider: "{{ netconf }}" + +- name: Create user + net_user: + name: test_user + state: present + role: operator + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user' in config.xml" + - "'read-only' in config.xml" + +- name: Create user again (idempotent) + net_user: + name: test_user + state: present + role: operator + provider: "{{ netconf }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: Delete user + net_user: + name: test_user + state: absent + role: operator + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user' not in config.xml" + - "'test_user' not in config.xml" + +- name: Delete user again (idempotent check) + net_user: + name: test_user + state: absent + role: operator + provider: "{{ netconf }}" + register: result + +- assert: + that: + - "result.changed == false" + +- name: Teardown list of users + net_user: + collection: + - {name: test_user1, state: absent} + - {name: test_user2, state: absent} + provider: "{{ netconf }}" + register: result + +- name: Create list of users + net_user: + collection: + - {name: test_user1, role: operator, state: present} + - {name: test_user2, role: read-only, state: present} + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user1' in config.xml" + - "'test_user2' in config.xml" + +- name: Delete list of users + net_user: + collection: + - {name: test_user1, role: operator, state: absent} + - {name: test_user2, role: read-only, state: absent} + provider: "{{ netconf }}" + register: result + +- name: Get running configuration + junos_rpc: + rpc: get-configuration + provider: "{{ netconf }}" + register: config + +- assert: + that: + - "result.changed == true" + - "'test_user1' not in config.xml" + - "'test_user2' not in config.xml" diff --git a/test/integration/targets/net_user/tests/netconf/basic.yaml b/test/integration/targets/net_user/tests/netconf/basic.yaml new file mode 100644 index 00000000000..5ff7cf5af8e --- /dev/null +++ b/test/integration/targets/net_user/tests/netconf/basic.yaml @@ -0,0 +1,3 @@ +--- +- include: "{{ role_path }}/tests/junos/basic.yaml" + when: hostvars[inventory_hostname]['ansible_network_os'] == 'junos'