From 835942a5455b2ac5ed3541c076f89a0fd096cfe2 Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Thu, 22 May 2014 17:13:29 +0000 Subject: [PATCH 1/2] Add backwards compatibility for dashboard template variable format --- src/dogshell/dashboard.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/dogshell/dashboard.py b/src/dogshell/dashboard.py index 4fe240b..4a8d353 100644 --- a/src/dogshell/dashboard.py +++ b/src/dogshell/dashboard.py @@ -187,10 +187,13 @@ def _post(self, args): except: raise Exception('bad json parameter') if args.template_variables: - try: - tpl_vars = json.loads(args.template_variables) - except Exception: - raise Exception('bad template_variable json parameter') + if '[' not in args.template_variables: + tpl_vars = [v.strip() for v in args.template_variables.split(',')] + else: + try: + tpl_vars = json.loads(args.template_variables) + except Exception: + raise Exception('bad template_variable json parameter') else: tpl_vars = [] @@ -214,10 +217,13 @@ def _update(self, args): except: raise Exception('bad json parameter') if args.template_variables: - try: - tpl_vars = json.loads(args.template_variables) - except Exception: - raise Exception('bad template_variable json parameter') + if '[' not in args.template_variables: + tpl_vars = [v.strip() for v in args.template_variables.split(',')] + else: + try: + tpl_vars = json.loads(args.template_variables) + except Exception: + raise Exception('bad template_variable json parameter') else: tpl_vars = [] res = self.dog.update_dashboard(args.dashboard_id, args.title, args.description, From f80094ce7a23f40e64e8d97c40ac9c830a65197f Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Fri, 30 May 2014 15:10:03 +0000 Subject: [PATCH 2/2] Use template var argparse type --- src/dogshell/dashboard.py | 42 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/dogshell/dashboard.py b/src/dogshell/dashboard.py index 4a8d353..b36dbbd 100644 --- a/src/dogshell/dashboard.py +++ b/src/dogshell/dashboard.py @@ -25,7 +25,9 @@ def setup_parser(self, subparsers): post_parser.add_argument('title', help='title for the new dashboard') post_parser.add_argument('description', help='short description of the dashboard') post_parser.add_argument('graphs', help='graph definitions as a JSON string. if unset, reads from stdin.', nargs="?") - post_parser.add_argument('--template_variables', help='a json list of template variable dicts, e.g. \'[{"name": "host", "prefix": "host", "default": "host:my-host"}]\'') + post_parser.add_argument('--template_variables', type=_template_variables, default=[], + help='a json list of template variable dicts, e.g. \ + \'[{"name": "host", "prefix": "host", "default": "host:my-host"}]\'') post_parser.set_defaults(func=self._post) @@ -34,7 +36,9 @@ def setup_parser(self, subparsers): update_parser.add_argument('title', help='new title for the dashboard') update_parser.add_argument('description', help='short description of the dashboard') update_parser.add_argument('graphs', help='graph definitions as a JSON string. if unset, reads from stdin.', nargs="?") - update_parser.add_argument('--template_variables', help='a json list of template variable dicts, e.g. \'[{"name": "host", "prefix": "host", "default": "host:my-host"}]\'') + update_parser.add_argument('--template_variables', type=_template_variables, default=[], + help='a json list of template variable dicts, e.g. \ + \'[{"name": "host", "prefix": "host", "default": "host:my-host"}]\'') update_parser.set_defaults(func=self._update) show_parser = verb_parsers.add_parser('show', help='Show a dashboard definition.') @@ -186,19 +190,8 @@ def _post(self, args): graphs = json.loads(graphs) except: raise Exception('bad json parameter') - if args.template_variables: - if '[' not in args.template_variables: - tpl_vars = [v.strip() for v in args.template_variables.split(',')] - else: - try: - tpl_vars = json.loads(args.template_variables) - except Exception: - raise Exception('bad template_variable json parameter') - - else: - tpl_vars = [] res = self.dog.create_dashboard(args.title, args.description, graphs, - template_variables=tpl_vars) + template_variables=args.template_variables) report_warnings(res) report_errors(res) if format == 'pretty': @@ -216,18 +209,9 @@ def _update(self, args): graphs = json.loads(graphs) except: raise Exception('bad json parameter') - if args.template_variables: - if '[' not in args.template_variables: - tpl_vars = [v.strip() for v in args.template_variables.split(',')] - else: - try: - tpl_vars = json.loads(args.template_variables) - except Exception: - raise Exception('bad template_variable json parameter') - else: - tpl_vars = [] + res = self.dog.update_dashboard(args.dashboard_id, args.title, args.description, - graphs, template_variables=tpl_vars) + graphs, template_variables=args.template_variables) report_warnings(res) report_errors(res) if format == 'pretty': @@ -294,3 +278,11 @@ def _escape(self, s): def _pretty_json(self, obj): return json.dumps(obj, sort_keys=True, indent=2) +def _template_variables(tpl_var_input): + if '[' not in tpl_var_input: + return [v.strip() for v in tpl_var_input.split(',')] + else: + try: + return json.loads(tpl_var_input) + except Exception: + raise argparse.ArgumentTypeError('bad template_variable json parameter')