From af87048042b3b8bd65f26f32e9c257dc8a881b4f Mon Sep 17 00:00:00 2001 From: Mark Reynolds Date: Jun 08 2020 15:13:14 +0000 Subject: Issue 51136 - dsctl and dsidm do not errors correctly when using JSON Description: dsctl and dsidm were not returning errors in a JSON object when the JSON CLI option is requested. This breaks the UI when errors occur. fixes: https://pagure.io/389-ds-base/issue/51136 Reviewed by: firstyear & spichugi(Thanks!) --- diff --git a/src/lib389/cli/dsconf b/src/lib389/cli/dsconf index 5143756..71fc2b6 100755 --- a/src/lib389/cli/dsconf +++ b/src/lib389/cli/dsconf @@ -1,7 +1,7 @@ #!/usr/bin/python3 # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2016 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). @@ -139,7 +139,7 @@ if __name__ == '__main__': msg = format_error_to_dict(e) if args and args.json: - sys.stderr.write(json.dumps(msg)) + sys.stderr.write(json.dumps(msg, indent=4)) else: log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) result = False diff --git a/src/lib389/cli/dscreate b/src/lib389/cli/dscreate index 083c30c..dc4c706 100755 --- a/src/lib389/cli/dscreate +++ b/src/lib389/cli/dscreate @@ -80,7 +80,7 @@ if __name__ == '__main__': log.debug(e, exc_info=True) msg = format_error_to_dict(e) if args and args.json: - sys.stderr.write(json.dumps(msg)) + sys.stderr.write(json.dumps(msg, indent=4)) else: log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) result = False diff --git a/src/lib389/cli/dsctl b/src/lib389/cli/dsctl index 9deda70..21aabd1 100755 --- a/src/lib389/cli/dsctl +++ b/src/lib389/cli/dsctl @@ -140,7 +140,10 @@ if __name__ == '__main__': except Exception as e: log.debug(e, exc_info=True) msg = format_error_to_dict(e) - log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) + if args.json: + sys.stderr.write(json.dumps(msg, indent=4)) + else: + log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) result = False disconnect_instance(inst) diff --git a/src/lib389/cli/dsidm b/src/lib389/cli/dsidm index fb0b6a2..ae38d14 100755 --- a/src/lib389/cli/dsidm +++ b/src/lib389/cli/dsidm @@ -133,7 +133,10 @@ if __name__ == '__main__': except Exception as e: log.debug(e, exc_info=True) msg = format_error_to_dict(e) - log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) + if args.json: + sys.stderr.write(json.dumps(msg, indent=4)) + else: + log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) result = False disconnect_instance(inst) diff --git a/src/lib389/lib389/__init__.py b/src/lib389/lib389/__init__.py index 0ff1ab1..4d2a8b4 100644 --- a/src/lib389/lib389/__init__.py +++ b/src/lib389/lib389/__init__.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # Copyright (C) 2019 William Brown # All rights reserved. # @@ -2968,7 +2968,7 @@ class DirSrv(SimpleLDAPObject, object): self.log.info('Backup: %s - %s (%s)', bak, bak_date, bak_size) if use_json: - print(json.dumps(json_result)) + print(json.dumps(json_result, indent=4)) return True @@ -3012,7 +3012,7 @@ class DirSrv(SimpleLDAPObject, object): self.log.info('{} ({}), Created ({}), Size ({})'.format(ldif_file, ldif_suffix, ldif_date, ldif_size)) if use_json: - print(json.dumps(json_result)) + print(json.dumps(json_result, indent=4)) return True diff --git a/src/lib389/lib389/agreement.py b/src/lib389/lib389/agreement.py index e2b0dc3..efcd507 100644 --- a/src/lib389/lib389/agreement.py +++ b/src/lib389/lib389/agreement.py @@ -229,7 +229,7 @@ class Agreement(DSLdapObject): 'con_maxcsn': con_maxcsn, 'state': agmt_status['state'], 'reason': agmt_status['message'] - }) + }, indent=4) else: return "In Synchronization" except: @@ -253,7 +253,7 @@ class Agreement(DSLdapObject): 'con_maxcsn': con_maxcsn, 'state': agmt_status['state'], 'reason': repl_msg - }) + }, indent=4) else: return ("Not in Synchronization: supplier " + "(%s) consumer (%s) State (%s) Reason (%s)" % @@ -337,7 +337,7 @@ class Agreement(DSLdapObject): status = str(e) if just_status: if use_json: - return (json.dumps(status)) + return (json.dumps(status, indent=4)) else: return status @@ -387,7 +387,7 @@ class Agreement(DSLdapObject): 'replication-status': [status], 'replication-lag-time': [lag_time] } - return (json.dumps(result)) + return (json.dumps(result, indent=4)) else: retstr = ( "Status For Agreement: \"%(cn)s\" (%(nsDS5ReplicaHost)s:" diff --git a/src/lib389/lib389/cli_conf/plugins/automember.py b/src/lib389/lib389/cli_conf/plugins/automember.py index bc5e55e..cccfbd7 100644 --- a/src/lib389/lib389/cli_conf/plugins/automember.py +++ b/src/lib389/lib389/cli_conf/plugins/automember.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). @@ -37,7 +37,7 @@ def definition_list(inst, basedn, log, args): else: result.append(definition.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: @@ -94,7 +94,7 @@ def regex_list(inst, basedn, log, args): else: result.append(regex.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: diff --git a/src/lib389/lib389/cli_conf/plugins/dna.py b/src/lib389/lib389/cli_conf/plugins/dna.py index a86034d..2904b04 100644 --- a/src/lib389/lib389/cli_conf/plugins/dna.py +++ b/src/lib389/lib389/cli_conf/plugins/dna.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). @@ -62,7 +62,7 @@ def dna_list(inst, basedn, log, args): else: result.append(config.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: @@ -125,7 +125,7 @@ def dna_config_list(inst, basedn, log, args): else: result.append(config.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: diff --git a/src/lib389/lib389/cli_conf/plugins/linkedattr.py b/src/lib389/lib389/cli_conf/plugins/linkedattr.py index 12b75e1..5a69eb2 100644 --- a/src/lib389/lib389/cli_conf/plugins/linkedattr.py +++ b/src/lib389/lib389/cli_conf/plugins/linkedattr.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # Copyright (C) 2019 William Brown # All rights reserved. # @@ -30,7 +30,7 @@ def linkedattr_list(inst, basedn, log, args): else: result.append(config.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: diff --git a/src/lib389/lib389/cli_conf/plugins/managedentries.py b/src/lib389/lib389/cli_conf/plugins/managedentries.py index 99ffc94..478efeb 100644 --- a/src/lib389/lib389/cli_conf/plugins/managedentries.py +++ b/src/lib389/lib389/cli_conf/plugins/managedentries.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). @@ -48,7 +48,7 @@ def mep_config_list(inst, basedn, log, args): else: result.append(config.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: @@ -113,7 +113,7 @@ def mep_template_list(inst, basedn, log, args): else: result.append(template.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: diff --git a/src/lib389/lib389/cli_conf/plugins/passthroughauth.py b/src/lib389/lib389/cli_conf/plugins/passthroughauth.py index dc115e0..7198d86 100644 --- a/src/lib389/lib389/cli_conf/plugins/passthroughauth.py +++ b/src/lib389/lib389/cli_conf/plugins/passthroughauth.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). @@ -73,7 +73,8 @@ def pta_list(inst, basedn, log, args): urls = plugin.get_urls() if args.json: log.info(json.dumps({"type": "list", - "items": [{"id": id, "url": value} for id, value in urls.items()]})) + "items": [{"id": id, "url": value} for id, value in urls.items()]}, + indent=4)) else: if len(urls) > 0: for _, value in urls.items(): @@ -138,7 +139,7 @@ def pam_pta_list(inst, basedn, log, args): else: result.append(config.rdn) if args.json: - log.info(json.dumps({"type": "list", "items": result_json})) + log.info(json.dumps({"type": "list", "items": result_json}, indent=4)) else: if len(result) > 0: for i in result: diff --git a/src/lib389/lib389/cli_conf/security.py b/src/lib389/lib389/cli_conf/security.py index 86f9ec7..6d9fe1e 100644 --- a/src/lib389/lib389/cli_conf/security.py +++ b/src/lib389/lib389/cli_conf/security.py @@ -1,5 +1,5 @@ # --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2019 Red Hat, Inc. +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). diff --git a/src/lib389/lib389/cli_idm/__init__.py b/src/lib389/lib389/cli_idm/__init__.py index 28648ad..59aedd9 100644 --- a/src/lib389/lib389/cli_idm/__init__.py +++ b/src/lib389/lib389/cli_idm/__init__.py @@ -1,5 +1,6 @@ # --- BEGIN COPYRIGHT BLOCK --- # Copyright (C) 2016, William Brown +# Copyright (C) 2020 Red Hat, Inc. # All rights reserved. # # License: GPL (version 3 or any later version). @@ -65,7 +66,7 @@ def _generic_list(inst, basedn, log, manager_class, args=None): ol = mc.list() if len(ol) == 0: if args and args.json: - print(json.dumps({"type": "list", "items": []})) + print(json.dumps({"type": "list", "items": []}, indent=4)) else: log.info("No objects to display") elif len(ol) > 0: @@ -79,7 +80,7 @@ def _generic_list(inst, basedn, log, manager_class, args=None): else: log.info(o_str) if args and args.json: - print(json.dumps(json_result)) + print(json.dumps(json_result, indent=4)) # Display these entries better!