def main(): logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO) parser = argparse.ArgumentParser() parser.add_argument('-p', '--path', required=True, nargs='+', type=str, help='List of path to YAML definition files') parser.add_argument('--project', help='(deprecated, use path) Location of the file containing project definition.') parser.add_argument('-o', '--out', help='(deprecated, use config file and file exporter) Path to output folder') parser.add_argument('-c', '--config', default='./.grafana/grafana_dashboards.yaml', help='Configuration file containing fine-tuned setup of builder\'s components.') parser.add_argument('--context', default='{}', help='YAML structure defining parameters for dashboard definition.' ' Effectively overrides any parameter defined on project level.') parser.add_argument('--plugins', nargs='+', type=str, help='List of external component plugins to load') parser.add_argument('--exporter', nargs='+', type=str, default=set(), dest='exporters', help='List of dashboard exporters') parser.add_argument('--message', required=False, type=str, help='Set a commit message for the Grafana version history') args = parser.parse_args() if args.plugins: for plugin in args.plugins: try: imp.load_source('grafana_dashboards.components.$loaded', plugin) except Exception as e: print('Cannot load plugin %s: %s' % (plugin, str(e))) if args.project: logging.warn("Using deprecated option '--project'") args.path.add(args.project) paths = _process_paths(args.path) config = Config(args.config) exporters = set(args.exporters) if args.out: logging.warn("Using deprecated option '-o/--out'") exporters.add('file') config.get_config('file').update(output_folder=args.out) if args.message: config.get_config('grafana').update(commit_message=args.message) dashboard_exporters = _initialize_exporters(exporters, [FileExporter, ElasticSearchExporter, GrafanaExporter], config) context = config.get_config('context') context.update(yaml.load(args.context, Loader=yaml.GDBLoader)) projects = DefinitionParser().load_projects(paths) project_processor = ProjectProcessor(dashboard_exporters) project_processor.process_projects(projects, context)
def main(): logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO) parser = argparse.ArgumentParser() parser.add_argument('-p', '--path', required=True, nargs='+', type=str, help='List of path to YAML definition files') parser.add_argument('--project', help='(deprecated, use path) Location of the file containing project definition.') parser.add_argument('-o', '--out', help='(deprecated, use config file and file exporter) Path to output folder') parser.add_argument('-c', '--config', default='./.grafana/grafana_dashboards.yaml', help='Configuration file containing fine-tuned setup of builder\'s components.') parser.add_argument('--context', default='{}', help='YAML structure defining parameters for dashboard definition.' ' Effectively overrides any parameter defined on project level.') parser.add_argument('--plugins', nargs='+', type=str, help='List of external component plugins to load') parser.add_argument('--exporter', nargs='+', type=str, default=set(), dest='exporters', help='List of dashboard exporters') args = parser.parse_args() if args.plugins: for plugin in args.plugins: try: imp.load_source('grafana_dashboards.components.$loaded', plugin) except Exception as e: print('Cannot load plugin %s: %s' % (plugin, str(e))) if args.project: logging.warn("Using deprecated option '--project'") args.path.add(args.project) paths = _process_paths(args.path) config = Config(args.config) exporters = set(args.exporters) if args.out: logging.warn("Using deprecated option '-o/--out'") exporters.add('file') config.get_config('file').update(output_folder=args.out) dashboard_exporters = _initialize_exporters(exporters, [FileExporter, ElasticSearchExporter, GrafanaExporter], config) context = config.get_config('context') context.update(yaml.load(args.context)) projects = DefinitionParser().load_projects(paths) project_processor = ProjectProcessor(dashboard_exporters) project_processor.process_projects(projects, context)
def test_project_processor(): dashboard_processor = MagicMock() processor = ProjectProcessor([dashboard_processor]) project = MagicMock() context = MagicMock() dashboard = MagicMock() project.get_contexts.return_value = [context] project.get_dashboards.return_value = [dashboard] parent_context = MagicMock() # noinspection PyTypeChecker processor.process_projects([project], parent_context) project.get_contexts.assert_called_once_with(parent_context) dashboard.gen_json.assert_called_with(context) context.expand_placeholders.assert_called_with(dashboard.name) dashboard_processor.process_dashboard.assert_called_once_with( project.name, context.expand_placeholders(), dashboard.gen_json())
def test_project_processor(): dashboard_processor = MagicMock() processor = ProjectProcessor([dashboard_processor]) project = MagicMock() context = MagicMock() dashboard = MagicMock() project.get_contexts.return_value = [context] project.get_dashboards.return_value = [dashboard] parent_context = MagicMock() # noinspection PyTypeChecker processor.process_projects([project], parent_context) project.get_contexts.assert_called_once_with(parent_context) dashboard.gen_json.assert_called_with(context) context.expand_placeholders.assert_called_with(dashboard.name) dashboard_processor.process_dashboard.assert_called_once_with(project.name, context.expand_placeholders(), dashboard.gen_json())