Skip to content

Commit

Permalink
nxos_snmp cleanup (ansible#28922)
Browse files Browse the repository at this point in the history
* Clean up nxos_snmp_contact & nxos_snmp_location

* Bring nxos_snmp_community in line

* Bring nxos_snmp_host in line

* And I would have gotten away with it too,

if it weren't for those meddling sanity tests

* Bring nxos_snmp_traps & nxos_snmp_user in line

* Appease Shippable
  • Loading branch information
Qalthos authored Sep 13, 2017
1 parent 1a266e2 commit 8c03609
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 361 deletions.
19 changes: 9 additions & 10 deletions lib/ansible/modules/network/nxos/nxos_snmp_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
options:
community:
description:
Expand Down Expand Up @@ -78,9 +80,7 @@
'''


import re

from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule

Expand All @@ -91,7 +91,7 @@ def execute_show_command(command, module):
'output': 'json',
}

return run_commands(module, [command])
return run_commands(module, command)


def apply_key_map(key_map, table):
Expand All @@ -117,24 +117,21 @@ def flatten_list(command_lists):


def get_snmp_groups(module):
command = 'show snmp group'
data = execute_show_command(command, module)[0]

data = execute_show_command('show snmp group', module)[0]
group_list = []

try:
group_table = data['TABLE_role']['ROW_role']
for group in group_table:
group_list.append(group['role_name'])
except (KeyError, AttributeError):
return group_list
pass

return group_list


def get_snmp_community(module, find_filter=None):
command = 'show snmp community'
data = execute_show_command(command, module)[0]
data = execute_show_command('show snmp community', module)[0]

community_dict = {}

Expand Down Expand Up @@ -225,6 +222,7 @@ def main():
delta = dict(set(proposed.items()).difference(existing.items()))

commands = []

if state == 'absent':
if existing:
command = "no snmp-server community {0}".format(community)
Expand All @@ -239,6 +237,7 @@ def main():
results['changed'] = True
if not module.check_mode:
load_config(module, cmds)

if 'configure' in cmds:
cmds.pop(0)
results['commands'] = cmds
Expand Down
30 changes: 11 additions & 19 deletions lib/ansible/modules/network/nxos/nxos_snmp_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

import re

from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule

Expand All @@ -77,7 +77,7 @@ def execute_show_command(command, module):
'output': 'text',
}

return run_commands(module, [command])
return run_commands(module, command)


def flatten_list(command_lists):
Expand All @@ -92,43 +92,34 @@ def flatten_list(command_lists):

def get_snmp_contact(module):
contact = {}
contact_regex = '.*snmp-server\scontact\s(?P<contact>\S+).*'
command = 'show run snmp'
contact_regex = r'^\s*snmp-server\scontact\s(?P<contact>.+)$'

body = execute_show_command(command, module)[0]

try:
match_contact = re.match(contact_regex, body, re.DOTALL)
group_contact = match_contact.groupdict()
contact['contact'] = group_contact["contact"]
except AttributeError:
contact = {}
body = execute_show_command('show run snmp', module)[0]
match_contact = re.search(contact_regex, body, re.M)
if match_contact:
contact['contact'] = match_contact.group("contact")

return contact


def main():
argument_spec = dict(
contact=dict(required=True, type='str'),
state=dict(choices=['absent', 'present'],
default='present')
state=dict(choices=['absent', 'present'], default='present'),
)

argument_spec.update(nxos_argument_spec)

module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

warnings = list()
check_args(module, warnings)
results = {'changed': False, 'commands': [], 'warnings': warnings}


contact = module.params['contact']
state = module.params['state']

existing = get_snmp_contact(module)
proposed = dict(contact=contact)
commands = []

if state == 'absent':
Expand All @@ -140,11 +131,12 @@ def main():

cmds = flatten_list(commands)
if cmds:
results['changed'] = True
if not module.check_mode:
load_config(module, cmds)

if 'configure' in cmds:
cmds.pop(0)
results['changed'] = True
results['commands'] = cmds

module.exit_json(**results)
Expand Down
Loading

0 comments on commit 8c03609

Please sign in to comment.