Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/dogshell/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.')
Expand Down Expand Up @@ -186,16 +190,8 @@ def _post(self, args):
graphs = json.loads(graphs)
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')

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':
Expand All @@ -213,15 +209,9 @@ def _update(self, args):
graphs = json.loads(graphs)
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')
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':
Expand Down Expand Up @@ -288,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')