Move yaml functions into its own class
This will centeralize our overlapping yaml code paths. Change-Id: Ib8bc0fc93b49363cb511d9891a24ea48fdee136d Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
parent
b02e007258
commit
9030cad526
@ -12,11 +12,10 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import yaml
|
||||
|
||||
from oslo_config import cfg
|
||||
|
||||
from grafana_dashboards.grafana import Grafana
|
||||
from grafana_dashboards.parser import YamlParser
|
||||
from grafana_dashboards.schema.dashboard import Dashboard
|
||||
|
||||
grafana_opts = [
|
||||
@ -40,9 +39,10 @@ CONF.register_opts(grafana_opts, group='grafana')
|
||||
class Builder(object):
|
||||
def __init__(self):
|
||||
self.grafana = Grafana(CONF.grafana.url, CONF.grafana.apikey)
|
||||
self.parser = YamlParser()
|
||||
|
||||
def update_dashboard(self, path):
|
||||
data = yaml.load(open(path))
|
||||
data = self.parser.load(path)
|
||||
schema = Dashboard()
|
||||
result = schema.validate(data)
|
||||
self.grafana.create_dashboard(result, overwrite=True)
|
||||
|
21
grafana_dashboards/parser.py
Normal file
21
grafana_dashboards/parser.py
Normal file
@ -0,0 +1,21 @@
|
||||
# Copyright 2015 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class YamlParser(object):
|
||||
|
||||
def load(self, path):
|
||||
return yaml.load(open(path))
|
@ -21,8 +21,8 @@ import json
|
||||
import os
|
||||
import re
|
||||
import testtools
|
||||
import yaml
|
||||
|
||||
from grafana_dashboards.parser import YamlParser
|
||||
from grafana_dashboards.schema import dashboard
|
||||
|
||||
|
||||
@ -49,8 +49,8 @@ def get_scenarios(fixtures_path, in_ext='yaml', out_ext='json'):
|
||||
|
||||
|
||||
class TestCase(object):
|
||||
|
||||
"""Test case base class for all unit tests."""
|
||||
parser = YamlParser()
|
||||
|
||||
def _read_raw_content(self):
|
||||
# if None assume empty file
|
||||
@ -61,15 +61,9 @@ class TestCase(object):
|
||||
|
||||
return content
|
||||
|
||||
def _read_yaml_content(self, filename):
|
||||
with open(filename, 'r') as yaml_file:
|
||||
content = yaml.load(yaml_file)
|
||||
|
||||
return content
|
||||
|
||||
def test_yaml_snippet(self):
|
||||
expected_json = self._read_raw_content()
|
||||
yaml_content = self._read_yaml_content(self.in_filename)
|
||||
yaml_content = self.parser.load(self.in_filename)
|
||||
|
||||
schema = dashboard.Dashboard()
|
||||
valid_yaml = schema.validate(yaml_content)
|
||||
|
Loading…
x
Reference in New Issue
Block a user