Add capability to use grafana folderid parameter

- retro-compatibility with default folder 'General' == 0
 - add to config file with folderid key in grafana section
 - add to cmd with --grafana-folderid

Change-Id: Iebfc5613f4c622d3d49d2f34df77ad3695f6b046
This commit is contained in:
Andy Ladjadj 2019-02-18 11:20:05 +01:00 committed by ladjadj
parent 53fea97346
commit 67f55d53d6
6 changed files with 20 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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