# Path: grafana_dashboards/client/grafana.py
# class GrafanaExporter(DashboardExporter):
#     def __init__(self, **kwargs):
#         super(GrafanaExporter, self).__init__()
#         self._host = os.getenv('GRAFANA_HOST', kwargs.get('host'))
#         password = os.getenv('GRAFANA_PASSWORD', kwargs.get('password'))
#         username = os.getenv('GRAFANA_USERNAME', kwargs.get('username'))
#         auth_token = os.getenv('GRAFANA_TOKEN', kwargs.get('token'))
#         use_kerberos = os.getenv('GRAFANA_USE_KERBEROS', kwargs.get('use_kerberos'))
# 
#         if use_kerberos:
#             self._connection = KerberosConnection(self._host)
#         elif auth_token:
#             self._connection = BearerAuthConnection(auth_token, self._host)
#         else:
#             self._connection = BasicAuthConnection(username, password, self._host)
# 
#     def process_dashboard(self, project_name, dashboard_name, dashboard_data):
#         super(GrafanaExporter, self).process_dashboard(project_name, dashboard_name, dashboard_data)
#         body = {'overwrite': True, 'dashboard': dashboard_data}
# 
#         if 'folderId' in dashboard_data:
#             body.update({'folderId': dashboard_data['folderId']})
# 
#         logger.info("Uploading dashboard '%s' to %s", dashboard_name, self._host)
#         self._connection.make_request('/api/dashboards/db', body)


from mock import MagicMock
from grafana_dashboards.client.grafana import GrafanaExporter
# 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.
from __future__ import unicode_literals



__author__ = 'Jakub Plichta <jakub.plichta@gmail.com>'


def test_grafana():
    exporter = GrafanaExporter(host='host', username='username', password='password')
    exporter._connection = MagicMock()

    dashboard_data = {'title': 'title', 'tags': []}
    exporter.process_dashboard('project_name', 'dashboard_name', dashboard_data)

    body = {'overwrite': True, 'dashboard': dashboard_data}
    # noinspection PyProtectedMember
    exporter._connection.make_request.assert_called_once_with('/api/dashboards/db',
                                                              body)


def test_grafana_with_kerberos():

# Path: grafana_dashboards/client/grafana.py
# class GrafanaExporter(DashboardExporter):
#     def __init__(self, **kwargs):
#         super(GrafanaExporter, self).__init__()
#         self._host = os.getenv('GRAFANA_HOST', kwargs.get('host'))
#         password = os.getenv('GRAFANA_PASSWORD', kwargs.get('password'))
#         username = os.getenv('GRAFANA_USERNAME', kwargs.get('username'))
#         auth_token = os.getenv('GRAFANA_TOKEN', kwargs.get('token'))
#         use_kerberos = os.getenv('GRAFANA_USE_KERBEROS', kwargs.get('use_kerberos'))
# 
#         if use_kerberos:
#             self._connection = KerberosConnection(self._host)
#         elif auth_token:
#             self._connection = BearerAuthConnection(auth_token, self._host)
#         else:
#             self._connection = BasicAuthConnection(username, password, self._host)
# 
#     def process_dashboard(self, project_name, dashboard_name, dashboard_data):
#         super(GrafanaExporter, self).process_dashboard(project_name, dashboard_name, dashboard_data)
#         body = {'overwrite': True, 'dashboard': dashboard_data}
# 
#         if 'folderId' in dashboard_data:
#             body.update({'folderId': dashboard_data['folderId']})
# 
#         logger.info("Uploading dashboard '%s' to %s", dashboard_name, self._host)
#         self._connection.make_request('/api/dashboards/db', body)


# Path: tests/grafana_dashboards/client/test_grafana.py
from mock import MagicMock
from grafana_dashboards.client.grafana import GrafanaExporter

# 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.
from __future__ import unicode_literals



__author__ = 'Jakub Plichta <jakub.plichta@gmail.com>'


def test_grafana():
    exporter = GrafanaExporter(host='host', username='username', password='password')
    exporter._connection = MagicMock()

    dashboard_data = {'title': 'title', 'tags': []}
    exporter.process_dashboard('project_name', 'dashboard_name', dashboard_data)

    body = {'overwrite': True, 'dashboard': dashboard_data}
    # noinspection PyProtectedMember
    exporter._connection.make_request.assert_called_once_with('/api/dashboards/db',
                                                              body)


def test_grafana_with_kerberos():

    exporter = GrafanaExporter(host='host', use_kerberos='true')