Skip to content

Commit

Permalink
Refactor/fix nxos_nxapi to use show run (ansible#28675)
Browse files Browse the repository at this point in the history
* Refactor/fix nxos_nxapi to use show run

* Fix unit tests

* Python 3 compatibility
  • Loading branch information
mikewiebe authored and Qalthos committed Aug 28, 2017
1 parent af93968 commit 3d46258
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 24 deletions.
18 changes: 11 additions & 7 deletions lib/ansible/modules/network/nxos/nxos_nxapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def map_obj_to_commands(want, have, module):
return commands

def parse_http(data):
http_res = [r'HTTP Port:\s+(\d+)', r'HTTP Listen on port (\d+)']
http_res = [r'nxapi http port (\d+)']
http_port = None

for regex in http_res:
Expand All @@ -208,7 +208,7 @@ def parse_http(data):
return {'http': http_port is not None, 'http_port': http_port}

def parse_https(data):
https_res = [r'HTTPS Port:\s+(\d+)', r'HTTPS Listen on port (\d+)']
https_res = [r'nxapi https port (\d+)']
https_port = None

for regex in https_res:
Expand All @@ -220,15 +220,19 @@ def parse_https(data):
return {'https': https_port is not None, 'https_port': https_port}

def parse_sandbox(data):
match = re.search(r'Sandbox:\s+(.+)$', data, re.M)
sandbox = [item for item in data.split('\n') if re.search(r'.*sandbox.*', item)]
value = False
if match:
value = match.group(1) == 'Enabled'
if sandbox and sandbox[0] == 'nxapi sandbox':
value = True
return {'sandbox': value}

def map_config_to_obj(module):
out = run_commands(module, ['show nxapi'], check_rc=False)[0]
if out == '':
out = run_commands(module, ['show run all | inc nxapi'], check_rc=False)[0]
match = re.search(r'no feature nxapi', out, re.M)
# There are two possible outcomes when nxapi is disabled on nxos platforms.
# 1. Nothing is displayed in the running config.
# 2. The 'no feature nxapi' command is displayed in the running config.
if match or out == '':
return {'state': 'absent'}

out = str(out).strip()
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/nxos_nxapi/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
---
# Only cli tests for this module since cli is used
# to test nxapi
- { include: cli.yaml, tags: ['cli'] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Assert configuration changes
assert:
that:
- result.stdout[0]['TABLE_listen_on_port']['ROW_listen_on_port'].l_port
- result.stdout[0]['TABLE_listen_on_port']['ROW_listen_on_port'].l_port | string | search("9443")
- result.stdout[0]['operation_status'].o_status == 'nxapi enabled'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Assert configuration changes
assert:
that:
- result.stdout[0].http_port is not defined
- result.stdout[0].https_port | string | search("9443")
- result.stdout[0].sandbox_status == 'Enabled'
18 changes: 10 additions & 8 deletions test/integration/targets/nxos_nxapi/tests/cli/configure.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
- debug: msg="START cli/configure.yaml"

- set_fact: nxapi_sandbox_option="yes"
when: platform | search('N7K')

- name: Setup - put NXAPI in stopped state
nxos_nxapi:
state: absent
Expand All @@ -9,7 +12,7 @@
- name: Configure NXAPI
nxos_nxapi:
enable_http: no
enable_sandbox: no
enable_sandbox: "{{nxapi_sandbox_option|default(omit)}}"
enable_https: yes
https_port: 9443
provider: "{{ cli }}"
Expand All @@ -21,17 +24,16 @@
provider: "{{ cli }}"
register: result

- name: Assert configuration changes
assert:
that:
- result.stdout[0].http_port is not defined
- result.stdout[0].https_port == 9443
- result.stdout[0].sandbox_status == 'Disabled'
- include: targets/nxos_nxapi/tasks/platform/n7k/assert_changes.yaml
when: platform | match('N7K')

- include: targets/nxos_nxapi/tasks/platform/default/assert_changes.yaml
when: not (platform | search('N7K'))

- name: Configure NXAPI again
nxos_nxapi:
enable_http: no
enable_sandbox: no
enable_sandbox: "{{nxapi_sandbox_option|default(omit)}}"
enable_https: yes
https_port: 9443
provider: "{{ cli }}"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/targets/nxos_nxapi/tests/cli/disable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,5 @@
assert:
that:
result.changed == false
# FIXME https://github.com/ansible/ansible-modules-core/issues/4955
ignore_errors: yes

- debug: msg="END cli/disable.yaml"
2 changes: 1 addition & 1 deletion test/integration/targets/nxos_nxapi/tests/cli/enable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

- name: Enable NXAPI
nxos_nxapi:
state: started
state: present
provider: "{{ cli }}"
register: result

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feature nxapi
nxapi http port 80

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
feature nxapi
nxapi http port 80
no nxapi https
no nxapi sandbox

0 comments on commit 3d46258

Please sign in to comment.