diff --git a/grafana_dashboards/builder.py b/grafana_dashboards/builder.py index 66b9655..425ee21 100644 --- a/grafana_dashboards/builder.py +++ b/grafana_dashboards/builder.py @@ -28,6 +28,7 @@ class Builder(object): self.cache = Cache( config.get('cache', 'cachedir'), config.getboolean('cache', 'enabled')) + self.folder_id = config.getint('grafana', 'folderid') self.grafana = Grafana( url=config.get('grafana', 'url'), key=config.get('grafana', 'apikey')) @@ -84,7 +85,9 @@ class Builder(object): for name in data: data, md5 = self.parser.get_dashboard(name) if self.cache.has_changed(name, md5): - self.grafana.dashboard.create(name, data, overwrite=True) + self.grafana.dashboard.create(name, data, + overwrite=True, + folder_id=self.folder_id) self.cache.set(name, md5) else: LOG.debug("'%s' has not changed" % name) diff --git a/grafana_dashboards/cmd.py b/grafana_dashboards/cmd.py index a8eaca5..47682c7 100644 --- a/grafana_dashboards/cmd.py +++ b/grafana_dashboards/cmd.py @@ -53,6 +53,9 @@ class Client(object): parser.add_argument( '--grafana-apikey', dest='grafana_apikey', help='API key to access grafana.') + parser.add_argument( + '--grafana-folderid', dest='grafana_folderid', + help='The id of the folder to save the dashboard in.') parser.add_argument( '--version', dest='version', action='version', version=__version__, help="show " @@ -94,6 +97,9 @@ class Client(object): if self.args.grafana_apikey: self.config.set('grafana', 'apikey', self.args.grafana_apikey) LOG.debug('Grafana APIKey overridden') + if self.args.grafana_folderid: + self.config.set('grafana', 'folderid', self.args.grafana_folderid) + LOG.debug('Grafana Folderid overridden') def setup_logging(self): if self.args.debug: diff --git a/grafana_dashboards/config.py b/grafana_dashboards/config.py index 5ecd4ed..a61db5d 100644 --- a/grafana_dashboards/config.py +++ b/grafana_dashboards/config.py @@ -27,3 +27,4 @@ class Config(ConfigParser.ConfigParser): self.add_section('grafana') self.set('grafana', 'apikey', '') self.set('grafana', 'url', 'http://localhost:8080') + self.set('grafana', 'folderid', '0') diff --git a/grafana_dashboards/grafana/dashboard.py b/grafana_dashboards/grafana/dashboard.py index 3f634b6..aed9990 100644 --- a/grafana_dashboards/grafana/dashboard.py +++ b/grafana_dashboards/grafana/dashboard.py @@ -25,7 +25,7 @@ class Dashboard(object): self.url = utils.urljoin(url, 'api/dashboards/db/') self.session = session - def create(self, name, data, overwrite=False): + def create(self, name, data, overwrite=False, folder_id=0): """Create a new dashboard :param name: URL friendly title of the dashboard @@ -35,12 +35,15 @@ class Dashboard(object): :param overwrite: Overwrite existing dashboard with newer version or with the same dashboard title :type overwrite: bool + :param folder_id: The id of the folder to save the dashboard in. + :type folder_id: int :raises Exception: if dashboard already exists """ dashboard = { 'dashboard': data, + 'folderId': folder_id, 'overwrite': overwrite, } if not overwrite and self.is_dashboard(name): diff --git a/tests/cmd/test_override.py b/tests/cmd/test_override.py index 9c96eac..6bd575a 100644 --- a/tests/cmd/test_override.py +++ b/tests/cmd/test_override.py @@ -28,6 +28,7 @@ class TestCaseArgs(TestCase): required = [ 'Grafana URL override: http://example.grafana.org:3000', '.*?^Grafana APIKey overridden', + '.*?^Grafana Folderid overridden', '.*?^Validating schema in %s' % self.path, ] @@ -36,6 +37,8 @@ class TestCaseArgs(TestCase): 'http://example.grafana.org:3000', '--grafana-apikey', 'xyz', + '--grafana-folderid', + '1', 'validate', self.path, ] diff --git a/tests/test_config.py b/tests/test_config.py index 0e1cb9f..eb26c8b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -29,5 +29,7 @@ class TestCaseConfig(TestCase): self.config.get('cache', 'cachedir'), '~/.cache/grafyaml') self.assertEqual( self.config.get('grafana', 'apikey'), '') + self.assertEqual( + self.config.get('grafana', 'folderid'), '0') self.assertEqual( self.config.get('grafana', 'url'), 'http://localhost:8080')