From 32371e3fe712b08bc3e3b47075741234ad784586 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Mon, 31 Jan 2022 19:41:29 -0500 Subject: Vyos snmp_server Resource Module (#227) Vyos snmp_server Resource Module SUMMARY Added vyos_snmp_server resource module ISSUE TYPE New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Nilashish Chakraborty Reviewed-by: Ashwini Mhatre Reviewed-by: None --- .../targets/vyos_snmp_server/defaults/main.yaml | 3 + .../targets/vyos_snmp_server/meta/main.yaml | 2 + .../targets/vyos_snmp_server/tasks/cli.yaml | 19 ++++ .../targets/vyos_snmp_server/tasks/main.yaml | 4 + .../targets/vyos_snmp_server/tests/cli/_parsed.cfg | 19 ++++ .../tests/cli/_populate_config.yaml | 18 +++ .../vyos_snmp_server/tests/cli/_remove_config.yaml | 8 ++ .../vyos_snmp_server/tests/cli/deleted.yaml | 40 +++++++ .../vyos_snmp_server/tests/cli/empty_config.yaml | 60 ++++++++++ .../vyos_snmp_server/tests/cli/gathered.yaml | 24 ++++ .../targets/vyos_snmp_server/tests/cli/merged.yaml | 63 +++++++++++ .../vyos_snmp_server/tests/cli/overridden.yaml | 67 +++++++++++ .../targets/vyos_snmp_server/tests/cli/parsed.yaml | 17 +++ .../vyos_snmp_server/tests/cli/rendered.yaml | 39 +++++++ .../vyos_snmp_server/tests/cli/replaced.yaml | 67 +++++++++++ .../targets/vyos_snmp_server/vars/main.yaml | 126 +++++++++++++++++++++ 16 files changed, 576 insertions(+) create mode 100644 tests/integration/targets/vyos_snmp_server/defaults/main.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/meta/main.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tasks/cli.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tasks/main.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml create mode 100644 tests/integration/targets/vyos_snmp_server/vars/main.yaml (limited to 'tests/integration/targets') diff --git a/tests/integration/targets/vyos_snmp_server/defaults/main.yaml b/tests/integration/targets/vyos_snmp_server/defaults/main.yaml new file mode 100644 index 0000000..852a6be --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: '[^_].*' +test_items: [] diff --git a/tests/integration/targets/vyos_snmp_server/meta/main.yaml b/tests/integration/targets/vyos_snmp_server/meta/main.yaml new file mode 100644 index 0000000..91da2a7 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/meta/main.yaml @@ -0,0 +1,2 @@ +--- +... diff --git a/tests/integration/targets/vyos_snmp_server/tasks/cli.yaml b/tests/integration/targets/vyos_snmp_server/tasks/cli.yaml new file mode 100644 index 0000000..93eb2fe --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tasks/cli.yaml @@ -0,0 +1,19 @@ +--- +- name: Collect all cli test cases + find: + paths: '{{ role_path }}/tests/cli' + patterns: '{{ testcase }}.yaml' + use_regex: true + 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 (connection=ansible.netcommon.network_cli) + include: '{{ test_case_to_run }}' + vars: + ansible_connection: ansible.netcommon.network_cli + with_items: '{{ test_items }}' + loop_control: + loop_var: test_case_to_run diff --git a/tests/integration/targets/vyos_snmp_server/tasks/main.yaml b/tests/integration/targets/vyos_snmp_server/tasks/main.yaml new file mode 100644 index 0000000..b957d2f --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tasks/main.yaml @@ -0,0 +1,4 @@ +--- +- include: cli.yaml + tags: + - network_cli diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg b/tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg new file mode 100644 index 0000000..9750253 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/_parsed.cfg @@ -0,0 +1,19 @@ +set service snmp community routers authorization 'ro' +set service snmp community routers client '203.0.113.10' +set service snmp community routers client '203.0.113.20' +set service snmp community routers network '192.0.2.0/24' +set service snmp community routers network '2001::/64' +set service snmp contact 'admin@example.com' +set service snmp listen-address 172.16.254.36 port '161' +set service snmp listen-address 2001::1 +set service snmp location 'UK, London' +set service snmp trap-target 203.0.113.10 +set service snmp v3 engineid '000000000000000000000002' +set service snmp v3 group default mode 'ro' +set service snmp v3 group default view 'default' +set service snmp v3 user vyos auth plaintext-key 'vyos12345678' +set service snmp v3 user vyos auth type 'sha' +set service snmp v3 user vyos group 'default' +set service snmp v3 user vyos privacy plaintext-key 'vyos12345678' +set service snmp v3 user vyos privacy type 'aes' +set service snmp v3 view default oid 1 diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml new file mode 100644 index 0000000..f4d89f5 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/_populate_config.yaml @@ -0,0 +1,18 @@ +--- +- name: setup + vyos.vyos.vyos_config: + lines: + - set service snmp community switches authorization rw + - set service snmp community bridges client 1.1.1.1 + - set service snmp community bridges client 12.1.1.10 + - set service snmp listen-address 20.1.1.1 + - set service snmp listen-address 100.1.2.1 port 33 + - set service snmp v3 user adminuser engineid 33 + - set service snmp v3 user adminuser auth type sha + - set service snmp v3 user adminuser auth plaintext-key abc1234567 + - set service snmp v3 user adminuser privacy type aes + - set service snmp v3 user adminuser privacy plaintext-key abc1234567 + - set service snmp contact admin2@ex.com + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml new file mode 100644 index 0000000..12cfb75 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/_remove_config.yaml @@ -0,0 +1,8 @@ +--- +- name: Remove pre-existing snmp_server config + vyos.vyos.vyos_snmp_server: + config: + state: deleted + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml new file mode 100644 index 0000000..1fa32c5 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/deleted.yaml @@ -0,0 +1,40 @@ +--- +- debug: + msg: START vyos_snmp_server deleted integration tests on connection={{ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Delete the provided configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + state: deleted + + - name: Assert that before dicts were correctly generated + assert: + that: + - result.changed == true + - result.commands == deleted.commands + + - name: Assert that the after dicts were correctly generated + assert: + that: + - result.after == {} + + - name: Delete the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml new file mode 100644 index 0000000..558b53f --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/empty_config.yaml @@ -0,0 +1,60 @@ +--- +- debug: + msg: START vyos_snmp_server empty_config integration tests on connection={{ + ansible_connection }} + +- name: Merged with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: merged + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state merged' + +- name: Replaced with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: replaced + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state replaced' + +- name: Overridden with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: overridden + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state overridden' + +- name: Parsed with empty running_config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + running_config: + state: parsed + +- assert: + that: + - result.msg == 'value of running_config parameter must not be empty for state + parsed' + +- name: Rendered with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_snmp_server: + config: + state: rendered + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state rendered' diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml new file mode 100644 index 0000000..184faa5 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/gathered.yaml @@ -0,0 +1,24 @@ +--- +- debug: + msg: START vyos_snmp_server gathered integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Gather config from the device in structured format. + register: result + vyos.vyos.vyos_snmp_server: + state: gathered + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - name: Assert that facts are correctly generated + assert: + that: + - result.changed == false + - result.gathered == ansible_facts['network_resources']['snmp_server'] diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml new file mode 100644 index 0000000..e372166 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/merged.yaml @@ -0,0 +1,63 @@ +--- +- debug: + msg: START vyos_snmp_server merged integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Merge the provided configuration with the existing running configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + communities: + - name: "switches" + authorization_type: "rw" + - name: "bridges" + clients: ["1.1.1.1", "12.1.1.10"] + contact: "admin2@ex.com" + listen_addresses: + - address: "20.1.1.1" + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - assert: + that: + - result.commands|length == 10 + - result.changed == true + - result.commands|symmetric_difference(merged.commands) == [] + - result.after == ansible_facts['network_resources']['snmp_server'] + - result.after == merged.after + + + - name: Assert that before dicts were correctly generated + assert: + that: + - result.before == {} + + - name: + Merge the provided configuration with the existing running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml new file mode 100644 index 0000000..5f3d9c9 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/overridden.yaml @@ -0,0 +1,67 @@ +--- +- debug: + msg: START vyos_snmp_server overridden integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Override the existing configuration with the provided running configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + communities: + - name: "bridges" + networks: ["1.1.1.0/24", "12.1.1.0/24"] + location: "RDU" + listen_addresses: + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + engine_id: "33" + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + - user: guestuser2 + authentication: + plaintext_key: "opq1234567" + type: "sha" + privacy: + plaintext_key: "opq1234567" + type: "aes" + views: + - view: "default" + oid: 1 + state: overridden + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - assert: + that: + - result.changed == true + - result.after == ansible_facts['network_resources']['snmp_server'] + - result.commands|length == 13 + + + - name: Override the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml new file mode 100644 index 0000000..e189134 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/parsed.yaml @@ -0,0 +1,17 @@ +--- +- debug: + msg: START vyos_snmp_server parsed integration tests on connection={{ ansible_connection + }} + +- name: Provide the running configuration for parsing (config to be parsed) + register: result + vyos.vyos.vyos_snmp_server: + running_config: "{{ lookup('file', '_parsed.cfg') }}" + state: parsed + + +- name: Assert that config was correctly parsed + assert: + that: + - result.changed == false + - result.parsed == parsed.after diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml new file mode 100644 index 0000000..e238827 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/rendered.yaml @@ -0,0 +1,39 @@ +--- +- debug: + msg: START vyos_snmp_server rendered integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Render the given configuration in the form of native commands + register: result + vyos.vyos.vyos_snmp_server: + config: + communities: + - name: "switches" + authorization_type: "rw" + - name: "bridges" + clients: ["1.1.1.1", "12.1.1.10"] + contact: "admin2@ex.com" + listen_addresses: + - address: "20.1.1.1" + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + state: rendered + + - assert: + that: + - result.changed == false + - result.rendered|symmetric_difference(merged.commands) == [] + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml b/tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml new file mode 100644 index 0000000..0b5a564 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/tests/cli/replaced.yaml @@ -0,0 +1,67 @@ +--- +- debug: + msg: START vyos_snmp_server replaced integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Replace the existing configuration with the provided running configuration + register: result + vyos.vyos.vyos_snmp_server: &id001 + config: + communities: + - name: "bridges" + networks: ["1.1.1.0/24", "12.1.1.0/24"] + location: "RDU" + listen_addresses: + - address: "100.1.2.1" + port: 33 + snmp_v3: + users: + - user: adminuser + engine_id: "33" + authentication: + plaintext_key: "abc1234567" + type: "sha" + privacy: + plaintext_key: "abc1234567" + type: "aes" + - user: guestuser2 + authentication: + plaintext_key: "opq1234567" + type: "sha" + privacy: + plaintext_key: "opq1234567" + type: "aes" + views: + - view: "default" + oid: 1 + state: replaced + + - vyos.vyos.vyos_facts: + gather_network_resources: snmp_server + + - assert: + that: + - result.changed == true + - result.after == ansible_facts['network_resources']['snmp_server'] + - result.commands|length == 13 + + + - name: Replace the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_snmp_server: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_snmp_server/vars/main.yaml b/tests/integration/targets/vyos_snmp_server/vars/main.yaml new file mode 100644 index 0000000..060421b --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/vars/main.yaml @@ -0,0 +1,126 @@ +--- +merged: + before: {} + commands: + - set service snmp community switches authorization rw + - set service snmp community bridges client 1.1.1.1 + - set service snmp community bridges client 12.1.1.10 + - set service snmp listen-address 20.1.1.1 + - set service snmp listen-address 100.1.2.1 port 33 + - set service snmp v3 user adminuser auth type sha + - set service snmp v3 user adminuser auth plaintext-key ******** + - set service snmp v3 user adminuser privacy type aes + - set service snmp v3 user adminuser privacy plaintext-key ******** + - set service snmp contact admin2@ex.com + after: + communities: + - clients: + - 1.1.1.1 + - 12.1.1.10 + name: "bridges" + - authorization_type: "rw" + name: "switches" + contact: "admin2@ex.com" + listen_addresses: + - address: "100.1.2.1" + port: 33 + - address: "20.1.1.1" + snmp_v3: + users: + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "adminuser" + +overridden: + commands: + - delete service snmp contact admin2@ex.com + - delete service snmp listen-address 20.1.1.1 + - delete service snmp community switches authorization rw + - delete service snmp community bridges client 12.1.1.10 + - delete service snmp community bridges client 1.1.1.1 + - set service snmp community bridges network 1.1.1.0/24 + - set service snmp community bridges network 12.1.1.0/24 + - set service snmp v3 user guestuser2 auth type sha + - set service snmp v3 user guestuser2 auth plaintext-key ******** + - set service snmp v3 user guestuser2 privacy type aes + - set service snmp v3 user guestuser2 privacy plaintext-key ******** + - set service snmp v3 view default oid 1 + - set service snmp location \'RDU\' + + after: + communities: + - name: "bridges" + networks: + - "1.1.1.0/24" + - "12.1.1.0/24" + - name: "switches" + listen_addresses: + - address: "100.1.2.1" + port: 33 + location: "RDU, NC" + snmp_v3: + users: + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "adminuser" + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "guestuser2" + views: + - oid: 1 + view: "default" + +deleted: + commands: + - delete service snmp + after: {} + +parsed: + after: + communities: + - authorization_type: "ro" + clients: + - 203.0.113.10 + - 203.0.113.20 + name: "routers" + networks: + - 192.0.2.0/24 + - 2001::/64 + contact: "admin@example.com" + listen_addresses: + - address: "172.16.254.36" + port: 161 + - address: "2001::1" + location: "UK, London" + snmp_v3: + engine_id: "000000000000000000000002" + groups: + - group: "default" + mode: "ro" + view: "default" + users: + - authentication: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "sha" + group: "default" + privacy: + plaintext_key: "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" + type: "aes" + user: "vyos" + views: + - oid: "1" + view: "default" + trap_target: + address: "203.0.113.10" -- cgit v1.2.3