Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix junos_rpc and junos_user broken issues #24238

Merged
merged 2 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions lib/ansible/modules/network/junos/junos_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@
returned: always
type: list
"""
from ncclient.xml_ import new_ele, sub_ele, to_xml, to_ele
from xml.etree.ElementTree import Element, SubElement, tostring

from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netconf import send_request
from ansible.module_utils.six import iteritems

USE_PERSISTENT_CONNECTION = True


def main():
Expand All @@ -101,10 +103,15 @@ def main():
output=dict(default='xml', choices=['xml', 'json', 'text']),
)

argument_spec.update(junos_argument_spec)

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

result = {'changed': False}
warnings = list()
check_args(module, warnings)

result = {'changed': False, 'warnings': warnings}

rpc = str(module.params['rpc']).replace('_', '-')

Expand All @@ -115,37 +122,34 @@ def main():

xattrs = {'format': module.params['output']}

element = new_ele(module.params['rpc'], xattrs)
element = Element(module.params['rpc'], xattrs)

for key, value in iteritems(args):
key = str(key).replace('_', '-')
if isinstance(value, list):
for item in value:
child = sub_ele(element, key)
child = SubElement(element, key)
if item is not True:
child.text = item
else:
child = sub_ele(element, key)
child = SubElement(element, key)
if value is not True:
child.text = value

reply = send_request(module, element)

result['xml'] = str(to_xml(reply))
result['xml'] = str(tostring(reply))

if module.params['output'] == 'text':
reply = to_ele(reply)
data = reply.xpath('//output')
result['output'] = data[0].text.strip()
data = reply.find('.//output')
result['output'] = data.text.strip()
result['output_lines'] = result['output'].split('\n')

elif module.params['output'] == 'json':
reply = to_ele(reply)
data = reply.xpath('//rpc-reply')
result['output'] = module.from_json(data[0].text.strip())
result['output'] = module.from_json(reply.text.strip())

else:
result['output'] = str(to_xml(reply)).split('\n')
result['output'] = str(tostring(reply)).split('\n')

module.exit_json(**result)

Expand Down
34 changes: 22 additions & 12 deletions lib/ansible/modules/network/junos/junos_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,41 +116,45 @@
"""
from functools import partial

from ncclient.xml_ import new_ele, sub_ele, to_xml
from xml.etree.ElementTree import Element, SubElement, tostring

from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.junos import load_config
from ansible.module_utils.six import iteritems

ROLES = ['operator', 'read-only', 'super-user', 'unauthorized']
USE_PERSISTENT_CONNECTION = True


def map_obj_to_ele(want):
element = new_ele('system')
login = sub_ele(element, 'login', {'replace': 'replace'})
element = Element('system')
login = SubElement(element, 'login', {'replace': 'replace'})

for item in want:
if item['state'] != 'present':
operation = 'delete'
else:
operation = 'replace'

user = sub_ele(login, 'user', {'operation': operation})
user = SubElement(login, 'user', {'operation': operation})

sub_ele(user, 'name').text = item['name']
SubElement(user, 'name').text = item['name']

if operation == 'replace':
sub_ele(user, 'class').text = item['role']
SubElement(user, 'class').text = item['role']

if item.get('full_name'):
sub_ele(user, 'full-name').text = item['full_name']
SubElement(user, 'full-name').text = item['full_name']

if item.get('sshkey'):
auth = sub_ele(user, 'authentication')
ssh_rsa = sub_ele(auth, 'ssh-rsa')
key = sub_ele(ssh_rsa, 'name').text = item['sshkey']
auth = SubElement(user, 'authentication')
ssh_rsa = SubElement(auth, 'ssh-rsa')
key = SubElement(ssh_rsa, 'name').text = item['sshkey']

return element


def get_param_value(key, item, module):
# if key doesn't exist in the item, get it from module.params
if not item.get(key):
Expand All @@ -170,6 +174,7 @@ def get_param_value(key, item, module):

return value


def map_params_to_obj(module):
users = module.params['users']
if not users:
Expand Down Expand Up @@ -229,11 +234,16 @@ def main():

mutually_exclusive = [('users', 'name')]

argument_spec.update(junos_argument_spec)

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

result = {'changed': False}
warnings = list()
check_args(module, warnings)

result = {'changed': False, 'warnings': warnings}

want = map_params_to_obj(module)
ele = map_obj_to_ele(want)
Expand All @@ -242,7 +252,7 @@ def main():
if module.params['purge']:
kwargs['action'] = 'replace'

diff = load_config(module, ele, **kwargs)
diff = load_config(module, tostring(ele), warnings, **kwargs)

if diff:
result.update({
Expand Down
2 changes: 0 additions & 2 deletions test/sanity/pep8/legacy-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,6 @@ lib/ansible/modules/network/junos/junos_config.py
lib/ansible/modules/network/junos/junos_facts.py
lib/ansible/modules/network/junos/junos_netconf.py
lib/ansible/modules/network/junos/junos_package.py
lib/ansible/modules/network/junos/junos_rpc.py
lib/ansible/modules/network/junos/junos_user.py
lib/ansible/modules/network/lenovo/cnos_conditional_template.py
lib/ansible/modules/network/lenovo/cnos_template.py
lib/ansible/modules/network/lenovo/cnos_vlan.py
Expand Down