From 301defd9156997b952df179c0c6ddc5b48db4013 Mon Sep 17 00:00:00 2001 From: Ken Partridge <41302157+syncpak@users.noreply.github.com> Date: Fri, 9 Nov 2018 14:22:22 -0800 Subject: [PATCH] cpm_user concat string changes (#45044) * Add WTI OOB and PDU Device status, control and configuration module * removed accidental file inclusions * removed accidental file * remove unneeded legacy files * Changed str() to to_native() Changed + string concat to % * added to_native() to the remainder of strings being read externally --- .../modules/remote_management/cpm/cpm_user.py | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/lib/ansible/modules/remote_management/cpm/cpm_user.py b/lib/ansible/modules/remote_management/cpm/cpm_user.py index b352ae4c733..1994c2bca1a 100644 --- a/lib/ansible/modules/remote_management/cpm/cpm_user.py +++ b/lib/ansible/modules/remote_management/cpm/cpm_user.py @@ -212,39 +212,36 @@ from ansible.module_utils.urls import open_url, ConnectionError, SSLValidationEr def assemble_json(cpmmodule): - json_load = "" - - json_load = '{"users":' - json_load = json_load + '{"username": "' + cpmmodule.params["user_name"] + '"' + json_load = '{"users":{"username": "%s"' % to_native((cpmmodule.params["user_name"])) # for Adding there must be a password present if cpmmodule.params["user_pass"] is not None and (len(cpmmodule.params["user_pass"]) > 0): - json_load = json_load + ',"newpasswd": "' + cpmmodule.params["user_pass"] + '"' + json_load = '%s,"newpasswd": "%s"' % (json_load, to_native(cpmmodule.params["user_pass"])) if cpmmodule.params["user_accesslevel"] is not None: - json_load = json_load + ',"accesslevel": ' + str(cpmmodule.params["user_accesslevel"]) + '' + json_load = '%s,"accesslevel": %s' % (json_load, to_native(cpmmodule.params["user_accesslevel"])) if cpmmodule.params["user_portaccess"] is not None: - json_load = json_load + ',"portaccess": ' + cpmmodule.params["user_portaccess"] + '' + json_load = '%s,"portaccess": %s' % (json_load, to_native(cpmmodule.params["user_portaccess"])) if cpmmodule.params["user_plugaccess"] is not None: - json_load = json_load + ',"plugaccess": ' + cpmmodule.params["user_plugaccess"] + '' + json_load = '%s,"plugaccess": %s' % (json_load, to_native(cpmmodule.params["user_plugaccess"])) if cpmmodule.params["user_groupaccess"] is not None: - json_load = json_load + ',"groupaccess": ' + cpmmodule.params["user_groupaccess"] + '' + json_load = '%s,"groupaccess": %s' % (json_load, to_native(cpmmodule.params["user_groupaccess"])) if cpmmodule.params["user_accessserial"] is not None: - json_load = json_load + ',"accessserial": ' + str(cpmmodule.params["user_accessserial"]) + '' + json_load = '%s,"accessserial": %s' % (json_load, to_native(cpmmodule.params["user_accessserial"])) if cpmmodule.params["user_accessssh"] is not None: - json_load = json_load + ',"accessssh": ' + str(cpmmodule.params["user_accessssh"]) + '' + json_load = '%s,"accessssh": %s' % (json_load, to_native(cpmmodule.params["user_accessssh"])) if cpmmodule.params["user_accessweb"] is not None: - json_load = json_load + ',"accessweb": ' + str(cpmmodule.params["user_accessweb"]) + '' + json_load = '%s,"accessweb": %s' % (json_load, to_native(cpmmodule.params["user_accessweb"])) if cpmmodule.params["user_accessoutbound"] is not None: - json_load = json_load + ',"accessoutbound": ' + str(cpmmodule.params["user_accessoutbound"]) + '' + json_load = '%s,"accessoutbound": %s' % (json_load, to_native(cpmmodule.params["user_accessoutbound"])) if cpmmodule.params["user_accessapi"] is not None: - json_load = json_load + ',"accessapi": ' + str(cpmmodule.params["user_accessapi"]) + '' + json_load = '%s,"accessapi": %s' % (json_load, to_native(cpmmodule.params["user_accessapi"])) if cpmmodule.params["user_accessmonitor"] is not None: - json_load = json_load + ',"accessmonitor": ' + str(cpmmodule.params["user_accessmonitor"]) + '' + json_load = '%s,"accessmonitor": %s' % (json_load, to_native(cpmmodule.params["user_accessmonitor"])) if cpmmodule.params["user_callbackphone"] is not None: - json_load = json_load + ',"callbackphone": "' + cpmmodule.params["user_callbackphone"] + '"' + json_load = '%s,"callbackphone": "%s"' % (json_load, to_native(cpmmodule.params["user_callbackphone"])) + + json_load = '%s}}' % (json_load) - json_load = json_load + '}' - json_load = json_load + '}' return json_load @@ -283,7 +280,7 @@ def run_module(): if module.check_mode: return result - auth = to_text(base64.b64encode(to_bytes('{0}:{1}'.format(module.params['cpm_username'], module.params['cpm_password']), + auth = to_text(base64.b64encode(to_bytes('{0}:{1}'.format(to_native(module.params['cpm_username']), to_native(module.params['cpm_password'])), errors='surrogate_or_strict'))) if module.params['use_https'] is True: @@ -293,21 +290,21 @@ def run_module(): payload = None if (module.params['cpm_action'] == 'getuser'): - fullurl = ("%s%s/api/v2/config/users?username=%s" % (protocol, module.params['cpm_url'], module.params['user_name'])) + fullurl = ("%s%s/api/v2/config/users?username=%s" % (protocol, to_native(module.params['cpm_url']), to_native(module.params['user_name']))) method = 'GET' elif (module.params['cpm_action'] == 'adduser'): if module.params["user_pass"] is None or (len(module.params["user_pass"]) == 0): module.fail_json(msg='user_pass not defined.', **result) payload = assemble_json(module) - fullurl = ("%s%s/api/v2/config/users" % (protocol, module.params['cpm_url'])) + fullurl = ("%s%s/api/v2/config/users" % (protocol, to_native(module.params['cpm_url']))) method = 'POST' elif (module.params['cpm_action'] == 'edituser'): payload = assemble_json(module) - fullurl = ("%s%s/api/v2/config/users" % (protocol, module.params['cpm_url'])) + fullurl = ("%s%s/api/v2/config/users" % (protocol, to_native(module.params['cpm_url']))) method = 'PUT' elif (module.params['cpm_action'] == 'deleteuser'): - fullurl = ("%s%s/api/v2/config/users?username=%s" % (protocol, module.params['cpm_url'], module.params['user_name'])) + fullurl = ("%s%s/api/v2/config/users?username=%s" % (protocol, to_native(module.params['cpm_url']), to_native(module.params['user_name']))) method = 'DELETE' try: @@ -330,7 +327,6 @@ def run_module(): module.fail_json(**fail_json) result['data'] = to_text(response.read()) - module.exit_json(**result)