#51139 Issue 51136 - dsctl and dsidm do not errors correctly when using JSON
Closed 3 years ago by spichugi. Opened 3 years ago by mreynolds.
mreynolds/389-ds-base issue51136  into  master

file modified
+2 -2
@@ -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 @@ 

          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

file modified
+1 -1
@@ -80,7 +80,7 @@ 

          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

file modified
+4 -1
@@ -140,7 +140,10 @@ 

      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)

  

file modified
+4 -1
@@ -133,7 +133,10 @@ 

      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)

@@ -1,5 +1,5 @@ 

  # --- BEGIN COPYRIGHT BLOCK ---

- # Copyright (C) 2019 Red Hat, Inc.

+ # Copyright (C) 2020 Red Hat, Inc.

  # Copyright (C) 2019 William Brown <william@blackhats.net.au>

  # All rights reserved.

  #
@@ -2968,7 +2968,7 @@ 

                  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 @@ 

                  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

  

@@ -229,7 +229,7 @@ 

                                      'con_maxcsn': con_maxcsn,

                                      'state': agmt_status['state'],

                                      'reason': agmt_status['message']

-                                 })

+                                 }, indent=4)

                              else:

                                  return "In Synchronization"

                  except:
@@ -253,7 +253,7 @@ 

                      '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 @@ 

                  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 @@ 

                        '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:"

@@ -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 @@ 

          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 @@ 

          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:

@@ -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 @@ 

          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 @@ 

          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:

@@ -1,5 +1,5 @@ 

  # --- BEGIN COPYRIGHT BLOCK ---

- # Copyright (C) 2019 Red Hat, Inc.

+ # Copyright (C) 2020 Red Hat, Inc.

  # Copyright (C) 2019 William Brown <william@blackhats.net.au>

  # All rights reserved.

  #
@@ -30,7 +30,7 @@ 

          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:

@@ -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 @@ 

          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 @@ 

          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:

@@ -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 @@ 

      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 @@ 

          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:

@@ -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).

@@ -1,5 +1,6 @@ 

  # --- BEGIN COPYRIGHT BLOCK ---

  # Copyright (C) 2016, William Brown <william at blackhats.net.au>

+ # Copyright (C) 2020 Red Hat, Inc.

  # All rights reserved.

  #

  # License: GPL (version 3 or any later version).
@@ -65,7 +66,7 @@ 

      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 @@ 

              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!

Description:

dsctl and dsidm were not returning errors in a JSON object when the JSON CLI option was requested. This breaks the UI when errors occur.

fixes: https://pagure.io/389-ds-base/issue/51136

Looks good!

Can we, maybe, fix the rest of the indent issues?
Discovered like this:

cd src/lib389/
grep -rni json.dump | grep -v indent

Looks good!
Can we, maybe, fix the rest of the indent issues?
Discovered like this:
cd src/lib389/
grep -rni json.dump | grep -v indent

I thought I covered them all in a previous patch when I was doing other UI work, but I obviously missed quite a few. I'll add these into the patch and merge it. Thanks!

rebased onto 079b874c733ae99074379caa36bc9921df5c1f49

3 years ago

rebased onto af87048

3 years ago

Pull-Request has been merged by mreynolds

3 years ago

389-ds-base is moving from Pagure to Github. This means that new issues and pull requests
will be accepted only in 389-ds-base's github repository.

This pull request has been cloned to Github as issue and is available here:
- https://github.com/389ds/389-ds-base/issues/4192

If you want to continue to work on the PR, please navigate to the github issue,
download the patch from the attachments and file a new pull request.

Thank you for understanding. We apologize for all inconvenience.

Pull-Request has been closed by spichugi

3 years ago